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