xref: /plugin/farmer/action/startup.php (revision 1da41c8bdb9ce0b590d5b69552c4ddb4d0aef860)
1a646d519SAndreas Gohr<?php
2*1da41c8bSAndreas Gohr
3*1da41c8bSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4*1da41c8bSAndreas Gohruse dokuwiki\Extension\EventHandler;
5*1da41c8bSAndreas Gohruse dokuwiki\Extension\Event;
6*1da41c8bSAndreas Gohr
7a646d519SAndreas Gohr/**
80a5d2da2SAndreas Gohr * DokuWiki Plugin farmer (Action Component)
9a646d519SAndreas Gohr *
10*1da41c8bSAndreas Gohr * Handles Farm mechanisms on DokuWiki startup
11*1da41c8bSAndreas Gohr *
120a5d2da2SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
13a646d519SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
140a5d2da2SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
15a646d519SAndreas Gohr */
16*1da41c8bSAndreas Gohrclass action_plugin_farmer_startup extends ActionPlugin
17*1da41c8bSAndreas Gohr{
18b96c66ccSAndreas Gohr    /** @var  helper_plugin_farmer */
19b96c66ccSAndreas Gohr    protected $helper;
20b96c66ccSAndreas Gohr
21b96c66ccSAndreas Gohr    /**
22b96c66ccSAndreas Gohr     * action_plugin_farmer_startup constructor.
23b96c66ccSAndreas Gohr     */
24*1da41c8bSAndreas Gohr    public function __construct()
25*1da41c8bSAndreas Gohr    {
26b96c66ccSAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
27b96c66ccSAndreas Gohr    }
28b96c66ccSAndreas Gohr
29a646d519SAndreas Gohr    /**
30a646d519SAndreas Gohr     * plugin should use this method to register its handlers with the DokuWiki's event controller
31a646d519SAndreas Gohr     *
32*1da41c8bSAndreas Gohr     * @param EventHandler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER
33a646d519SAndreas Gohr     */
34*1da41c8bSAndreas Gohr    public function register(EventHandler $controller)
35*1da41c8bSAndreas Gohr    {
36*1da41c8bSAndreas Gohr        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handleStartUp');
37a646d519SAndreas Gohr    }
38a646d519SAndreas Gohr
39*1da41c8bSAndreas Gohr    /**
40*1da41c8bSAndreas Gohr     * Handle the startup event
41*1da41c8bSAndreas Gohr     *
42*1da41c8bSAndreas Gohr     * @param Event $event
43*1da41c8bSAndreas Gohr     * @param $param
44*1da41c8bSAndreas Gohr     */
45*1da41c8bSAndreas Gohr    public function handleStartUp(Event $event, $param)
46*1da41c8bSAndreas Gohr    {
47b96c66ccSAndreas Gohr        if ($this->helper->wasNotfound()) $this->handleNotFound();
48a646d519SAndreas Gohr    }
49a646d519SAndreas Gohr
50a646d519SAndreas Gohr    /**
51a646d519SAndreas Gohr     * Handles the animal not found case
52a646d519SAndreas Gohr     *
53a646d519SAndreas Gohr     * Will abort the current script unless the farmer is wanted
54a646d519SAndreas Gohr     */
55*1da41c8bSAndreas Gohr    protected function handleNotFound()
56*1da41c8bSAndreas Gohr    {
57b96c66ccSAndreas Gohr        /** @noinspection PhpUnusedLocalVariableInspection */
58b96c66ccSAndreas Gohr        global $conf, $lang;
59b96c66ccSAndreas Gohr        $config = $this->helper->getConfig();
60a646d519SAndreas Gohr        $show = $config['notfound']['show'];
61a646d519SAndreas Gohr        $url = $config['notfound']['url'];
62a646d519SAndreas Gohr        if ($show == 'farmer') return;
63a646d519SAndreas Gohr
64a646d519SAndreas Gohr        if ($show == '404' || $show == 'list') {
65a646d519SAndreas Gohr            http_status(404);
66a646d519SAndreas Gohr            $body = $this->locale_xhtml('notfound_' . $show);
67b96c66ccSAndreas Gohr            /** @noinspection PhpUnusedLocalVariableInspection */
68a646d519SAndreas Gohr            $title = '404';
69a646d519SAndreas Gohr            if ($show == 'list') {
70b96c66ccSAndreas Gohr                /** @noinspection PhpUnusedLocalVariableInspection */
71a646d519SAndreas Gohr                $body .= $this->animalList();
72a646d519SAndreas Gohr            }
73a646d519SAndreas Gohr
74f31951d4SAndreas Gohr            include __DIR__ . '/../includes/template.php';
75a646d519SAndreas Gohr            exit;
76a646d519SAndreas Gohr        }
77a646d519SAndreas Gohr
78a646d519SAndreas Gohr        if ($show == 'redirect' && $url) {
79a646d519SAndreas Gohr            send_redirect($url);
80a646d519SAndreas Gohr        }
81a646d519SAndreas Gohr    }
82a646d519SAndreas Gohr
83a646d519SAndreas Gohr    /**
84a646d519SAndreas Gohr     * Retrun a HTML list of animals
85a646d519SAndreas Gohr     *
86a646d519SAndreas Gohr     * @return string
87a646d519SAndreas Gohr     */
88*1da41c8bSAndreas Gohr    protected function animalList()
89*1da41c8bSAndreas Gohr    {
90a646d519SAndreas Gohr        $html = '<ul>';
91b96c66ccSAndreas Gohr        $animals = $this->helper->getAllAnimals();
92a646d519SAndreas Gohr        foreach ($animals as $animal) {
93c4c8e953SAndreas Gohr            $link = $this->helper->getAnimalURL($animal);
94a646d519SAndreas Gohr            $html .= '<li><div class="li"><a href="' . $link . '">' . hsc($animal) . '</a></div></li>';
95a646d519SAndreas Gohr        }
96a646d519SAndreas Gohr        $html .= '</ul>';
97a646d519SAndreas Gohr        return $html;
98a646d519SAndreas Gohr    }
99a646d519SAndreas Gohr}
100