1<?php
2include_once("./Services/Component/classes/class.ilPluginConfigGUI.php");
3include_once('./Services/ActiveRecord/class.ActiveRecordList.php');
4include_once('./Services/ActiveRecord/Views/Index/class.arIndexTableGUI.php');
5include_once('./Services/UICore/classes/class.ilTemplate.php');
6
7/**
8 * @author  Timon Amstutz <timon.amstutz@ilub.unibe.ch>
9 * @version 2.0.7
10 *
11 */
12class arGUI
13{
14
15    /**
16     * @var ilCtrl
17     */
18    protected $ctrl;
19    /**
20     * @var ilTemplate
21     */
22    protected $tpl;
23    /**
24     * @var ilAccessHandler
25     */
26    protected $access;
27    /**
28     * @ar ilLanguage
29     */
30    protected $lng;
31    /**
32     * @var ilPlugin
33     */
34    protected $plugin_object = null;
35    /**
36     * @var string
37     */
38    protected $record_type = "";
39    /**
40     * @var ActiveRecord
41     */
42    protected $ar;
43    /**
44     * @var string
45     */
46    protected $lng_prefix = "";
47
48
49    /**
50     * @param          $record_type
51     * @param ilPlugin $plugin_object
52     */
53    public function __construct($record_type, ilPlugin $plugin_object = null)
54    {
55        global $DIC;
56        $tpl = $DIC['tpl'];
57        $ilCtrl = $DIC['ilCtrl'];
58        $ilAccess = $DIC['ilAccess'];
59        $lng = $DIC['lng'];
60
61        $this->lng = $lng;
62
63        if ($plugin_object) {
64            $this->setLngPrefix($plugin_object->getPrefix());
65            $plugin_object->loadLanguageModule();
66        }
67
68        $this->tpl = $tpl;
69        $this->ctrl = $ilCtrl;
70        $this->access = $ilAccess;
71        $this->plugin_object = $plugin_object;
72        $this->record_type = $record_type;
73        $this->ar = new $record_type();
74    }
75
76
77    public function executeCommand()
78    {
79        $cmd = $this->ctrl->getCmd();
80        switch ($cmd) {
81            case "edit":
82            case "update":
83            case "view":
84            case "delete":
85                $this->$cmd(arIndexTableGUI::domid_decode($_GET['ar_id']));
86                break;
87            case "multiAction":
88                $action_name = $_POST["index_table_multi_action_2"];
89                $this->multiAction($action_name);
90                break;
91            default:
92                $this->$cmd();
93                break;
94        }
95    }
96
97
98    /**
99     * @param arIndexTableGUI $table_gui
100     */
101    public function index(arIndexTableGUI $table_gui = null)
102    {
103        if (!$table_gui) {
104            $index_table_gui_class = $this->record_type . "IndexTableGUI";
105            /**
106             * @var arIndexTableGUI $table_gui
107             */
108            $table_gui = new $index_table_gui_class($this, "index", new ActiveRecordList($this->ar));
109        }
110        $this->tpl->setContent($table_gui->getHTML());
111    }
112
113
114    public function applyFilter()
115    {
116        $index_table_gui_class = $this->record_type . "IndexTableGUI";
117        /**
118         * @var arIndexTableGUI $table_gui
119         */
120        $table_gui = new $index_table_gui_class($this, "index", new ActiveRecordList($this->ar));
121        $table_gui->applyFilter();
122        $this->index();
123    }
124
125
126    public function resetFilter()
127    {
128        $index_table_gui_class = $this->record_type . "IndexTableGUI";
129        /**
130         * @var arIndexTableGUI $table_gui
131         */
132        $table_gui = new $index_table_gui_class($this, "index", new ActiveRecordList($this->ar));
133        $table_gui->resetFilter();
134        $this->index();
135    }
136
137
138    /**
139     * @param string $action_name
140     */
141    public function multiAction($action_name = "")
142    {
143        $ids = array();
144        if ($_POST['id']) {
145            foreach ($_POST['id'] as $id) {
146                $ids[] = arIndexTableGUI::domid_decode($id);
147            }
148        }
149
150        if (empty($ids)) {
151            ilUtil::sendFailure($this->txt("no_checkbox", false), true);
152            $this->ctrl->redirect($this, "index");
153        }
154
155        switch ($action_name) {
156            case "delete":
157                $this->deleteMultiple($ids);
158                break;
159            default:
160                $this->customMultiAction($action_name, $ids);
161                break;
162        }
163    }
164
165
166    /**
167     * @param string $action_name
168     * @param null   $ids
169     */
170    public function customMultiAction($action_name = "", $ids = null)
171    {
172    }
173
174
175    /**
176     * Configure screen
177     */
178    public function edit($id)
179    {
180        $edit_gui_class = $this->record_type . "EditGUI";
181        /**
182         * @var arEditGUI $edit_gui
183         */
184        $edit_gui = new $edit_gui_class($this, $this->ar->find($id));
185        $this->tpl->setContent($edit_gui->getHTML());
186    }
187
188
189    public function add()
190    {
191        $edit_gui_class = $this->record_type . "EditGUI";
192        /**
193         * @var arEditGUI $edit_gui
194         */
195        $edit_gui = new $edit_gui_class($this, $this->ar);
196        $this->tpl->setContent($edit_gui->getHTML());
197    }
198
199
200    public function create()
201    {
202        $edit_gui_class = $this->record_type . "EditGUI";
203        /**
204         * @var arEditGUI $edit_gui
205         */
206        $edit_gui = new $edit_gui_class($this, $this->ar);
207        $this->save($edit_gui);
208    }
209
210
211    /**
212     * @param $id
213     */
214    public function update($id)
215    {
216        $edit_gui_class = $this->record_type . "EditGUI";
217        /**
218         * @var arEditGUI $edit_gui
219         */
220        $edit_gui = new $edit_gui_class($this, $this->ar->find($id));
221        $this->save($edit_gui);
222    }
223
224
225    /**
226     * @param arEditGUI $edit_gui
227     */
228    public function save(arEditGUI $edit_gui)
229    {
230        if ($edit_gui->saveObject()) {
231            ilUtil::sendSuccess($this->getRecordCreatedMessage());
232            $this->ctrl->redirect($this, "index");
233        } else {
234            $this->tpl->setContent($edit_gui->getHTML());
235        }
236    }
237
238
239    /**
240     * @return string
241     */
242    public function getRecordCreatedMessage()
243    {
244        return $this->txt(('record_created'), true);
245    }
246
247
248    /**
249     * @param $id
250     */
251    public function view($id)
252    {
253        $display_gui_class = $this->record_type . "DisplayGUI";
254        /**
255         * @var arDisplayGUI $display_gui
256         */
257        $display_gui = new $display_gui_class($this, $this->ar->find($id));
258        $this->tpl->setContent($display_gui->getHtml());
259    }
260
261
262    /**
263     * @param $id
264     */
265    public function delete($id)
266    {
267        $this->deleteMultiple(array( $id ));
268    }
269
270
271    /**
272     * @param $ids []
273     */
274    public function deleteMultiple($ids = null)
275    {
276        $delete_gui_class = $this->record_type . "DeleteGUI";
277        /**
278         * @var arDeleteGUI $delete_gui
279         */
280        $delete_gui = new $delete_gui_class($this, "delete", new ActiveRecordList($this->ar), "delete", $ids);
281        if (count($ids) == 1) {
282            ilUtil::sendQuestion($this->getDeleteRecordConfirmationMessage());
283        } else {
284            ilUtil::sendQuestion($this->getDeleteRecordsConfirmationMessage());
285        }
286        $this->tpl->setContent($delete_gui->getHTML());
287    }
288
289
290    /**
291     * @return string
292     */
293    public function getDeleteRecordsConfirmationMessage()
294    {
295        return $this->txt(('delete_records_confirmation'), true);
296    }
297
298
299    /**
300     * @return string
301     */
302    public function getDeleteRecordConfirmationMessage()
303    {
304        return $this->txt(('delete_record_confirmation'), true);
305    }
306
307
308    public function deleteItems()
309    {
310        $nr_ids = $_POST['nr_ids'];
311        for ($i = 0; $i < $nr_ids; $i++) {
312            $id = $_POST['delete_id_' . $i];
313            $record = $this->ar->find($id);
314            $record->delete();
315        }
316        if ($i == 1) {
317            ilUtil::sendSuccess($this->getDeleteRecordMessage(), true);
318        } else {
319            ilUtil::sendSuccess($this->getDeleteRecordsMessage(), true);
320        }
321
322        $this->ctrl->redirect($this, "index");
323    }
324
325
326    /**
327     * @return string
328     */
329    public function getDeleteRecordsMessage()
330    {
331        return $this->txt(('records_deleted'), true);
332    }
333
334
335    /**
336     * @return string
337     */
338    public function getDeleteRecordMessage()
339    {
340        return $this->txt(('record_deleted'), true);
341    }
342
343
344    /**
345     * @param string $lng_prefix
346     */
347    public function setLngPrefix($lng_prefix)
348    {
349        $this->lng_prefix = $lng_prefix;
350    }
351
352
353    /**
354     * @return string
355     */
356    public function getLngPrefix()
357    {
358        return $this->lng_prefix;
359    }
360
361
362    /**
363     * @param      $txt
364     * @param bool $plugin_txt
365     *
366     * @return string
367     */
368    public function txt($txt, $plugin_txt = true)
369    {
370        if ($this->getLngPrefix() != "" && $plugin_txt) {
371            return $this->lng->txt($this->getLngPrefix() . "_" . $txt, $this->getLngPrefix());
372        } else {
373            return $this->lng->txt($txt);
374        }
375    }
376}
377