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