ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilADTLocationSearchBridgeSingle.php
Go to the documentation of this file.
1 <?php
2 
3 require_once "Services/ADT/classes/Bridges/class.ilADTSearchBridgeSingle.php";
4 
6 {
7  protected $radius; // [int]
8 
9  protected function isValidADTDefinition(ilADTDefinition $a_adt_def)
10  {
11  return ($a_adt_def instanceof ilADTLocationDefinition);
12  }
13 
14 
15  // table2gui / filter
16 
17  public function loadFilter()
18  {
19  $value = $this->readFilter();
20  if ($value !== null) {
21  // :TODO:
22  }
23  }
24 
25 
26  // form
27 
28  public function addToForm()
29  {
30  global $lng;
31 
32  $adt = $this->getADT();
33 
34  $default = false;
35  if ($adt->isNull()) {
36  // see ilPersonalProfileGUI::addLocationToForm()
37 
38  // use installation default
39  include_once("./Services/Maps/classes/class.ilMapUtil.php");
41  $adt->setLatitude($def["latitude"]);
42  $adt->setLongitude($def["longitude"]);
43  $adt->setZoom($def["zoom"]);
44 
45  $default = true;
46  }
47 
48  $optional = new ilCheckboxInputGUI($this->getTitle(), $this->addToElementId("tgl"));
49 
50  if (!$default && !$adt->isNull()) {
51  $optional->setChecked(true);
52  }
53 
54  $loc = new ilLocationInputGUI($lng->txt("location"), $this->getElementId());
55  $loc->setLongitude($adt->getLongitude());
56  $loc->setLatitude($adt->getLatitude());
57  $loc->setZoom($adt->getZoom());
58  $optional->addSubItem($loc);
59 
60  $rad = new ilNumberInputGUI($lng->txt("form_location_radius"), $this->addToElementId("rad"));
61  $rad->setSize(4);
62  $rad->setSuffix($lng->txt("form_location_radius_km"));
63  $rad->setValue($this->radius);
64  $rad->setRequired(true);
65  $optional->addSubItem($rad);
66 
67  $this->addToParentElement($optional);
68  }
69 
70  protected function shouldBeImportedFromPost($a_post)
71  {
72  return (bool) $a_post["tgl"];
73  }
74 
75  public function importFromPost(array $a_post = null)
76  {
77  $post = $this->extractPostValues($a_post);
78 
79  if ($post && $this->shouldBeImportedFromPost($post)) {
80  $tgl = $this->getForm()->getItemByPostVar($this->addToElementId("tgl"));
81  $tgl->setChecked(true);
82 
83  $item = $this->getForm()->getItemByPostVar($this->getElementId());
84  $item->setLongitude($post["longitude"]);
85  $item->setLatitude($post["latitude"]);
86  $item->setZoom($post["zoom"]);
87 
88  $this->radius = (int) $post["rad"];
89 
90  $this->getADT()->setLongitude($post["longitude"]);
91  $this->getADT()->setLatitude($post["latitude"]);
92  $this->getADT()->setZoom($post["zoom"]);
93  } else {
94  // optional empty is valid
95  $this->force_valid = true;
96 
97  $this->getADT()->setLongitude(null);
98  $this->getADT()->setLatitude(null);
99  $this->getADT()->setZoom(null);
100  $this->radius = null;
101  }
102  }
103 
104  public function isValid()
105  {
106  return (parent::isValid() && ((int) $this->radius || (bool) $this->force_valid));
107  }
108 
109 
110  // bounding
111 
120  protected function getBoundingBox($a_latitude, $a_longitude, $a_radius)
121  {
122  $earth_radius = 6371;
123 
124  // http://www.d-mueller.de/blog/umkreissuche-latlong-und-der-radius/
125  $max_lat = $a_latitude + rad2deg($a_radius/$earth_radius);
126  $min_lat = $a_latitude - rad2deg($a_radius/$earth_radius);
127  $max_long = $a_longitude + rad2deg($a_radius/$earth_radius/cos(deg2rad($a_latitude)));
128  $min_long = $a_longitude - rad2deg($a_radius/$earth_radius/cos(deg2rad($a_latitude)));
129 
130  return array(
131  "lat" => array("min"=>$min_lat, "max"=>$max_lat)
132  ,"long" => array("min"=>$min_long, "max"=>$max_long)
133  );
134  }
135 
136 
137  // db
138 
139  public function getSQLCondition($a_element_id)
140  {
141  global $ilDB;
142 
143  if (!$this->isNull() && $this->isValid()) {
144  $box = $this->getBoundingBox($this->getADT()->getLatitude(), $this->getADT()->getLongitude(), $this->radius);
145 
146  $res = array();
147  $res[] = $a_element_id . "_lat >= " . $ilDB->quote($box["lat"]["min"], "float");
148  $res[] = $a_element_id . "_lat <= " . $ilDB->quote($box["lat"]["max"], "float");
149  $res[] = $a_element_id . "_long >= " . $ilDB->quote($box["long"]["min"], "float");
150  $res[] = $a_element_id . "_long <= " . $ilDB->quote($box["long"]["max"], "float");
151 
152  return "(" . implode(" AND ", $res) . ")";
153  }
154  }
155 
156 
157  // import/export
158 
159  public function getSerializedValue()
160  {
161  if (!$this->isNull() && $this->isValid()) {
162  return serialize(array(
163  "lat" => $this->getADT()->getLatitude()
164  ,"long" => $this->getADT()->getLongitude()
165  ,"zoom" => $this->getADT()->getZoom()
166  ,"radius" => (int) $this->radius
167  ));
168  }
169  }
170 
171  public function setSerializedValue($a_value)
172  {
173  $a_value = unserialize($a_value);
174  if (is_array($a_value)) {
175  $this->getADT()->setLatitude($a_value["lat"]);
176  $this->getADT()->setLongitude($a_value["long"]);
177  $this->getADT()->setZoom($a_value["zoom"]);
178  $this->radius = (int) $a_value["radius"];
179  }
180  }
181 }
setLongitude($a_longitude)
Set Longitude.
This class represents a checkbox property in a property form.
extractPostValues(array $a_post=null)
Extract data from (post) values.
foreach($_POST as $key=> $value) $res
readFilter()
Load value(s) from filter store (in session)
This class represents a number property in a property form.
static getDefaultSettings()
Get default longitude, latitude and zoom.
This class represents a location property in a property form.
$post
Definition: post.php:34
Create styles array
The data for the language used.
setSize($a_size)
Set Size.
getBoundingBox($a_latitude, $a_longitude, $a_radius)
Get bounding box for location circum search.
global $lng
Definition: privfeed.php:17
global $ilDB
$def
Definition: croninfo.php:21
ADT definition base class.
getElementId()
Get element id.
addToParentElement(ilFormPropertyGUI $a_field)
Add form field to parent element.
addToElementId($a_add)
Add sub-element.