ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
31 
32  $lng = $DIC['lng'];
33 
34  $adt = $this->getADT();
35 
36  $default = false;
37  if ($adt->isNull()) {
38  // see ilPersonalProfileGUI::addLocationToForm()
39 
40  // use installation default
41  include_once("./Services/Maps/classes/class.ilMapUtil.php");
43  $adt->setLatitude($def["latitude"]);
44  $adt->setLongitude($def["longitude"]);
45  $adt->setZoom($def["zoom"]);
46 
47  $default = true;
48  }
49 
50  $optional = new ilCheckboxInputGUI($this->getTitle(), $this->addToElementId("tgl"));
51 
52  if (!$default && !$adt->isNull()) {
53  $optional->setChecked(true);
54  }
55 
56  $loc = new ilLocationInputGUI($lng->txt("location"), $this->getElementId());
57  $loc->setLongitude($adt->getLongitude());
58  $loc->setLatitude($adt->getLatitude());
59  $loc->setZoom($adt->getZoom());
60  $optional->addSubItem($loc);
61 
62  $rad = new ilNumberInputGUI($lng->txt("form_location_radius"), $this->addToElementId("rad"));
63  $rad->setSize(4);
64  $rad->setSuffix($lng->txt("form_location_radius_km"));
65  $rad->setValue($this->radius);
66  $rad->setRequired(true);
67  $optional->addSubItem($rad);
68 
69  $this->addToParentElement($optional);
70  }
71 
72  protected function shouldBeImportedFromPost($a_post)
73  {
74  return (bool) $a_post["tgl"];
75  }
76 
77  public function importFromPost(array $a_post = null)
78  {
79  $post = $this->extractPostValues($a_post);
80 
81  if ($post && $this->shouldBeImportedFromPost($post)) {
82  $tgl = $this->getForm()->getItemByPostVar($this->addToElementId("tgl"));
83  $tgl->setChecked(true);
84 
85  $item = $this->getForm()->getItemByPostVar($this->getElementId());
86  $item->setLongitude($post["longitude"]);
87  $item->setLatitude($post["latitude"]);
88  $item->setZoom($post["zoom"]);
89 
90  $this->radius = (int) $post["rad"];
91 
92  $this->getADT()->setLongitude($post["longitude"]);
93  $this->getADT()->setLatitude($post["latitude"]);
94  $this->getADT()->setZoom($post["zoom"]);
95  } else {
96  // optional empty is valid
97  $this->force_valid = true;
98 
99  $this->getADT()->setLongitude(null);
100  $this->getADT()->setLatitude(null);
101  $this->getADT()->setZoom(null);
102  $this->radius = null;
103  }
104  }
105 
106  public function isValid()
107  {
108  return (parent::isValid() && ((int) $this->radius || (bool) $this->force_valid));
109  }
110 
111 
112  // bounding
113 
122  protected function getBoundingBox($a_latitude, $a_longitude, $a_radius)
123  {
124  $earth_radius = 6371;
125 
126  // http://www.d-mueller.de/blog/umkreissuche-latlong-und-der-radius/
127  $max_lat = $a_latitude + rad2deg($a_radius / $earth_radius);
128  $min_lat = $a_latitude - rad2deg($a_radius / $earth_radius);
129  $max_long = $a_longitude + rad2deg($a_radius / $earth_radius / cos(deg2rad($a_latitude)));
130  $min_long = $a_longitude - rad2deg($a_radius / $earth_radius / cos(deg2rad($a_latitude)));
131 
132  return array(
133  "lat" => array("min" => $min_lat, "max" => $max_lat)
134  ,"long" => array("min" => $min_long, "max" => $max_long)
135  );
136  }
137 
138 
139  // db
140 
141  public function getSQLCondition($a_element_id)
142  {
143  global $DIC;
144 
145  $ilDB = $DIC['ilDB'];
146 
147  if (!$this->isNull() && $this->isValid()) {
148  $box = $this->getBoundingBox($this->getADT()->getLatitude(), $this->getADT()->getLongitude(), $this->radius);
149 
150  $res = array();
151  $res[] = $a_element_id . "_lat >= " . $ilDB->quote($box["lat"]["min"], "float");
152  $res[] = $a_element_id . "_lat <= " . $ilDB->quote($box["lat"]["max"], "float");
153  $res[] = $a_element_id . "_long >= " . $ilDB->quote($box["long"]["min"], "float");
154  $res[] = $a_element_id . "_long <= " . $ilDB->quote($box["long"]["max"], "float");
155 
156  return "(" . implode(" AND ", $res) . ")";
157  }
158  }
159 
160 
161  // import/export
162 
163  public function getSerializedValue()
164  {
165  if (!$this->isNull() && $this->isValid()) {
166  return serialize(array(
167  "lat" => $this->getADT()->getLatitude()
168  ,"long" => $this->getADT()->getLongitude()
169  ,"zoom" => $this->getADT()->getZoom()
170  ,"radius" => (int) $this->radius
171  ));
172  }
173  }
174 
175  public function setSerializedValue($a_value)
176  {
177  $a_value = unserialize($a_value);
178  if (is_array($a_value)) {
179  $this->getADT()->setLatitude($a_value["lat"]);
180  $this->getADT()->setLongitude($a_value["long"]);
181  $this->getADT()->setZoom($a_value["zoom"]);
182  $this->radius = (int) $a_value["radius"];
183  }
184  }
185 }
setLongitude($a_longitude)
Set Longitude.
global $DIC
Definition: saml.php:7
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)
$lng
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
$default
Definition: build.php:20
setSize($a_size)
Set Size.
getBoundingBox($a_latitude, $a_longitude, $a_radius)
Get bounding box for location circum search.
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.