Gino Gmaps  1.0.1
Modulo di gestione di mappe interattive per Gino CMS
 Tutto Strutture dati File Funzioni Variabili Gruppi Pagine
class.gmapsPolygon.php
Vai alla documentazione di questo file.
1 <?php
41 class gmapsPolygon extends propertyObject {
42 
43  public static $_tbl_polygon = "gmaps_polygon";
44  public static $_tbl_polygon_point = "gmaps_polygon_point";
45  public static $_tbl_polygon_polygon_ctg = "gmaps_polygon_polygon_ctg";
46  public static $_tbl_map_polygon = "gmaps_map_polygon";
47 
48  private $_controller;
49 
54 
58  private $_ctgs, $_ctgs_id;
59 
68  function __construct($id, $instance) {
69 
70  $this->_controller = $instance;
71  $this->_tbl_data = self::$_tbl_polygon;
72  parent::__construct($id);
73 
74  $this->_points = array();
75  $this->_points_id = array();
76  $query = "SELECT point_id FROM ".self::$_tbl_polygon_point." WHERE polygon_id='".$this->id."'";
77  $a = $this->_db->selectquery($query);
78  if(count($a)) {
79  foreach($a as $b) {
80  $this->_points[] = new gmapsPoint($b['point_id'], $this->_controller);
81  $this->_points_id[] = $b['point_id'];
82  }
83  }
84 
85  $this->_ctgs_id = array();
86  $this->_ctgs = array();
87  $query = "SELECT ctg_id FROM ".self::$_tbl_polygon_polygon_ctg." WHERE polygon_id='".$this->id."'";
88  $a = $this->_db->selectquery($query);
89  if(count($a)) {
90  foreach($a as $b) {
91  $this->_ctgs[] = new gmapsPolygonCtg($b['ctg_id'], $this->_controller);
92  $this->_ctgs_id[] = $b['ctg_id'];
93  }
94  }
95  }
96 
104  public function setInstance($value) {
105 
106  if($this->_p['instance']!=$value && !in_array('instance', $this->_chgP)) $this->_chgP[] = 'instance';
107  $this->_p['instance'] = $value;
108  return true;
109 
110  }
111 
120  public static function deleteInstance($instance) {
121 
122  $db = db::instance();
123 
124  // delete polygon point association
125  $query = "DELETE FROM ".self::$_tbl_polygon_point." WHERE polygon_id IN (SELECT id FROM ".self::$_tbl_polygon." WHERE instance='$instance')";
126  $res = $db->actionquery($query);
127 
128  // delete polygon ctg association
129  $query = "DELETE FROM ".self::$_tbl_polygon_polygon_ctg." WHERE polygon_id IN (SELECT id FROM ".self::$_tbl_polygon." WHERE instance='$instance')";
130  $res = $res && $db->actionquery($query);
131 
132  // delete polygon map association
133  $query = "DELETE FROM ".self::$_tbl_map_polygon." WHERE polygon_id IN (SELECT id FROM ".self::$_tbl_polygon." WHERE instance='$instance')";
134  $res = $res && $db->actionquery($query);
135 
136  // delete polygons
137  $query = "DELETE FROM ".self::$_tbl_polygon." WHERE instance='$instance'";
138  $res = $res && $db->actionquery($query);
139 
140  return $res;
141 
142  }
143 
156  public static function get($instance, $opts=null) {
157 
158  $where = "instance='".$instance."'".((isset($opts['where']) && $opts['where']) ? " AND ".$opts['where'] : '');
159  $order = isset($opts['order']) ? $opts['order'] : 'label';
160  $limit = isset($opts['limit']) ? "LIMIT ".$opts['limit'] : '';
161 
162  $res = array();
163 
164  $db = db::instance();
165  $query = "SELECT id FROM ".self::$_tbl_polygon." WHERE $where ORDER BY $order $limit";
166  $a = $db->selectquery($query);
167  foreach($a as $b) {
168  $res[] = $b['id'];
169  }
170 
171  return $res;
172 
173  }
174 
183  public static function getByCtg($ctg_id) {
184 
185  $res = array();
186 
187  $db = db::instance();
188  $query = "SELECT polygon_id FROM ".self::$_tbl_polygon_polygon_ctg." WHERE ctg_id='$ctg_id'";
189  $a = $db->selectquery($query);
190  foreach($a as $b) {
191  $res[] = $b['polygon_id'];
192  }
193 
194  return $res;
195 
196  }
197 
204  public function points() {
205 
206  return $this->_points;
207 
208  }
209 
216  public function points_id() {
217 
218  return $this->_points_id;
219 
220  }
221 
228  public function ctgs() {
229 
230  return $this->_ctgs;
231 
232  }
233 
241  public function addPoints($point_ids) {
242 
243  if(!is_array($point_ids)) $points = array($point_ids);
244 
245  $values = array();
246  foreach($point_ids as $pid) {
247  $values[] = "('".$this->id."', '".$pid."')";
248  }
249 
250  $db = db::instance();
251  $query = "INSERT INTO ".self::$_tbl_polygon_point." (polygon_id, point_id) VALUES ".implode(',', $values).";";
252 
253  return $db->actionquery($query);
254  }
255 
263  public function removePoints($point_ids) {
264 
265  if(!is_array($point_ids)) $points = array($point_ids);
266 
267  $values = array();
268  foreach($point_ids as $pid) {
269  $values[] = "point_id='".$pid."'";
270  }
271 
272  $db = db::instance();
273  $query = "DELETE FROM ".self::$_tbl_polygon_point." WHERE polygon_id='".$this->id."' AND (".implode(" OR ", $values).")";
274 
275  return $db->actionquery($query);
276  }
277 
285  public function form($redirect) {
286 
287  if(isset($_POST['submit'])) {
288  $gform = new Form('form_polygon', 'post', false, array('verifyToken'=>true));
289  $gform->save('dataform');
290  $req_error = $gform->arequired();
291  if(!$req_error) {
292  $ctgs_id = cleanVar($_POST, 'ctg', 'array', '');
293  $this->label = cleanVar($_POST, 'label', 'string', '');
294  $this->lat = cleanVar($_POST, 'lat', 'string', '');
295  $this->lng = cleanVar($_POST, 'lng', 'string', '');
296  $this->description = cleanVarEditor($_POST, 'description', '');
297  $this->color = cleanVar($_POST, 'color', 'string', '');
298  $this->width = cleanVar($_POST, 'width', 'int', '');
299  $this->updateDbData();
300  $this->saveCtgs($ctgs_id);
301  header("Location: ".$redirect);
302  exit();
303  }
304  else {
305  if(!($_POST['lat'] && $_POST['lng'])) {
306  exit(error::errorMessage(array('error'=>_("Rappresentare l'area sulla mappa seguendo le istruzioni.")), $_SERVER['QUERY_STRING']));
307  }
308  exit(error::errorMessage(array('error'=>1), $_SERVER['QUERY_STRING']));
309  }
310  }
311 
312  $registry = registry::instance();
313  $registry->addJs("http://maps.googleapis.com/maps/api/js?key=AIzaSyArAE-uBvCZTRaf_eaFn4umUdESmoUvoxM&sensor=true");
314  $registry->addJs("http://ajs.otto.to.it/sources/0.1/ajs/ajs.js");
315 
316  if($this->id) {
317  $title = _("Modifica area");
318  $submit = _("modifica");
319  }
320  else {
321  $title = _("Nuova area");
322  $submit= _("inserisci");
323  }
324  $htmlsection = new htmlSection(array('class'=>'admin', 'headerTag'=>'header', 'headerLabel'=>$title));
325 
326  $gform = new Form('form_polygon', 'post', true, array('trnsl_table'=>$this->_tbl_data, 'trnsl_id'=>$this->id));
327  $gform->load('dataform');
328  $required = "label,ctg,lat,lng";
329  $buffer = $gform->form('', false, $required, array('generateToken'=>true));
330  $buffer .= $gform->hidden('id', $this->id);
331 
332  $buffer .= $gform->hidden('lat', $this->lat, array('id'=>'lat'));
333  $buffer .= $gform->hidden('lng', $this->lng, array('id'=>'lng'));
334 
335  $buffer .= $gform->multipleCheckbox('ctg[]', $gform->retvar('ctg', $this->_ctgs_id), gmapsPolygonCtg::getForSelect($this->instance), _("Categorie"), array('required'=>true));
336  $buffer .= $gform->cinput('label', 'text', $gform->retvar('label', htmlInput($this->label)), _("Etichetta"), array('required'=>true, 'maxlength'=>200, 'size'=>40, 'trnsl'=>true, 'field'=>'label'));
337  $buffer .= $gform->cell($this->formMap());
338  $buffer .= $gform->fcktextarea('description', $gform->retvar('description', htmlInputEditor($this->description)), _("Descrizione"), array('fck_toolbar'=>"Default", 'trnsl'=>true, 'field'=>'description'));
339  $buffer .= $gform->cinput('color', 'text', $gform->retvar('color', htmlInput($this->color)), array(_("Colore"), _("codice esadecimale, es. #ff0000")), array('required'=>true, 'maxlength'=>7, 'size'=>7, 'pattern'=>'^#[0-9abcdefABCDEF]{6}$', 'hint'=>_("Inserire il colore in esadecimale")));
340  $buffer .= $gform->cinput('width', 'text', $gform->retvar('width', htmlInput($this->width)), array(_("Spessore tracciato"), _("in px")), array('required'=>true, 'maxlength'=>2, 'size'=>2, 'pattern'=>'^[0-9]*$', 'hint'=>_("Inserire un numero intero")));
341  $buffer .= $gform->cinput('submit', 'submit', $submit, '', array('classField'=>'submit'));
342 
343  $buffer .= $gform->cform();
344 
345  $htmlsection->content = $buffer;
346 
347  return $htmlsection->render();
348 
349  }
350 
357  private function formMap() {
358 
359  $buffer = "<p>Per geolocalizzare l'area utilizzare lo strumento mappa qui sotto. Seguire la seguente procedura:</p>";
360  $buffer .= "<ol>";
361  $buffer .= "<li>"._("Premere il pulsante 'polygon'")."</li>";
362  $buffer .= "<li>"._("Cliccare nel punto desiderato sulla mappa per settare ogni punto del percorso. Se si dispone di un indirizzo si può utilizzare il campo di testo, scrivere l'indirizzo e poi premere 'draw'. E' possibile modificare direttamente l'area agendo sui controlli.")."</li>";
363  $buffer .= "<li>"._("Quando l'area tracciata corrisponde a quella desiderato permere il pulsante 'export map'.")."</li>";
364  $buffer .= "</ol>";
365  $buffer .= "<div id=\"map_canvas\" style=\"width:100%; height: 300px;\"></div>";
366 
367  $buffer .= "<script>";
368  $buffer .= "ajs.use(['ajs.maps.gmapdraw'], function() {
369 
370  fillFields = function(data) {
371  var lat = [];
372  var lng = [];
373  for(var i = 0, tot = data.polygon[0].length; i < tot; i++) {
374  lat.push(data.polygon[0][i].lat);
375  lng.push(data.polygon[0][i].lng);
376  }
377  $('lat').set('value', lat.join(','));
378  $('lng').set('value', lng.join(','));
379  alert('".jsVar(_("L'area è stata settata correttamente, prosegui con la compilazione del form"))."');
380  }
381 
382  mymap = new ajs.maps.gmapdraw.map('map_canvas', {
383  zoom: 10,
384  export_map_callback: fillFields,
385  tools: {'polygon': { options: { max_items_allowed: 1 }}},
386  tips_map_ctrl: null
387  });
388  mymap.render();
389 
390  if($('lat').value && $('lng').value) {
391 
392  var lat = $('lat').value.split(',');
393  var lng = $('lng').value.split(',');
394 
395  var parr = [];
396  for(var i = 0, tot = lat.length; i < tot; i++) {
397  parr.push({lat: lat[i], lng: lng[i]});
398  }
399 
400  var data = {polygon: [parr]};
401  mymap.importMap(data);
402  mymap.gmap().setCenter(new google.maps.LatLng(lat[(Math.round(lat.length/2))], lng[(Math.round(lng.length/2))]));
403  }
404 
405 
406  });";
407  $buffer .= "</script>";
408 
409  return $buffer;
410 
411  }
412 
420  private function saveCtgs($ctgs_id) {
421 
422  $db = db::instance();
423  $query = "DELETE FROM ".self::$_tbl_polygon_polygon_ctg." WHERE polygon_id='".$this->id."'";
424  $res = $db->actionquery($query);
425 
426  $values = array();
427  foreach($ctgs_id as $ctg_id) {
428  $values[] = "('".$this->id."', '".$ctg_id."')";
429  }
430  $query = "INSERT INTO ".self::$_tbl_polygon_polygon_ctg." (polygon_id, ctg_id) VALUES ".implode(',', $values).";";
431  $res = $db->actionquery($query);
432 
433  return $res;
434 
435  }
436 
445  public function gmapCode($map, $bounds) {
446 
447  $buffer = "var polygon_path_".$this->id." = new google.maps.MVCArray();";
448  $lats = explode(',', $this->lat);
449  $lngs = explode(',', $this->lng);
450  for($i=0; $i<count($lats); $i++) {
451  $lat = $lats[$i];
452  $lng = $lngs[$i];
453  $buffer .= "polygon_path_".$this->id.".push(new google.maps.LatLng($lat, $lng));";
454  $buffer .= $bounds.".extend(new google.maps.LatLng($lat, $lng));";
455  }
456  $buffer .= " var polygon_".$this->id." = new google.maps.Polygon({path: polygon_path_".$this->id.", map:$map});";
457 
458  foreach($this->_points as $point) {
459  $buffer .= $point->gmapCode($map, $bounds);
460  }
461 
462  return $buffer;
463  }
464 
473  public function jsObject($map_obj, $map_id) {
474 
475  $db = db::instance();
476 
477  $buffer = '';
478 
479  $categories = array();
480  foreach($this->_ctgs as $ctg) {
481  $categories[] = jsVar($ctg->ml('name'));
482  }
483 
484  $fields = "{
485  id: '".$this->id."',
486  label: '".jsVar($this->ml('label'))."',
487  categories: '".implode(', ', $categories)."',
488  description: '".jsVar(cutHtmlText(HtmlChars($this->ml('description')), 120, '...', false, false, false, null))."',
489  lat: '".jsVar($this->lat)."',
490  lng: '".jsVar($this->lng)."',
491  color: '".$this->color."',
492  width: '".$this->width."'
493  }";
494 
495 
496  $info_url = HOME_FILE.'?pt['.$this->_controller->instanceName().'-infowindow]&polygon_id='.$this->id.'&map_id='.$map_id;
497 
498  $buffer .= "new GmapPolygon($fields, [".implode(',', $this->_ctgs_id)."], $map_obj, '$info_url');";
499 
500  return $buffer;
501 
502  }
503 
511  public function delete() {
512 
513  $db = db::instance();
514 
515  // delete ctg association
516  $query = "DELETE FROM ".self::$_tbl_polygon_polygon_ctg." WHERE polygon_id='".$this->id."'";
517  $res = $db->actionquery($query);
518 
519  // delete map association
520  $query = "DELETE FROM ".self::$_tbl_map_polygon." WHERE polygon_id='".$this->id."'";
521  $res = $res && $db->actionquery($query);
522 
523  // delete point association
524  $query = "DELETE FROM ".self::$_tbl_polygon_point." WHERE polygon_id='".$this->id."'";
525  $res = $res && $db->actionquery($query);
526 
527  $res = $res && $this->deleteDbData();
528 
529  return $res;
530 
531  }
532 }
533 
534 ?>