1<?php
2include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
3
4/**
5 * Class ilCalendarAppointmentPresentationGUI
6 *
7 * @author	Jesús López <lopez@leifos.com>
8 * @version  $Id$
9 * @ilCtrl_Calls ilCalendarAppointmentPresentationGUI: ilInfoScreenGUI, ilCalendarAppointmentGUI
10*/
11class ilCalendarAppointmentPresentationGUI
12{
13    const MODE_MODAL = "modal";
14    const MODE_LIST_ITEM = "list_item";
15
16    protected $seed = null;
17    protected static $instance = null;
18    protected $settings = null;
19    protected $appointment;
20
21    protected $mode = self::MODE_MODAL;
22
23    protected $toolbar;
24    protected $info_screen;
25
26
27    /**
28     * @var \ILIAS\UI\Component\Item\Standard|null
29     */
30    protected $list_item = null;
31
32    /**
33     * Singleton
34     *
35     * @access public
36     * @param
37     * @param
38     * @return
39     */
40    protected function __construct(ilDate $seed = null, $a_app)
41    {
42        global $DIC;
43        $this->lng = $DIC->language();
44        $this->ctrl = $DIC->ctrl();
45
46        $this->settings = ilCalendarSettings::_getInstance();
47
48        $this->seed = $seed;
49        $this->appointment = $a_app;
50
51        $this->tpl = $DIC["tpl"];
52
53        $this->info_screen = new ilInfoScreenGUI($this);
54        $this->toolbar = new ilToolbarGUI();
55    }
56
57    /**
58     * Set list item mode
59     *
60     * @param \ILIAS\UI\Component\Item\Standard $a_val
61     */
62    public function setListItemMode(\ILIAS\UI\Component\Item\Standard $a_val)
63    {
64        $this->list_item = $a_val;
65        $this->mode = self::MODE_LIST_ITEM;
66    }
67
68    /**
69     * Get list item mode
70     *
71     * @return \ILIAS\UI\Component\Item\Standard
72     */
73    public function getListItem()
74    {
75        return $this->list_item;
76    }
77
78    /**
79     * get singleton instance
80     *
81     * @access public
82     * @param ilDate $seed
83     * @param  $a_app
84     * @return ilCalendarAppointmentPresentationGUI
85     * @static
86     */
87    public static function _getInstance(ilDate $seed, $a_app)
88    {
89        return new static($seed, $a_app);
90    }
91
92    public function executeCommand()
93    {
94        global $DIC;
95
96        $ilCtrl = $DIC['ilCtrl'];
97
98        $next_class = $ilCtrl->getNextClass($this);
99        $cmd = $ilCtrl->getCmd("getHTML");
100
101        switch ($next_class) {
102            case 'ilcalendarappointmentgui':
103                include_once('./Services/Calendar/classes/class.ilCalendarAppointmentGUI.php');
104                $app = new ilCalendarAppointmentGUI($this->seed, $this->seed, (int) $_GET['app_id']);
105                $this->ctrl->forwardCommand($app);
106                break;
107
108            default:
109                if ($next_class != '') {
110                    // get the path and include
111                    $class_path = $this->ctrl->lookupClassPath($next_class);
112                    include_once($class_path);
113
114                    // check if the class implements our interface
115                    $class_name = $this->ctrl->getClassForClasspath($class_path);
116                    if (in_array("ilCalendarAppointmentPresentation", class_implements($class_name))) {
117                        // forward command to class
118                        $gui_class = new $class_name($this->appointment, $this->info_screen, $this->toolbar, null);
119                        $this->ctrl->forwardCommand($gui_class);
120                    }
121                }
122                break;
123        }
124    }
125
126    /**
127     * Get seed date
128     */
129    public function getSeed()
130    {
131        return $this->seed;
132    }
133
134    /**
135     * Get modal html
136     * @return string
137     */
138    public function getHTML()
139    {
140        if ($this->mode == self::MODE_MODAL) {
141            return $this->getModalHTML();
142        }
143        if ($this->mode == self::MODE_LIST_ITEM) {
144            return $this->modifyListItem();
145        }
146        return "";
147    }
148
149    /**
150     * Get modal html
151     * @return string
152     */
153    public function getModalHTML()
154    {
155        include_once "./Services/Calendar/classes/AppointmentPresentation/class.ilAppointmentPresentationFactory.php";
156
157        $tpl = new ilTemplate('tpl.appointment_presentation.html', true, true, 'Services/Calendar');
158
159        $info_screen = $this->info_screen;
160        $info_screen->setFormAction($this->ctrl->getFormAction($this));
161
162        #21529 create new toolbar with unique id using the entry id for this purpose
163        //$toolbar = $this->toolbar;
164        $toolbar = new ilToolbarGUI();
165        $toolbar->setId($this->appointment['event']->getEntryId());
166
167        $f = ilAppointmentPresentationFactory::getInstance($this->appointment, $info_screen, $toolbar, null);
168
169        $this->ctrl->getHTML($f);
170        $content = $info_screen->getHTML();
171
172        //because #21529
173        $plugin_results = $this->getContentByPlugins($content, $toolbar);
174        $content = $plugin_results['content'];
175        $toolbar = $plugin_results['toolbar'];
176
177        // show toolbar
178        $tpl->setCurrentBlock("toolbar");
179        $tpl->setVariable("TOOLBAR", $toolbar->getHTML());
180        $tpl->parseCurrentBlock();
181
182
183        // show infoscreen
184        $tpl->setVariable("CONTENT", $content);
185
186        return $tpl->get();
187    }
188
189    /**
190     * Modify List item
191     */
192    public function modifyListItem()
193    {
194        $li = $this->getListItem();
195        include_once "./Services/Calendar/classes/AppointmentPresentation/class.ilAppointmentPresentationFactory.php";
196        $f = ilAppointmentPresentationFactory::getInstance($this->appointment, null, null, $li);
197        $this->ctrl->getHTML($f);
198        $this->list_item = $f->getListItem();
199    }
200
201    protected function getActivePlugins()
202    {
203        global $DIC;
204
205        $ilPluginAdmin = $DIC['ilPluginAdmin'];
206
207        $res = array();
208
209        foreach ($ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "Calendar", "capm") as $plugin_name) {
210            $res[] = $ilPluginAdmin->getPluginObject(
211                IL_COMP_SERVICE,
212                "Calendar",
213                "capm",
214                $plugin_name
215            );
216        }
217
218        return $res;
219    }
220
221    protected function getContentByPlugins($a_content, $a_toolbar)
222    {
223        $content = $a_content;
224        $toolbar = $a_toolbar;
225        foreach ($this->getActivePlugins() as $plugin) {
226            //pass only the appointment stuff
227            $plugin->setAppointment($this->appointment['event'], new ilDateTime($this->appointment['dstart']));
228
229            if ($new_infoscreen = $plugin->infoscreenAddContent($this->info_screen)) {
230                $this->info_screen = $new_infoscreen;
231            }
232
233            $content = $this->info_screen->getHTML();
234            $extra_content = $plugin->addExtraContent();
235            if ($extra_content != '') {
236                $content .= $extra_content;
237            }
238
239            if ($new_content = $plugin->replaceContent()) {
240                $content = $new_content;
241            }
242
243            if ($new_toolbar = $plugin->toolbarAddItems($toolbar)) {
244                $toolbar = $new_toolbar;
245            }
246
247            if ($new_toolbar = $plugin->toolbarReplaceContent()) {
248                $new_toolbar->setId($a_toolbar->getId());
249                $toolbar = $new_toolbar;
250            }
251        }
252
253        return array(
254            'content' => $content,
255            'toolbar' => $toolbar
256        );
257    }
258}
259