1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2007 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24/**
25* This class represents a duration (typical hh:mm:ss) property in a property form.
26*
27* @author Alex Killing <alex.killing@gmx.de>
28* @version $Id$
29* @ingroup	ServicesForm
30*/
31class ilDurationInputGUI extends ilFormPropertyGUI
32{
33    /**
34     * @var ilLanguage
35     */
36    protected $lng;
37
38    protected $months = 0;
39    protected $days = 0;
40    protected $hours = 0;
41    protected $minutes = 0;
42    protected $seconds = 0;
43    protected $showmonths = false;
44    protected $showdays = false;
45    protected $showhours = true;
46    protected $showminutes = true;
47    protected $showseconds = false;
48
49    /**
50    * Constructor
51    *
52    * @param	string	$a_title	Title
53    * @param	string	$a_postvar	Post Variable
54    */
55    public function __construct($a_title = "", $a_postvar = "")
56    {
57        global $DIC;
58
59        $this->lng = $DIC->language();
60        parent::__construct($a_title, $a_postvar);
61        $this->setType("duration");
62    }
63
64    /**
65    * Set Days.
66    *
67    * @param	int	$a_days	Days
68    */
69    public function setDays($a_days)
70    {
71        $this->days = $a_days;
72    }
73
74    /**
75    * Get Days.
76    *
77    * @return	int	Days
78    */
79    public function getDays()
80    {
81        return (int) $this->days;
82    }
83
84    /**
85    * Set Hours.
86    *
87    * @param	int	$a_hours	Hours
88    */
89    public function setHours($a_hours)
90    {
91        $this->hours = $a_hours;
92    }
93
94    /**
95    * Get Hours.
96    *
97    * @return	int	Hours
98    */
99    public function getHours()
100    {
101        return (int) $this->hours;
102    }
103
104    /**
105    * Set Minutes.
106    *
107    * @param	int	$a_minutes	Minutes
108    */
109    public function setMinutes($a_minutes)
110    {
111        $this->minutes = $a_minutes;
112    }
113
114    /**
115    * Get Minutes.
116    *
117    * @return	int	Minutes
118    */
119    public function getMinutes()
120    {
121        return (int) $this->minutes;
122    }
123
124    /**
125    * Set Seconds.
126    *
127    * @param	int	$a_seconds	Seconds
128    */
129    public function setSeconds($a_seconds)
130    {
131        $this->seconds = $a_seconds;
132    }
133
134    /**
135     * set months
136     *
137     * @access public
138     * @param int months
139     *
140     */
141    public function setMonths($a_months)
142    {
143        $this->months = $a_months;
144    }
145
146    /**
147     * get months
148     *
149     * @access public
150     *
151     */
152    public function getMonths()
153    {
154        return (int) $this->months;
155    }
156
157    /**
158    * Get Seconds.
159    *
160    * @return	int	Seconds
161    */
162    public function getSeconds()
163    {
164        return (int) $this->seconds;
165    }
166
167    /**
168     * Set show months
169     *
170     * @access public
171     * @param boolean $a_show_month
172     */
173    public function setShowMonths($a_show_months)
174    {
175        $this->showmonths = $a_show_months;
176    }
177
178    /**
179     * Get show months
180     *
181     * @access public
182     */
183    public function getShowMonths()
184    {
185        return $this->showmonths;
186    }
187
188    /**
189    * Set Show Days.
190    *
191    * @param	boolean	$a_showdays	Show Days
192    */
193    public function setShowDays($a_showdays)
194    {
195        $this->showdays = $a_showdays;
196    }
197
198    /**
199    * Get Show Days.
200    *
201    * @return	boolean	Show Days
202    */
203    public function getShowDays()
204    {
205        return $this->showdays;
206    }
207
208    /**
209    * Set Show Hours.
210    *
211    * @param	boolean	$a_showhours	Show Hours
212    */
213    public function setShowHours($a_showhours)
214    {
215        $this->showhours = $a_showhours;
216    }
217
218    /**
219    * Get Show Hours.
220    *
221    * @return	boolean	Show Hours
222    */
223    public function getShowHours()
224    {
225        return $this->showhours;
226    }
227
228    /**
229    * Set Show Minutes.
230    *
231    * @param	boolean	$a_showminutes	Show Minutes
232    */
233    public function setShowMinutes($a_showminutes)
234    {
235        $this->showminutes = $a_showminutes;
236    }
237
238    /**
239    * Get Show Minutes.
240    *
241    * @return	boolean	Show Minutes
242    */
243    public function getShowMinutes()
244    {
245        return $this->showminutes;
246    }
247
248    /**
249    * Set Show Seconds.
250    *
251    * @param	boolean	$a_showseconds	Show Seconds
252    */
253    public function setShowSeconds($a_showseconds)
254    {
255        $this->showseconds = $a_showseconds;
256    }
257
258    /**
259    * Get Show Seconds.
260    *
261    * @return	boolean	Show Seconds
262    */
263    public function getShowSeconds()
264    {
265        return $this->showseconds;
266    }
267
268    /**
269    * Set value by array
270    *
271    * @param	array	$a_values	value array
272    */
273    public function setValueByArray($a_values)
274    {
275        $this->setMonths($a_values[$this->getPostVar()]["MM"]);
276        $this->setDays($a_values[$this->getPostVar()]["dd"]);
277        $this->setHours($a_values[$this->getPostVar()]["hh"]);
278        $this->setMinutes($a_values[$this->getPostVar()]["mm"]);
279        $this->setSeconds($a_values[$this->getPostVar()]["ss"]);
280    }
281
282    /**
283    * Check input, strip slashes etc. set alert, if input is not ok.
284    *
285    * @return	boolean		Input ok, true/false
286    */
287    public function checkInput()
288    {
289        $lng = $this->lng;
290
291        $_POST[$this->getPostVar()]["MM"] =
292            ilUtil::stripSlashes($_POST[$this->getPostVar()]["MM"]);
293        $_POST[$this->getPostVar()]["dd"] =
294            ilUtil::stripSlashes($_POST[$this->getPostVar()]["dd"]);
295        $_POST[$this->getPostVar()]["hh"] =
296            ilUtil::stripSlashes($_POST[$this->getPostVar()]["hh"]);
297        $_POST[$this->getPostVar()]["mm"] =
298            ilUtil::stripSlashes($_POST[$this->getPostVar()]["mm"]);
299        $_POST[$this->getPostVar()]["ss"] =
300            ilUtil::stripSlashes($_POST[$this->getPostVar()]["ss"]);
301
302        return true;
303    }
304
305    /**
306    * Insert property html
307    *
308    * @return	int	Size
309    */
310    public function insert($a_tpl)
311    {
312        $html = $this->render();
313
314        $a_tpl->setCurrentBlock("prop_generic");
315        $a_tpl->setVariable("PROP_GENERIC", $html);
316        $a_tpl->parseCurrentBlock();
317    }
318
319    /**
320    * Insert property html
321    *
322    */
323    public function render()
324    {
325        $lng = $this->lng;
326
327        $tpl = new ilTemplate("tpl.prop_duration.html", true, true, "Services/Form");
328
329        if ($this->getShowMonths()) {
330            $tpl->setCurrentBlock("dur_months");
331            $tpl->setVariable("TXT_MONTHS", $lng->txt("form_months"));
332            $val = array();
333            for ($i = 0; $i <= 36; $i++) {
334                $val[$i] = $i;
335            }
336            $tpl->setVariable(
337                "SELECT_MONTHS",
338                ilUtil::formSelect(
339                    $this->getMonths(),
340                    $this->getPostVar() . "[MM]",
341                    $val,
342                    false,
343                    true,
344                    0,
345                    '',
346                    '',
347                    $this->getDisabled()
348                )
349            );
350            $tpl->parseCurrentBlock();
351        }
352        if ($this->getShowDays()) {
353            $tpl->setCurrentBlock("dur_days");
354            $tpl->setVariable("TXT_DAYS", $lng->txt("form_days"));
355            $val = array();
356            for ($i = 0; $i <= 366; $i++) {
357                $val[$i] = $i;
358            }
359            $tpl->setVariable(
360                "SELECT_DAYS",
361                ilUtil::formSelect(
362                    $this->getDays(),
363                    $this->getPostVar() . "[dd]",
364                    $val,
365                    false,
366                    true,
367                    0,
368                    '',
369                    '',
370                    $this->getDisabled()
371                )
372            );
373            $tpl->parseCurrentBlock();
374        }
375        if ($this->getShowHours()) {
376            $tpl->setCurrentBlock("dur_hours");
377            $tpl->setVariable("TXT_HOURS", $lng->txt("form_hours"));
378            $val = array();
379            for ($i = 0; $i <= 23; $i++) {
380                $val[$i] = $i;
381            }
382            $tpl->setVariable(
383                "SELECT_HOURS",
384                ilUtil::formSelect(
385                    $this->getHours(),
386                    $this->getPostVar() . "[hh]",
387                    $val,
388                    false,
389                    true,
390                    0,
391                    '',
392                    '',
393                    $this->getDisabled()
394                )
395            );
396            $tpl->parseCurrentBlock();
397        }
398        if ($this->getShowMinutes()) {
399            $tpl->setCurrentBlock("dur_minutes");
400            $tpl->setVariable("TXT_MINUTES", $lng->txt("form_minutes"));
401            $val = array();
402            for ($i = 0; $i <= 59; $i++) {
403                $val[$i] = $i;
404            }
405            $tpl->setVariable(
406                "SELECT_MINUTES",
407                ilUtil::formSelect(
408                    $this->getMinutes(),
409                    $this->getPostVar() . "[mm]",
410                    $val,
411                    false,
412                    true,
413                    0,
414                    '',
415                    '',
416                    $this->getDisabled()
417                )
418            );
419            $tpl->parseCurrentBlock();
420        }
421        if ($this->getShowSeconds()) {
422            $tpl->setCurrentBlock("dur_seconds");
423            $tpl->setVariable("TXT_SECONDS", $lng->txt("form_seconds"));
424            $val = array();
425            for ($i = 0; $i <= 59; $i++) {
426                $val[$i] = $i;
427            }
428            $tpl->setVariable(
429                "SELECT_SECONDS",
430                ilUtil::formSelect(
431                    $this->getSeconds(),
432                    $this->getPostVar() . "[ss]",
433                    $val,
434                    false,
435                    true,
436                    0,
437                    '',
438                    '',
439                    $this->getDisabled()
440                )
441            );
442            $tpl->parseCurrentBlock();
443        }
444
445        return $tpl->get();
446    }
447
448    /**
449    * Get HTML for table filter
450    */
451    public function getTableFilterHTML()
452    {
453        $html = $this->render();
454        return $html;
455    }
456
457    /**
458     * serialize data
459     */
460    public function serializeData()
461    {
462        $data = array("months" => $this->getMonths(),
463            "days" => $this->getDays(),
464            "hours" => $this->getHours(),
465            "minutes" => $this->getMinutes(),
466            "seconds" => $this->getSeconds());
467
468        return serialize($data);
469    }
470
471    /**
472     * unserialize data
473     */
474    public function unserializeData($a_data)
475    {
476        $data = unserialize($a_data);
477
478        $this->setMonths($data["months"]);
479        $this->setDays($data["days"]);
480        $this->setHours($data["hours"]);
481        $this->setMinutes($data["minutes"]);
482        $this->setSeconds($data["seconds"]);
483    }
484
485    /**
486     * Get combined value in seconds
487     *
488     * @return int
489     */
490    public function getValueInSeconds()
491    {
492        $value = 0;
493        if ($this->getShowMonths()) {
494            $value += $this->getMonths() * 30 * 24 * 60 * 60;
495        }
496        if ($this->getShowDays()) {
497            $value += $this->getDays() * 24 * 60 * 60;
498        }
499        if ($this->getShowHours()) {
500            $value += $this->getHours() * 60 * 60;
501        }
502        if ($this->getShowMinutes()) {
503            $value += $this->getMinutes() * 60;
504        }
505        if ($this->getShowSeconds()) {
506            $value += $this->getSeconds();
507        }
508        return $value;
509    }
510}
511