1<?php
2/*
3        +-----------------------------------------------------------------------------+
4        | ILIAS open source                                                           |
5        +-----------------------------------------------------------------------------+
6        | Copyright (c) 1998-2006 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* Stores calendar categories
26*
27* @author Stefan Meyer <smeyer.ilias@gmx.de>
28* @version $Id$
29*
30* @ingroup ServicesCalendar
31*/
32
33class ilCalendarCategory
34{
35    const LTYPE_LOCAL = 1;
36    const LTYPE_REMOTE = 2;
37
38    private static $instances = null;
39
40    const DEFAULT_COLOR = '#04427e';
41
42    const TYPE_USR = 1;		// user
43    const TYPE_OBJ = 2;		// object
44    const TYPE_GLOBAL = 3;	// global
45    const TYPE_CH = 4;		// consultation hours
46    const TYPE_BOOK = 5;	// booking manager
47
48    protected static $SORTED_TYPES = array(
49        0 => self::TYPE_GLOBAL,
50        1 => self::TYPE_USR,
51        2 => self::TYPE_CH,
52        3 => self::TYPE_BOOK,
53        4 => self::TYPE_OBJ
54    );
55
56
57    protected $cat_id;
58    protected $color;
59    protected $type = self::TYPE_USR;
60    protected $obj_id;
61    protected $obj_type = null;
62    protected $title;
63
64    protected $location = self::LTYPE_LOCAL;
65    protected $remote_url;
66    protected $remote_user;
67    protected $remote_pass;
68    protected $remote_sync = null;
69
70    protected $db;
71
72
73    /**
74     * Constructor
75     *
76     * @access public
77     */
78    public function __construct($a_cat_id = 0)
79    {
80        global $DIC;
81
82        $ilDB = $DIC['ilDB'];
83
84        $this->db = $ilDB;
85        $this->cat_id = $a_cat_id;
86
87        $this->read();
88    }
89
90    /**
91     * get instance by obj_id
92     *
93     * @param int obj_id
94     * @return \ilCalendarCategory
95     * @static
96     */
97    public static function _getInstanceByObjId($a_obj_id)
98    {
99        global $DIC;
100
101        $ilDB = $DIC['ilDB'];
102
103        $query = "SELECT cat_id FROM cal_categories " .
104            "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
105            "AND type = " . $ilDB->quote(self::TYPE_OBJ, 'integer');
106        $res = $ilDB->query($query);
107        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
108            return new ilCalendarCategory($row->cat_id);
109        }
110        return null;
111    }
112
113    /**
114     * Get instance by category id
115     * @param int $a_cat_id
116     * @return ilCalendarCategory
117     */
118    public static function getInstanceByCategoryId($a_cat_id)
119    {
120        if (!self::$instances[$a_cat_id]) {
121            return self::$instances[$a_cat_id] = new ilCalendarCategory($a_cat_id);
122        }
123        return self::$instances[$a_cat_id];
124    }
125
126    /**
127     * Lookup sort index of calendar type
128     */
129    public static function lookupCategorySortIndex($a_type_id)
130    {
131        return array_search($a_type_id, self::$SORTED_TYPES);
132    }
133    /**
134     * get all assigned appointment ids
135     * @return
136     * @param object $a_category_id
137     */
138    public static function lookupAppointments($a_category_id)
139    {
140        global $DIC;
141
142        $ilDB = $DIC['ilDB'];
143
144        $query = "SELECT * FROM cal_cat_assignments " .
145            'WHERE cat_id = ' . $ilDB->quote($a_category_id, 'integer');
146        $res = $ilDB->query($query);
147        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
148            $apps[] = $row->cal_id;
149        }
150        return $apps ? $apps : array();
151    }
152
153
154    /**
155     * get category id
156     *
157     * @access public
158     * @return int category id
159     */
160    public function getCategoryID()
161    {
162        return $this->cat_id;
163    }
164
165    /**
166     * set title
167     *
168     * @access public
169     * @param string title
170     * @return
171     */
172    public function setTitle($a_title)
173    {
174        $this->title = $a_title;
175    }
176
177    /**
178     * get title
179     *
180     * @access public
181     * @return string title
182     */
183    public function getTitle()
184    {
185        return $this->title;
186    }
187
188
189    /**
190     * set color
191     *
192     * @access public
193     * @param string color
194     */
195    public function setColor($a_color)
196    {
197        $this->color = $a_color;
198    }
199
200    /**
201     * get color
202     *
203     * @access public
204     * @return
205     */
206    public function getColor()
207    {
208        return $this->color;
209    }
210
211    /**
212     * set type
213     *
214     * @access public
215     * @param int type
216     */
217    public function setType($a_type)
218    {
219        $this->type = $a_type;
220    }
221
222    /**
223     * get type
224     *
225     * @access public
226     * @return
227     */
228    public function getType()
229    {
230        return $this->type;
231    }
232
233    /**
234     * set obj id
235     *
236     * @access public
237     * @param int obj_id
238     */
239    public function setObjId($a_obj_id)
240    {
241        $this->obj_id = $a_obj_id;
242    }
243
244    /**
245     * get obj_id
246     *
247     * @access public
248     * @return
249     */
250    public function getObjId()
251    {
252        return $this->obj_id;
253    }
254
255    /**
256     * get type
257     *
258     * @access public
259     */
260    public function getObjType()
261    {
262        return $this->obj_type;
263    }
264
265    public function getLocationType()
266    {
267        return $this->location;
268    }
269
270    public function setLocationType($a_type)
271    {
272        $this->location = $a_type;
273    }
274
275    public function setRemoteUrl($a_url)
276    {
277        $this->remote_url = $a_url;
278    }
279
280    public function getRemoteUrl()
281    {
282        return $this->remote_url;
283    }
284
285    public function setRemoteUser($a_user)
286    {
287        $this->remote_user = $a_user;
288    }
289
290    public function getRemoteUser()
291    {
292        return $this->remote_user;
293    }
294
295    public function setRemotePass($a_pass)
296    {
297        $this->remote_pass = $a_pass;
298    }
299
300    public function getRemotePass()
301    {
302        return $this->remote_pass;
303    }
304
305    /**
306     * Set remote sync last execution
307     * @param ilDateTime $dt
308     */
309    public function setRemoteSyncLastExecution(ilDateTime $dt = null)
310    {
311        $this->remote_sync = $dt;
312    }
313
314    /**
315     * Get last execution date of remote sync
316     * @return \ilDateTime
317     */
318    public function getRemoteSyncLastExecution()
319    {
320        if ($this->remote_sync instanceof ilDateTime) {
321            return $this->remote_sync;
322        }
323        return new ilDateTime();
324    }
325
326
327    /**
328     * add new category
329     *
330     * @access public
331     * @return
332     */
333    public function add()
334    {
335        global $DIC;
336
337        $ilDB = $DIC['ilDB'];
338
339        $next_id = $ilDB->nextId('cal_categories');
340
341        $query = "INSERT INTO cal_categories (cat_id,obj_id,color,type,title,loc_type,remote_url,remote_user,remote_pass,remote_sync) " .
342            "VALUES ( " .
343            $ilDB->quote($next_id, 'integer') . ", " .
344            $this->db->quote($this->getObjId(), 'integer') . ", " .
345            $this->db->quote($this->getColor(), 'text') . ", " .
346            $this->db->quote($this->getType(), 'integer') . ", " .
347            $this->db->quote($this->getTitle(), 'text') . ", " .
348            $this->db->quote($this->getLocationType(), 'integer') . ', ' .
349            $this->db->quote($this->getRemoteUrl(), 'text') . ', ' .
350            $this->db->quote($this->getRemoteUser(), 'text') . ', ' .
351            $this->db->quote($this->getRemotePass(), 'text') . ', ' .
352            $this->db->quote($this->getRemoteSyncLastExecution()->get(IL_CAL_DATETIME, '', ilTimeZone::UTC), 'timestamp') . ' ' .
353            ")";
354
355        $ilDB->manipulate($query);
356
357        $this->cat_id = $next_id;
358        return $this->cat_id;
359    }
360
361    /**
362     * update
363     *
364     * @access public
365     * @return
366     */
367    public function update()
368    {
369        global $DIC;
370
371        $ilDB = $DIC['ilDB'];
372
373        $query = "UPDATE cal_categories " .
374            "SET obj_id = " . $this->db->quote($this->getObjId(), 'integer') . ", " .
375            "color = " . $this->db->quote($this->getColor(), 'text') . ", " .
376            "type = " . $this->db->quote($this->getType(), 'integer') . ", " .
377            "title = " . $this->db->quote($this->getTitle(), 'text') . ", " .
378            "loc_type = " . $this->db->quote($this->getLocationType(), 'integer') . ', ' .
379            "remote_url = " . $this->db->quote($this->getRemoteUrl(), 'text') . ', ' .
380            "remote_user = " . $this->db->quote($this->getRemoteUser(), 'text') . ', ' .
381            "remote_pass = " . $this->db->quote($this->getRemotePass(), 'text') . ', ' .
382            'remote_sync = ' . $this->db->quote($this->getRemoteSyncLastExecution()->get(IL_CAL_DATETIME, '', ilTimeZone::UTC), 'timestamp') . ' ' .
383            "WHERE cat_id = " . $this->db->quote($this->cat_id, 'integer') . " ";
384        $res = $ilDB->manipulate($query);
385        return true;
386    }
387
388    /**
389     * delete
390     *
391     * @access public
392     * @return
393     */
394    public function delete()
395    {
396        global $DIC;
397
398        $ilDB = $DIC['ilDB'];
399
400        $query = "DELETE FROM cal_categories " .
401            "WHERE cat_id = " . $this->db->quote($this->cat_id, 'integer') . " ";
402        $res = $ilDB->manipulate($query);
403
404        include_once('./Services/Calendar/classes/class.ilCalendarVisibility.php');
405        ilCalendarVisibility::_deleteCategories($this->cat_id);
406
407        include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
408        foreach (ilCalendarCategoryAssignments::_getAssignedAppointments(array($this->cat_id)) as $app_id) {
409            include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
410            ilCalendarEntry::_delete($app_id);
411        }
412        ilCalendarCategoryAssignments::_deleteByCategoryId($this->cat_id);
413    }
414
415    /**
416     * validate
417     *
418     * @access public
419     * @return bool
420     */
421    public function validate()
422    {
423        if ($this->getLocationType() == ilCalendarCategory::LTYPE_REMOTE and !$this->getRemoteUrl()) {
424            return false;
425        }
426        if (strlen($this->getTitle()) and strlen($this->getColor()) and $this->getType()) {
427            return true;
428        }
429        return false;
430    }
431
432    /**
433     * read
434     *
435     * @access protected
436     */
437    private function read()
438    {
439        global $DIC;
440
441        $ilDB = $DIC['ilDB'];
442
443        if (!$this->cat_id) {
444            return true;
445        }
446
447        $query = "SELECT * FROM cal_categories " .
448            "WHERE cat_id = " . $this->db->quote($this->getCategoryID(), 'integer') . " ";
449        $res = $this->db->query($query);
450        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
451            $this->cat_id = $row->cat_id;
452            $this->obj_id = $row->obj_id;
453            $this->type = $row->type;
454            $this->color = $row->color;
455            $this->title = $row->title;
456            $this->location = $row->loc_type;
457            $this->remote_url = $row->remote_url;
458            $this->remote_user = $row->remote_user;
459            $this->remote_pass = $row->remote_pass;
460
461            if ($row->remote_sync) {
462                $this->remote_sync = new ilDateTime($row->remote_sync, IL_CAL_DATETIME, 'UTC');
463            } else {
464                $this->remote_sync = new ilDateTime();
465            }
466        }
467        if ($this->getType() == self::TYPE_OBJ) {
468            $this->title = ilObject::_lookupTitle($this->getObjId());
469            $this->obj_type = ilObject::_lookupType($this->getObjId());
470        }
471    }
472}
473