1<?php
2require_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
3require_once('./Services/ActiveRecord/Views/Display/class.arDisplayField.php');
4require_once('./Services/ActiveRecord/Views/Display/class.arDisplayFields.php');
5
6/**
7 * GUI-Class arDisplayGUI
8 *
9 * @author            Timon Amstutz <timon.amstutz@ilub.unibe.ch>
10 * @version           2.0.7
11 *
12 */
13class arDisplayGUI
14{
15
16    /**
17     * @var  ActiveRecord
18     */
19    protected $record;
20    /**
21     * @var arGUI
22     */
23    protected $parent_gui;
24    /**
25     * @var  ilCtrl
26     */
27    protected $ctrl;
28    /**
29     * @var  ilTemplate
30     */
31    protected $tpl;
32    /**
33     * @var string
34     */
35    protected $title = "";
36    /**
37     * @var arDisplayFields|array
38     */
39    protected $fields = array();
40    /**
41     * @var array
42     */
43    protected $data = array();
44    /**
45     * @var string
46     */
47    protected $back_button_name = "";
48    /**
49     * @var string
50     */
51    protected $back_button_target = "";
52    /**
53     * @var ilTemplate
54     */
55    protected $template;
56
57
58    /**
59     * @param arGUI        $parent_gui
60     * @param ActiveRecord $ar
61     */
62    public function __construct(arGUI $parent_gui, ActiveRecord $ar)
63    {
64        global $DIC;
65        $ilCtrl = $DIC['ilCtrl'];
66        $tpl = $DIC['tpl'];
67        /**
68         * @var ilCtrl     $ilCtrl
69         * @var ilTemplate $tpl
70         */
71        $this->ctrl = $ilCtrl;
72        $this->tpl = $tpl;
73        $this->ar = $ar;
74        $this->parent_gui = $parent_gui;
75
76        $this->ctrl->saveParameter($parent_gui, 'ar_id');
77
78        $this->init();
79    }
80
81
82    protected function init()
83    {
84        $this->initTitle();
85        $this->initFields();
86        $this->initBackButton();
87        $this->initTemplate();
88    }
89
90
91    protected function initTitle()
92    {
93        $this->setTitle(strtolower(str_replace("Record", "", get_class($this->ar))));
94    }
95
96
97    protected function initFields()
98    {
99        $this->fields = new arDisplayFields($this->ar);
100        $this->customizeFields();
101        $this->fields->sortFields();
102    }
103
104
105    protected function customizeFields()
106    {
107    }
108
109
110    protected function initBackButton()
111    {
112        $this->setBackButtonName($this->txt("back", false));
113        $this->setBackButtonTarget($this->ctrl->getLinkTarget($this->parent_gui, "index"));
114    }
115
116
117    protected function initTemplate()
118    {
119        $this->setTemplate(new ilTemplate("tpl.display.html", true, true, "Services/ActiveRecord"));
120    }
121
122
123    /**
124     * @return string
125     */
126    public function getHtml()
127    {
128        $this->getTemplate()->setVariable("TITLE", $this->title);
129        $this->setArFieldsData();
130        $this->getTemplate()->setVariable("BACK_BUTTON_NAME", $this->getBackButtonName());
131        $this->getTemplate()->setVariable("BACK_BUTTON_TARGET", $this->getBackButtonTarget());
132
133        return $this->getTemplate()->get();
134    }
135
136
137    protected function setArFieldsData()
138    {
139        foreach ($this->fields->getFields() as $field) {
140            /**
141             * @var arDisplayField $field
142             */
143            if ($field->getVisible()) {
144                $get_function = $field->getGetFunctionName();
145                $value = $this->ar->$get_function();
146                $this->getTemplate()->setCurrentBlock("entry");
147                $this->getTemplate()->setVariable("ITEM", $this->txt($field->getTxt()));
148                $this->getTemplate()->setVariable("VALUE", $this->setArFieldData($field, $value));
149                $this->getTemplate()->parseCurrentBlock();
150            }
151        }
152    }
153
154
155    /**
156     * @param arDisplayField $field
157     * @param                $value
158     *
159     * @return null|string
160     */
161    protected function setArFieldData(arDisplayField $field, $value)
162    {
163        if ($field->getCustomField()) {
164            return $this->setCustomFieldData($field);
165        } else {
166            if ($value == null) {
167                return $this->setEmptyFieldData($field);
168            } else {
169                if ($field->getIsCreatedByField()) {
170                    return $this->setCreatedByData($field, $value);
171                } else {
172                    if ($field->getIsModifiedByField()) {
173                        return $this->setModifiedByData($field, $value);
174                    } else {
175                        switch ($field->getFieldType()) {
176                            case 'integer':
177                            case 'float':
178                                return $this->setNumericData($field, $value);
179                            case 'text':
180                                return $this->setTextData($field, $value);
181                            case 'date':
182                            case 'time':
183                            case 'timestamp':
184                                return $this->setDateTimeData($field, $value);
185                            case 'clob':
186                                return $this->setClobData($field, $value);
187                        }
188                    }
189                }
190            }
191        }
192    }
193
194
195    /**
196     * @param arDisplayField $field
197     *
198     * @return string
199     */
200    protected function setEmptyFieldData(arDisplayField $field)
201    {
202        return $this->txt("", false);
203    }
204
205
206    /**
207     * @param arDisplayField $field
208     *
209     * @return string
210     */
211    protected function setCustomFieldData(arDisplayField $field)
212    {
213        return "CUSTOM-OVERRIDE: setCustomFieldData";
214    }
215
216
217    /**
218     * @param arDisplayField $field
219     * @param                $value
220     *
221     * @return string
222     */
223    protected function setModifiedByData(arDisplayField $field, $value)
224    {
225        $user = new ilObjUser($value);
226
227        return $user->getPublicName();
228    }
229
230
231    /**
232     * @param arDisplayField $field
233     * @param                $value
234     *
235     * @return string
236     */
237    protected function setCreatedByData(arDisplayField $field, $value)
238    {
239        $user = new ilObjUser($value);
240
241        return $user->getPublicName();
242    }
243
244
245    /**
246     * @param arDisplayField $field
247     * @param                $value
248     *
249     * @return mixed
250     */
251    protected function setNumericData(arDisplayField $field, $value)
252    {
253        return $value;
254    }
255
256
257    /**
258     * @param arDisplayField $field
259     * @param                $value
260     *
261     * @return mixed
262     */
263    protected function setTextData(arDisplayField $field, $value)
264    {
265        return $value;
266    }
267
268
269    /**
270     * @param arDisplayField $field
271     * @param                $value
272     *
273     * @return string
274     */
275    protected function setDateTimeData(arDisplayField $field, $value)
276    {
277        $datetime = new ilDateTime($value, IL_CAL_DATETIME);
278
279        return ilDatePresentation::formatDate($datetime, IL_CAL_DATETIME);
280    }
281
282
283    /**
284     * @param arDisplayField $field
285     * @param                $value
286     *
287     * @return mixed
288     */
289    protected function setClobData(arDisplayField $field, $value)
290    {
291        return $value;
292    }
293
294
295    /**
296     * @param string $back_button_name
297     */
298    public function setBackButtonName($back_button_name)
299    {
300        $this->back_button_name = $back_button_name;
301    }
302
303
304    /**
305     * @return string
306     */
307    public function getBackButtonName()
308    {
309        return $this->back_button_name;
310    }
311
312
313    /**
314     * @param $back_button_target
315     */
316    public function setBackButtonTarget($back_button_target)
317    {
318        $this->back_button_target = $back_button_target;
319    }
320
321
322    /**
323     * @return string
324     */
325    public function getBackButtonTarget()
326    {
327        return $this->back_button_target;
328    }
329
330
331    /**
332     * @param arDisplayFields $fields
333     */
334    public function setFields(arDisplayFields $fields)
335    {
336        $this->fields = $fields;
337    }
338
339
340    /**
341     * @return arDisplayFields
342     */
343    public function getFields()
344    {
345        return $this->fields;
346    }
347
348
349    /**
350     * @return arDisplayField []
351     */
352    public function getFieldsAsArray()
353    {
354        return $this->getFields()->getFields();
355    }
356
357
358    /**
359     * @param $field_name
360     *
361     * @return arDisplayField
362     */
363    public function getField($field_name)
364    {
365        return $this->getFields()->getField($field_name);
366    }
367
368
369    /**
370     * @param arDisplayField
371     */
372    public function addField(arDisplayField $field)
373    {
374        $this->getFields()->addField($field);
375    }
376
377
378    /**
379     * @param string $title
380     */
381    public function setTitle($title)
382    {
383        $this->title = $title;
384    }
385
386
387    /**
388     * @return string
389     */
390    public function getTitle()
391    {
392        return $this->title;
393    }
394
395
396    /**
397     * @param \ilTemplate $template
398     */
399    public function setTemplate($template)
400    {
401        $this->template = $template;
402    }
403
404
405    /**
406     * @return \ilTemplate
407     */
408    public function getTemplate()
409    {
410        return $this->template;
411    }
412
413
414    /**
415     * @param      $txt
416     * @param bool $plugin_txt
417     *
418     * @return string
419     */
420    protected function txt($txt, $plugin_txt = true)
421    {
422        return $this->parent_gui->txt($txt, $plugin_txt);
423    }
424}
425