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