ILIAS  Release_5_0_x_branch Revision 61816
 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  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
217  $tpl->setVariable("IMG_MULTI_ADD", ilGlyphGUI::get(ilGlyphGUI::ADD));
218  $tpl->setVariable("IMG_MULTI_REMOVE", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
219  $tpl->setVariable("TXT_MULTI_ADD", $lng->txt("add"));
220  $tpl->setVariable("TXT_MULTI_REMOVE", $lng->txt("remove"));
221 
222  if($slot)
223  {
224  $parts = explode("-", $slot);
225  $from = explode(":", $parts[0]);
226  $to = explode(":", $parts[1]);
227 
228  $tpl->setVariable("FROM_HH_VALUE", $from[0]);
229  $tpl->setVariable("FROM_MM_VALUE", $from[1]);
230  $tpl->setVariable("TO_HH_VALUE", $to[0]);
231  $tpl->setVariable("TO_MM_VALUE", $to[1]);
232  }
233 
234  // manage hidden buttons
235  if($row > 0)
236  {
237  // inline needed because of JS
238  $tpl->setVariable("ADD_STYLE", " style=\"display:none\"");
239  // $tpl->setVariable("ADD_CLASS", "ilNoDisplay");
240  }
241  else
242  {
243  // inline needed because of JS
244  $tpl->setVariable("RMV_STYLE", " style=\"display:none\"");
245  // $tpl->setVariable("RMV_CLASS", "ilNoDisplay");
246  }
247 
248  $tpl->parseCurrentBlock();
249 
250  $row++;
251  }
252 
253  return $tpl->get();
254  }
255 
261  function insert(&$a_tpl)
262  {
263  global $tpl;
264 
265  $tpl->addJavascript("Modules/BookingManager/js/ScheduleInput.js");
266 
267  $html = $this->render();
268 
269  $a_tpl->setCurrentBlock("prop_generic");
270  $a_tpl->setVariable("PROP_GENERIC", $html);
271  $a_tpl->parseCurrentBlock();
272  }
273 
279  protected static function parseTime($a_hours, $a_minutes)
280  {
281  $hours = (int)$a_hours;
282  $min = (int)$a_minutes;
283  if($hours > 23 || $min > 59)
284  {
285  return false;
286  }
287  return str_pad($hours, 2, "0", STR_PAD_LEFT).":".
288  str_pad($min, 2, "0", STR_PAD_LEFT);
289  }
290 }
291 
292 ?>