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