1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/Object/classes/class.ilObjectGUI.php';
5
6/**
7*
8* @author Stefan Meyer <meyer@leifos.com>
9* @version $Id$
10*
11*
12* @ilCtrl_Calls ilObjLoggingSettingsGUI: ilPermissionGUI
13*/
14class ilObjLoggingSettingsGUI extends ilObjectGUI
15{
16    const SECTION_SETTINGS = 'settings';
17    const SUB_SECTION_MAIN = 'log_general_settings';
18    const SUB_SECTION_COMPONENTS = 'log_components';
19    const SUB_SECTION_ERROR = 'log_error_settings';
20
21
22    public $tpl;
23    public $lng;
24    public $ctrl;
25    protected $tabs_gui;
26    protected $form;
27    protected $settings;
28
29
30    protected $log;
31
32
33
34    /**
35     * Constructor
36     *
37     * @access public
38     */
39    public function __construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output = true)
40    {
41        global $DIC;
42
43        $lng = $DIC['lng'];
44        $tpl = $DIC['tpl'];
45        $ilCtrl = $DIC['ilCtrl'];
46        $ilTabs = $DIC['ilTabs'];
47
48        $this->type = 'logs';
49        parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
50
51        $this->lng = $lng;
52
53        $this->tpl = $tpl;
54        $this->lng = $lng;
55        $this->ctrl = $ilCtrl;
56        $this->tabs_gui = $ilTabs;
57
58        $this->initSettings();
59        $this->initErrorSettings();
60        $this->lng->loadLanguageModule('logging');
61        $this->lng->loadLanguageModule('log');
62
63        include_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
64        $this->log = ilLoggerFactory::getLogger('log');
65    }
66
67    /**
68     *
69     * @return ilLogger
70     */
71    public function getLogger()
72    {
73        return $this->log;
74    }
75
76    /**
77     * Execute command
78     *
79     * @access public
80     *
81     */
82    public function executeCommand()
83    {
84        $next_class = $this->ctrl->getNextClass($this);
85        $cmd = $this->ctrl->getCmd();
86        $this->prepareOutput();
87
88        switch ($next_class) {
89            case 'ilpermissiongui':
90                $this->tabs_gui->setTabActive('perm_settings');
91                include_once("Services/AccessControl/classes/class.ilPermissionGUI.php");
92                $perm_gui = new ilPermissionGUI($this);
93                $ret = &$this->ctrl->forwardCommand($perm_gui);
94                break;
95
96            default:
97                if ($cmd == "" || $cmd == "view") {
98                    $cmd = "settings";
99                }
100                $this->$cmd();
101
102                break;
103        }
104        return true;
105    }
106
107
108    /**
109     * Get tabs
110     *
111     * @access public
112     *
113     */
114    public function getAdminTabs()
115    {
116        global $DIC;
117
118        $ilAccess = $DIC['ilAccess'];
119
120        if ($ilAccess->checkAccess("read", '', $this->object->getRefId())) {
121            $this->tabs_gui->addTarget(
122                static::SECTION_SETTINGS,
123                $this->ctrl->getLinkTargetByClass('ilobjloggingsettingsgui', "settings")
124            );
125        }
126        if ($ilAccess->checkAccess('edit_permission', '', $this->object->getRefId())) {
127            $this->tabs_gui->addTarget(
128                "perm_settings",
129                $this->ctrl->getLinkTargetByClass('ilpermissiongui', "perm")
130            );
131        }
132    }
133
134    public function setSubTabs($a_section)
135    {
136        $this->tabs_gui->addSubTab(
137            static::SUB_SECTION_MAIN,
138            $this->lng->txt(static::SUB_SECTION_MAIN),
139            $this->ctrl->getLinkTarget($this, 'settings')
140        );
141        $this->tabs_gui->addSubTab(
142            static::SUB_SECTION_ERROR,
143            $this->lng->txt(static::SUB_SECTION_ERROR),
144            $this->ctrl->getLinkTarget($this, 'errorSettings')
145        );
146        $this->tabs_gui->addSubTab(
147            static::SUB_SECTION_COMPONENTS,
148            $this->lng->txt(static::SUB_SECTION_COMPONENTS),
149            $this->ctrl->getLinkTarget($this, 'components')
150        );
151
152        $this->tabs_gui->activateSubTab($a_section);
153    }
154
155    protected function initSettings()
156    {
157        include_once("Services/Logging/classes/class.ilLoggingDBSettings.php");
158        $this->settings = ilLoggingDBSettings::getInstance();
159    }
160
161    /**
162     * Get log settings
163     * @return ilLogSettings
164     */
165    public function getSettings()
166    {
167        return $this->settings;
168    }
169
170    /**
171     * Show settings
172     * @access	public
173     */
174    public function settings(ilPropertyFormGUI $form = null)
175    {
176        global $DIC;
177
178        $ilAccess = $DIC['ilAccess'];
179        $ilErr = $DIC['ilErr'];
180
181        if (!$ilAccess->checkAccess('read', '', $this->object->getRefId())) {
182            $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
183        }
184
185        $this->tabs_gui->setTabActive(static::SECTION_SETTINGS);
186        $this->setSubTabs(static::SUB_SECTION_MAIN);
187
188        if (!$form instanceof ilPropertyFormGUI) {
189            $form = $this->initFormSettings();
190        }
191        $this->tpl->setContent($form->getHTML());
192
193        $this->getLogger()->debug('Currrent level is ' . $this->getSettings()->getLevel());
194
195        return true;
196    }
197
198    /**
199     * Save settings
200     * @access	public
201     */
202    public function updateSettings()
203    {
204        include_once 'Services/WebServices/RPC/classes/class.ilRPCServerSettings.php';
205
206        global $DIC;
207
208        $rbacsystem = $DIC['rbacsystem'];
209
210        if (!$rbacsystem->checkAccess('write', $this->object->getRefId())) {
211            $this->ilias->raiseError($this->lng->txt("permission_denied"), $this->ilias->error_obj->MESSAGE);
212        }
213
214
215        $form = $this->initFormSettings();
216        if ($form->checkInput()) {
217            $this->getSettings()->setLevel($form->getInput('level'));
218            $this->getSettings()->enableCaching($form->getInput('cache'));
219            $this->getSettings()->setCacheLevel($form->getInput('cache_level'));
220            $this->getSettings()->enableMemoryUsage($form->getInput('memory'));
221            $this->getSettings()->enableBrowserLog($form->getInput('browser'));
222            $this->getSettings()->setBrowserUsers($form->getInput('browser_users'));
223
224            $this->getLogger()->info(print_r($form->getInput('browser_users'), true));
225
226            $this->getSettings()->update();
227
228            ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
229            $this->ctrl->redirect($this, 'settings');
230            return true;
231        }
232
233        ilUtil::sendFailure($this->lng->txt('err_check_input'));
234        $form->setValuesByPost();
235        $this->settings($form);
236
237        return true;
238    }
239
240    /**
241     * Init settings form
242     *
243     */
244    protected function initFormSettings()
245    {
246        global $DIC;
247
248        $lng = $DIC['lng'];
249        $ilDB = $DIC['ilDB'];
250        $ilAccess = $DIC['ilAccess'];
251
252        include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
253        include_once './Services/Search/classes/class.ilSearchSettings.php';
254
255        $form = new ilPropertyFormGUI();
256        $form->setTitle($this->lng->txt('logs_settings'));
257        $form->setFormAction($this->ctrl->getFormAction($this));
258
259        if ($ilAccess->checkAccess('write', '', $this->object->getRefId())) {
260            $form->addCommandButton('updateSettings', $this->lng->txt('save'));
261        }
262
263        $level = new ilSelectInputGUI($this->lng->txt('log_log_level'), 'level');
264        $level->setOptions(ilLogLevel::getLevelOptions());
265        $level->setValue($this->getSettings()->getLevel());
266        $form->addItem($level);
267
268        $cache = new ilCheckboxInputGUI($this->lng->txt('log_cache_'), 'cache');
269        $cache->setInfo($this->lng->txt('log_cache_info'));
270        $cache->setValue(1);
271        $cache->setChecked($this->getSettings()->isCacheEnabled());
272        $form->addItem($cache);
273
274        $cache_level = new ilSelectInputGUI($this->lng->txt('log_cache_level'), 'cache_level');
275        $cache_level->setOptions(ilLogLevel::getLevelOptions());
276        $cache_level->setValue($this->getSettings()->getCacheLevel());
277        $cache->addSubItem($cache_level);
278
279        $memory = new ilCheckboxInputGUI($this->lng->txt('log_memory'), 'memory');
280        $memory->setValue(1);
281        $memory->setChecked($this->getSettings()->isMemoryUsageEnabled());
282        $form->addItem($memory);
283
284        // Browser handler
285        $browser = new ilCheckboxInputGUI($this->lng->txt('log_browser'), 'browser');
286        $browser->setValue(1);
287        $browser->setChecked($this->getSettings()->isBrowserLogEnabled());
288        $form->addItem($browser);
289
290        // users
291        $users = new ilTextInputGUI($this->lng->txt('log_browser_users'), 'browser_users');
292        $users->setValue(current($this->getSettings()->getBrowserLogUsers()));
293        $users->setMulti(true);
294        $users->setMultiValues($this->getSettings()->getBrowserLogUsers());
295
296        $this->getLogger()->debug(print_r($this->getSettings()->getBrowserLogUsers(), true));
297
298        $browser->addSubItem($users);
299
300
301        return $form;
302    }
303
304
305    /**
306     * Show components
307     */
308    protected function components()
309    {
310        $this->tabs_gui->activateTab(static::SECTION_SETTINGS);
311        $this->setSubTabs(static::SUB_SECTION_COMPONENTS);
312
313        include_once './Services/Logging/classes/class.ilLogComponentTableGUI.php';
314        $table = new ilLogComponentTableGUI($this, 'components');
315        $table->setEditable($this->checkPermissionBool('write'));
316        $table->init();
317        $table->parse();
318
319        $GLOBALS['DIC']['tpl']->setContent($table->getHTML());
320    }
321
322    /**
323     * Save form
324     */
325    protected function saveComponentLevels()
326    {
327        $this->checkPermission('write');
328
329        foreach ($_POST['level'] as $component_id => $value) {
330            ilLoggerFactory::getLogger('log')->debug($component_id);
331            ilLoggerFactory::getLogger('log')->debug($value);
332            include_once './Services/Logging/classes/class.ilLogComponentLevel.php';
333            $level = new ilLogComponentLevel($component_id, $value);
334            $level->update();
335        }
336
337        ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
338        $this->ctrl->redirect($this, 'components');
339    }
340
341    protected function resetComponentLevels()
342    {
343        $this->checkPermission('write');
344
345        foreach (ilLogComponentLevels::getInstance()->getLogComponents() as $component) {
346            $component->setLevel(null);
347            $component->update();
348        }
349        ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
350        $this->ctrl->redirect($this, 'components');
351    }
352
353    protected function errorSettings()
354    {
355        global $DIC;
356
357        $ilAccess = $DIC['ilAccess'];
358        $ilErr = $DIC['ilErr'];
359
360        if (!$ilAccess->checkAccess('read', '', $this->object->getRefId())) {
361            $ilErr->raiseError($this->lng->txt('permission_denied'), $ilErr->MESSAGE);
362        }
363
364        $this->tabs_gui->setTabActive(static::SECTION_SETTINGS);
365        $this->setSubTabs(static::SUB_SECTION_ERROR);
366
367        if (!$form instanceof ilPropertyFormGUI) {
368            $form = $this->initFormErrorSettings();
369        }
370        $this->tpl->setContent($form->getHTML());
371
372        $this->getLogger()->debug('Currrent level is ' . $this->getSettings()->getLevel());
373    }
374
375    protected function updateErrorSettings()
376    {
377        global $DIC;
378
379        $rbacsystem = $DIC['rbacsystem'];
380
381        if (!$rbacsystem->checkAccess('write', $this->object->getRefId())) {
382            $this->ilias->raiseError($this->lng->txt("permission_denied"), $this->ilias->error_obj->MESSAGE);
383        }
384
385        $form = $this->initFormErrorSettings();
386        if ($form->checkInput()) {
387            $this->getErrorSettings()->setMail($form->getInput('error_mail'));
388            $this->getErrorSettings()->update();
389
390            ilUtil::sendSuccess($this->lng->txt('error_settings_saved'), true);
391            $this->ctrl->redirect($this, 'errorSettings');
392        }
393
394        ilUtil::sendFailure($this->lng->txt('err_check_input'));
395        $form->setValuesByPost();
396        $this->errorSettings($form);
397    }
398
399    protected function initFormErrorSettings()
400    {
401        global $DIC;
402
403        $lng = $DIC['lng'];
404        $ilDB = $DIC['ilDB'];
405        $ilAccess = $DIC['ilAccess'];
406
407        require_once './Services/Form/classes/class.ilPropertyFormGUI.php';
408        require_once './Services/Search/classes/class.ilSearchSettings.php';
409
410        $form = new ilPropertyFormGUI();
411        $form->setTitle($this->lng->txt('logs_settings'));
412        $form->setFormAction($this->ctrl->getFormAction($this));
413
414        if ($ilAccess->checkAccess('write', '', $this->object->getRefId())) {
415            $form->addCommandButton('updateErrorSettings', $this->lng->txt('save'));
416        }
417
418        $folder = new ilNonEditableValueGUI($this->lng->txt('log_error_folder'), 'error_folder');
419        $folder->setValue($this->getErrorSettings()->folder());
420        $form->addItem($folder);
421
422        $mail = new ilTextInputGUI($this->lng->txt('log_error_mail'), 'error_mail');
423        $mail->setValue($this->getErrorSettings()->mail());
424        $form->addItem($mail);
425
426        return $form;
427    }
428
429    protected function initErrorSettings()
430    {
431        require_once("Services/Logging/classes/error/class.ilLoggingErrorSettings.php");
432        $this->error_settings = ilLoggingErrorSettings::getInstance();
433    }
434
435    protected function getErrorSettings()
436    {
437        return $this->error_settings;
438    }
439}
440