ILIAS  release_8 Revision v8.23
class.ilADTLocationSearchBridgeSingle.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
25 {
26  protected ?int $radius = null;
27  protected bool $force_valid = false;
28 
29  protected ilLanguage $lng;
30 
31  public function __construct(ilADTDefinition $a_adt_def)
32  {
33  global $DIC;
34 
35  $this->lng = $DIC->language();
36  parent::__construct($a_adt_def);
37  }
38 
39  protected function isValidADTDefinition(ilADTDefinition $a_adt_def): bool
40  {
41  return ($a_adt_def instanceof ilADTLocationDefinition);
42  }
43 
44  // table2gui / filter
45 
46  public function loadFilter(): void
47  {
48  $value = $this->readFilter();
49  if ($value !== null) {
50  // :TODO:
51  }
52  }
53 
54  // form
55 
56  public function addToForm(): void
57  {
58  $adt = $this->getADT();
59 
60  $default = false;
61  if ($adt->isNull()) {
62  // see ilPersonalProfileGUI::addLocationToForm()
63 
64  // use installation default
66  $adt->setLatitude((float) ($def["latitude"] ?? null));
67  $adt->setLongitude((float) ($def["longitude"] ?? null));
68  $adt->setZoom((int) ($def["zoom"] ?? 0));
69 
70  $default = true;
71  }
72 
73  $optional = new ilCheckboxInputGUI($this->getTitle(), $this->addToElementId("tgl"));
74 
75  if (!$default && !$adt->isNull()) {
76  $optional->setChecked(true);
77  }
78 
79  $loc = new ilLocationInputGUI($this->lng->txt("location"), $this->getElementId());
80  $loc->setLongitude($adt->getLongitude());
81  $loc->setLatitude($adt->getLatitude());
82  $loc->setZoom($adt->getZoom());
83  $optional->addSubItem($loc);
84 
85  $rad = new ilNumberInputGUI($this->lng->txt("form_location_radius"), $this->addToElementId("rad"));
86  $rad->setSize(4);
87  $rad->setSuffix($this->lng->txt("form_location_radius_km"));
88  $rad->setValue($this->radius);
89  $rad->setRequired(true);
90  $optional->addSubItem($rad);
91 
92  $this->addToParentElement($optional);
93  }
94 
95  protected function shouldBeImportedFromPost($a_post): bool
96  {
97  return (bool) ($a_post["tgl"] ?? false);
98  }
99 
100  public function importFromPost(array $a_post = null): bool
101  {
102  $post = $this->extractPostValues($a_post);
103 
104  if ($post && $this->shouldBeImportedFromPost($post)) {
105  $tgl = $this->getForm()->getItemByPostVar($this->addToElementId("tgl"));
106  $tgl->setChecked(true);
107 
108  $item = $this->getForm()->getItemByPostVar($this->getElementId());
109  $item->setLongitude($post["longitude"]);
110  $item->setLatitude($post["latitude"]);
111  $item->setZoom($post["zoom"]);
112 
113  $this->radius = (int) $post["rad"];
114 
115  $this->getADT()->setLongitude($post["longitude"]);
116  $this->getADT()->setLatitude($post["latitude"]);
117  $this->getADT()->setZoom($post["zoom"]);
118  } else {
119  // optional empty is valid
120  $this->force_valid = true;
121 
122  $this->getADT()->setLongitude(null);
123  $this->getADT()->setLatitude(null);
124  $this->getADT()->setZoom(null);
125  $this->radius = null;
126  }
127  return true;
128  }
129 
130  public function isValid(): bool
131  {
132  return (parent::isValid() && ((int) $this->radius || $this->force_valid));
133  }
134 
135 
136  // bounding
137 
145  protected function getBoundingBox(float $a_latitude, float $a_longitude, int $a_radius): array
146  {
147  $earth_radius = 6371;
148 
149  // http://www.d-mueller.de/blog/umkreissuche-latlong-und-der-radius/
150  $max_lat = $a_latitude + rad2deg($a_radius / $earth_radius);
151  $min_lat = $a_latitude - rad2deg($a_radius / $earth_radius);
152  $max_long = $a_longitude + rad2deg($a_radius / $earth_radius / cos(deg2rad($a_latitude)));
153  $min_long = $a_longitude - rad2deg($a_radius / $earth_radius / cos(deg2rad($a_latitude)));
154 
155  return array(
156  "lat" => array("min" => $min_lat, "max" => $max_lat)
157  ,
158  "long" => array("min" => $min_long, "max" => $max_long)
159  );
160  }
161 
162  // db
163 
164  public function getSQLCondition(string $a_element_id, int $mode = self::SQL_LIKE, array $quotedWords = []): string
165  {
166  if (!$this->isNull() && $this->isValid()) {
167  $box = $this->getBoundingBox(
168  $this->getADT()->getLatitude(),
169  $this->getADT()->getLongitude(),
170  $this->radius
171  );
172 
173  $res = [];
174  $res[] = $a_element_id . "_lat >= " . $this->db->quote($box["lat"]["min"], "float");
175  $res[] = $a_element_id . "_lat <= " . $this->db->quote($box["lat"]["max"], "float");
176  $res[] = $a_element_id . "_long >= " . $this->db->quote($box["long"]["min"], "float");
177  $res[] = $a_element_id . "_long <= " . $this->db->quote($box["long"]["max"], "float");
178 
179  return "(" . implode(" AND ", $res) . ")";
180  }
181  return '';
182  }
183 
184  // import/export
185 
186  public function getSerializedValue(): string
187  {
188  if (!$this->isNull() && $this->isValid()) {
189  return serialize(array(
190  "lat" => $this->getADT()->getLatitude()
191  ,
192  "long" => $this->getADT()->getLongitude()
193  ,
194  "zoom" => $this->getADT()->getZoom()
195  ,
196  "radius" => (int) $this->radius
197  ));
198  }
199  return '';
200  }
201 
202  public function setSerializedValue(string $a_value): void
203  {
204  $a_value = unserialize($a_value);
205  if (is_array($a_value)) {
206  $this->getADT()->setLatitude($a_value["lat"]);
207  $this->getADT()->setLongitude($a_value["long"]);
208  $this->getADT()->setZoom($a_value["zoom"]);
209  $this->radius = (int) $a_value["radius"];
210  }
211  }
212 }
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a checkbox property in a property form.
getBoundingBox(float $a_latitude, float $a_longitude, int $a_radius)
Get bounding box for location circum search.
extractPostValues(array $a_post=null)
Extract data from (post) values.
global $DIC
Definition: feed.php:28
readFilter()
Load value(s) from filter store (in session)
Class ilADTSearchBridgeSingle.
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.
isNull()
Is currently null.
getSQLCondition(string $a_element_id, int $mode=self::SQL_LIKE, array $quotedWords=[])
__construct(Container $dic, ilPlugin $plugin)
addToElementId(string $a_add)
Add sub-element.
$post
Definition: ltitoken.php:49
ADT definition base class.
setLongitude(?float $a_longitude)
addToParentElement(ilFormPropertyGUI $a_field)
Add form field to parent element.