source: extensions/Psli_BingMaps/admin/class/psli_entity.class.php @ 18290

Last change on this file since 18290 was 15448, checked in by psli, 12 years ago
File size: 10.2 KB
Line 
1<?php
2class Psli_Entity {
3        /***********************************************************************/
4        /* Controlers **********************************************************/
5        protected $data_changed;                // Used to know if data were changed
6        protected $status;                      // Status of this map ==> true:open, enable - false:closed, disable
7        /* Data ****************************************************************/
8        protected $id;                          // Unique id in table
9        protected $lat;
10        protected $lon;
11        protected $title;       
12        protected $idaction;
13        protected $zoommin;     
14        protected $zoommax;
15        protected $action;                      // 1: Zoom - 2: Category - 3: Picture
16        protected $type;                                // 1: pin - 2: zone - 3:picture
17        /* Maps ****************************************************************/
18        protected $maps;                                // Array of associated maps
19
20        /***********************************************************************/
21        /* Methods *************************************************************/
22        /* getEntities
23                        Return all entities in database order by type (zone, pin, picture)
24                        and by title
25        */
26        function getEntities() {
27                $query = '
28                        SELECT id
29                                FROM '.PSLI_ENTITIES_TABLE.'
30                                ORDER BY action, title;';
31                $result = pwg_query($query);
32                $entities = array();
33                while ($row = pwg_db_fetch_assoc($result))
34                {
35                        $entities[] = new Psli_Entity($row['id']);
36                }
37                return $entities;
38        }
39       
40        /* assignData
41                        Assign all data of this entity to template $template with data name
42                        $lat_name, $lon_name, $zoommin_name, $zoommax_name, $title_name,
43                        $actionid_name, $id_name
44        */
45        function assignData ($template, $lat_name, $lon_name, $zoommin_name, $zoommax_name, $title_name, $actionid_name, $id_name) {
46                $template->assign(
47                        array(                 
48                                $lat_name       => $this->lat,
49                                $lon_name   => $this->lon,
50                                $zoommin_name  => $this->zoommin,
51                                $zoommax_name  => $this->zoommax,
52                                $title_name => $this->title,
53                                $actionid_name    => $this->idaction,
54                                $id_name    => $this->id
55                        )
56                );
57        }
58        /* isOpened
59                        Return true if entity is open
60        */
61        function isOpened() {
62                return $this->status;
63        }
64        /* generateEntityList($template, $block, $entitytype)
65                        Create an heml select option identified by $block in
66                        template $ template for each entity filtered by
67                        $entitytype in "pin", "zone", "picture", "all".
68                        Current Entity is selected.
69        */
70        protected function generateList($template, $block, $entitytype) {
71                $entities = array();
72                $entities_selecteds = array();
73                if (! $this->status)
74                        $entities_selecteds[0] = l10n('No selected entity');
75               
76                switch ($entitytype)
77                {
78                        case "pin":                     
79                                $query = '
80                                        SELECT id, title
81                                                FROM '.PSLI_ENTITIES_TABLE.'
82                                                WHERE type = 1
83                                                ORDER BY title;';
84                                break;
85                        case "zone":                   
86                                $query = '
87                                        SELECT id, title
88                                                FROM '.PSLI_ENTITIES_TABLE.'
89                                                WHERE type = 2
90                                                ORDER BY title;';
91                                break;
92                        case "picture":                 
93                                $query = '
94                                        SELECT id, title
95                                                FROM '.PSLI_ENTITIES_TABLE.'
96                                                WHERE type = 3
97                                                ORDER BY title;';
98                                break;
99                        case "all":                     
100                                $query = '
101                                        SELECT id, title
102                                                FROM '.PSLI_ENTITIES_TABLE.'
103                                                ORDER BY title;';
104                                break;
105                }
106                $result = pwg_query($query);
107                while ($row = pwg_db_fetch_assoc($result))
108                {
109                        if ($row['id'] == $this->id)
110                                $entities_selecteds[$row['id']] = $row['title'];
111                        else
112                                $entities[$row['id']] = $row['title'];
113                }
114                $template->assign($block.'_selected', $entities_selecteds);
115                $template->assign($block, $entities);
116        }
117        /* delete
118                        Delete entity from the database.
119        */
120        function delete() {
121                global $debug;
122                $debug .= "PSLIDEBUG : delete";
123                if ($this->status == true)
124                {
125                        $debug .= "PSLIDEBUG : delete";
126                        $query = 'DELETE FROM '.PSLI_ENTITIES_MAPS_TABLE.' WHERE id_entity = '.$this->id.';';
127                        pwg_query($query);
128                        $query = 'DELETE FROM '.PSLI_ENTITIES_TABLE.' WHERE id = '.$this->id.';';
129                        pwg_query($query);
130                        $this->status = false;
131                        return true;
132                }
133                else   
134                        return false;
135        }
136        /* save
137                        Save entity data to the database
138        */
139        function save() {
140                if (($this->status == true) && ($this->data_changed == true))
141                {
142                        $query = '
143                                UPDATE '.PSLI_ENTITIES_TABLE.'
144                                        SET lat = '.$this->lat.',
145                                                lon = '.$this->lon.',
146                                                zoommin = '.$this->zoommin.',
147                                                zoommax = '.$this->zoommax.',
148                                                action = '.$this->action.',
149                                                id_action = '.$this->idaction.',
150                                                title = "'.addslashes($this->title).'"
151                                        WHERE id = '.$this->id.';';
152                        pwg_query($query);
153                       
154                        $q = 'DELETE FROM '.PSLI_ENTITIES_MAPS_TABLE.' WHERE id_entity = '.$this->id.';';
155                        pwg_query($q);                         
156                               
157                        foreach ($this->maps as $map)
158                        {
159                                $q = '
160                                        INSERT INTO '.PSLI_ENTITIES_MAPS_TABLE.' (id_entity, id_map)
161                                                VALUES ('.$this->id.', '.$map.');';
162                                pwg_query($q);
163                        }
164                       
165                        $this->data_changed = false;
166                }
167        }
168        /* open
169                        Open entity identified by id $open
170        */
171        function open ($open) {
172                $query = '
173                        SELECT id, lat, lon, zoomMin, zoomMax, action, id_action, title, type
174                                FROM '.PSLI_ENTITIES_TABLE.'
175                                WHERE id = '.$open.';';
176                $result = pwg_query($query);
177               
178                if (pwg_db_num_rows($result) > 0)
179                {
180                        $row = pwg_db_fetch_assoc($result);
181                        $this->id = $row['id'];
182                        $this->lat = $row['lat'];
183                        $this->lon = $row['lon'];
184                        $this->zoommin = $row['zoomMin'];
185                        $this->zoommax = $row['zoomMax'];
186                        $this->title = $row['title'];
187                        $this->action = $row['action'];
188                        $this->idaction = $row['id_action'];
189                        $this->type = $row['type'];
190                        $this->status = true;
191                        $this->data_changed = false;
192                       
193                        $query = '
194                                SELECT id_map
195                                        FROM '.PSLI_ENTITIES_MAPS_TABLE.'
196                                        WHERE id_entity = '.$this->id.' ORDER BY id_map;'       ;
197                        $result = pwg_query($query);
198                        if (pwg_db_num_rows($result) > 0)
199                        {
200                                while ($row = pwg_db_fetch_assoc($result))
201                                        array_push($this->maps,$row['id_map']);
202                        }
203                       
204                        return true;
205                }
206                else
207                        return false;
208        }
209        /* create
210                        Create an entity with default value
211        */
212        function create($typeentity) {
213                if ($this->status == false)
214                {
215                        switch ($typeentity)
216                        {
217                                case "pin":
218                                        $query = '
219                                                INSERT INTO '.PSLI_ENTITIES_TABLE.'
220                                                        (lat, lon, title, id_action, action, zoomMin, zoomMax, type)
221                                                        VALUES (0, 0, "'.l10n("New Pin").'", 2, 1, 1, 20, 1);';
222                                        $this->type = 1;
223                                        $this->action = 2;
224                                        $this->title = l10n("New Pin");
225                                        break;
226                                case "zone":
227                                        $query = '
228                                                INSERT INTO '.PSLI_ENTITIES_TABLE.'
229                                                        (lat, lon, title, id_action, action, zoomMin, zoomMax, type)
230                                                        VALUES (0, 0, "'.l10n("New Zone").'", 1, 1, 1, 20, 2);';
231                                        $this->type = 2;
232                                        $this->action = 1;
233                                        $this->title = l10n("New Zone");
234                                        break;
235                                case "picture":
236                                        $query = '
237                                                INSERT INTO '.PSLI_ENTITIES_TABLE.'
238                                                        (lat, lon, title, id_action, action, zoomMin, zoomMax, type)
239                                                        VALUES (0, 0, "'.l10n("New Picture").'", 3, 1, 1, 20, 3);';
240                                        $this->type = 3;
241                                        $this->action = 3;
242                                        $this->title = l10n("New Picture");
243                                        break;
244                        }
245                        pwg_query($query);
246                        $this->id = pwg_db_insert_id();
247                        $this->lat = 0;
248                        $this->lon = 0;
249                        $this->idaction = 1;
250                        $this->zoommin = 1;
251                        $this->zoommax = 20;
252                        $this->maps = Array();
253                        $this->status = true;
254                        $this->data_changed = false;
255                        return true;
256                }
257                else
258                        return false;
259        }
260       
261        /***********************************************************************/
262        /* Constructor *********************************************************/
263        function __construct ($openid = 0) {
264                $this->id = 0;
265                $this->lat = 0;
266                $this->lon = 0;
267                $this->zoom = 3;
268                $this->title = l10n("New Entity");
269                $this->status = false;
270                $this->maps = Array();
271                $this->data_changed = false;
272                if ($openid != 0)
273                        $this->open($openid);
274        }
275       
276        /***********************************************************************/
277        /* Getters  ************************************************************/
278        function getType() {
279                return $this->type;
280        }
281        function getActionId() {
282                return $this->idaction;
283        }
284        function getAction() {
285                return $this->action;
286        }
287        function getLatitude() {
288                return $this->lat;
289        }
290        function getLongitude() {
291                return $this->lon;
292        }
293        function getZoomMax() {
294                return $this->zoommax;
295        }
296        function getZoomMin() {
297                return $this->zoommin;
298        }
299        function getId() {
300                return $this->id;
301        }       
302        function getTitle() {
303                return $this->title;
304        }       
305       
306        /***********************************************************************/
307        /* Setters  ************************************************************/
308        function setAction($type, $value) {
309                if (($this->status == true) && ($type != $this->action))
310                {
311                        $this->action = $type;
312                        $this->data_changed = true;
313                }
314                if (($this->status == true) && ($value != $this->idaction))
315                {
316                        $this->idaction = $value;
317                        $this->data_changed = true;
318                }
319        }
320        function setZoomMax($value) {
321                if (($this->status == true) && ($value != $this->zoommax))
322                {
323                        $this->zoommax = $value;
324                        $this->data_changed = true;
325                }
326        }
327        function setZoomMin($value) {
328                if (($this->status == true) && ($value != $this->zoommin))
329                {
330                        $this->zoommin = $value;
331                        $this->data_changed = true;
332                }
333        }
334        function setLongitude($value) {
335                if (($this->status == true) && ($value != $this->lon))
336                {
337                        $this->lon = $value;
338                        $this->data_changed = true;
339                }
340        }
341        function setLatitude($value) {
342                if (($this->status == true) && ($value != $this->lat))
343                {
344                        $this->lat = $value;
345                        $this->data_changed = true;
346                }
347        }
348        function setTitle($value) {
349                if (($this->status == true) && ($value != $this->title))
350                {
351                        $this->title = $value;
352                        $this->data_changed = true;
353                }
354        }
355       
356        /***********************************************************************/
357        /* Methods for associated maps *****************************************/
358        /* isMapAssociated
359                        Return true if map identified by $map is associated with this entity
360        */
361        function isMapAssociated($map) {
362                return in_array($map, $this->maps);
363        }
364        /* clearMaps
365                        Remove all associated maps
366        */
367        function clearMaps () {
368                $this->maps = Array();
369                $this->data_changed = true;
370        }
371        /* addMap
372                        Add an associated map identified by $map
373        */
374        function addMap($map) {
375                array_push($this->maps,$map);
376                $this->data_changed = true;
377        }
378}
379?>
Note: See TracBrowser for help on using the repository browser.