1<?php
2include_once './Services/Calendar/classes/class.ilCalendarViewGUI.php';
3/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Calendar agenda list
7 *
8 * @author Alex Killing <killing@leifos.de>
9 * @ingroup ServicesCalendar
10 * @ilCtrl_Calls ilCalendarAgendaListGUI: ilCalendarAppointmentPresentationGUI
11 */
12class ilCalendarAgendaListGUI extends ilCalendarViewGUI
13{
14    const PERIOD_DAY = 1;
15    const PERIOD_WEEK = 2;
16    const PERIOD_MONTH = 3;
17    const PERIOD_HALF_YEAR = 4;
18
19    /**
20     * @var ilCtrl
21     */
22    protected $ctrl;
23
24    /**
25     * @var ilObjUser
26     */
27    protected $user;
28
29    /**
30     * @var ilLanguage
31     */
32    protected $lng;
33
34    /**
35     * @var int
36     */
37    protected $period = self::PERIOD_WEEK;
38
39    /**
40     * @var ilDate
41     */
42    protected $period_end_day = null;
43
44
45
46    /**
47     * Constructor
48     *
49     * @param ilDate $seed
50     * @todo make parent constructor (initialize) and init also seed and other common stuff
51     */
52    public function __construct(ilDate $seed)
53    {
54        parent::__construct($seed, ilCalendarViewGUI::CAL_PRESENTATION_AGENDA_LIST);
55
56        $this->ctrl->saveParameter($this, "cal_agenda_per");
57
58        //$qp = $DIC->http()->request()->getQueryParams();
59        #21479
60        $qp = $_GET;
61        if ((int) $qp["cal_agenda_per"] > 0 && (int) $qp["cal_agenda_per"] <= 4) {
62            $this->period = $qp["cal_agenda_per"];
63        } elseif ($period = ilSession::get('cal_list_view')) {
64            $this->period = $period;
65        }
66
67        $get_seed = $qp["seed"];
68        $this->ctrl->setParameterByClass("ilcalendarinboxgui", "seed", $this->seed->get(IL_CAL_DATE));
69        $end_date = clone $this->seed;
70        switch ($this->period) {
71            case self::PERIOD_DAY:
72                $end_date->increment(IL_CAL_DAY, 1);
73                break;
74
75            case self::PERIOD_WEEK:
76                $end_date->increment(IL_CAL_WEEK, 1);
77                break;
78
79            case self::PERIOD_MONTH:
80                $end_date->increment(IL_CAL_MONTH, 1);
81                break;
82
83            case self::PERIOD_HALF_YEAR:
84                $end_date->increment(IL_CAL_MONTH, 6);
85                break;
86        }
87        $this->period_end_day = $end_date->get(IL_CAL_DATE);
88    }
89
90    /**
91     * Execute command
92     */
93    public function executeCommand()
94    {
95        $next_class = $this->ctrl->getNextClass($this);
96        $cmd = $this->ctrl->getCmd("getHTML");
97
98        switch ($next_class) {
99            case "ilcalendarappointmentpresentationgui":
100                $this->ctrl->setReturn($this, "");
101                include_once("./Services/Calendar/classes/class.ilCalendarAppointmentPresentationGUI.php");
102                $gui = ilCalendarAppointmentPresentationGUI::_getInstance(new ilDate($this->seed->get(IL_CAL_DATE), IL_CAL_DATE), $this->getCurrentApp());
103                $this->ctrl->forwardCommand($gui);
104                break;
105
106            default:
107                $this->ctrl->setReturn($this, "");
108                if (in_array($cmd, array("getHTML", "getModalForApp"))) {
109                    return $this->$cmd();
110                }
111        }
112    }
113
114    /**
115     * Get output
116     *
117     * @param
118     * @return
119     */
120    public function getHTML()
121    {
122        $navigation = new ilCalendarHeaderNavigationGUI($this, new ilDate($this->seed->get(IL_CAL_DATE), IL_CAL_DATE), ilDateTime::DAY);
123        $navigation->getHTML();
124
125        // set return now (after header navigation) to the list (e.g. for profile links)
126        $this->ctrl->setReturn($this, "");
127
128        // get events
129        $events = $this->getEvents();
130        $events = ilUtil::sortArray($events, "dstart", "asc", true);
131
132        $df = new \ILIAS\Data\Factory();
133        $items = array();
134        $groups = array();
135        $modals = array();
136        $group_date = new ilDate(0, IL_CAL_UNIX);
137        $end_day = new ilDate($this->period_end_day, IL_CAL_DATE);
138        $end_day->increment(ilDateTime::DAY, -1);
139        foreach ($events as $e) {
140            if ($e['event']->isFullDay()) {
141                // begin/end is Date (without timzone)
142                $begin = new ilDate($e['dstart'], IL_CAL_UNIX);
143                $end = new ilDate($e['dend'], IL_CAL_UNIX);
144            } else {
145                // begin/end is DateTime (with timezone conversion)
146                $begin = new ilDateTime($e['dstart'], IL_CAL_UNIX);
147                $end = new ilDateTime($e['dend'], IL_CAL_UNIX);
148            }
149
150            //  if the begin is before seed date (due to timezone conversion) => continue
151            if (ilDateTime::_before(
152                $begin,
153                $this->seed,
154                ilDateTime::DAY,
155                $GLOBALS['DIC']->user()->getTimezone()
156            )) {
157                continue;
158            }
159
160            if (ilDateTime::_after(
161                $begin,
162                $end_day,
163                ilDateTime::DAY,
164                $GLOBALS['DIC']->user()->getTimezone()
165            )
166            ) {
167                break;
168            }
169
170
171            // initialize group date for first iteration
172            if ($group_date->isNull()) {
173                $group_date = new ilDate(
174                    $begin->get(IL_CAL_DATE, '', $GLOBALS['DIC']->user()->getTimezone()),
175                    IL_CAL_DATE
176                );
177            }
178
179            if (!ilDateTime::_equals($group_date, $begin, IL_CAL_DAY, $GLOBALS['DIC']->user()->getTimezone())) {
180                // create new group
181                $groups[] = $this->ui_factory->item()->group(
182                    ilDatePresentation::formatDate($group_date, false, true),
183                    $items
184                );
185
186                $group_date = new ilDate(
187                    $begin->get(IL_CAL_DATE, '', $GLOBALS['DIC']->user()->getTimezone()),
188                    IL_CAL_DATE
189                );
190                $items = [];
191            }
192
193            // get calendar
194            $cat_id = ilCalendarCategoryAssignments::_lookupCategory($e["event"]->getEntryId());
195            $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
196
197            $properties = array();
198
199            /*TODO:
200             * All this code related with the ctrl and shy button can be centralized in
201             * ilCalendarViewGUI refactoring the method getAppointmentShyButton or
202             * if we want extend this class from ilCalendarInboxGUI we can just keep it here.
203             */
204
205            // shy button for title
206            $this->ctrl->setParameter($this, 'app_id', $e["event"]->getEntryId());
207            $this->ctrl->setParameter($this, 'dt', $e['dstart']);
208            $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
209
210            $url = $this->ctrl->getLinkTarget($this, "getModalForApp", "", true, false);
211            $this->ctrl->setParameter($this, "app_id", $_GET["app_id"]);
212            $this->ctrl->setParameter($this, "dt", $_GET["dt"]);
213            $this->ctrl->setParameter($this, 'modal_title', $_GET["modal_title"]);
214            $modal = $this->ui_factory->modal()->roundtrip('', [])->withAsyncRenderUrl($url);
215            $shy = $this->ui_factory->button()->shy($e["event"]->getPresentationTitle(false), "")->withOnClick($modal->getShowSignal());
216
217            $modals[] = $modal;
218            if ($e['event']->isFullDay()) {
219                $lead_text = $this->lng->txt("cal_all_day");
220            } else {
221                $lead_text = ilDatePresentation::formatPeriod($begin, $end, true);
222            }
223            $li = $this->ui_factory->item()->standard($shy)
224                ->withDescription("" . nl2br(strip_tags($e["event"]->getDescription())))
225                ->withLeadText($lead_text)
226                ->withProperties($properties)
227                ->withColor($df->color('#' . $cat_info["color"]));
228
229            if ($li_edited_by_plugin = $this->getPluginAgendaItem($li, $e['event'])) {
230                $li = $li_edited_by_plugin;
231            }
232
233            // add type specific actions/properties
234            include_once("./Services/Calendar/classes/class.ilCalendarAppointmentPresentationGUI.php");
235            $app_gui = ilCalendarAppointmentPresentationGUI::_getInstance(new ilDate($this->seed->get(IL_CAL_DATE), IL_CAL_DATE), $e);
236            $app_gui->setListItemMode($li);
237            $this->ctrl->getHTML($app_gui);
238            $items[] = $app_gui->getListItem();
239        }
240        // terminate last group
241        if (!$group_date->isNull()) {
242            $groups[] = $this->ui_factory->item()->group(
243                ilDatePresentation::formatDate($group_date, false, true),
244                $items
245            );
246        }
247
248        // list actions
249        $images = array_fill(1, 4, "<span class=\"ilAdvNoImg\"></span>");
250        if ($cal_agenda_per = (int) $_GET['cal_agenda_per']) {
251            $images[$cal_agenda_per] = "<img src='./templates/default/images/icon_checked.svg' alt='Month'>";
252        } else {
253            $images[$this->period] = "<img src='./templates/default/images/icon_checked.svg' alt='Month'>";
254        }
255
256        #21479 Set seed if the view does not contain any event.
257        $this->ctrl->setParameter($this, 'seed', $this->seed->get(IL_CAL_DATE));
258
259        $items = array();
260        $this->ctrl->setParameter($this, "cal_agenda_per", self::PERIOD_DAY);
261        $items[] = $this->ui_factory->button()->shy($images[1] . "1 " . $this->lng->txt("day"), $this->ctrl->getLinkTarget($this, "getHTML"));
262        $this->ctrl->setParameter($this, "cal_agenda_per", self::PERIOD_WEEK);
263        $items[] = $this->ui_factory->button()->shy($images[2] . "1 " . $this->lng->txt("week"), $this->ctrl->getLinkTarget($this, "getHTML"));
264        $this->ctrl->setParameter($this, "cal_agenda_per", self::PERIOD_MONTH);
265        $items[] = $this->ui_factory->button()->shy($images[3] . "1 " . $this->lng->txt("month"), $this->ctrl->getLinkTarget($this, "getHTML"));
266        $this->ctrl->setParameter($this, "cal_agenda_per", self::PERIOD_HALF_YEAR);
267        $items[] = $this->ui_factory->button()->shy($images[4] . "6 " . $this->lng->txt("months"), $this->ctrl->getLinkTarget($this, "getHTML"));
268        $this->ctrl->setParameter($this, "cal_agenda_per", $this->period);
269
270
271        $actions = $this->ui_factory->dropdown()->standard($items)->withLabel($this->lng->txt("days"));
272
273        $list_title =
274            $this->lng->txt("cal_agenda") . ": " . ilDatePresentation::formatDate(new ilDate($this->seed->get(IL_CAL_DATE), IL_CAL_DATE));
275        if ($this->period != self::PERIOD_DAY) {
276            $end_day = new ilDate($this->period_end_day, IL_CAL_DATE);
277            $end_day->increment(ilDateTime::DAY, -1);
278            $list_title .= " - " . ilDatePresentation::formatDate($end_day);
279        }
280
281        $list = $this->ui_factory->panel()->listing()->standard($list_title, $groups)
282            ->withActions($actions);
283
284
285        $comps = array_merge($modals, array($list));
286
287        $html = $this->ui_renderer->render($comps);
288
289        if (count($groups) == 0) {
290            $tpl = $this->ui->mainTemplate();
291            $html .= $tpl->getMessageHTML($this->lng->txt("cal_no_events_info"));
292        }
293
294        return $html;
295    }
296
297    /**
298     * @param $a_item  ILIAS\UI\Component\Item\Item
299     * @param $appointment
300     * @return $li ILIAS\UI\Component\Item\Item
301     */
302    public function getPluginAgendaItem($a_item, $appointment)
303    {
304        //"capg" is the plugin slot id for AppointmentCustomGrid
305        foreach ($this->getActivePlugins("capg") as $plugin) {
306            $plugin->setAppointment($appointment, $appointment->getStart());
307            $li = $plugin->editAgendaItem($a_item);
308        }
309        return $li;
310    }
311
312    /**
313     * needed in CalendarInboxGUI to get events using a proper period.
314     * todo define default period only once (self::PERIOD_WEEK, protected $period = self::PERIOD_WEEK)
315     * @return int|mixed
316     */
317    public static function getPeriod()
318    {
319        #21479
320        $qp = $_GET;
321        if ((int) $qp["cal_agenda_per"] > 0 && (int) $qp["cal_agenda_per"] <= 4) {
322            return $qp["cal_agenda_per"];
323        } elseif ($period = ilSession::get('cal_list_view')) {
324            return $period;
325        } else {
326            return self::PERIOD_WEEK;
327        }
328    }
329}
330