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

Last change on this file since 7453 was 7308, checked in by grum, 13 years ago

fix bug and implement new features
bug:1926, bug:1927, bug:1929, bug:1930, bug:1931, bug:1939, bug:1946

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