Gino Gmaps  1.0.1
Modulo di gestione di mappe interattive per Gino CMS
 Tutto Strutture dati File Funzioni Variabili Gruppi Pagine
class.gmapsPoint.php
Vai alla documentazione di questo file.
1 <?php
59 class gmapsPoint extends propertyObject {
60 
61  private static $_tbl_point = "gmaps_point";
62  private static $_tbl_point_image = "gmaps_point_image";
63  private static $_tbl_point_video = "gmaps_point_video";
64  private static $_tbl_point_event = "gmaps_point_event";
65  private static $_tbl_point_attachment = "gmaps_point_attachment";
66  private static $_tbl_point_collection = "gmaps_point_collection";
67  private static $_tbl_point_collection_image = "gmaps_point_collection_image";
68  private static $_tbl_point_service = "gmaps_point_service";
69  private static $_tbl_point_point_ctg = "gmaps_point_point_ctg";
70  private static $_tbl_map_point = 'gmaps_map_point';
71 
72  private $_controller;
73 
77  private $_ctgs, $_ctgs_id;
78 
83 
87  private $_images;
88 
92  private $_videos;
93 
97  private $_events;
98 
102  private $_attachments;
103 
107  private $_collections;
108 
117  function __construct($id, $instance) {
118 
119  $this->_controller = $instance;
120 
121  $this->_tbl_data = self::$_tbl_point;
122  parent::__construct($id);
123 
124  $this->_ctgs_id = array();
125  $this->_ctgs = array();
126  $query = "SELECT ctg_id FROM ".self::$_tbl_point_point_ctg." WHERE point_id='".$this->id."'";
127  $a = $this->_db->selectquery($query);
128  if(count($a)) {
129  foreach($a as $b) {
130  $this->_ctgs[] = new gmapsPointCtg($b['ctg_id'], $this->_controller);
131  $this->_ctgs_id[] = $b['ctg_id'];
132  }
133  }
134 
135  $this->_services_id = array();
136  $this->_services = array();
137  $query = "SELECT service_id FROM ".self::$_tbl_point_service." WHERE point_id='".$this->id."'";
138  $a = $this->_db->selectquery($query);
139  if(count($a)) {
140  foreach($a as $b) {
141  $this->_services[] = new gmapsService($b['service_id'], $this->_controller);
142  $this->_services_id[] = $b['service_id'];
143  }
144  }
145 
146  $this->_images = array();
147  $query = "SELECT id FROM ".self::$_tbl_point_image." WHERE point_id='".$this->id."'";
148  $a = $this->_db->selectquery($query);
149  if(count($a)) {
150  foreach($a as $b) {
151  $this->_images[] = new gmapsImage($b['id'], $this->_controller);
152  }
153  }
154 
155  $this->_videos = array();
156  $query = "SELECT id FROM ".self::$_tbl_point_video." WHERE point_id='".$this->id."'";
157  $a = $this->_db->selectquery($query);
158  if(count($a)) {
159  foreach($a as $b) {
160  $this->_videos[] = new gmapsVideo($b['id'], $this->_controller);
161  }
162  }
163 
164  $this->_events = array();
165  $query = "SELECT id FROM ".self::$_tbl_point_event." WHERE point_id='".$this->id."'";
166  $a = $this->_db->selectquery($query);
167  if(count($a)) {
168  foreach($a as $b) {
169  $this->_events[] = new gmapsEvent($b['id'], $this->_controller);
170  }
171  }
172 
173  $this->_attachments = array();
174  $query = "SELECT id FROM ".self::$_tbl_point_attachment." WHERE point_id='".$this->id."'";
175  $a = $this->_db->selectquery($query);
176  if(count($a)) {
177  foreach($a as $b) {
178  $this->_attachments[] = new gmapsAttachment($b['id'], $this->_controller);
179  }
180  }
181 
182  $this->_collections = array();
183  $query = "SELECT id FROM ".self::$_tbl_point_collection." WHERE point_id='".$this->id."'";
184  $a = $this->_db->selectquery($query);
185  if(count($a)) {
186  foreach($a as $b) {
187  $this->_collections[] = new gmapsCollection($b['id'], $this->_controller);
188  }
189  }
190  }
191 
199  public function setInstance($value) {
200 
201  if($this->_p['instance']!=$value && !in_array('instance', $this->_chgP)) $this->_chgP[] = 'instance';
202  $this->_p['instance'] = $value;
203  return true;
204 
205  }
206 
215  public static function deleteInstance($instance) {
216 
217  $db = db::instance();
218 
219  // delete point ctg association
220  $query = "DELETE FROM ".self::$_tbl_point_point_ctg." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
221  $res = $db->actionquery($query);
222 
223  // delete point service association
224  $query = "DELETE FROM ".self::$_tbl_point_service." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
225  $res = $res && $db->actionquery($query);
226 
227  // delete point image association
228  $query = "DELETE FROM ".self::$_tbl_point_image." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
229  $res = $res && $db->actionquery($query);
230 
231  // delete point video association
232  $query = "DELETE FROM ".self::$_tbl_point_video." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
233  $res = $res && $db->actionquery($query);
234 
235  // delete point events association
236  $query = "DELETE FROM ".self::$_tbl_point_event." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
237  $res = $res && $db->actionquery($query);
238 
239  // delete point attachments association
240  $query = "DELETE FROM ".self::$_tbl_point_attachment." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
241  $res = $res && $db->actionquery($query);
242 
243  // delete point map association
244  $query = "DELETE FROM ".self::$_tbl_map_point." WHERE point_id IN (SELECT id FROM ".self::$_tbl_point." WHERE instance='$instance')";
245  $res = $res && $db->actionquery($query);
246 
247  // delete points
248  $query = "DELETE FROM ".self::$_tbl_point." WHERE instance='$instance'";
249  $res = $res && $db->actionquery($query);
250 
251  return $res;
252 
253  }
254 
261  public function ctgs() {
262 
263  return $this->_ctgs;
264 
265  }
266 
273  public function services() {
274 
275  return $this->_services;
276 
277  }
278 
285  public function images() {
286 
287  return $this->_images;
288 
289  }
290 
297  public function videos() {
298 
299  return $this->_videos;
300 
301  }
302 
309  public function events() {
310 
311  return $this->_events;
312 
313  }
314 
321  public function attachments() {
322 
323  return $this->_attachments;
324 
325  }
326 
333  public function collections() {
334 
335  return $this->_collections;
336 
337  }
338 
339 
352  public static function get($instance, $opts=null) {
353 
354  $where = "instance='".$instance."'".((isset($opts['where']) && $opts['where']) ? " AND ".$opts['where'] : '');
355  $ctg = gOpt('ctg', $opts, null);
356  if($ctg) {
357  $where .= " AND id IN (SELECT point_id FROM ".self::$_tbl_point_point_ctg." WHERE ctg_id='".$ctg."')";
358  }
359  $order = isset($opts['order']) ? $opts['order'] : 'label';
360  $limit = isset($opts['limit']) ? "LIMIT ".$opts['limit'] : '';
361 
362  $res = array();
363 
364  $db = db::instance();
365  $query = "SELECT id FROM ".self::$_tbl_point." WHERE $where ORDER BY $order $limit";
366  $a = $db->selectquery($query);
367  foreach($a as $b) {
368  $res[] = $b['id'];
369  }
370 
371  return $res;
372 
373  }
374 
383  public static function getByMarker($marker_id) {
384 
385  $res = array();
386 
387  $db = db::instance();
388  $query = "SELECT point_id FROM ".self::$_tbl_point." WHERE marker='$marker_id'";
389  $a = $db->selectquery($query);
390  foreach($a as $b) {
391  $res[] = $b['point_id'];
392  }
393 
394  return $res;
395 
396  }
397 
398 
407  public static function getByService($service_id) {
408 
409  $res = array();
410 
411  $db = db::instance();
412  $query = "SELECT point_id FROM ".self::$_tbl_point_service." WHERE service='$service_id'";
413  $a = $db->selectquery($query);
414  foreach($a as $b) {
415  $res[] = $b['point_id'];
416  }
417 
418  return $res;
419 
420  }
421 
430  public static function getByCtg($ctg_id) {
431 
432  $res = array();
433 
434  $db = db::instance();
435  $query = "SELECT point_id FROM ".self::$_tbl_point_point_ctg." WHERE ctg_id='$ctg_id'";
436  $a = $db->selectquery($query);
437  foreach($a as $b) {
438  $res[] = $b['point_id'];
439  }
440 
441  return $res;
442 
443  }
444 
452  public function form($redirect) {
453 
454  if(isset($_POST['submit'])) {
455  $gform = new Form('form_point', 'post', false, array('verifyToken'=>true));
456  $gform->save('dataform');
457  $req_error = $gform->arequired();
458  if(!$req_error) {
459  if(!$this->id) {
460  $this->insert_date = date('Y-m-d H:i:s');
461  }
462  $this->last_edit_date = date('Y-m-d H:i:s');
463  $ctgs_id = cleanVar($_POST, 'ctg', 'array', '');
464  $services_id = cleanVar($_POST, 'service', 'array', '');
465  $this->label = cleanVar($_POST, 'label', 'string', '');
466  $this->address = cleanVar($_POST, 'address', 'string', '');
467  $this->cap = cleanVar($_POST, 'cap', 'string', '');
468  $this->city = cleanVar($_POST, 'city', 'string', '');
469  $this->nation = cleanVar($_POST, 'nation', 'int', '');
470  $this->lat = cleanVar($_POST, 'lat', 'string', '');
471  $this->lng = cleanVar($_POST, 'lng', 'string', '');
472  $this->marker = cleanVar($_POST, 'marker', 'int', '');
473  $this->description = cleanVarEditor($_POST, 'description', '');
474  $this->information = cleanVarEditor($_POST, 'information', '');
475  $this->phone = cleanVar($_POST, 'phone', 'string', '');
476  $this->email = cleanVar($_POST, 'email', 'string', '');
477  $this->web = cleanVar($_POST, 'web', 'string', '');
478  $this->opening_hours = cleanVar($_POST, 'web', 'opening_hours', '');
479  $this->updating = cleanVar($_POST, 'web', 'updating', '');
480  $this->updating_email = cleanVar($_POST, 'web', 'updating_email', '');
481  if($this->updating && !$this->updating_email) {
482  exit(error::errorMessage(array('error'=>_("Inserire anche una email per l'invio del codice di aggiornamento")), $_SERVER['QUERY_STRING']));
483  }
484  $this->updateDbData();
485  $this->saveCtgs($ctgs_id);
486  $this->saveServices($services_id);
487  header("Location: ".$redirect);
488  exit();
489  }
490  else {
491  if(!($_POST['lat'] && $_POST['lng'])) {
492  exit(error::errorMessage(array('error'=>_("Rappresentare il punto sulla mappa seguendo le istruzioni.")), $_SERVER['QUERY_STRING']));
493  }
494  exit(error::errorMessage(array('error'=>1), $_SERVER['QUERY_STRING']));
495  }
496  }
497 
498  $registry = registry::instance();
499  $registry->addJs("http://maps.googleapis.com/maps/api/js?key=AIzaSyArAE-uBvCZTRaf_eaFn4umUdESmoUvoxM&sensor=true");
500  $registry->addJs("http://ajs.otto.to.it/sources/0.1/ajs/ajs.js");
501 
502  if($this->id) {
503  $title = _("Modifica punto di interesse");
504  $submit = _("modifica");
505  }
506  else {
507  $title = _("Nuovo punto di interesse");
508  $submit= _("inserisci");
509  }
510  $htmlsection = new htmlSection(array('class'=>'admin', 'headerTag'=>'header', 'headerLabel'=>$title));
511 
512  $gform = new Form('form_point', 'post', true, array('trnsl_table'=>$this->_tbl_data, 'trnsl_id'=>$this->id));
513  $gform->load('dataform');
514  $required = "label,lat,lng,ctg,cap,city,address,nation";
515  $buffer = $gform->form('', false, $required, array('generateToken'=>true));
516  $buffer .= $gform->hidden('id', $this->id);
517 
518  $buffer .= $gform->hidden('lat', $this->lat, array('id'=>'lat'));
519  $buffer .= $gform->hidden('lng', $this->lng, array('id'=>'lng'));
520 
521  $buffer .= $gform->multipleCheckbox('ctg[]', $gform->retvar('ctg', $this->_ctgs_id), gmapsPointCtg::getForSelect($this->instance), _("Categorie"), array('required'=>true));
522  $buffer .= $gform->multipleCheckbox('service[]', $gform->retvar('service', $this->_services_id), gmapsService::getForSelect($this->instance), _("Servizi"), array('required'=>false));
523  $buffer .= $gform->cinput('label', 'text', $gform->retvar('label', htmlInput($this->label)), _("Etichetta"), array('required'=>true, 'maxlength'=>200, 'size'=>40, 'trnsl'=>true, 'field'=>'label'));
524  $buffer .= $gform->cinput('address', 'text', $gform->retvar('address', htmlInput($this->address)), _("Indirizzo"), array('required'=>true, 'maxlength'=>200, 'size'=>40, 'trnsl'=>false));
525  $buffer .= $gform->cinput('cap', 'text', $gform->retvar('cap', htmlInput($this->cap)), _("Cap"), array('required'=>true, 'maxlength'=>16, 'size'=>5, 'trnsl'=>false));
526  $buffer .= $gform->cinput('city', 'text', $gform->retvar('city', htmlInput($this->city)), _("Città"), array('required'=>true, 'maxlength'=>200, 'size'=>40, 'trnsl'=>true, 'field'=>'city'));
527  $query = "SELECT id, ".$_SESSION['lngDft']." FROM nation ORDER BY ".$_SESSION['lngDft']." ASC";
528  $buffer .= $gform->cselect('nation', $gform->retvar('nation', htmlInput($this->nation)), $query, _("Nazione"), array("required"=>true));
529  $buffer .= $gform->cselect('marker', $gform->retvar('marker', htmlInput($this->marker)), gmapsMarker::getForSelect($this->instance), array(_("Marker"), _("lasciare vuoto se si intende visualizzare il marker di default di google")), array("required"=>false));
530  $buffer .= $gform->cell($this->formMap());
531  $buffer .= $gform->fcktextarea('description', $gform->retvar('description', htmlInputEditor($this->description)), _("Descrizione"), array('fck_toolbar'=>"Default", 'trnsl'=>true, 'field'=>'description'));
532  $buffer .= $gform->fcktextarea('information', $gform->retvar('information', htmlInputEditor($this->information)), _("Informazioni"), array('fck_toolbar'=>"Default", 'trnsl'=>true, 'field'=>'information'));
533 
534  $buffer .= $gform->cinput('phone', 'text', $gform->retvar('phone', htmlInput($this->phone)), _("Telefono"), array('required'=>false, 'maxlength'=>16, 'size'=>8, 'trnsl'=>false));
535  $buffer .= $gform->cinput('email', 'text', $gform->retvar('email', htmlInput($this->email)), _("Email"), array('required'=>false, 'maxlength'=>64, 'size'=>20, 'trnsl'=>false));
536  $buffer .= $gform->cinput('web', 'text', $gform->retvar('web', htmlInput($this->web)), _("Web"), array('required'=>false, 'maxlength'=>200, 'size'=>40, 'trnsl'=>false));
537  $buffer .= $gform->ctextarea('opening_hours', $gform->retvar('opening_hours', htmlInput($this->opening_hours)), _("Orari di apertura"), array('required'=>false, 'cols'=>60, 'rows'=>4, 'trnsl'=>true, 'field'=>'opening_hours'));
538 
539  $buffer .= $gform->cradio('updating', $gform->retvar('updating', htmlInput($this->updating)), array('0'=>_('no'), 1=>_('si')), 0, _("Aggiornamento"), array('required'=>true));
540  $buffer .= $gform->cinput('updating_email', 'text', $gform->retvar('updating_email', htmlInput($this->updating_email)), _("Email codice aggiornamento"), array('required'=>false, 'maxlength'=>64, 'size'=>20, 'trnsl'=>false));
541  $buffer .= $gform->cinput('submit', 'submit', $submit, '', array('classField'=>'submit'));
542 
543  $buffer .= $gform->cform();
544 
545  $htmlsection->content = $buffer;
546 
547  return $htmlsection->render();
548 
549  }
550 
557  private function formMap() {
558 
559  $buffer = "<p>Per geolocalizzare il punto di interesse utilizzare lo strumento mappa qui sotto. Seguire la seguente procedura:</p>";
560  $buffer .= "<ol>";
561  $buffer .= "<li>"._("Premere il pulsante 'point'")."</li>";
562  $buffer .= "<li>"._("Cliccare nel punto desiderato sulla mappa per settare il punto. Una volta comparso il marker è possibile affinare la posizione spostandolo. Se si dispone di un indirizzo si può utilizzare il campo di testo, scrivere l'indirizzo e poi premere 'draw'. Anche in questo caso è poi possibile affinare la posizione in un secondo momento.")."</li>";
563  $buffer .= "<li>"._("Quando il marker si trova nella posizione desiderata permere il pulsante 'export map'.")."</li>";
564  $buffer .= "</ol>";
565  $buffer .= "<div id=\"map_canvas\" style=\"width:100%; height: 300px;\"></div>";
566 
567  $buffer .= "<script>";
568  $buffer .= "ajs.use(['ajs.maps.gmapdraw'], function() {
569 
570  fillFields = function(data) {
571  $('lat').set('value', data.point[0].lat.round(10));
572  $('lng').set('value', data.point[0].lng.round(10));
573  alert('".jsVar(_("Il punto è stato settato correttamente, prosegui con la compilazione del form"))."');
574  }
575 
576  mymap = new ajs.maps.gmapdraw.map('map_canvas', {
577  zoom: 10,
578  export_map_callback: fillFields,
579  tools: {'point': { options: { max_items_allowed: 1 }}},
580  tips_map_ctrl: null
581  });
582  mymap.render();
583 
584  if($('lat').value && $('lng').value) {
585  var data = {point: [{lat: $('lat').value, lng:$('lng').value}]};
586  mymap.importMap(data);
587  mymap.gmap().setCenter(new google.maps.LatLng($('lat').value, $('lng').value));
588  }
589 
590 
591  });";
592  $buffer .= "</script>";
593 
594  return $buffer;
595 
596  }
597 
606  public function gmapCode($map, $bounds) {
607 
608  $buffer = "var marker_".$this->id." = new google.maps.Marker({
609  position: new google.maps.LatLng(".$this->lat.", ".$this->lng."),
610  map: $map
611  });";
612  $buffer .= $bounds.".extend(new google.maps.LatLng(".$this->lat.", ".$this->lng."));";
613  if($this->marker) {
614  $marker = new gmapsMarker($this->marker, $this->_controller);
615  $buffer .= "marker_".$this->id.".setIcon(new google.maps.MarkerImage('".$marker->iconUrl()."'));";
616  if($marker->shadow) {
617  $buffer .= "marker_".$this->id.".setShadow(new google.maps.MarkerImage('".$marker->shadowUrl()."'));";
618  }
619  }
620 
621  return $buffer;
622 
623  }
624 
635  public function jsObject($map_obj, $polylines, $polygons, $map_id) {
636 
637  $db = db::instance();
638  $nation = $db->getFieldFromId('nation', $_SESSION['lng'], 'id', $this->nation);
639 
640  if($this->marker) {
641  $marker = new gmapsMarker($this->marker, $this->_controller);
642  $marker_icon = $marker->iconUrl();
643  $marker_shadow = $marker->shadowUrl();
644  }
645  else {
646  $marker_icon = null;
647  $marker_shadow = null;
648  }
649 
650  $categories = array();
651  foreach($this->_ctgs as $ctg) {
652  $categories[] = jsVar($ctg->ml('name'));
653  }
654 
655  $services = array();
656  foreach($this->_services as $s) {
657  $services[] = jsVar($s->ml('name'));
658  }
659 
660  $fields = "{
661  id: '".$this->id."',
662  label: '".jsVar($this->ml('label'))."',
663  categories: '".implode(', ', $categories)."',
664  services: '".implode(', ', $services)."',
665  description: '".jsVar(cutHtmlText(HtmlChars($this->ml('description')), 120, '...', false, false, false, null))."',
666  address: '".jsVar($this->ml('address'))."',
667  cap: '".jsVar($this->cap)."',
668  city: '".jsVar($this->ml('city'))."',
669  nation: '".jsVar($nation)."',
670  lat: '".jsVar($this->lat)."',
671  lng: '".jsVar($this->lng)."',
672  icon: ".($marker_icon ? "'$marker_icon'" : "null").",
673  shadow: ".($marker_shadow ? "'$marker_shadow'" : "null")."
674  }";
675 
676  $info_url = HOME_FILE.'?pt['.$this->_controller->instanceName().'-infowindow]&point_id='.$this->id.'&map_id='.$map_id;
677 
678  return "new GmapPoint($fields, [".implode(',', $this->_ctgs_id)."], [".implode(',', $polylines)."], [".implode(',', $polygons)."], $map_obj, '$info_url');";
679 
680  }
681 
689  private function saveCtgs($ctgs_id) {
690 
691  $db = db::instance();
692  $query = "DELETE FROM ".self::$_tbl_point_point_ctg." WHERE point_id='".$this->id."'";
693  $res = $db->actionquery($query);
694 
695  $values = array();
696  foreach($ctgs_id as $ctg_id) {
697  $values[] = "('".$this->id."', '".$ctg_id."')";
698  }
699  $query = "INSERT INTO ".self::$_tbl_point_point_ctg." (point_id, ctg_id) VALUES ".implode(',', $values).";";
700  $res = $db->actionquery($query);
701 
702  return $res;
703 
704  }
705 
713  private function saveServices($services_id) {
714 
715  $db = db::instance();
716  $query = "DELETE FROM ".self::$_tbl_point_service." WHERE point_id='".$this->id."'";
717  $res = $db->actionquery($query);
718 
719  $values = array();
720  foreach($services_id as $service_id) {
721  $values[] = "('".$this->id."', '".$service_id."')";
722  }
723  $query = "INSERT INTO ".self::$_tbl_point_service." (point_id, service_id) VALUES ".implode(',', $values).";";
724  $res = $db->actionquery($query);
725 
726  return $res;
727 
728  }
729 
737  public function delete() {
738 
739  $db = db::instance();
740 
741  // delete ctg associations
742  $query = "DELETE FROM ".self::$_tbl_point_point_ctg." WHERE point_id='".$this->id."'";
743  $res = $db->actionquery($query);
744 
745  // delete map associations
746  $query = "DELETE FROM ".self::$_tbl_map_point." WHERE point_id='".$this->id."'";
747  $res = $db->actionquery($query);
748 
749  // delete service associations
750  $query = "DELETE FROM ".self::$_tbl_point_service." WHERE point_id='".$this->id."'";
751  $res = $db->actionquery($query);
752 
753  // delete images
754  foreach($this->_images as $image) {
755  $image->delete();
756  }
757 
758  // delete videos
759  foreach($this->_videos as $video) {
760  $video->delete();
761  }
762 
763  // delete events
764  foreach($this->_events as $event) {
765  $event->delete();
766  }
767 
768  // delete attachments
769  foreach($this->_attachments as $attachment) {
770  $attachment->delete();
771  }
772 
773  // delete collections
774  foreach($this->_collections as $collection) {
775  $collection->delete();
776  }
777 
778  return $this->deleteDbData();
779 
780  }
781 
782 
783 }
784 
785 ?>