ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectCustomUserFieldsGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
24 include_once('Services/Membership/classes/class.ilMemberAgreement.php');
25 
36 {
37  const MODE_CREATE = 1;
38  const MODE_UPDATE = 2;
39 
40  private $form = null;
41 
42  private $lng;
43  private $tpl;
44  private $ctrl;
45  private $tabs_gui;
46 
47  private $obj_id;
48  private $ref_id;
49 
50  private $cdf;
51 
59  public function __construct($a_obj_id)
60  {
61  global $lng,$tpl,$ilCtrl,$ilTabs;
62 
63  $this->lng = $lng;
64  $this->lng->loadLanguageModule('ps');
65  $this->lng->loadLanguageModule(ilObject::_lookupType($a_obj_id));
66 
67  $this->tpl = $tpl;
68  $this->ctrl = $ilCtrl;
69  $this->tabs_gui = $ilTabs;
70 
71  $this->obj_id = $a_obj_id;
72 
73  // Currently only supported for container objects
74  $refs = ilObject::_getAllReferences($this->obj_id);
75  $this->ref_id = end($refs);
76  }
77 
84  public function executeCommand()
85  {
86  global $ilErr, $ilAccess, $lng;
87 
88  if(!$ilAccess->checkAccess('write','',$this->ref_id))
89  {
90  $ilErr->raiseError($lng->txt('permission_denied'),$ilErr->WARNING);
91  }
92 
93  $cmd = $this->ctrl->getCmd();
94 
95  switch($next_class = $this->ctrl->getNextClass($this))
96  {
97  default:
98  if(!$cmd)
99  {
100  $cmd = 'show';
101  }
102  $this->$cmd();
103  break;
104  }
105  }
106 
111  public function getObjId()
112  {
113  return $this->obj_id;
114  }
115 
120  protected function show()
121  {
123  {
124  ilUtil::sendInfo($this->lng->txt('ps_cdf_warning_modify'));
125  }
126  $this->listFields();
127  }
128 
133  protected function listFields()
134  {
135  global $ilToolbar;
136 
137  $ilToolbar->addButton(
138  $this->lng->txt('ps_cdf_add_field'),
139  $this->ctrl->getLinkTarget($this,'addField')
140  );
141 
142  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldsTableGUI.php';
143  $table = new ilObjectCustomUserFieldsTableGUI($this,'listFields');
144  $table->parse(ilCourseDefinedFieldDefinition::_getFields($this->getObjId()));
145  $this->tpl->setContent($table->getHTML());
146  }
147 
152  protected function saveFields()
153  {
155  foreach($fields as $field_obj)
156  {
157  $field_obj->enableRequired((bool) isset($_POST['required'][$field_obj->getId()]));
158  $field_obj->update();
159  }
160 
162  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
163  $this->listFields();
164  return true;
165  }
166 
171  protected function confirmDeleteFields()
172  {
173  if(!count($_POST['field_ids']))
174  {
175  ilUtil::sendFailure($this->lng->txt('ps_cdf_select_one'));
176  $this->listFields();
177  return false;
178  }
179  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
180  $confirm = new ilConfirmationGUI();
181  $confirm->setFormAction($this->ctrl->getFormAction($this));
182  $confirm->setHeaderText($this->lng->txt('ps_cdf_delete_sure'));
183 
184  foreach($_POST['field_ids'] as $field_id)
185  {
186  $tmp_field = new ilCourseDefinedFieldDefinition($this->getObjId(),$field_id);
187 
188  $confirm->addItem('field_ids[]', $field_id, $tmp_field->getName());
189  }
190 
191  $confirm->setConfirm($this->lng->txt('delete'), 'deleteFields');
192  $confirm->setCancel($this->lng->txt('cancel'), 'listFields');
193  $this->tpl->setContent($confirm->getHTML());
194  }
195 
200  protected function deleteFields()
201  {
202  foreach((array) $_POST['field_ids'] as $field_id)
203  {
204  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id,$field_id);
205  $tmp_field->delete();
206  }
207 
208  ilMemberAgreement::_deleteByObjId($this->obj_id);
209 
210  ilUtil::sendSuccess($this->lng->txt('ps_cdf_deleted'));
211  $this->listFields();
212  return true;
213  }
214 
219  protected function addField()
220  {
221  $this->initFieldForm(self::MODE_CREATE);
222 
223  $this->form->getItemByPostVar('va')->setValues(array(''));
224 
225  $this->tpl->setContent($this->form->getHTML());
226  }
227 
232  protected function saveField()
233  {
234  $this->initFieldForm(self::MODE_CREATE);
235  if($this->form->checkInput())
236  {
237  $udf = new ilCourseDefinedFieldDefinition($this->getObjId());
238  $udf->setName($this->form->getInput('na'));
239  $udf->setType($this->form->getInput('ty'));
240  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
241  $udf->enableRequired($this->form->getInput('re'));
242  $udf->save();
243 
244  ilUtil::sendSuccess($this->lng->txt('ps_cdf_added_field'));
245  $this->listFields();
246  return true;
247  }
248  // not valid
249  ilUtil::sendFailure($this->lng->txt('err_check_input'));
250  $this->form->setValuesByPost();
251  $this->tpl->setContent($this->form->getHTML());
252  return false;
253  }
254 
260  protected function editField()
261  {
262  if(!$_REQUEST['field_id'])
263  {
264  $this->listFields();
265  return false;
266  }
267 
268  $this->initFieldForm(self::MODE_UPDATE);
269 
270  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(),(int) $_REQUEST['field_id']);
271  $this->form->getItemByPostVar('na')->setValue($udf->getName());
272  $this->form->getItemByPostVar('ty')->setValue($udf->getType());
273  $this->form->getItemByPostVar('re')->setChecked($udf->isRequired());
274  $this->form->getItemByPostVar('va')->setValues($udf->getValues());
275 
276  $this->tpl->setContent($this->form->getHTML());
277  }
278 
283  protected function updateField()
284  {
285  $this->initFieldForm(self::MODE_UPDATE);
286 
287  if($this->form->checkInput())
288  {
289  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(),(int) $_REQUEST['field_id']);
290  $udf->setName($this->form->getInput('na'));
291  $udf->setType($this->form->getInput('ty'));
292  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
293  $udf->enableRequired($this->form->getInput('re'));
294  $udf->update();
295 
296  // Finally reset member agreements
298  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
299  $this->listFields();
300  return true;
301  }
302 
303  ilUtil::sendFailure($this->lng->txt('err_check_input'));
304  $this->form->setValuesByPost();
305  $this->tpl->setContent($this->form->getHTML());
306  return false;
307  }
308 
313  protected function initFieldForm($a_mode)
314  {
315  if($this->form instanceof ilPropertyFormGUI)
316  {
317  return true;
318  }
319  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
320  $this->form = new ilPropertyFormGUI();
321 
322  switch($a_mode)
323  {
324  case self::MODE_CREATE:
325  $this->form->setFormAction($this->ctrl->getFormAction($this));
326  $this->form->setTitle($this->lng->txt('ps_cdf_add_field'));
327  $this->form->addCommandButton('saveField', $this->lng->txt('save'));
328  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
329  break;
330 
331  case self::MODE_UPDATE:
332  $this->ctrl->setParameter($this,'field_id',(int) $_REQUEST['field_id']);
333  $this->form->setFormAction($this->ctrl->getFormAction($this));
334  $this->form->setTitle($this->lng->txt('ps_cdf_edit_field'));
335  $this->form->addCommandButton('updateField', $this->lng->txt('save'));
336  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
337  break;
338  }
339 
340  // Name
341  $na = new ilTextInputGUI($this->lng->txt('ps_cdf_name'),'na');
342  $na->setSize(32);
343  $na->setMaxLength(255);
344  $na->setRequired(true);
345  $this->form->addItem($na);
346 
347  // Type
348  $ty = new ilRadioGroupInputGUI($this->lng->txt('ps_field_type'),'ty');
349  $ty->setRequired(true);
350  $this->form->addItem($ty);
351 
352  if($a_mode == self::MODE_UPDATE)
353  {
354  $ty->setDisabled(true); // #14888
355  }
356 
357  // Text type
358  $ty_te = new ilRadioOption($this->lng->txt('ps_type_txt_long'),IL_CDF_TYPE_TEXT);
359  $ty->addOption($ty_te);
360 
361  // Select Type
362  $ty_se = new ilRadioOption($this->lng->txt('ps_type_select_long'),IL_CDF_TYPE_SELECT);
363  $ty->addOption($ty_se);
364 
365  // Select Type Values
366  $ty_se_mu = new ilTextWizardInputGUI($this->lng->txt('ps_cdf_value'),'va');
367  $ty_se_mu->setRequired(true);
368  $ty_se_mu->setSize(32);
369  $ty_se_mu->setMaxLength(128);
370  $ty_se->addSubItem($ty_se_mu);
371 
372 
373  // Required
374  $re = new ilCheckboxInputGUI($this->lng->txt('ps_cdf_required'),'re');
375  $re->setValue(1);
376  $this->form->addItem($re);
377  }
378 }
379 ?>