1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5require_once 'Modules/Chatroom/classes/class.ilChatroomFormFactory.php';
6require_once 'Modules/Chatroom/classes/class.ilChatroomAdmin.php';
7require_once 'Modules/Chatroom/classes/class.ilChatroomConfigFileHandler.php';
8
9/**
10 * Class ilChatroomAdminViewGUI
11 * @author  Jan Posselt <jposselt@databay.de>
12 * @author  Thomas Joußen <tjoussen@databay.de>
13 * @version $Id$
14 * @ingroup ModulesChatroom
15 */
16class ilChatroomAdminViewGUI extends ilChatroomGUIHandler
17{
18    const CHATROOM_README_PATH = '/Modules/Chatroom/README.md';
19
20    /**
21     * @var ilSetting
22     */
23    protected $commonSettings;
24
25    /**
26     * @var ilTemplate
27     */
28    protected $ilTpl;
29
30    /**
31     * Constructor
32     * @param ilChatroomObjectGUI $gui
33     */
34    public function __construct(ilChatroomObjectGUI $gui)
35    {
36        global $DIC;
37
38        parent::__construct($gui);
39        $this->commonSettings = new ilSetting('common');
40        $this->ilTpl = $DIC->ui()->mainTemplate();
41    }
42
43    /**
44     * {@inheritdoc}
45     */
46    public function executeDefault($method)
47    {
48        $this->ilCtrl->redirect($this->gui, 'view-clientsettings');
49    }
50
51    /**
52     * Saves settings fetched from $_POST
53     */
54    public function saveSettings()
55    {
56        $this->redirectIfNoPermission('write');
57
58        $factory = new ilChatroomFormFactory();
59        $form = $factory->getGeneralSettingsForm();
60
61        if (!$form->checkInput()) {
62            $form->setValuesByPost();
63            $this->serversettings($form);
64            return;
65        }
66
67        $settings = array(
68            'protocol' => $form->getInput('protocol'),
69            'port' => $form->getInput('port'),
70            'address' => $form->getInput('address'),
71            'cert' => $form->getInput('cert'),
72            'key' => $form->getInput('key'),
73            'dhparam' => $form->getInput('dhparam'),
74            'log' => $form->getInput('log'),
75            'error_log' => $form->getInput('error_log'),
76            'log_level' => $form->getInput('log_level'),
77            'ilias_proxy' => $form->getInput('ilias_proxy'),
78            'ilias_url' => $form->getInput('ilias_url'),
79            'client_proxy' => $form->getInput('client_proxy'),
80            'client_url' => $form->getInput('client_url'),
81            'sub_directory' => $form->getInput('sub_directory'),
82            'deletion_mode' => $form->getInput('deletion_mode'),
83            'deletion_unit' => $form->getInput('deletion_unit'),
84            'deletion_value' => $form->getInput('deletion_value'),
85            'deletion_time' => $form->getInput('deletion_time'),
86        );
87
88        $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
89        $adminSettings->saveGeneralSettings((object) $settings);
90
91        $fileHandler = new ilChatroomConfigFileHandler();
92        $fileHandler->createServerConfigFile($settings);
93
94        ilUtil::sendSuccess($this->ilLng->txt('settings_has_been_saved'), true);
95        $this->ilCtrl->redirect($this->gui, 'view-serversettings');
96    }
97
98    /**
99     * Prepares view form and displays it.
100     * @param ilPropertyFormGUI $form
101     */
102    public function serversettings(ilPropertyFormGUI $form = null)
103    {
104        $this->redirectIfNoPermission('read');
105
106        $this->defaultActions();
107        $this->gui->switchToVisibleMode();
108
109        $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
110        $serverSettings = $adminSettings->loadGeneralSettings();
111
112        if ($form === null) {
113            $factory = new ilChatroomFormFactory();
114            $form = $factory->getGeneralSettingsForm();
115            $form->setValuesByArray($serverSettings);
116        }
117
118        $this->checkServerConnection($serverSettings);
119
120        $form->setTitle($this->ilLng->txt('chatserver_settings_title'));
121        if (ilChatroom::checkUserPermissions('write', $this->gui->ref_id, false)) {
122            $form->addCommandButton('view-saveSettings', $this->ilLng->txt('save'));
123        }
124        $form->setFormAction($this->ilCtrl->getFormAction($this->gui, 'view-saveSettings'));
125
126        $settingsTpl = $this->createSettingTemplate($form);
127        $this->ilTpl->setVariable('ADM_CONTENT', $settingsTpl->get());
128    }
129
130    /**
131     *
132     */
133    private function defaultActions()
134    {
135        $chatSettings = new ilSetting('chatroom');
136        if ($chatSettings->get('chat_enabled', false)) {
137            $this->forcePublicRoom();
138        }
139    }
140
141    /**
142     *
143     */
144    public function forcePublicRoom()
145    {
146        $ref_id = ilObjChatroom::_getPublicRefId();
147        if (!$ref_id) {
148            $this->createPublicRoom();
149            return;
150        }
151
152        $instance = ilObjectFactory::getInstanceByRefId($ref_id, false);
153        if (!$instance) {
154            $this->createPublicRoom();
155            return;
156        }
157
158        $obj_id = ilObject::_lookupObjId($ref_id);
159        if (!$obj_id) {
160            $this->createPublicRoom();
161            return;
162        }
163
164        if (!ilObject::_hasUntrashedReference($obj_id)) {
165            $this->createPublicRoom();
166            return;
167        }
168
169        require_once 'Modules/Chatroom/classes/class.ilChatroomInstaller.php';
170        ilChatroomInstaller::ensureCorrectPublicChatroomTreeLocation($ref_id);
171    }
172
173    /**
174     * Creates a public chatroom.
175     */
176    public function createPublicRoom()
177    {
178        require_once 'Modules/Chatroom/classes/class.ilChatroomInstaller.php';
179        ilChatroomInstaller::createDefaultPublicRoom(true);
180        ilUtil::sendSuccess($this->ilLng->txt('public_chat_created'), true);
181    }
182
183    /**
184     * Checks for server connection. If no connection if possible, show info in gui
185     * @param array $serverSettings
186     */
187    protected function checkServerConnection(array $serverSettings)
188    {
189        require_once 'Modules/Chatroom/classes/class.ilChatroomServerConnector.php';
190
191        if (
192            $serverSettings['port'] &&
193            $serverSettings['address'] &&
194            !(boolean) @ilChatroomServerConnector::checkServerConnection(false)
195        ) {
196            ilUtil::sendInfo($this->ilLng->txt('chat_cannot_connect_to_server'));
197        }
198    }
199
200    /**
201     * @param ilPropertyFormGUI $form
202     * @return ilTemplate
203     */
204    protected function createSettingTemplate(ilPropertyFormGUI $form)
205    {
206        $furtherInformation = sprintf($this->ilLng->txt('server_further_information'), $this->getReadmePath());
207        $serverTpl = new ilTemplate('tpl.chatroom_serversettings.html', true, true, 'Modules/Chatroom');
208        $serverTpl->setVariable('VAL_SERVERSETTINGS_FORM', $form->getHTML());
209        $serverTpl->setVariable('LBL_SERVERSETTINGS_FURTHER_INFORMATION', $furtherInformation);
210
211        return $serverTpl;
212    }
213
214    /**
215     * Get the path to the README.txt file
216     * @return string
217     */
218    protected function getReadmePath()
219    {
220        return ilUtil::_getHttpPath() . self::CHATROOM_README_PATH;
221    }
222
223    /**
224     * Saves client settings fetched from $_POST
225     */
226    public function saveClientSettings()
227    {
228        $this->redirectIfNoPermission('write');
229
230        $factory = new ilChatroomFormFactory();
231        $form = $factory->getClientSettingsForm();
232
233        if (!$form->checkInput()) {
234            $form->setValuesByPost();
235            $this->clientsettings($form);
236            return;
237        }
238
239        $convIdleStateTime = max(1, (int) (int) $form->getInput('conversation_idle_state_in_minutes'));
240
241        $settings = array(
242            'name' => (string) $form->getInput('client_name'),
243            'enable_osd' => (boolean) $form->getInput('enable_osd'),
244            'enable_osc' => (boolean) $form->getInput('enable_osc'),
245            'enable_browser_notifications' => (boolean) $form->getInput('enable_browser_notifications'),
246            'conversation_idle_state_in_minutes' => $convIdleStateTime,
247            'osd_intervall' => (int) $form->getInput('osd_intervall'),
248            'chat_enabled' => ((boolean) $form->getInput('chat_enabled')),
249            'enable_smilies' => (boolean) $form->getInput('enable_smilies'),
250            'play_invitation_sound' => (boolean) $form->getInput('play_invitation_sound'),
251            'auth' => $form->getInput('auth')
252        );
253
254        if (!$settings['chat_enabled']) {
255            $settings['enable_osc'] = false;
256        }
257
258        $notificationSettings = new ilSetting('notifications');
259        $notificationSettings->set('osd_polling_intervall', (int) $form->getInput('osd_intervall'));
260        $notificationSettings->set('enable_osd', (boolean) $form->getInput('enable_osd'));
261
262        $chatSettings = new ilSetting('chatroom');
263        $chatSettings->set('chat_enabled', $settings['chat_enabled']);
264        $chatSettings->set('enable_browser_notifications', $settings['enable_browser_notifications']);
265        $chatSettings->set('conversation_idle_state_in_minutes', $convIdleStateTime);
266        $chatSettings->set('enable_osc', $settings['enable_osc']);
267        $chatSettings->set('play_invitation_sound', (boolean) $form->getInput('play_invitation_sound'));
268
269        $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
270        $adminSettings->saveClientSettings((object) $settings);
271
272        $fileHandler = new ilChatroomConfigFileHandler();
273        $fileHandler->createClientConfigFile($settings);
274
275        ilUtil::sendSuccess($this->ilLng->txt('settings_has_been_saved'), true);
276        $this->ilCtrl->redirect($this->gui, 'view-clientsettings');
277    }
278
279    /**
280     * @param ilPropertyFormGUI $form
281     */
282    public function clientsettings(ilPropertyFormGUI $form = null)
283    {
284        $this->redirectIfNoPermission(array('visible','read'));
285
286        $this->defaultActions();
287        $this->gui->switchToVisibleMode();
288
289        $adminSettings = new ilChatroomAdmin($this->gui->object->getId());
290        $serverSettings = $adminSettings->loadGeneralSettings();
291
292        if ($form === null) {
293            $clientSettings = $adminSettings->loadClientSettings();
294            $factory = new ilChatroomFormFactory();
295            $form = $factory->getClientSettingsForm();
296            $form->setValuesByArray($clientSettings);
297        }
298
299        $this->checkServerConnection($serverSettings);
300
301        $form->setTitle($this->ilLng->txt('general_settings_title'));
302        if (ilChatroom::checkUserPermissions('write', $this->gui->ref_id, false)) {
303            $form->addCommandButton('view-saveClientSettings', $this->ilLng->txt('save'));
304        }
305        $form->setFormAction($this->ilCtrl->getFormAction($this->gui, 'view-saveClientSettings'));
306
307        $settingsTpl = $this->createSettingTemplate($form);
308        $this->ilTpl->setVariable('ADM_CONTENT', $settingsTpl->get());
309    }
310}
311