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
24include_once('./Services/Table/classes/class.ilTable2GUI.php');
25
26/**
27* show presentation of calendar category side block
28*
29* @author Stefan Meyer <smeyer.ilias@gmx.de>
30* @version $Id$
31*
32* @ingroup ServicesCalendar
33*/
34
35class ilCalendarCategoryTableGUI extends ilTable2GUI
36{
37    private $seed = null;
38
39    /**
40     * Constructor
41     *
42     * @access public
43     * @param
44     * @return
45     */
46    public function __construct($a_parent_obj, ilDateTime $seed = null)
47    {
48        global $DIC;
49
50        $lng = $DIC['lng'];
51        $ilCtrl = $DIC['ilCtrl'];
52        $ilUser = $DIC['ilUser'];
53
54        // this should be deprecated
55        die("ilCalendarCategoryTableGUI::_construct");
56
57
58        $this->lng = $lng;
59        $this->lng->loadLanguageModule('dateplaner');
60        $this->ctrl = $ilCtrl;
61
62        $this->seed = $seed;
63
64        $this->setId('calmng');
65
66        parent::__construct($a_parent_obj, 'showCategories');
67        $this->setFormName('categories');
68        $this->addColumn('', '', "1", true);
69        $this->addColumn($this->lng->txt('type'), 'type_sortable', "1");
70        $this->addColumn($this->lng->txt('title'), 'title', "100%");
71        $this->addColumn('', 'subscription', '');
72
73        $this->ctrl->setParameterByClass(get_class($this->getParentObject()), 'seed', $this->seed->get(IL_CAL_DATE));
74        $this->setFormAction($this->ctrl->getFormAction($a_parent_obj));
75        $this->setRowTemplate("tpl.show_category_row.html", "Services/Calendar");
76        $this->disable('sort');
77        if (!$ilUser->prefs["screen_reader_optimization"]) {
78            $this->disable('header');
79        }
80
81        //$this->setShowRowsSelector(true);
82        $this->disable('numinfo');
83        $this->enable('select_all');
84        $this->setSelectAllCheckbox('selected_cat_ids');
85        $this->setDisplayAsBlock(true);
86
87        $this->setDefaultOrderDirection('asc');
88        $this->setDefaultOrderField('type_sortable');
89
90        // Show add calendar button
91        $this->addCommandButton('add', $this->lng->txt('cal_add_calendar'));
92    }
93
94    /**
95     * fill row
96     *
97     * @access protected
98     * @param
99     * @return
100     */
101    protected function fillRow($a_set)
102    {
103        $this->tpl->setVariable('VAL_ID', $a_set['id']);
104        if (!$a_set['hidden']) {
105            $this->tpl->setVariable('VAL_CHECKED', 'checked="checked"');
106        }
107        $this->tpl->setVariable('VAL_TITLE', $a_set['title']);
108        $this->tpl->setVariable('BGCOLOR', $a_set['color']);
109
110        $this->ctrl->setParameter($this->getParentObject(), 'category_id', $a_set['id']);
111        $this->tpl->setVariable('EDIT_LINK', $this->ctrl->getLinkTarget($this->getParentObject(), 'details'));
112        $this->tpl->setVariable('TXT_EDIT', $this->lng->txt('edit'));
113
114        switch ($a_set['type']) {
115            case ilCalendarCategory::TYPE_GLOBAL:
116                $this->tpl->setVariable('IMG_SRC', ilUtil::getImagePath('icon_calg.svg'));
117                $this->tpl->setVariable('IMG_ALT', $this->lng->txt('cal_type_system'));
118                break;
119
120            case ilCalendarCategory::TYPE_USR:
121                $this->tpl->setVariable('IMG_SRC', ilUtil::getImagePath('icon_usr.svg'));
122                $this->tpl->setVariable('IMG_ALT', $this->lng->txt('cal_type_personal'));
123                break;
124
125            case ilCalendarCategory::TYPE_OBJ:
126                $type = ilObject::_lookupType($a_set['obj_id']);
127                $this->tpl->setVariable('IMG_SRC', ilUtil::getImagePath('icon_' . $type . '.svg'));
128                $this->tpl->setVariable('IMG_ALT', $this->lng->txt('cal_type_' . $type));
129                break;
130        }
131        if (strlen($a_set['path'])) {
132            $this->tpl->setCurrentBlock('calendar_path');
133            $this->tpl->setVariable('ADD_PATH_INFO', $a_set['path']);
134            $this->tpl->parseCurrentBlock();
135        }
136
137        // Subscription link
138        $this->tpl->setVariable('SUB_SRC', ilRSSButtonGUI::get(ilRSSButtonGUI::ICON_ICAL));
139        $this->ctrl->setParameterByClass('ilcalendarsubscriptiongui', 'seed', $this->seed->get(IL_CAL_DATE));
140        $this->ctrl->setParameterByClass('ilcalendarsubscriptiongui', 'category_id', $a_set['id']);
141        $this->tpl->setVariable('SUB_LINK', $this->ctrl->getLinkTargetByClass(array('ilcalendarpresentationgui','ilcalendarsubscriptiongui')));
142        $this->ctrl->setParameterByClass('ilcalendarsubscriptiongui', 'category_id', "");
143        $this->tpl->setVariable('SUB_ALT', $this->lng->txt('ical_export'));
144    }
145
146    /**
147     * parse
148     *
149     * @access public
150     * @return
151     */
152    public function parse()
153    {
154        global $DIC;
155
156        $ilUser = $DIC['ilUser'];
157        $tree = $DIC['tree'];
158
159        include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
160        include_once('./Services/Calendar/classes/class.ilCalendarVisibility.php');
161
162        $hidden_obj = ilCalendarVisibility::_getInstanceByUserId($ilUser->getId());
163        $hidden = $hidden_obj->getHidden();
164
165        $cats = ilCalendarCategories::_getInstance($ilUser->getId());
166        $all = $cats->getCategoriesInfo();
167        $tmp_title_counter = array();
168        $categories = array();
169        foreach ($all as $category) {
170            $tmp_arr['obj_id'] = $category['obj_id'];
171            $tmp_arr['id'] = $category['cat_id'];
172            $tmp_arr['hidden'] = (bool) in_array($category['cat_id'], $hidden);
173            $tmp_arr['title'] = $category['title'];
174            $tmp_arr['type'] = $category['type'];
175
176            // Append object type to make type sortable
177            $tmp_arr['type_sortable'] = ilCalendarCategory::lookupCategorySortIndex($category['type']);
178            if ($category['type'] == ilCalendarCategory::TYPE_OBJ) {
179                $tmp_arr['type_sortable'] .= ('_' . ilObject::_lookupType($category['obj_id']));
180            }
181
182            $tmp_arr['color'] = $category['color'];
183            $tmp_arr['editable'] = $category['editable'];
184
185            $categories[] = $tmp_arr;
186
187            // count title for appending the parent container if there is more than one entry.
188            $tmp_title_counter[$category['type'] . '_' . $category['title']]++;
189        }
190
191        $path_categories = array();
192        foreach ($categories as $cat) {
193            if ($cat['type'] == ilCalendarCategory::TYPE_OBJ) {
194                if ($tmp_title_counter[$cat['type'] . '_' . $cat['title']] > 1) {
195                    foreach (ilObject::_getAllReferences($cat['obj_id']) as $ref_id) {
196                        $cat['path'] = $this->buildPath($ref_id);
197                        break;
198                    }
199                }
200            }
201            $path_categories[] = $cat;
202        }
203        $this->setData($path_categories);
204    }
205
206    protected function buildPath($a_ref_id)
207    {
208        global $DIC;
209
210        $tree = $DIC['tree'];
211
212        $path_arr = $tree->getPathFull($a_ref_id, ROOT_FOLDER_ID);
213        $counter = 0;
214        unset($path_arr[count($path_arr) - 1]);
215
216        foreach ($path_arr as $data) {
217            if ($counter++) {
218                $path .= " -> ";
219            }
220            $path .= $data['title'];
221        }
222        if (strlen($path) > 30) {
223            return '...' . substr($path, -30);
224        }
225        return $path;
226    }
227}
228