ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDclFieldEditGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
18 {
22  protected $obj_id;
23 
27  protected $table_id;
28 
32  protected $parent_obj;
33 
37  protected $table;
38 
42  protected $form;
46  protected $field_obj;
47 
48 
56  public function __construct(ilDclTableListGUI $a_parent_obj)
57  {
58  global $DIC;
59  $ilCtrl = $DIC['ilCtrl'];
60 
61  $this->obj_id = $a_parent_obj->obj_id;
62  $this->parent_obj = $a_parent_obj;
63 
64  $this->table_id = $_GET["table_id"];
65  $this->field_id = $_GET['field_id'];
66 
67  if ($this->field_id) {
68  $this->field_obj = ilDclCache::getFieldCache($this->field_id);
69  } else {
70  $datatype = null;
71  if (isset($_POST['datatype']) && in_array($_POST['datatype'], array_keys(ilDclDatatype::getAllDatatype()))) {
72  $datatype = $_POST['datatype'];
73  }
74  $this->field_obj = ilDclFieldFactory::getFieldModelInstance($this->field_id, $datatype);
75  if (!$this->table_id) {
76  $ilCtrl->redirectByClass("ilDclTableListGUI", "listFields");
77  }
78  $this->field_obj->setTableId($this->table_id);
79  $ilCtrl->saveParameter($this, "table_id");
80  }
81 
82  $this->table = ilDclCache::getTableCache($this->table_id);
83  }
84 
85 
89  public function executeCommand()
90  {
91  global $DIC;
92  $ilCtrl = $DIC['ilCtrl'];
93  $ilCtrl->saveParameter($this, 'field_id');
94 
95  $cmd = $ilCtrl->getCmd();
96 
97  if (!$this->checkAccess()) {
98  $this->permissionDenied();
99 
100  return;
101  }
102 
103  switch ($cmd) {
104  case "update":
105  $this->save("update");
106  break;
107  default:
108  $this->$cmd();
109  break;
110  }
111 
112  return true;
113  }
114 
115 
119  public function create()
120  {
121  global $DIC;
122  $tpl = $DIC['tpl'];
123 
124  $this->initForm();
125  $tpl->setContent($this->form->getHTML());
126  }
127 
128 
132  public function edit()
133  {
134  global $DIC;
135  $tpl = $DIC['tpl'];
136 
137  $this->initForm("edit");
138 
139  $this->field_obj->fillPropertiesForm($this->form);
140 
141  $tpl->setContent($this->form->getHTML());
142  }
143 
144 
145  /*
146  * permissionDenied
147  */
148  public function permissionDenied()
149  {
150  global $DIC;
151  $tpl = $DIC['tpl'];
152  $tpl->setContent("Permission denied");
153  }
154 
155 
159  public function confirmDelete()
160  {
161  global $DIC;
162  $ilCtrl = $DIC['ilCtrl'];
163  $lng = $DIC['lng'];
164  $tpl = $DIC['tpl'];
165 
166  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
167  $conf = new ilConfirmationGUI();
168  $conf->setFormAction($ilCtrl->getFormAction($this));
169  $conf->setHeaderText($lng->txt('dcl_confirm_delete_field'));
170 
171  $conf->addItem('field_id', (int) $this->field_obj->getId(), $this->field_obj->getTitle());
172 
173  $conf->setConfirm($lng->txt('delete'), 'delete');
174  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
175 
176  $tpl->setContent($conf->getHTML());
177  }
178 
179 
183  public function cancelDelete()
184  {
185  global $DIC;
186  $ilCtrl = $DIC['ilCtrl'];
187 
188  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
189  }
190 
191 
192  /*
193  * delete
194  */
195  public function delete()
196  {
197  global $DIC;
198  $ilCtrl = $DIC['ilCtrl'];
199 
200  $this->table->deleteField($this->field_obj->getId());
201  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
202  }
203 
204 
205  /*
206  * cancel
207  */
208  public function cancel()
209  {
210  global $DIC;
211  $ilCtrl = $DIC['ilCtrl'];
212  $ilCtrl->redirectByClass("ildclfieldlistgui", "listFields");
213  }
214 
215 
221  public function initForm($a_mode = "create")
222  {
223  global $DIC;
224  $ilCtrl = $DIC['ilCtrl'];
225  $lng = $DIC['lng'];
226 
227  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
228  $this->form = new ilPropertyFormGUI();
229 
230  if ($a_mode == "edit") {
231  $this->form->setTitle($lng->txt('dcl_edit_field'));
232  $hidden_prop = new ilHiddenInputGUI("field_id");
233  $this->form->addItem($hidden_prop);
234 
235  $this->form->setFormAction($ilCtrl->getFormAction($this), "update");
236 
237  $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
238  } else {
239  $this->form->setTitle($lng->txt('dcl_new_field'));
240  $hidden_prop = new ilHiddenInputGUI("table_id");
241  $hidden_prop->setValue($this->field_obj->getTableId());
242  $this->form->addItem($hidden_prop);
243 
244  $this->form->setFormAction($ilCtrl->getFormAction($this), "save");
245 
246  $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
247  }
248  $this->form->addCommandButton('cancel', $lng->txt('cancel'));
249 
250  $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
251  $text_prop->setRequired(true);
252  $text_prop->setInfo(sprintf($lng->txt('fieldtitle_allow_chars'), ilDclBaseFieldModel::_getTitleInvalidChars(false)));
253  $text_prop->setValidationRegexp(ilDclBaseFieldModel::_getTitleInvalidChars(true));
254  $this->form->addItem($text_prop);
255 
256  // Description
257  $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
258  $this->form->addItem($text_prop);
259 
260  $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'), 'datatype');
261 
262  foreach (ilDclDatatype::getAllDatatype() as $datatype) {
263  $model = new ilDclBaseFieldModel();
264  $model->setDatatypeId($datatype->getId());
265 
266  if ($a_mode == 'edit' && $datatype->getId() == $this->field_obj->getDatatypeId()) {
267  $model = $this->field_obj;
268  }
269 
270  $field_representation = ilDclFieldFactory::getFieldRepresentationInstance($model);
271  $field_representation->addFieldCreationForm($edit_datatype, $this->getDataCollectionObject(), $a_mode);
272  }
273  $edit_datatype->setRequired(true);
274 
275  //you can't change type but we still need it in POST
276  if ($a_mode == "edit") {
277  $edit_datatype->setDisabled(true);
278  }
279  $this->form->addItem($edit_datatype);
280 
281  // Required
282  $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
283  $this->form->addItem($cb);
284 
285  //Unique
286  $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
287  $cb->setInfo($lng->txt('dcl_unique_desc'));
288  $this->form->addItem($cb);
289  }
290 
291 
297  public function save($a_mode = "create")
298  {
299  global $DIC;
300  $ilCtrl = $DIC['ilCtrl'];
301  $lng = $DIC['lng'];
302  $tpl = $DIC['tpl'];
303 
304  $this->initForm($a_mode == "update" ? "edit" : "create");
305 
306  if ($this->checkInput($a_mode)) {
307 
308  // check if confirmation is needed and if so, fetch and render confirmationGUI
309  if (($a_mode == "update") && !($this->form->getInput('confirmed')) && $this->field_obj->isConfirmationRequired($this->form)) {
310  $ilConfirmationGUI = $this->field_obj->getConfirmationGUI($this->form);
311  $tpl->setContent($ilConfirmationGUI->getHTML());
312  return;
313  }
314 
315  $title = $this->form->getInput("title");
316  if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
317  ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
318  }
319 
320  $this->field_obj->setTitle($title);
321  $this->field_obj->setDescription($this->form->getInput("description"));
322  $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
323  $this->field_obj->setRequired($this->form->getInput("required"));
324  $this->field_obj->setUnique($this->form->getInput("unique"));
325 
326  if ($a_mode == "update") {
327  $this->field_obj->doUpdate();
328  } else {
329  $this->field_obj->setOrder($this->table->getNewFieldOrder());
330  $this->field_obj->doCreate();
331  }
332 
333  // Get possible properties and save them
334  $this->field_obj->storePropertiesFromForm($this->form);
335 
336 
337  $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
338 
339  if ($a_mode == "update") {
340  ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"), true);
341  } else {
342  $this->table->addField($this->field_obj);
343  $this->table->buildOrderFields();
344  ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
345  }
346  $ilCtrl->redirectByClass(strtolower("ilDclFieldListGUI"), "listFields");
347  } else {
348  $this->form->setValuesByPost();
349  $tpl->setContent($this->form->getHTML());
350  }
351  }
352 
353 
361  protected function checkInput($a_mode)
362  {
363  global $DIC;
364  $lng = $DIC['lng'];
365  $return = $this->form->checkInput();
366 
367  // load specific model for input checking
368  $datatype_id = $this->form->getInput('datatype');
369  if ($datatype_id != null && is_numeric($datatype_id)) {
370  $base_model = new ilDclBaseFieldModel();
371  $base_model->setDatatypeId($datatype_id);
372  $field_validation_class = ilDclFieldFactory::getFieldModelInstanceByClass($base_model);
373 
374  if (!$field_validation_class->checkFieldCreationInput($this->form)) {
375  $return = false;
376  }
377  }
378 
379  // Don't allow multiple fields with the same title in this table
380  if ($a_mode == 'create') {
381  if ($title = $this->form->getInput('title')) {
382  if (ilDclTable::_hasFieldByTitle($title, $this->table_id)) {
383  $inputObj = $this->form->getItemByPostVar('title');
384  $inputObj->setAlert($lng->txt("dcl_field_title_unique"));
385  $return = false;
386  }
387  }
388  }
389 
390  if (!$return) {
391  ilUtil::sendFailure($lng->txt("form_input_not_valid"));
392  }
393 
394  return $return;
395  }
396 
397 
401  protected function checkAccess()
402  {
403  if ($field_id = $this->field_obj->getId()) {
404  return ilObjDataCollectionAccess::hasAccessToField($this->getDataCollectionObject()->ref_id, $this->table_id, $field_id);
405  } else {
406  return ilObjDataCollectionAccess::hasAccessToFields($this->getDataCollectionObject()->ref_id, $this->table_id);
407  }
408  }
409 
413  public function getDataCollectionObject()
414  {
415  return $this->parent_obj->getDataCollectionObject();
416  }
417 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
Class ilDclBaseFieldModel.
static _getTitleInvalidChars($a_as_regex=true)
All valid chars for filed titles.
This class represents a property form user interface.
static getAllDatatype()
Get all possible Datatypes.
global $DIC
Definition: saml.php:7
static _hasFieldByTitle($title, $obj_id)
Checks if a table has a field with the given title.
$_GET["client_id"]
$tpl
Definition: ilias.php:10
static getFieldCache($field_id=0)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
edit()
create field edit form
This class represents a checkbox property in a property form.
Class ilDclTableListGUI.
create()
create field add form
static getTableCache($table_id=0)
save($a_mode="create")
save Field
global $ilCtrl
Definition: ilias.php:18
initForm($a_mode="create")
initEditCustomForm
setInfo($a_info)
Set Information Text.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
This class represents a hidden form property in a property form.
__construct(ilDclTableListGUI $a_parent_obj)
Constructor.
executeCommand()
execute command
This class represents a property in a property form.
static getFieldModelInstance($field_id, $datatype=null)
Get FieldModel from field-id and datatype.
static hasAccessToFields($ref_id, $table_id)
This class represents a text property in a property form.
checkInput($a_mode)
Check input of form.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
Class ilDclFieldEditGUI.
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
global $lng
Definition: privfeed.php:17
This class represents a text area property in a property form.
static hasAccessToField($ref_id, $table_id, $field_id)
$_POST["username"]
setRequired($a_required)
Set Required.
Confirmation screen class.