ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 {
15  protected $value;
16 
23  function __construct($a_title = "", $a_postvar = "")
24  {
25  parent::__construct($a_title, $a_postvar);
26  }
27 
33  function setValue($a_value)
34  {
35  $this->value = $a_value;
36  }
37 
43  function getValue()
44  {
45  return $this->value;
46  }
47 
53  public function setValidationFailureMessage($a_msg)
54  {
55  $this->validationFailureMessage = $a_msg;
56  }
57 
58  public function getValidationFailureMessage()
59  {
60  return $this->validationFailureMessage;
61  }
62 
68  function setValueByArray($a_values)
69  {
70  $this->setValue(self::getPostData($this->getPostVar(), false));
71  }
72 
78  function checkInput()
79  {
80  global $lng;
81 
82  $data = self::getPostData($this->getPostVar(), false);
83  if(sizeof($data))
84  {
85  // slots may not overlap
86  foreach($data as $slot => $days)
87  {
88  if(!$days)
89  {
90  $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
91  return false;
92  }
93 
94  $parts = explode("-", $slot);
95  $from = str_replace(":", "", $parts[0]);
96  $to = str_replace(":", "", $parts[1]);
97 
98  foreach($data as $rslot => $rdays)
99  {
100  if($slot != $rslot && $rdays && array_intersect($days, $rdays))
101  {
102  $rparts = explode("-", $rslot);
103  $rfrom = str_replace(":", "", $rparts[0]);
104  $rto = str_replace(":", "", $rparts[1]);
105 
106  if(($rfrom > $from && $rfrom < $to) ||
107  ($rto > $from && $rto < $to) ||
108  ($rfrom < $from && $rto > $to))
109  {
110  $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
111  return false;
112  }
113  }
114  }
115  }
116 
117  return true;
118  }
119 
120  if ($this->getRequired())
121  {
122  $this->setAlert($lng->txt("msg_input_is_required"));
123  return false;
124  }
125 
126  return true;
127  }
128 
129  static function getPostData($a_post_var, $a_remove_invalid = true)
130  {
131  $res = array();
132  for($loop = 0; $loop < 24; $loop++)
133  {
134  $days = $_POST[$a_post_var."_days~".$loop];
135  $from = self::parseTime($_POST[$a_post_var."_from_hh~".$loop],
136  $_POST[$a_post_var."_from_mm~".$loop]);
137  $to = self::parseTime($_POST[$a_post_var."_to_hh~".$loop],
138  $_POST[$a_post_var."_to_mm~".$loop]);
139 
140  // only if any part was edited (js based gui)
141  if($days || $from != "00:00" || $to != "00:00")
142  {
143  $slot = $from."-".$to;
144  if($days)
145  {
146  if(isset($res[$slot]))
147  {
148  $res[$slot] = array_unique(array_merge($res[$slot], $days));
149  }
150  else
151  {
152  $res[$slot] = $days;
153  }
154  }
155  else
156  {
157  $res[$slot] = array();
158  }
159 
160  if($a_remove_invalid && !($days && $from && $to && $from != $to))
161  {
162  unset($res[$slot]);
163  }
164  }
165  }
166 
167  return $res;
168  }
169 
173  protected function render($a_mode = "")
174  {
175  global $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  {
184  $def = array(null=>null);
185  }
186 
187  $days = array("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su");
188  $row = 0;
189  foreach($def as $slot => $days_select)
190  {
191  $tpl->setCurrentBlock("days");
192  foreach($days as $day)
193  {
194  $day_value = strtolower($day);
195 
196  $tpl->setVariable("ROW", $row);
197  $tpl->setVariable("ID", $this->getFieldId());
198  $tpl->setVariable("POST_VAR", $this->getPostVar());
199  $tpl->setVariable("DAY", $day_value);
200  $tpl->setVariable("TXT_DAY", $lng->txt($day."_short"));
201 
202  if($days_select && in_array($day_value, $days_select))
203  {
204  $tpl->setVariable("DAY_STATUS", " checked=\"checked\"");
205  }
206 
207  $tpl->parseCurrentBlock();
208  }
209 
210  $tpl->setCurrentBlock("row");
211  $tpl->setVariable("ROW", $row);
212  $tpl->setVariable("ID", $this->getFieldId());
213  $tpl->setVariable("POST_VAR", $this->getPostVar());
214  $tpl->setVariable("TXT_FROM", $lng->txt("cal_from"));
215  $tpl->setVariable("TXT_TO", $lng->txt("cal_until"));
216  $tpl->setVariable("IMG_MULTI_ADD", ilUtil::getImagePath('edit_add.png'));
217  $tpl->setVariable("IMG_MULTI_REMOVE", ilUtil::getImagePath('edit_remove.png'));
218  $tpl->setVariable("TXT_MULTI_ADD", $lng->txt("add"));
219  $tpl->setVariable("TXT_MULTI_REMOVE", $lng->txt("remove"));
220 
221  if($slot)
222  {
223  $parts = explode("-", $slot);
224  $from = explode(":", $parts[0]);
225  $to = explode(":", $parts[1]);
226 
227  $tpl->setVariable("FROM_HH_VALUE", $from[0]);
228  $tpl->setVariable("FROM_MM_VALUE", $from[1]);
229  $tpl->setVariable("TO_HH_VALUE", $to[0]);
230  $tpl->setVariable("TO_MM_VALUE", $to[1]);
231  }
232 
233  // manage hidden buttons
234  if($row > 0)
235  {
236  $tpl->setVariable("ADD_STYLE", " style=\"display:none\"");
237  }
238  else
239  {
240  $tpl->setVariable("RMV_STYLE", " style=\"display:none\"");
241  }
242 
243  $tpl->parseCurrentBlock();
244 
245  $row++;
246  }
247 
248  return $tpl->get();
249  }
250 
256  function insert(&$a_tpl)
257  {
258  global $tpl;
259 
260  $tpl->addJavascript("Modules/BookingManager/js/ScheduleInput.js");
261 
262  $html = $this->render();
263 
264  $a_tpl->setCurrentBlock("prop_generic");
265  $a_tpl->setVariable("PROP_GENERIC", $html);
266  $a_tpl->parseCurrentBlock();
267  }
268 
274  protected static function parseTime($a_hours, $a_minutes)
275  {
276  $hours = (int)$a_hours;
277  $min = (int)$a_minutes;
278  if($hours > 23 || $min > 59)
279  {
280  return false;
281  }
282  return str_pad($hours, 2, "0", STR_PAD_LEFT).":".
283  str_pad($min, 2, "0", STR_PAD_LEFT);
284  }
285 }
286 
287 ?>