ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilLocationInputGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
30  protected ?float $latitude = null;
31  protected ?float $longitude = null;
32  protected ?int $zoom = null;
33  protected string $address = "";
34 
35  public function __construct(
36  string $a_title = "",
37  string $a_postvar = ""
38  ) {
39  global $DIC;
40 
41  $this->lng = $DIC->language();
42  $this->rbacsystem = $DIC->rbac()->system();
43  parent::__construct($a_title, $a_postvar);
44  $this->setType("location");
45  }
46 
47  public function setLatitude(?float $a_latitude): void
48  {
49  $this->latitude = $a_latitude;
50  }
51 
52  public function getLatitude(): ?float
53  {
54  return $this->latitude;
55  }
56 
57  public function setLongitude(?float $a_longitude): void
58  {
59  $this->longitude = $a_longitude;
60  }
61 
62  public function getLongitude(): ?float
63  {
64  return $this->longitude;
65  }
66 
67  public function setZoom(?int $a_zoom): void
68  {
69  $this->zoom = $a_zoom;
70  }
71 
72  public function getZoom(): ?int
73  {
74  return $this->zoom;
75  }
76 
77  public function setAddress(string $a_address): void
78  {
79  $this->address = $a_address;
80  }
81 
82  public function getAddress(): string
83  {
84  return $this->address;
85  }
86 
87  public function setValueByArray(array $a_values): void
88  {
89  $lat = (isset($a_values[$this->getPostVar()]["latitude"]) && $a_values[$this->getPostVar()]["latitude"] != "")
90  ? (float) $a_values[$this->getPostVar()]["latitude"]
91  : null;
92  $lon = (isset($a_values[$this->getPostVar()]["longitude"]) && $a_values[$this->getPostVar()]["longitude"] != "")
93  ? (float) $a_values[$this->getPostVar()]["longitude"]
94  : null;
95  $this->setLatitude($lat);
96  $this->setLongitude($lon);
97  $this->setZoom((int) $a_values[$this->getPostVar()]["zoom"]);
98  }
99 
100  public function checkInput(): bool
101  {
102  $lng = $this->lng;
103 
104  $val = $this->strArray($this->getPostVar());
105  if ($this->getRequired() && (
106  !isset($val["latitude"]) || trim($val["latitude"]) == "" ||
107  !isset($val["longitude"]) || trim($val["longitude"]) == ""
108  )) {
109  $this->setAlert($lng->txt("msg_input_is_required"));
110  return false;
111  }
112  return true;
113  }
114 
115  public function getInput(): array
116  {
117  $val = $this->strArray($this->getPostVar());
118  return [
119  "latitude" => (float) ($val["latitude"] ?? 0),
120  "longitude" => (float) ($val["longitude"] ?? 0),
121  "zoom" => (int) ($val["zoom"] ?? 0),
122  "address" => ($val["address"] ?? "")
123  ];
124  }
125 
126  public function insert(ilTemplate $a_tpl): void
127  {
128  $lng = $this->lng;
129  $rbacsystem = $this->rbacsystem;
130  $levels = [];
131 
132  $lng->loadLanguageModule("maps");
133  $tpl = new ilTemplate("tpl.prop_location.html", true, true, "components/ILIAS/Form");
134  $tpl->setVariable("POST_VAR", $this->getPostVar());
135  $tpl->setVariable("TXT_ZOOM", $lng->txt("maps_zoom_level"));
136  $tpl->setVariable("TXT_LATITUDE", $lng->txt("maps_latitude"));
137  $tpl->setVariable("TXT_LONGITUDE", $lng->txt("maps_longitude"));
138  $tpl->setVariable("LOC_DESCRIPTION", $lng->txt("maps_std_location_desc"));
139 
140  $lat = is_numeric($this->getLatitude())
141  ? $this->getLatitude()
142  : 0;
143  $long = is_numeric($this->getLongitude())
144  ? $this->getLongitude()
145  : 0;
146  $tpl->setVariable("PROPERTY_VALUE_LAT", $lat);
147  $tpl->setVariable("PROPERTY_VALUE_LONG", $long);
148  for ($i = 0; $i <= 18; $i++) {
149  $levels[$i] = $i;
150  }
151 
152  $map_id = "map_" . md5(uniqid());
153 
154  $tpl->setVariable(
155  "ZOOM_SELECT",
157  $this->getZoom(),
158  $this->getPostVar() . "[zoom]",
159  $levels,
160  false,
161  true,
162  0,
163  "",
164  array("id" => $map_id . "_zoom",
165  "onchange" => "ilUpdateMap('" . $map_id . "');")
166  )
167  );
168  $tpl->setVariable("MAP_ID", $map_id);
169  $tpl->setVariable("ID", $this->getPostVar());
170 
171  // only show address input if geolocation url available
172  // else, if admin: show warning.
173 
174  if ($this->geolocationAvailiable()) {
175  $tpl->setVariable("TXT_ADDR", $lng->txt("address"));
176  $tpl->setVariable("TXT_LOOKUP", $lng->txt("maps_lookup_address"));
177  $tpl->setVariable("TXT_ADDRESS", $this->getAddress());
178  $tpl->setVariable("MAP_ID_ADDR", $map_id);
179  $tpl->setVariable("POST_VAR_ADDR", $this->getPostVar());
180  } else {
181  if ($rbacsystem->checkAccess("visible", SYSTEM_FOLDER_ID)) {
182  $tpl->setVariable("TEXT", $lng->txt("configure_geolocation"));
183  }
184  }
185 
186  $map_gui = ilMapUtil::getMapGUI();
187  $map_gui->setMapId($map_id)
188  ->setLatitude((string) $lat)
189  ->setLongitude((string) $long)
190  ->setZoom($this->getZoom())
191  ->setEnableTypeControl(true)
192  ->setEnableLargeMapControl(true)
193  ->setEnableUpdateListener(true)
194  ->setEnableCentralMarker(true);
195 
196  $tpl->setVariable("MAP", $map_gui->getHtml());
197 
198  $a_tpl->setCurrentBlock("prop_generic");
199  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
200  $a_tpl->parseCurrentBlock();
201  }
202 
203  protected function geolocationAvailiable(): bool
204  {
205  switch (ilMapUtil::getType()) {
206  case 'openlayers':
207  return (bool) ilMapUtil::getStdGeolocationServer();
208  case 'googlemaps':
209  return true;
210  default:
211  return false;
212  }
213  }
214 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static getType()
__construct(string $a_title="", string $a_postvar="")
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
setValueByArray(array $a_values)
static formSelect( $selected, string $varname, array $options, bool $multiple=false, bool $direct_text=false, int $size=0, string $style_class="", array $attribs=[], bool $disabled=false)
Builds a select form field with options and shows the selected option first.
loadLanguageModule(string $a_module)
Load language module.
const SYSTEM_FOLDER_ID
Definition: constants.php:35
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
checkAccess(string $a_operations, int $a_ref_id, string $a_type="")
checkAccess represents the main method of the RBAC-system in ILIAS3 developers want to use With this ...
static getMapGUI()
Get an instance of the GUI class.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
setAddress(string $a_address)
This class represents a location property in a property form.
global $DIC
Definition: shib_login.php:26
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static getStdGeolocationServer()
Returns the reverse geolocation server to be used in the installation.
setLatitude(?float $a_latitude)
__construct(Container $dic, ilPlugin $plugin)
setLongitude(?float $a_longitude)