ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilScheduleInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/Form/classes/class.ilFormPropertyGUI.php";
5 
14 {
18  protected $tpl;
19 
20  protected $value;
21 
28  public function __construct($a_title = "", $a_postvar = "")
29  {
30  global $DIC;
31 
32  $this->lng = $DIC->language();
33  $this->tpl = $DIC["tpl"];
34  parent::__construct($a_title, $a_postvar);
35  }
36 
42  public function setValue($a_value)
43  {
44  $this->value = $a_value;
45  }
46 
52  public function getValue()
53  {
54  return $this->value;
55  }
56 
62  public function setValidationFailureMessage($a_msg)
63  {
64  $this->validationFailureMessage = $a_msg;
65  }
66 
67  public function getValidationFailureMessage()
68  {
69  return $this->validationFailureMessage;
70  }
71 
77  public function setValueByArray($a_values)
78  {
79  $this->setValue(self::getPostData($this->getPostVar(), false));
80  }
81 
87  public function checkInput()
88  {
89  $lng = $this->lng;
90 
91  $data = self::getPostData($this->getPostVar(), false);
92  if (sizeof($data)) {
93  // slots may not overlap
94  foreach ($data as $slot => $days) {
95  if (!$days) {
96  $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
97  return false;
98  }
99  $parts = explode("-", $slot);
100  $from = str_replace(":", "", $parts[0]);
101  $to = str_replace(":", "", $parts[1]);
102  if ($from > $to) {
103  $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
104  return false;
105  }
106 
107  foreach ($data as $rslot => $rdays) {
108  if ($slot != $rslot && $rdays && array_intersect($days, $rdays)) {
109  $rparts = explode("-", $rslot);
110  $rfrom = str_replace(":", "", $rparts[0]);
111  $rto = str_replace(":", "", $rparts[1]);
112 
113  if (($rfrom > $from && $rfrom < $to) ||
114  ($rto > $from && $rto < $to) ||
115  ($rfrom < $from && $rto > $to)) {
116  $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
117  return false;
118  }
119  }
120  }
121  }
122 
123  return true;
124  }
125 
126  if ($this->getRequired()) {
127  $this->setAlert($lng->txt("msg_input_is_required"));
128  return false;
129  }
130 
131  return true;
132  }
133 
134  public static function getPostData($a_post_var, $a_remove_invalid = true)
135  {
136  $res = array();
137  for ($loop = 0; $loop < 240; $loop++) {
138  $days = $_POST[$a_post_var . "_days~" . $loop];
139  $from = self::parseTime(
140  $_POST[$a_post_var . "_from_hh~" . $loop],
141  $_POST[$a_post_var . "_from_mm~" . $loop]
142  );
143  $to = self::parseTime(
144  $_POST[$a_post_var . "_to_hh~" . $loop],
145  $_POST[$a_post_var . "_to_mm~" . $loop]
146  );
147 
148  // only if any part was edited (js based gui)
149  if ($days || $from != "00:00" || $to != "00:00") {
150  $slot = $from . "-" . $to;
151  if ($days) {
152  if (isset($res[$slot])) {
153  $res[$slot] = array_unique(array_merge($res[$slot], $days));
154  } else {
155  $res[$slot] = $days;
156  }
157  } else {
158  $res[$slot] = array();
159  }
160 
161  if ($a_remove_invalid && !($days && $from && $to && $from != $to)) {
162  unset($res[$slot]);
163  }
164  }
165  }
166 
167  return $res;
168  }
169 
173  protected function render($a_mode = "")
174  {
175  $lng = $this->lng;
176 
177  $tpl = new ilTemplate("tpl.schedule_input.html", true, true, "Modules/BookingManager");
178 
179  $lng->loadLanguageModule("dateplaner");
180 
181  $def = $this->getValue();
182  if (!$def) {
183  $def = array(null => null);
184  }
185 
186  $days = array("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su");
187  $row = 0;
188  foreach ($def as $slot => $days_select) {
189  $tpl->setCurrentBlock("days");
190  foreach ($days as $day) {
191  $day_value = strtolower($day);
192 
193  $tpl->setVariable("ROW", $row);
194  $tpl->setVariable("ID", $this->getFieldId());
195  $tpl->setVariable("POST_VAR", $this->getPostVar());
196  $tpl->setVariable("DAY", $day_value);
197  $tpl->setVariable("TXT_DAY", $lng->txt($day . "_short"));
198 
199  if ($days_select && in_array($day_value, $days_select)) {
200  $tpl->setVariable("DAY_STATUS", " checked=\"checked\"");
201  }
202 
203  $tpl->parseCurrentBlock();
204  }
205 
206  $tpl->setCurrentBlock("row");
207  $tpl->setVariable("ROW", $row);
208  $tpl->setVariable("ID", $this->getFieldId());
209  $tpl->setVariable("POST_VAR", $this->getPostVar());
210  $tpl->setVariable("TXT_FROM", $lng->txt("cal_from"));
211  $tpl->setVariable("TXT_TO", $lng->txt("cal_until"));
212  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
213  $tpl->setVariable("IMG_MULTI_ADD", ilGlyphGUI::get(ilGlyphGUI::ADD));
214  $tpl->setVariable("IMG_MULTI_REMOVE", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
215  $tpl->setVariable("TXT_MULTI_ADD", $lng->txt("add"));
216  $tpl->setVariable("TXT_MULTI_REMOVE", $lng->txt("remove"));
217 
218  if ($slot) {
219  $parts = explode("-", $slot);
220  $from = explode(":", $parts[0]);
221  $to = explode(":", $parts[1]);
222 
223  $tpl->setVariable("FROM_HH_VALUE", $from[0]);
224  $tpl->setVariable("FROM_MM_VALUE", $from[1]);
225  $tpl->setVariable("TO_HH_VALUE", $to[0]);
226  $tpl->setVariable("TO_MM_VALUE", $to[1]);
227  }
228 
229  // manage hidden buttons
230  if ($row > 0) {
231  // inline needed because of JS
232  $tpl->setVariable("ADD_STYLE", " style=\"display:none\"");
233  // $tpl->setVariable("ADD_CLASS", "ilNoDisplay");
234  } else {
235  // inline needed because of JS
236  $tpl->setVariable("RMV_STYLE", " style=\"display:none\"");
237  // $tpl->setVariable("RMV_CLASS", "ilNoDisplay");
238  }
239 
240  $tpl->parseCurrentBlock();
241 
242  $row++;
243  }
244 
245  return $tpl->get();
246  }
247 
253  public function insert(&$a_tpl)
254  {
255  $tpl = $this->tpl;
256 
257  $tpl->addJavascript("Modules/BookingManager/js/ScheduleInput.js");
258 
259  $html = $this->render();
260 
261  $a_tpl->setCurrentBlock("prop_generic");
262  $a_tpl->setVariable("PROP_GENERIC", $html);
263  $a_tpl->parseCurrentBlock();
264  }
265 
271  protected static function parseTime($a_hours, $a_minutes)
272  {
273  $hours = (int) $a_hours;
274  $min = (int) $a_minutes;
275  if ($hours > 23 || $min > 59) {
276  return false;
277  }
278  return str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" .
279  str_pad($min, 2, "0", STR_PAD_LEFT);
280  }
281 }
setValue($a_value)
Set Value.
setValidationFailureMessage($a_msg)
Set message string for validation failure.
global $DIC
Definition: saml.php:7
getPostVar()
Get Post Variable.
static parseTime($a_hours, $a_minutes)
Parse/normalize incoming time values.
static get($a_glyph, $a_text="")
Get glyph html.
$from
This class represents a text property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.
setAlert($a_alert)
Set Alert Text.
foreach($_POST as $key=> $value) $res
setValueByArray($a_values)
Set value by array.
insert(&$a_tpl)
Insert property html.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
static getPostData($a_post_var, $a_remove_invalid=true)
render($a_mode="")
Render item.
$row
checkInput()
Check input, strip slashes etc.
This class represents a property in a property form.
$def
Definition: croninfo.php:21
$_POST["username"]
$html
Definition: example_001.php:87
$data
Definition: bench.php:6