ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilAdvancedMDRecordGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
15 {
16  const MODE_EDITOR = 1;
17  const MODE_SEARCH = 2;
18  const MODE_INFO = 3;
20 
21  // glossary
22  const MODE_REC_SELECTION = 4; // record selection (per object)
23  const MODE_FILTER = 5; // filter (as used e.g. in tables)
24  const MODE_TABLE_HEAD = 6; // table header (columns)
25  const MODE_TABLE_CELLS = 7; // table cells
26 
27  protected $lng;
28 
29  private $mode;
30  private $obj_type;
31  private $sub_type;
32  private $obj_id;
33  private $ref_id = null;
34 
35  private $form;
36  private $search_values = array();
37 
38  protected $editor_form; // [array]
39 
40  // $adv_ref_id - $adv_type - $adv_subtype:
41  // Object, that defines the adv md records being used. Default is $this->object, but the
42  // context may set another object (e.g. media pool for media objects)
46  protected $adv_ref_id = null;
50  protected $adv_type = null;
54  protected $adv_subtype = null;
55 
56 
65  public function __construct($a_mode, $a_obj_type = '', $a_obj_id = '', $a_sub_type = '', $a_sub_id = '')
66  {
67  global $DIC;
68 
69  $lng = $DIC['lng'];
70 
71  $this->lng = $lng;
72  $this->mode = $a_mode;
73  $this->obj_type = $a_obj_type;
74  $this->obj_id = $a_obj_id;
75  $this->sub_type = $a_sub_type;
76  $this->sub_id = $a_sub_id;
77 
78  if ($a_obj_id) {
79  $refs = ilObject::_getAllReferences($a_obj_id);
80  $this->ref_id = end($refs);
81  }
82  }
83 
90  public function setAdvMdRecordObject($a_adv_ref_id, $a_adv_type, $a_adv_subtype = "-")
91  {
92  $this->adv_ref_id = $a_adv_ref_id;
93  $this->adv_type = $a_adv_type;
94  $this->adv_subtype = $a_adv_subtype;
95  }
96 
102  public function getAdvMdRecordObject()
103  {
104  if ($this->adv_type == null) {
106  }
108  }
109 
110 
116  public function setRefId($a_ref_id)
117  {
118  $this->ref_id = $a_ref_id;
119  }
120 
128  public function setPropertyForm($form)
129  {
130  $this->form = $form;
131  }
132 
139  public function setSearchValues($a_values)
140  {
141  $this->search_values = $a_values;
142  }
143 
144 
152  public function setInfoObject($info)
153  {
154  $this->info = $info;
155  }
156 
164  public function parse()
165  {
166  switch ($this->mode) {
167  case self::MODE_EDITOR:
168  return $this->parseEditor();
169 
170  case self::MODE_SEARCH:
171  return $this->parseSearch();
172 
173  case self::MODE_INFO:
174  return $this->parseInfoPage();
175 
176  case self::MODE_APP_PRESENTATION:
177  return $this->parseAppointmentPresentation();
178 
179  case self::MODE_REC_SELECTION:
180  return $this->parseRecordSelection();
181 
182  case self::MODE_FILTER:
183  return $this->parseFilter();
184 
185  case self::MODE_TABLE_HEAD:
186  return $this->parseTableHead();
187 
188  case self::MODE_TABLE_CELLS:
189  return $this->parseTableCells();
190 
191  default:
192  die('Not implemented yet');
193  }
194  }
195 
196 
197  //
198  // editor
199  //
200 
204  protected function parseEditor()
205  {
206  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
207  $this->editor_form = array();
208 
209  foreach ($this->getActiveRecords() as $record_obj) {
210  /* :TODO:
211  if($this->handleECSDefinitions($def))
212  {
213  continue;
214  }
215  */
216 
217  $record_id = $record_obj->getRecordId();
218 
219  $values = new ilAdvancedMDValues($record_id, $this->obj_id, $this->sub_type, $this->sub_id);
220  $values->read();
221  $defs = $values->getDefinitions();
222 
223  // empty record?
224  if (!sizeof($defs)) {
225  continue;
226  }
227 
228  $adt_group_form = ilADTFactory::getInstance()->getFormBridgeForInstance($values->getADTGroup());
229  $adt_group_form->setForm($this->form);
230  $adt_group_form->setTitle($record_obj->getTitle());
231  $adt_group_form->setInfo($record_obj->getDescription());
232 
233  foreach ($defs as $def) {
234  $element = $adt_group_form->getElement($def->getFieldId());
235  $element->setTitle($def->getTitle());
236  $element->setInfo($def->getDescription());
237 
238  // definition may customize ADT form element
239  $def->prepareElementForEditor($element);
240 
241  if ($values->isDisabled($def->getFieldId())) {
242  $element->setDisabled(true);
243  }
244  }
245 
246  $adt_group_form->addToForm();
247 
248  $this->editor_form[$record_id] = array("values" => $values, "form" => $adt_group_form);
249  }
250  }
251 
257  public function importEditFormPostValues()
258  {
259  // #13774
260  if (!is_array($this->editor_form)) {
261  return false;
262  }
263 
264  $valid = true;
265 
266  foreach ($this->editor_form as $item) {
267  $item["form"]->importFromPost();
268  if (!$item["form"]->validate()) {
269  $valid = false;
270  }
271  }
272 
273  return $valid;
274  }
275 
283  public function writeEditForm($a_obj_id = null, $a_sub_id = null)
284  {
285  if (!sizeof($this->editor_form)) {
286  return false;
287  }
288 
289  // switch ids?
290  if ($a_obj_id) {
291  $this->obj_id = $a_obj_id;
292  }
293  if ($a_sub_id) {
294  $this->sub_id = $a_sub_id;
295  }
296 
297  foreach ($this->editor_form as $item) {
298  if ($a_obj_id || $a_sub_id) {
299  // switch active record to updated primary keys, e.g. after creation
300  $item["values"]->setActiveRecordPrimary($this->obj_id, $this->sub_type, $this->sub_id);
301  }
302 
303  $item["values"]->write();
304  }
305 
306  return true;
307  }
308 
309 
310  //
311  // search
312  //
313 
317  private function parseSearch()
318  {
319  // this is NOT used for the global search, see ilLuceneAdvancedSearchFields::getFormElement()
320  // (so searchable flag is NOT relevant)
321  //
322  // current usage: wiki page element "[amd] page list"
323 
324  $this->lng->loadLanguageModule('search');
325 
326  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
327 
328  $this->search_form = array();
329  foreach ($this->getActiveRecords() as $record) {
330  $fields = ilAdvancedMDFieldDefinition::getInstancesByRecordId($record->getRecordId(), true);
331 
332  // empty record?
333  if (!sizeof($fields)) {
334  continue;
335  }
336 
338  $section->setTitle($record->getTitle());
339  $section->setInfo($record->getDescription());
340  $this->form->addItem($section);
341 
342  foreach ($fields as $field) {
343  $field_form = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($field->getADTDefinition(), true, false);
344  $field_form->setForm($this->form);
345  $field_form->setElementId("advmd[" . $field->getFieldId() . "]");
346  $field_form->setTitle($field->getTitle());
347 
348  if (is_array($this->search_form_values) &&
349  isset($this->search_form_values[$field->getFieldId()])) {
350  $field->setSearchValueSerialized($field_form, $this->search_form_values[$field->getFieldId()]);
351  }
352 
353  $field->prepareElementForSearch($field_form);
354 
355  $field_form->addToForm();
356 
357  $this->search_form[$field->getFieldId()] = array("def" => $field, "value" => $field_form);
358  }
359  }
360  }
361 
367  public function importSearchForm()
368  {
369  if (!sizeof($this->search_form)) {
370  return false;
371  }
372 
373  $valid = true;
374  $res = array();
375 
376  foreach ($this->search_form as $field_id => $item) {
377  $item["value"]->importFromPost();
378  if (!$item["value"]->validate()) {
379  $valid = false;
380  }
381  $value = $item["def"]->getSearchValueSerialized($item["value"]);
382  if ($value !== null) {
383  $res[$field_id] = $value;
384  }
385  }
386 
387  if ($valid) {
388  return $res;
389  }
390  }
391 
392  public function setSearchFormValues(array $a_values)
393  {
394  $this->search_form_values = $a_values;
395  }
396 
401  private function parseInfoPage()
402  {
403  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
404  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
405  include_once('Services/ADT/classes/class.ilADTFactory.php');
406 
407  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->obj_id, $this->obj_type, $this->sub_type, $this->sub_id) as $record_id => $a_values) {
408  // this correctly binds group and definitions
409  $a_values->read();
410 
411  $this->info->addSection(ilAdvancedMDRecord::_lookupTitle($record_id));
412 
413  $defs = $a_values->getDefinitions();
414  foreach ($a_values->getADTGroup()->getElements() as $element_id => $element) {
415  if (!$element->isNull()) {
416  $this->info->addProperty(
417  $defs[$element_id]->getTitle(),
418  ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getHTML()
419  );
420  }
421  }
422  }
423  }
424 
429  private function parseAppointmentPresentation()
430  {
432 
433  $definitions = ilAdvancedMDFieldDefinition::getInstancesByObjType($this->obj_type);
434  $definitions = $sub->sortDefinitions($definitions);
435 
436  $positions = array();
437  foreach ($definitions as $position => $value) {
438  $positions[$value->getFieldId()] = $position;
439  }
440 
441  $array_elements = array();
442  foreach (ilAdvancedMDValues::getInstancesForObjectId($this->obj_id, $this->obj_type, $this->sub_type, $this->sub_id) as $record_id => $a_values) {
443  // this correctly binds group and definitions
444  $a_values->read();
445 
446  $defs = $a_values->getDefinitions();
447  foreach ($a_values->getADTGroup()->getElements() as $element_id => $element) {
448  if (!$element->isNull()) {
449  $presentation_bridge = ilADTFactory::getInstance()->getPresentationBridgeForInstance($element);
450  #21615
451  if (get_class($element) == 'ilADTLocation') {
452  $presentation_bridge->setSize("100%", "200px");
453  #22638
454  $presentation_value = $presentation_bridge->getHTML();
455  $presentation_value .= "<script>ilInitMaps();</script>";
456  } else {
457  #22638
458  $presentation_value = strip_tags($presentation_bridge->getHTML());
459  }
460  $array_elements[$positions[$element_id]] =
461  [
462  "title" => $defs[$element_id]->getTitle(),
463  "value" => $presentation_value
464  ];
465  }
466  }
467  }
468 
469  // already sorted by record positions
470  return $array_elements;
471  }
472 
473  //
474  // :TODO: ECS
475  //
476 
484  private function handleECSDefinitions($a_definition)
485  {
486  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
487  include_once('./Services/WebServices/ECS/classes/class.ilECSServerSettings.php');
488 
489  if (ilECSServerSettings::getInstance()->activeServerExists() or
490  ($this->obj_type != 'crs' and $this->obj_type != 'rcrs')
491  ) {
492  return false;
493  }
494  return false;
495  }
496 
503  private function showECSStart($def)
504  {
505  global $DIC;
506 
507  $ilUser = $DIC['ilUser'];
508 
509  $this->lng->loadLanguageModule('ecs');
510 
511  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValue.php');
512  $value_start = ilAdvancedMDValue::_getInstance($this->obj_id, $def->getFieldId());
513 
514  $unixtime = $value_start->getValue() ? $value_start->getValue() : mktime(8, 0, 0, date('m'), date('d'), date('Y'));
515 
516  $time = new ilDateTimeInputGUI($this->lng->txt('ecs_event_appointment'), 'md[' . $def->getFieldId() . ']');
517  $time->setShowTime(true);
518  $time->setDate(new ilDateTime($unixtime, IL_CAL_UNIX));
519  /*
520  $time->enableDateActivation($this->lng->txt('enabled'),
521  'md_activated['.$def->getFieldId().']',
522  $value_start->getValue() ? true : false);
523  */
524  $time->setDisabled($value_start->isDisabled());
525 
527  if ($field_id = $mapping->getMappingByECSName(0, 'end')) {
528  $value_end = ilAdvancedMDValue::_getInstance($this->obj_id, $field_id);
529 
530  list($hours, $minutes) = $this->parseDuration($value_start->getValue(), $value_end->getValue());
531 
532  $duration = new ilDurationInputGUI($this->lng->txt('ecs_duration'), 'ecs_duration');
533  $duration->setHours($hours);
534  $duration->setMinutes($minutes);
535  #$duration->setInfo($this->lng->txt('ecs_duration_info'));
536  $duration->setShowHours(true);
537  $duration->setShowMinutes(true);
538  $time->addSubItem($duration);
539  }
540 
541  if ($field_id = $mapping->getMappingByECSName(0, 'cycle')) {
542  $value = ilAdvancedMDValue::_getInstance($this->obj_id, $field_id);
543  $cycle_def = ilAdvancedMDFieldDefinition::getInstance($field_id);
544  switch ($cycle_def->getFieldType()) {
546  $text = new ilTextInputGUI($cycle_def->getTitle(), 'md[' . $cycle_def->getFieldId() . ']');
547  $text->setValue($value->getValue());
548  $text->setSize(20);
549  $text->setMaxLength(512);
550  $text->setDisabled($value->isDisabled());
551  $time->addSubItem($text);
552  break;
553 
555  $select = new ilSelectInputGUI($cycle_def->getTitle(), 'md[' . $cycle_def->getFieldId() . ']');
556  $select->setOptions($cycle_def->getFieldValuesForSelect());
557  $select->setValue($value->getValue());
558  $select->setDisabled($value->isDisabled());
559  $time->addSubItem($select);
560  break;
561  }
562  }
563  if ($field_id = $mapping->getMappingByECSName(0, 'room')) {
564  $value = ilAdvancedMDValue::_getInstance($this->obj_id, $field_id);
565  $room_def = ilAdvancedMDFieldDefinition::getInstance($field_id);
566  switch ($room_def->getFieldType()) {
568  $text = new ilTextInputGUI($room_def->getTitle(), 'md[' . $room_def->getFieldId() . ']');
569  $text->setValue($value->getValue());
570  $text->setSize(20);
571  $text->setMaxLength(512);
572  $text->setDisabled($value->isDisabled());
573  $time->addSubItem($text);
574  break;
575 
577  $select = new ilSelectInputGUI($room_def->getTitle(), 'md[' . $room_def->getFieldId() . ']');
578  $select->setOptions($cycle_def->getFieldValuesForSelect());
579  $select->setValue($value->getValue());
580  $select->setDisabled($value->isDisabled());
581  $time->addSubItem($select);
582  break;
583  }
584  }
585  $this->form->addItem($time);
586  }
587 
595  protected function parseDuration($u_start, $u_end)
596  {
597  if ($u_start >= $u_end) {
598  return array(0,0);
599  }
600  $diff = $u_end - $u_start;
601  $hours = (int) ($diff / (60 * 60));
602  $min = (int) (($diff % 3600) / 60);
603  return array($hours,$min);
604  }
605 
606 
607  //
608  // glossary
609  //
610 
617  public function parseRecordSelection($a_sec_head = "")
618  {
619  global $DIC;
620 
621  $ilUser = $DIC['ilUser'];
622 
623  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
624  $first = true;
625  foreach (ilAdvancedMDRecord::_getActivatedRecordsByObjectType($this->obj_type, $this->sub_type) as $record_obj) {
626  $selected = ilAdvancedMDRecord::getObjRecSelection($this->obj_id, $this->sub_type);
627  if ($first) {
628  $first = false;
630  $sec_tit = ($a_sec_head == "")
631  ? $this->lng->txt("meta_adv_records")
632  : $a_sec_head;
633  $section->setTitle($sec_tit);
634  $this->form->addItem($section);
635  }
636 
637  // checkbox for each active record
638  $cb = new ilCheckboxInputGUI($record_obj->getTitle(), "amet_use_rec[]");
639  $cb->setInfo($record_obj->getDescription());
640  $cb->setValue($record_obj->getRecordId());
641  if (in_array($record_obj->getRecordId(), $selected)) {
642  $cb->setChecked(true);
643  }
644  $this->form->addItem($cb);
645  }
646  }
647 
654  public function saveSelection()
655  {
656  $sel = ilUtil::stripSlashesArray($_POST["amet_use_rec"]);
657  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
658  ilAdvancedMDRecord::saveObjRecSelection($this->obj_id, $this->sub_type, $sel);
659  }
660 
666  public function setTableGUI($a_val)
667  {
668  $this->table_gui = $a_val;
669  }
670 
676  public function getTableGUI()
677  {
678  return $this->table_gui;
679  }
680 
686  public function setRowData($a_val)
687  {
688  $this->row_data = $a_val;
689  }
690 
696  public function getRowData()
697  {
698  return $this->row_data;
699  }
700 
701  protected function getActiveRecords()
702  {
703  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
706  }
707 
714  private function parseFilter()
715  {
716  $this->adt_search = array();
717 
718  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
719  foreach ($this->getActiveRecords() as $record_obj) {
720  $record_id = $record_obj->getRecordId();
721 
723  foreach ($defs as $def) {
724  // some input GUIs do NOT support filter rendering yet
725  if (!$def->isFilterSupported()) {
726  continue;
727  }
728 
729  /* :TODO:
730  if($this->handleECSDefinitions($def))
731  {
732  continue;
733  }
734  */
735 
736  $this->adt_search[$def->getFieldId()] = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def->getADTDefinition(), true, false);
737  $this->adt_search[$def->getFieldId()]->setTableGUI($this->table_gui);
738  $this->adt_search[$def->getFieldId()]->setTitle($def->getTitle());
739  $this->adt_search[$def->getFieldId()]->setElementId('md_' . $def->getFieldId());
740 
741  $this->adt_search[$def->getFieldId()]->loadFilter();
742  $this->adt_search[$def->getFieldId()]->addToForm();
743  }
744  }
745  }
746 
750  public function importFilter()
751  {
752  if (!is_array($this->adt_search)) {
753  return;
754  }
755 
756  foreach ($this->adt_search as $element) {
757  $element->importFromPost();
758  }
759  }
760 
766  public function getFilterElements($a_only_non_empty = true)
767  {
768  if (!is_array($this->adt_search)) {
769  return;
770  }
771 
772  $res = array();
773 
774  foreach ($this->adt_search as $def_id => $element) {
775  if (!$element->isNull() ||
776  !(bool) $a_only_non_empty) {
777  $res[$def_id] = $element;
778  }
779  }
780 
781  return $res;
782  }
783 
784 
785  //
786  // :TODO: OBSOLETE? not used in glossary
787  //
788 
792  private function parseTableHead()
793  {
794  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
795  foreach ($this->getActiveRecords() as $record_obj) {
796  $record_id = $record_obj->getRecordId();
797 
799  foreach ($defs as $def) {
800  if ($this->handleECSDefinitions($def)) {
801  continue;
802  }
803 
804  $this->table_gui->addColumn($def->getTitle(), 'md_' . $def->getFieldId());
805  }
806  }
807  }
808 
812  private function parseTableCells()
813  {
814  $data = $this->getRowData();
815  $html = "";
816 
817  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
818  foreach ($this->getActiveRecords() as $record_obj) {
819  $record_id = $record_obj->getRecordId();
820 
822  foreach ($defs as $def) {
823  if ($this->handleECSDefinitions($def)) {
824  continue;
825  }
826 
827  $html .= "<td class='std'>" . $data['md_' . $def->getFieldId()] . "</td>";
828  }
829  }
830  return $html;
831  }
832 }
parseDuration($u_start, $u_end)
parse hours and minutes from duration
This class represents a duration (typical hh:mm:ss) property in a property form.
static getInstancesForObjectId($a_obj_id, $a_obj_type=null, $a_sub_type="-", $a_sub_id=0)
Get instances for given object id.
importSearchForm()
Load edit form values from post.
parseTableCells()
Parse table cells.
This class represents a selection list property in a property form.
setRefId($a_ref_id)
Set ref_id for context.
global $DIC
Definition: saml.php:7
setPropertyForm($form)
set property form object
importEditFormPostValues()
Load edit form values from post.
This class represents a section header in a property form.
static getInstance()
Get singleton instance.
$valid
handleECSDefinitions($a_definition)
handle ecs definitions
setHours($a_hours)
Set Hours.
This class represents a checkbox property in a property form.
parseRecordSelection($a_sec_head="")
Parse property form in editor mode.
parseTableHead()
Parse property for table head.
const IL_CAL_UNIX
static getInstance()
Get singleton.
static saveObjRecSelection($a_obj_id, $a_sub_type="", array $a_records=null, $a_delete_before=true)
Save repository object record selection.
static _getAllReferences($a_id)
get all reference ids of object
static _getInstanceByObjectType($a_type)
Singleton: use this method to get an instance.
static _getSelectedRecordsByObject($a_obj_type, $a_ref_id, $a_sub_type="")
Get selected records by object.
This class represents a date/time property in a property form.
writeEditForm($a_obj_id=null, $a_sub_id=null)
Write edit form values to db.
setInfo($a_info)
Set Information Text.
$section
Definition: Utf8Test.php:83
$time
Definition: cron.php:21
getFilterElements($a_only_non_empty=true)
Get SQL conditions for current filter value(s)
static getInstancesByRecordId($a_record_id, $a_only_searchable=false)
Get definitions by record id.
foreach($_POST as $key=> $value) $res
setInfoObject($info)
get info sections
__construct($a_mode, $a_obj_type='', $a_obj_id='', $a_sub_type='', $a_sub_id='')
Constructor.
$values
static getInstancesByObjType($a_obj_type, $a_active_only=true)
static getInstance($a_field_id, $a_type=null)
Get definition instance by type.
$text
Definition: errorreport.php:18
This class represents a text property in a property form.
Date and time handling
$ilUser
Definition: imgupload.php:18
setSearchValues($a_values)
Set values for search form.
setOptions($a_options)
Set Options.
static _lookupTitle($a_record_id)
Lookup title.
parseFilter()
Parse property for filter (table)
static _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type="", $a_only_optional=false)
Get activated records by object type.
static stripSlashesArray($a_arr, $a_strip_html=true, $a_allow="")
Strip slashes from array.
showECSStart($def)
Show special form for ecs start.
getAdvMdRecordObject()
Get adv md record type.
setAdvMdRecordObject($a_adv_ref_id, $a_adv_type, $a_adv_subtype="-")
Set object, that defines the adv md records being used.
parseAppointmentPresentation()
Presentation for calendar agenda list.
$def
Definition: croninfo.php:21
parseEditor()
Parse property form in editor mode.
static _getInstance()
Get Singleton instance.
$info
Definition: index.php:5
info()
Definition: info.php:2
parseInfoPage()
Presentation for info page.
$_POST["username"]
$html
Definition: example_001.php:87
saveSelection()
Save selection per object.
static getObjRecSelection($a_obj_id, $a_sub_type="")
Get repository object record selection.
importFilter()
Import filter (post) values.
$data
Definition: bench.php:6