source: extensions/GMaps/gmaps_root.class.inc.php @ 15907

Last change on this file since 15907 was 12213, checked in by grum, 13 years ago

fix bugs
bug:2042 - Category map is not available if option "apply to subcategories" is not selected

  • Property svn:executable set to *
File size: 12.3 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : GMaps
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.fr
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  GMaps_root : common classe for admin and public classes
13
14  --------------------------------------------------------------------------- */
15  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php');
16  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCss.class.inc.php');
17
18  class GMaps_root extends CommonPlugin
19  {
20    const KML_DIRECTORY='plugins/GMaps/kml/';
21    const ID_MODE_CATEGORY = 'C';
22    const ID_MODE_MAP='M';
23
24    protected $css;
25    protected $maps=array();
26    protected $forceDisplay=0;
27
28    /**
29     * check the available AMD release
30     */
31    static public function checkAMDRelease()
32    {
33      if(!defined('AMD_VERSION')) return(false);
34
35      $currentAMDRelease = explode(".", GPC_VERSION);
36
37      $neededAMDRelease=explode('.', GMAPS_AMD_NEEDED);
38      $major=$neededAMDRelease[0];
39      $minor=$neededAMDRelease[1];
40      $minor2=$neededAMDRelease[2];
41
42      if(($currentAMDRelease[0]>$major) ||
43         ($currentAMDRelease[0]==$major)&&($currentAMDRelease[1]>$minor) ||
44         ($currentAMDRelease[0]==$major)&&($currentAMDRelease[1]==$minor)&&($currentAMDRelease[2]>=$minor2))
45      {
46        return(true);
47      }
48      return(false);
49    }
50
51    /**
52     * check if AMD plugin is activated and mode
53     *
54     * @return String : 'none' if plugin is not installed
55     *                  'inactive' if plugin is not active
56     *                  'basic' if plugin active in basic mode
57     *                  'advanced' if plugin active in advanced mode
58     */
59    static public function checkAMDActivated()
60    {
61      if(!self::checkAMDRelease()) return('none');
62
63      $sql="SELECT state FROM ".PLUGINS_TABLE." WHERE id='AMetaData';";
64      $result=pwg_query($sql);
65      if($result)
66      {
67        $row=pwg_db_fetch_assoc($result);
68        if(!(isset($row['state']) and $row['state']='active')) return('inactive');
69
70        $amdConfig=array();
71        GPCCore::loadConfig('amd', $amdConfig);
72
73        if($amdConfig['newInstall']=='n') return($amdConfig['amd_InterfaceMode']);
74      }
75
76      return('none');
77    }
78
79
80    public function __construct($prefixeTable, $filelocation)
81    {
82      $this->setPluginName('GMaps');
83      $this->setPluginNameFiles("gmaps");
84      parent::__construct($prefixeTable, $filelocation);
85      $this->section_name=$this->getPluginNameFiles();
86
87      $this->setTablesList(array('maps', 'category_maps', 'cache', 'cache_id', 'kmlfiles'));
88      $this->css = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles().".css");
89    }
90
91    public function __destruct()
92    {
93      parent::__destruct();
94    }
95
96    public function initEvents()
97    {
98    }
99
100
101    /*
102      surchage of CommonPlugin->saveConfig function
103    */
104    public function saveConfig()
105    {
106      if(parent::saveConfig())
107      {
108        return(true);
109      }
110      return(false);
111    }
112
113    /*
114      surchage of CommonPlugin->saveConfig function
115    */
116    public function loadConfig()
117    {
118      parent::loadConfig();
119    }
120
121    /*
122      intialize default values
123    */
124    public function initConfig()
125    {
126      //global $user;
127      $this->config=array(
128        'popupAutomaticSize' => 0.8
129      );
130    }
131
132
133    /**
134     *
135     */
136    protected function configForTemplate()
137    {
138      global $template;
139
140      $template->assign('gmapsConfig', $this->config);
141    }
142
143
144    public function getAdminLink($mode='')
145    {
146      if($mode=='ajax')
147      {
148        return('plugins/'.basename(dirname($this->getFileLocation())).'/gmaps_ajax.php');
149      }
150      else
151      {
152        return(parent::getAdminLink());
153      }
154    }
155
156    protected function displayResult($action_msg, $result)
157    {
158      global $page;
159
160      if($result)
161      {
162        array_push($page['infos'], $action_msg);
163      }
164      else
165      {
166        array_push($page['errors'], $action_msg);
167      }
168    }
169
170
171    /* ---------------------------------------------------------------------------
172
173    --------------------------------------------------------------------------- */
174
175    /**
176     * update the given bound with the given coords
177     *
178     * @param &Array $bounds : the bounds (N,S,E,W)
179     * @param Array $coords  : coords (0=>lat, 1=>lng)
180     */
181    protected function updateBounds(&$bounds, $coords)
182    {
183      if($bounds['E']<$coords['lng']) $bounds['E']=$coords['lng'];
184      if($bounds['N']<$coords['lat']) $bounds['N']=$coords['lat'];
185      if($bounds['W']>$coords['lng']) $bounds['W']=$coords['lng'];
186      if($bounds['S']>$coords['lat']) $bounds['S']=$coords['lat'];
187    }
188
189    /**
190     * build the list of maps (initialize the $this->maps var)
191     *
192     * used by GMaps_ajax & GMaps_pip classes
193     *
194     * @param String $category : id of categories id
195     * @param String $page : 'P' for picture page, 'C' for category page
196     * @param String $mode : self::ID_MODE_CATEGORY = given id is a category id
197     *                       self::ID_MODE_MAP      = given id is a map id
198     */
199    protected function buildMapList($id, $page, $mode)
200    {
201      global $template ;
202
203      $this->maps=array();
204      $this->forceDisplay=0;
205
206      if($mode==self::ID_MODE_CATEGORY)
207      {
208        if($page=='C')
209        {
210          $where=" displayType='IC' ";
211        }
212        else
213        {
214          $where=" (displayType='IP' or displayType='MP') ";
215        }
216
217        /*
218         * sql request select all possible association, sorted from the highest to
219         * the lowest priority
220         */
221        $sql="SELECT DISTINCT pgcm.id, pgcm.categoryId,
222                pgcm.imgSort, pgcm.applySubCat, pgcm.kmlFileUrl, pgcm.kmlFileId, pgkf.file AS kmlFileUrlId,
223                pgcm.icon, pgcm.title, pgcm.marker,
224                pgmm.displayType, pgmm.sizeMode,
225                pgmm.width, pgmm.height, pgmm.zoomLevel,
226                pgmm.mapType, pgmm.mapTypeControl, pgmm.scaleControl, pgmm.streetViewControl,
227                pgmm.navigationControl, pgmm.style, pgmm.zoomLevelMaxActivated,
228                IF(pgcm.categoryId=0, 0, pct.global_rank) AS priorityRank,
229                pgcm.forceDisplay
230              FROM ((".$this->tables['category_maps']." pgcm
231                    LEFT JOIN ".$this->tables['maps']." pgmm ON pgcm.mapId = pgmm.id)
232                    LEFT JOIN ".CATEGORIES_TABLE." pct ON (FIND_IN_SET(pgcm.categoryId, pct.uppercats)!=0 OR pgcm.categoryId=0))
233                    LEFT JOIN ".$this->tables['kmlfiles']." pgkf ON  pgkf.id = pgcm.kmlFileId
234              WHERE $where ";
235        if($id!=0)
236        {
237          $sql.=" AND (pgcm.categoryId ='$id' or pct.id!='$id' AND pgcm.applySubCat='y') ";
238        }
239        else
240        {
241          $sql.=" AND pgcm.categoryId = 0 AND pgcm.applySubCat='y' ";
242        }
243        $sql.=" ORDER BY priorityRank DESC, pgmm.displayType ASC, pgcm.categoryId ASC, pgcm.imgSort ASC;";
244      }
245      elseif($mode==self::ID_MODE_MAP)
246      {
247        $sql="SELECT DISTINCT
248                pgmm.displayType, pgmm.sizeMode,
249                pgmm.width, pgmm.height, pgmm.zoomLevel,
250                pgmm.mapType, pgmm.mapTypeControl, pgmm.scaleControl, pgmm.streetViewControl,
251                pgmm.navigationControl, pgmm.style, pgmm.zoomLevelMaxActivated,
252                '' AS kmlFileUrl, 0 AS kmlFileId, 'y' AS forceDisplay
253              FROM ".$this->tables['maps']." pgmm
254              WHERE pgmm.id=$id";
255      }
256      else
257      {
258        return(false);
259      }
260      $result=pwg_query($sql);
261
262      if($result)
263      {
264        // for each found row, choose the highest only
265        $pcat='';
266        $displayType=array(
267         'IC' => true,
268         'IP' => true,
269         'MP' => true,
270        );
271        while($row=pwg_db_fetch_assoc($result))
272        {
273          // if an kml file id is given, apply the url of the file (needs to give the complete URI for google)
274          if($row['kmlFileId']>0 and $row['kmlFileUrlId']!='') $row['kmlFileUrl']=get_absolute_root_url().PWG_LOCAL_DIR.self::KML_DIRECTORY.rawurlencode($row['kmlFileUrlId']);
275
276          if($row['displayType']!='MP' and $mode==self::ID_MODE_CATEGORY)
277          {
278            $themeConf=$template->get_template_vars('themeconf');
279            $fileName=preg_replace('/^([i|c]\d+x\d+)(?:_\d+){0,1}(\..*)/i', '$1$2', $row['icon']);
280
281            if(file_exists(PHPWG_ROOT_PATH.$themeConf['icon_dir'].'/gmaps/'.$fileName))
282            {
283              $row['icon']=$themeConf['icon_dir'].'/gmaps/'.$fileName;
284              $row['iconStyle']=false;
285            }
286            else
287            {
288              $row['icon']='plugins/GMaps/img/'.$row['icon'];
289              $row['iconStyle']=true;
290            }
291          }
292
293          if($displayType[$row['displayType']])
294          {
295            if($row['displayType']=='MP')
296            {
297              if($displayType['MP'] and ($row['categoryId']==$pcat or $pcat=='')) $this->maps[]=$row;
298              if($displayType['MP'] and $row['categoryId']!=$pcat and $pcat!='') $displayType['MP']=false;
299              $pcat=$row['categoryId'];
300            }
301            else
302            {
303              $this->maps[]=$row;
304              $displayType[$row['displayType']]=false;
305              if($row['forceDisplay']=='y' and ($row['kmlFileUrl']!='' or $row['kmlFileId']!=0)) $this->forceDisplay++;
306            }
307          }
308        }
309      }
310    }
311
312
313    /**
314     * returns a new requestId for the cache
315     */
316    protected function getCacheRequestId()
317    {
318      global $user;
319
320      $returned=0;
321
322      // new requestId
323      $sql="INSERT INTO ".$this->tables['cache_id']."
324            VALUES ('', '".$user['id']."', '".date('Y-m-d H:i:s')."');";
325      $result=pwg_query($sql);
326      if($result)
327      {
328        $returned=pwg_db_insert_id();
329      }
330
331      return($returned);
332    }
333
334    /**
335     * update date for a cache requestId
336     */
337    protected function updateCacheRequestId($requestId)
338    {
339      global $user;
340
341      // new requestId
342      $sql="UPDATE ".$this->tables['cache_id']."
343            SET `date` = '".date('Y-m-d H:i:s')."'
344            WHERE requestId='$requestId';";
345      pwg_query($sql);
346    }
347
348    /**
349     * clean the cache : cache values older than given number of second are
350     * deleted
351     *
352     * @param Integer $time : number of second
353     */
354    protected function cleanCache($time=300)
355    {
356      $date=date('Y-m-d H:i:s', time()-$time);
357
358      $sql="DELETE FROM ".$this->tables['cache']."
359              USING ".$this->tables['cache']."
360                JOIN ".$this->tables['cache_id']." pgci
361                ON ".$this->tables['cache'].".requestId = pgci.requestId
362            WHERE pgci.`date` < '$date';";
363      pwg_query($sql);
364
365      $sql="DELETE FROM ".$this->tables['cache_id']."
366            WHERE `date` < '$date';";
367      pwg_query($sql);
368    }
369
370  } //class
371
372
373
374class GMaps_functions
375{
376  static private $tables = Array();
377  static private $config = Array();
378
379  /**
380   * initialise the class
381   *
382   * @param String $prefixeTable : the piwigo prefixe used on tables name
383   * @param String $pluginNameFile : the plugin name used for tables name
384   */
385  static public function init($prefixeTable)
386  {
387    GPCCore::loadConfig(GMaps_root::$pluginNameFile, self::$config);
388    $list=GMaps_root::$pluginTables;
389
390    for($i=0;$i<count($list);$i++)
391    {
392      self::$tables[$list[$i]]=$prefixeTable.GMaps_root::$pluginNameFile.'_'.$list[$i];
393    }
394  }
395
396
397  /**
398   *  return all HTML&JS code necessary to display a dialogbox to choose
399   *  geographic area
400   */
401  static public function dialogBoxGMaps()
402  {
403    global $template;
404
405    $template->set_filename('gmaps_choose',
406                  dirname(__FILE__).'/templates/gmaps_dialog_area_choose.tpl');
407
408    $datas=Array();
409
410    $template->assign('datas', $datas);
411
412    return($template->parse('gmaps_choose', true));
413  }
414
415  /**
416   * convert a decimal degree to D°M'S'
417   *
418   * @param Float $value : the value to convert
419   * @return String : value converted in DMS
420   */
421  static public function DDtoDMS($value)
422  {
423    $degrees=(int)$value;
424    $value=($value-$degrees)*60;
425    $minutes=(int)$value;
426    $seconds=($value-$minutes)*60;
427
428    return(sprintf('%d° %d\' %.2f"', $degrees, $minutes, $seconds));
429  }
430} //GMaps_functions
431
432
433
434?>
Note: See TracBrowser for help on using the repository browser.