1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* GUI class for account codes
6*
7* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
8* @version $Id$
9*
10* @ilCtrl_Calls ilAccountCodesGUI:
11* @ingroup ServicesUser
12*/
13class ilAccountCodesGUI
14{
15    protected $ref_id; // [int]
16
17    /**
18     * Constructor
19     *
20     * @param int $a_ref_id
21     */
22    public function __construct($a_ref_id)
23    {
24        global $DIC;
25
26        $lng = $DIC['lng'];
27
28        $this->ref_id = $a_ref_id;
29        $lng->loadLanguageModule("user");
30    }
31
32    public function executeCommand()
33    {
34        global $DIC;
35
36        $ilCtrl = $DIC['ilCtrl'];
37
38        $next_class = $ilCtrl->getNextClass($this);
39        $cmd = $ilCtrl->getCmd();
40
41        switch ($next_class) {
42            default:
43                if (!$cmd) {
44                    $cmd = "listCodes";
45                }
46                $this->$cmd();
47                break;
48        }
49
50        return true;
51    }
52
53    public function listCodes()
54    {
55        global $DIC;
56
57        $ilAccess = $DIC['ilAccess'];
58        $ilErr = $DIC['ilErr'];
59        $ilCtrl = $DIC['ilCtrl'];
60        $ilToolbar = $DIC['ilToolbar'];
61        $lng = $DIC['lng'];
62        $tpl = $DIC['tpl'];
63
64        if (!$ilAccess->checkAccess('read', '', $this->ref_id)) {
65            $ilErr->raiseError($lng->txt("msg_no_perm_read"), $ilErr->MESSAGE);
66        }
67
68        include_once "Services/UIComponent/Button/classes/class.ilLinkButton.php";
69        $button = ilLinkButton::getInstance();
70        $button->setCaption("user_account_codes_add");
71        $button->setUrl($ilCtrl->getLinkTarget($this, "addCodes"));
72        $ilToolbar->addButtonInstance($button);
73
74        include_once("./Services/User/classes/class.ilAccountCodesTableGUI.php");
75        $ctab = new ilAccountCodesTableGUI($this, "listCodes");
76        $tpl->setContent($ctab->getHTML());
77    }
78
79    public function initAddCodesForm()
80    {
81        global $DIC;
82
83        $ilCtrl = $DIC['ilCtrl'];
84        $lng = $DIC['lng'];
85
86        include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
87
88        $this->form_gui = new ilPropertyFormGUI();
89        $this->form_gui->setFormAction($ilCtrl->getFormAction($this, 'createCodes'));
90        $this->form_gui->setTitle($lng->txt('user_account_codes_edit_header'));
91
92        $count = new ilNumberInputGUI($lng->txt('user_account_codes_number'), 'acc_codes_number');
93        $count->setSize(4);
94        $count->setMaxLength(4);
95        $count->setMinValue(1);
96        $count->setMaxValue(1000);
97        $count->setRequired(true);
98        $this->form_gui->addItem($count);
99
100        $valid = new ilRadioGroupInputGUI($lng->txt('user_account_code_valid_until'), 'valid_type');
101        $valid->setRequired(true);
102
103        $unl = new ilRadioOption($lng->txt('user_account_code_valid_until_unlimited'), 'valid_unlimited');
104        $valid->addOption($unl);
105
106        $st = new ilRadioOption($lng->txt('user_account_code_valid_until_static'), 'valid_static');
107        $valid->addOption($st);
108
109        $dt = new ilDateTimeInputGUI($lng->txt('date'), 'valid_date');
110        $dt->setRequired(true);
111        $st->addSubItem($dt);
112
113        $dyn = new ilRadioOption($lng->txt('user_account_code_valid_until_dynamic'), 'valid_dynamic');
114        $valid->addOption($dyn);
115
116        $ds = new ilNumberInputGUI($lng->txt('days'), 'valid_days');
117        $ds->setSize(5);
118        $ds->setRequired(true);
119        $dyn->addSubItem($ds);
120
121        $this->form_gui->addItem($valid);
122
123        $this->form_gui->addCommandButton('createCodes', $lng->txt('create'));
124        $this->form_gui->addCommandButton('listCodes', $lng->txt('cancel'));
125    }
126
127    public function addCodes()
128    {
129        global $DIC;
130
131        $ilAccess = $DIC['ilAccess'];
132        $ilErr = $DIC['ilErr'];
133        $tpl = $DIC['tpl'];
134        $lng = $DIC['lng'];
135
136        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
137            $ilErr->raiseError($lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
138        }
139
140        $this->initAddCodesForm();
141        $tpl->setContent($this->form_gui->getHTML());
142    }
143
144    public function createCodes()
145    {
146        global $DIC;
147
148        $ilAccess = $DIC['ilAccess'];
149        $ilErr = $DIC['ilErr'];
150        $lng = $DIC['lng'];
151        $tpl = $DIC['tpl'];
152        $ilCtrl = $DIC['ilCtrl'];
153
154        if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
155            $ilErr->raiseError($lng->txt("msg_no_perm_write"), $ilErr->MESSAGE);
156        }
157
158        $this->initAddCodesForm();
159        if ($this->form_gui->checkInput()) {
160            $number = $this->form_gui->getInput('acc_codes_number');
161            switch ($this->form_gui->getInput('valid_type')) {
162                case 'valid_unlimited':
163                    $valid = 0;
164                    break;
165
166                case 'valid_static':
167                    $valid = $this->form_gui->getItemByPostVar('valid_date')->getDate()->get(IL_CAL_DATE);
168                    break;
169
170                case 'valid_dynamic':
171                    $valid = $this->form_gui->getInput('valid_days');
172                    break;
173            }
174
175            include_once './Services/User/classes/class.ilAccountCode.php';
176
177            $stamp = time();
178            for ($loop = 1; $loop <= $number; $loop++) {
179                ilAccountCode::create($valid, $stamp);
180            }
181
182            ilUtil::sendSuccess($lng->txt('saved_successfully'), true);
183            $ilCtrl->redirect($this, "listCodes");
184        } else {
185            $this->form_gui->setValuesByPost();
186            $tpl->setContent($this->form_gui->getHtml());
187        }
188    }
189
190    public function deleteCodes()
191    {
192        global $DIC;
193
194        $lng = $DIC['lng'];
195        $ilCtrl = $DIC['ilCtrl'];
196
197        include_once './Services/User/classes/class.ilAccountCode.php';
198        ilAccountCode::deleteCodes($_POST["id"]);
199
200        ilUtil::sendSuccess($lng->txt('info_deleted'), true);
201        $ilCtrl->redirect($this, "listCodes");
202    }
203
204    public function deleteConfirmation()
205    {
206        global $DIC;
207
208        $ilErr = $DIC['ilErr'];
209        $lng = $DIC['lng'];
210        $ilCtrl = $DIC['ilCtrl'];
211        $tpl = $DIC['tpl'];
212
213        if (!isset($_POST["id"])) {
214            $ilErr->raiseError($lng->txt("no_checkbox"), $ilErr->MESSAGE);
215        }
216
217        include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
218        $gui = new ilConfirmationGUI();
219        $gui->setHeaderText($lng->txt("info_delete_sure"));
220        $gui->setCancel($lng->txt("cancel"), "listCodes");
221        $gui->setConfirm($lng->txt("confirm"), "deleteCodes");
222        $gui->setFormAction($ilCtrl->getFormAction($this, "deleteCodes"));
223
224        include_once './Services/User/classes/class.ilAccountCode.php';
225        $data = ilAccountCode::loadCodesByIds($_POST["id"]);
226        foreach ($data as $code) {
227            $gui->addItem("id[]", $code["code_id"], $code["code"]);
228        }
229
230        $tpl->setContent($gui->getHTML());
231    }
232
233    public function resetCodesFilter()
234    {
235        include_once("./Services/User/classes/class.ilAccountCodesTableGUI.php");
236        $utab = new ilAccountCodesTableGUI($this, "listCodes");
237        $utab->resetOffset();
238        $utab->resetFilter();
239
240        $this->listCodes();
241    }
242
243    public function applyCodesFilter()
244    {
245        include_once("./Services/User/classes/class.ilAccountCodesTableGUI.php");
246        $utab = new ilAccountCodesTableGUI($this, "listCodes");
247        $utab->resetOffset();
248        $utab->writeFilterToSession();
249
250        $this->listCodes();
251    }
252
253    public function exportCodes()
254    {
255        global $DIC;
256
257        $ilAccess = $DIC['ilAccess'];
258        $ilErr = $DIC['ilErr'];
259        $lng = $DIC['lng'];
260
261        if (!$ilAccess->checkAccess('read', '', $this->ref_id)) {
262            $ilErr->raiseError($lng->txt("msg_no_perm_read"), $ilErr->MESSAGE);
263        }
264
265        include_once("./Services/User/classes/class.ilAccountCodesTableGUI.php");
266        $utab = new ilAccountCodesTableGUI($this, "listCodes");
267
268        include_once './Services/User/classes/class.ilAccountCode.php';
269        $codes = ilAccountCode::getCodesForExport($utab->filter["code"], $utab->filter["valid_until"], $utab->filter["generated"]);
270
271        if (sizeof($codes)) {
272            // #13497
273            ilUtil::deliverData(implode("\r\n", $codes), "ilias_account_codes_" . date("d-m-Y") . ".txt", "text/plain");
274        } else {
275            ilUtil::sendFailure($lng->txt("account_export_codes_no_data"));
276            $this->listCodes();
277        }
278    }
279}
280