source: extensions/GMaps/gmaps_ajax.php @ 7054

Last change on this file since 7054 was 7054, checked in by grum, 14 years ago

First commit

File size: 11.4 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: GMaps
5 * -----------------------------------------------------------------------------
6 * Author     : Grum
7 *   email    : grum@piwigo.org
8 *   website  : http://photos.grum.fr
9 *   PWG user : http://forum.piwigo.org/profile.php?id=3706
10 *
11 *   << May the Little SpaceFrog be with you ! >>
12 *
13 * -----------------------------------------------------------------------------
14 *
15 * See main.inc.php for release information
16 *
17 * manage all the ajax requests
18 * -----------------------------------------------------------------------------
19 */
20
21  define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
22
23  /*
24   * set ajax module in admin mode if request is used for admin interface
25   */
26  if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
27  if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']))
28  {
29    define('IN_ADMIN', true);
30  }
31
32  // the common.inc.php file loads all the main.inc.php plugins files
33  include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
34  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
35  include_once('gmaps_root.class.inc.php');
36
37  load_language('plugin.lang', GMAPS_PATH);
38
39
40  class GMaps_Ajax extends GMaps_root
41  {
42    /**
43     * constructor
44     */
45    public function __construct($prefixeTable, $filelocation)
46    {
47      parent::__construct($prefixeTable, $filelocation);
48      $this->loadConfig();
49      $this->checkRequest();
50      $this->returnAjaxContent();
51    }
52
53    /**
54     * check the $_REQUEST values and set default values
55     *
56     */
57    protected function checkRequest()
58    {
59      global $user;
60
61      if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
62
63      // check if asked function is valid
64      if(!($_REQUEST['ajaxfct']=='admin.maps.getList' or
65           $_REQUEST['ajaxfct']=='admin.maps.setMap' or
66           $_REQUEST['ajaxfct']=='admin.maps.getMap' or
67           $_REQUEST['ajaxfct']=='admin.maps.deleteMap'
68           )) $_REQUEST['ajaxfct']='';
69
70      if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
71
72
73      if($_REQUEST['ajaxfct']!='')
74      {
75        /*
76         * check admin.maps.getList values
77         */
78        if($_REQUEST['ajaxfct']=="admin.maps.getList")
79        {
80        }
81
82        /*
83         * check admin.maps.getMap values
84         */
85        if($_REQUEST['ajaxfct']=="admin.maps.getMap")
86        {
87          if(!isset($_REQUEST['id']) or
88             !is_numeric($_REQUEST['id']) or
89             $_REQUEST['id']=='') $_REQUEST['ajaxfct']='';
90        }
91
92        /*
93         * check admin.maps.deleteMap values
94         */
95        if($_REQUEST['ajaxfct']=="admin.maps.deleteMap")
96        {
97          if(!isset($_REQUEST['id']) or
98             !is_numeric($_REQUEST['id']) or
99             $_REQUEST['id']=='') $_REQUEST['ajaxfct']='';
100        }
101
102        /*
103         * check admin.maps.gsetMap values
104         */
105        if($_REQUEST['ajaxfct']=="admin.maps.setMap")
106        {
107          if(!isset($_REQUEST['id']) or
108             !isset($_REQUEST['datas']) or !is_array($_REQUEST['datas']))
109          {
110            $_REQUEST['ajaxfct']='';
111          }
112          else
113          {
114            if(!(isset($_REQUEST['datas']['mapId']) &&
115                 isset($_REQUEST['datas']['name']) &&
116                 isset($_REQUEST['datas']['width']) &&
117                 isset($_REQUEST['datas']['height']) &&
118                 isset($_REQUEST['datas']['zoomLevel']) &&
119                 isset($_REQUEST['datas']['mapType']) &&
120                 isset($_REQUEST['datas']['mapTypeControl']) &&
121                 isset($_REQUEST['datas']['navigationControl']) &&
122                 isset($_REQUEST['datas']['scaleControl']) &&
123                 isset($_REQUEST['datas']['style'])))
124            {
125              $_REQUEST['ajaxfct']='';
126            }
127            else
128            {
129              if(!is_numeric($_REQUEST['datas']['width']) or
130                  $_REQUEST['datas']['width']<100 or
131                  $_REQUEST['datas']['width']>1280) $_REQUEST['datas']['width']=470;
132
133              if(!is_numeric($_REQUEST['datas']['height']) or
134                  $_REQUEST['datas']['height']<100 or
135                  $_REQUEST['datas']['height']>1280) $_REQUEST['datas']['height']=210;
136
137              if(!is_numeric($_REQUEST['datas']['zoomLevel']) or
138                  $_REQUEST['datas']['zoomLevel']<1 or
139                  $_REQUEST['datas']['zoomLevel']>20) $_REQUEST['datas']['zoomLevel']=4;
140
141              if(!($_REQUEST['datas']['mapType']=='hybrid' or
142                  $_REQUEST['datas']['mapType']=='terrain' or
143                  $_REQUEST['datas']['mapType']=='roadmap' or
144                  $_REQUEST['datas']['mapType']=='satellite')) $_REQUEST['datas']['mapType']='hybrid';
145
146              if(!is_numeric($_REQUEST['datas']['mapTypeControl']) or
147                  $_REQUEST['datas']['mapTypeControl']<-1 or
148                  $_REQUEST['datas']['mapTypeControl']>2) $_REQUEST['datas']['mapTypeControl']=0;
149
150              if(!is_numeric($_REQUEST['datas']['navigationControl']) or
151                  $_REQUEST['datas']['navigationControl']<-1 or
152                  $_REQUEST['datas']['navigationControl']>3) $_REQUEST['datas']['navigationControl']=0;
153
154              if(!($_REQUEST['datas']['scaleControl']=='y' or
155                   $_REQUEST['datas']['scaleControl']=='n')) $_REQUEST['datas']['scaleControl']='y';
156            }
157          }
158        }
159
160
161      }
162    } //checkRequest
163
164
165    /**
166     * return ajax content
167     */
168    protected function returnAjaxContent()
169    {
170      $result="<p class='errors'>An error has occured</p>";
171      switch($_REQUEST['ajaxfct'])
172      {
173        case 'admin.maps.getList':
174          $result=$this->ajax_gmaps_admin_mapsGetList();
175          break;
176        case 'admin.maps.getMap':
177          $result=$this->ajax_gmaps_admin_mapsGetMap($_REQUEST['id']);
178          break;
179        case 'admin.maps.setMap':
180          $result=$this->ajax_gmaps_admin_mapsSetMap($_REQUEST['id'], $_REQUEST['datas']);
181          break;
182        case 'admin.maps.deleteMap':
183          $result=$this->ajax_gmaps_admin_mapsDeleteMap($_REQUEST['id']);
184          break;
185      }
186      GPCAjax::returnResult($result);
187    }
188
189
190    /*------------------------------------------------------------------------*
191     *
192     * ADMIN FUNCTIONS
193     *
194     *----------------------------------------------------------------------- */
195
196    /**
197     * return a HTML list of defined maps
198     *
199     * @return String
200     */
201    private function ajax_gmaps_admin_mapsGetList()
202    {
203      global $template;
204
205      $template->set_filename('list_page',
206                    dirname($this->getFileLocation()).'/admin/gmaps_maps_iListMaps.tpl');
207
208      $datas=Array();
209
210      $sql="SELECT id, mapId, name, width, height, zoomLevel, mapType, mapTypeControl, navigationControl, scaleControl
211            FROM ".$this->tables['maps']."
212            ORDER BY id";
213      $result=pwg_query($sql);
214      if($result)
215      {
216        while($row=pwg_db_fetch_assoc($result))
217        {
218          $row['dimensions']=$row['width'].'x'.$row['height'];
219
220          $row['mapType']=l10n('gmaps_googleMapType_'.$row['mapType']);
221          $row['mapTypeControl']=l10n('gmaps_googleMapTypeControl_'.$row['mapTypeControl']);
222          $row['navigationControl']=l10n('gmaps_googleMapNavigationControl_'.$row['navigationControl']);
223          $row['scaleControl']=($row['scaleControl']=='y')?l10n('gmaps_display_visible'):l10n('gmaps_display_hidden');
224          $datas[]=$row;
225        }
226      }
227
228      $template->assign('datas', $datas);
229      return($template->parse('list_page', true));
230    } //ajax_gmaps_admin_mapsGetList
231
232
233    /**
234     * set properties for a given map id ; if no map id is given, create a new
235     * map
236     *
237     * @param String id : the map Id
238     * @param Array datas : properties of the map (assuming array index were
239     *                      checked by the checkRequest function)
240     * @return String : the num id, or an error message
241     */
242    private function ajax_gmaps_admin_mapsSetMap($id, $properties)
243    {
244      global $template;
245
246      // if create a new map, check if map id is unique
247      $sql="SELECT id, mapId FROM ".$this->tables['maps']."
248            WHERE mapId='".$properties['mapId']."';";
249      $result=pwg_query($sql);
250      if($result)
251      {
252        while($row=pwg_db_fetch_assoc($result))
253        {
254          if($row['id']!=$id)
255          {
256            return('iBDMapId!'.l10n('gmaps_mapIdAlreadyExist'));
257          }
258        }
259      }
260
261      if($id=='')
262      {
263        $sql="INSERT INTO ".$this->tables['maps']."
264              VALUES ('', '".mysql_escape_string($properties['mapId'])."', '".
265                mysql_escape_string($properties['name'])."', '".
266                $properties['width']."', '".
267                $properties['height']."', '".
268                $properties['zoomLevel']."', '".
269                $properties['mapType']."', '".
270                $properties['mapTypeControl']."', '".
271                $properties['navigationControl']."', '".
272                $properties['scaleControl']."', '".
273                mysql_escape_string($properties['style'])."');";
274        $result=pwg_query($sql);
275        $id=pwg_db_insert_id();
276      }
277      else
278      {
279        $sql="UPDATE ".$this->tables['maps']."
280              SET name='".mysql_escape_string($properties['name'])."', mapId='".
281                $properties['mapId']."', width='".
282                $properties['width']."', height='".
283                $properties['height']."', zoomLevel='".
284                $properties['zoomLevel']."', mapType='".
285                $properties['mapType']."', mapTypeControl='".
286                $properties['mapTypeControl']."', navigationControl='".
287                $properties['navigationControl']."', scaleControl='".
288                $properties['scaleControl']."', style='".
289                mysql_escape_string($properties['style'])."'
290              WHERE id='$id';";
291        $result=pwg_query($sql);
292      }
293
294      return($id);
295    } //ajax_gmaps_admin_mapsSetMap
296
297
298    /**
299     * get properties for a given map id
300     *
301     * @param String id : the map Id
302     * @return String : data formatted in a JSON string
303     */
304    private function ajax_gmaps_admin_mapsGetMap($id)
305    {
306      $returned=array(
307        'id' => '',
308        'name' => '',
309        'mapId' => '',
310        'width' => 470,
311        'height' => 210,
312        'zoomLevel' => 4,
313        'mapType' => 'hybrid',
314        'mapTypeControl' => 0,
315        'navigationControl' => 0,
316        'scaleControl' => 'y'
317      );
318
319      $sql="SELECT id, mapId, name, width, height, zoomLevel, mapType, mapTypeControl, navigationControl, scaleControl
320            FROM ".$this->tables['maps']."
321            WHERE id='$id';";
322      $result=pwg_query($sql);
323      if($result)
324      {
325        while($row=pwg_db_fetch_assoc($result))
326        {
327          $returned=$row;
328        }
329      }
330
331      return(json_encode($returned));
332    }
333
334
335
336    /**
337     * delete a map
338     *
339     * @param String id : if of the map to delete
340     * @return String : ok or ko
341     */
342    private function ajax_gmaps_admin_mapsDeleteMap($id)
343    {
344      $sql="DELETE FROM ".$this->tables['maps']."
345            WHERE id='$id';";
346      $result=pwg_query($sql);
347      if($result)
348      {
349        $sql="DELETE FROM ".$this->tables['category_maps']."
350              WHERE map_id='$id';";
351        $result=pwg_query($sql);
352        if($result) return('ok');
353      }
354
355      return('ko!unknown error');
356    }
357
358
359
360  } //class
361
362
363  $returned=new GMaps_Ajax($prefixeTable, __FILE__);
364?>
Note: See TracBrowser for help on using the repository browser.