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