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 * Plugin Schedule: manage events per wiki @groups
7
8 */
9
10// ============================================================
11class poiDB {
12    var $modify = false;
13    var $dbFileName;
14    var $dbByInsee;
15    var $allLines;
16
17    // ============================================================
18    function __construct ($plugin) {
19        global $conf;
20        $this->dbFileName =
21                          ((!$conf['savedir'] || strpos ($conf['savedir'], '.') === 0) ? DOKU_INC : "").
22                          $conf['savedir'].'/'.trim ($plugin->getConf ('dataDir').'/poiDB.txt');
23        $this->read ();
24    }
25
26    // ============================================================
27    function getInsee ($insee) {
28        if (!isset ($this->dbByInsee [$insee]))
29            return array ();
30        return $this->dbByInsee [$insee];
31    }
32
33    // ============================================================
34    function addLine ($line) {
35        $this->cleanDB ();
36        $line = $this->cleanLine ($line);
37        if (!$this->allLines || !in_array ($line, $this->allLines)) {
38            $this->allLines [] = $line;
39            sort ($this->allLines);
40            $this->modify = true;
41        }
42        $this->write ();
43        echo "record".NL;
44    }
45
46    // ============================================================
47    function removeLine ($line) {
48        $this->cleanDB ();
49        $line = $this->cleanLine ($line);
50
51        if (($key = array_search ($line, $this->allLines)) !== false) {
52            unset ($this->allLines [$key]);
53            $this->modify = true;
54        }
55        $this->write ();
56    }
57
58    // ============================================================
59    function cleanLine ($line) {
60        // XXX vérifier nombre
61        $data = explode ("|", $line);
62        foreach ($data as $key => $val)
63            $data[$key] = trim ($val);
64        $data [1] = number_format ((float)$data[1], 4);
65        $data [2] = number_format ((float)$data[2], 4);
66        return implode ("|", $data);
67    }
68
69    // ============================================================
70    function cleanDB () {
71        if ($this->allLines)
72            foreach ($this->allLines as $key => $line) {
73                $lineCleaned = $this->cleanLine ($line);
74                if ($lineCleaned != $line)
75                    $this->modify = true;
76                $this->allLines [$key] = $lineCleaned;
77            }
78    }
79
80    // ============================================================
81    function read () {
82        $this->dbByInsee = array ();
83        $handle = @fopen ($this->dbFileName, "r");
84        if ($handle) {
85            while (($line = fgets ($handle)) !== false) {
86                $line = trim (preg_replace ("/#.*$/", "", str_replace ('\\\\', '~br~', $line)));
87                if (!$line)
88                    continue;
89                $this->allLines [] = $line;
90                list ($insee, $end) = explode ('|', $line, 2);
91                $this->dbByInsee [$insee][] = $end;
92            }
93            fclose ($handle);
94        }
95    }
96
97    // ============================================================
98    function write () {
99        if (!$this->modify) {
100            echo "POI DB unchanged !".NL;
101            return;
102        }
103        $handle = @fopen ($this->dbFileName, "w");
104        if ($handle == false)
105            die("unable to create file: ".$this->dbFileName); // XXX
106        foreach ($this->allLines as $line)
107            fputs ($handle, $line.NL);
108        fclose ($handle);
109        echo "POI DB updated !".NL;
110    }
111
112    // ============================================================
113}
114