1<?php
2/**
3 * @license    http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
4 * @author     Francois Merciol <dokuplugin@merciol.fr>
5 *
6 * INSEE city: code database
7 */
8
9if (!defined ('DOKU_INC'))
10  die ();
11if (!defined ('DOKU_PLUGIN'))
12  define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
13require_once (DOKU_PLUGIN.'action.php');
14
15class action_plugin_inseecity extends DokuWiki_Action_Plugin {
16    var $allDeps;
17    var $selectedDeps;
18
19    // ============================================================
20    function register (Doku_Event_Handler $controller) {
21        $controller->register_hook ('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'include_dependencies', array());
22        $controller->register_hook ('DOKUWIKI_STARTED', 'AFTER',  $this, 'jsinfo');
23    }
24
25    public function include_dependencies (Doku_Event $event, $param) {
26        $this->setVars ();
27		foreach ($this->selectedDeps as $dep)
28            $event->data['script'][] = array (
29                'type' => 'text/javascript',
30                'src' => DOKU_BASE.'lib/plugins/inseecity/dep/'.$dep.'.js',
31                'defer' => 'defer',
32                'charset' => 'utf-8',
33                '_data' => '',
34            );
35    }
36
37    // ============================================================
38    public function setVars () {
39        $pathDirObj = opendir (__DIR__."/dep/");
40        $exclude_array = explode ("|", ".|..");
41        $this->allDeps = [];
42        $this->selectedDeps = [];
43        while (false !== ($file = readdir ($pathDirObj))) {
44            if (in_array (strtolower ($file), $exclude_array))
45                continue;
46            $this->allDeps[] = preg_replace ('#(.*)\.js$#i', '$1', $file);
47        }
48        if ($this->getConf ('allDep'))
49            $this->selectedDeps = $this->allDeps;
50        else {
51            $this->selectedDeps = [];
52            foreach (explode (",",  $this->getConf ('selectedDeps')) as $dep) {
53                $dep = strtoupper (trim ($dep));
54                if (in_array ($dep, $this->allDeps))
55                    $this->selectedDeps [] = $dep;
56            }
57        }
58    }
59
60    // ============================================================
61    public function jsinfo(Doku_Event $event, $param) {
62        $this->setVars ();
63        global $JSINFO;
64        if (!isset($JSINFO['inseeCity']))
65            $JSINFO['inseeCity'] = array();
66        $JSINFO ['inseeCity']['selectedDeps'] = $this->selectedDeps;
67        $JSINFO ['inseeCity']['maxAutocomplete'] = $this->getConf ('maxAutocomplete');
68    }
69
70    // ============================================================
71}
72