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

Last change on this file since 31990 was 17653, checked in by grum, 12 years ago

bug:2710
bug:2731

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