ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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;
19 
20  // glossary
21  const MODE_REC_SELECTION = 4; // record selection (per object)
22  const MODE_FILTER = 5; // filter (as used e.g. in tables)
23  const MODE_TABLE_HEAD = 6; // table header (columns)
24  const MODE_TABLE_CELLS = 7; // table cells
25 
26  protected $lng;
27 
28  private $mode;
29  private $obj_type;
30  private $sub_type;
31  private $obj_id;
32 
33  private $form;
34  private $search_values = array();
35 
36  protected $editor_form; // [array]
37 
46  public function __construct($a_mode,$a_obj_type = '',$a_obj_id = '', $a_sub_type = '', $a_sub_id = '')
47  {
48  global $lng;
49 
50  $this->lng = $lng;
51  $this->mode = $a_mode;
52  $this->obj_type = $a_obj_type;
53  $this->obj_id = $a_obj_id;
54  $this->sub_type = $a_sub_type;
55  $this->sub_id = $a_sub_id;
56  }
57 
65  public function setPropertyForm($form)
66  {
67  $this->form = $form;
68  }
69 
76  public function setSearchValues($a_values)
77  {
78  $this->search_values = $a_values;
79  }
80 
81 
89  public function setInfoObject($info)
90  {
91  $this->info = $info;
92  }
93 
101  public function parse()
102  {
103  switch($this->mode)
104  {
105  case self::MODE_EDITOR:
106  return $this->parseEditor();
107 
108  case self::MODE_SEARCH:
109  return $this->parseSearch();
110 
111  case self::MODE_INFO:
112  return $this->parseInfoPage();
113 
114  case self::MODE_REC_SELECTION:
115  return $this->parseRecordSelection();
116 
117  case self::MODE_FILTER:
118  return $this->parseFilter();
119 
120  case self::MODE_TABLE_HEAD:
121  return $this->parseTableHead();
122 
123  case self::MODE_TABLE_CELLS:
124  return $this->parseTableCells();
125 
126  default:
127  die('Not implemented yet');
128  }
129  }
130 
131 
132  //
133  // editor
134  //
135 
139  protected function parseEditor()
140  {
141  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
142  $this->editor_form = array();
143 
144  foreach($this->getActiveRecords() as $record_obj)
145  {
146  /* :TODO:
147  if($this->handleECSDefinitions($def))
148  {
149  continue;
150  }
151  */
152 
153  $record_id = $record_obj->getRecordId();
154 
155  $values = new ilAdvancedMDValues($record_id, $this->obj_id, $this->sub_type, $this->sub_id);
156  $values->read();
157  $defs = $values->getDefinitions();
158 
159  // empty record?
160  if(!sizeof($defs))
161  {
162  continue;
163  }
164 
165  $adt_group_form = ilADTFactory::getInstance()->getFormBridgeForInstance($values->getADTGroup());
166  $adt_group_form->setForm($this->form);
167  $adt_group_form->setTitle($record_obj->getTitle());
168  $adt_group_form->setInfo($record_obj->getDescription());
169 
170  foreach($defs as $def)
171  {
172  $element = $adt_group_form->getElement($def->getFieldId());
173  $element->setTitle($def->getTitle());
174  $element->setInfo($def->getDescription());
175 
176  // definition may customize ADT form element
177  $def->prepareElementForEditor($element);
178 
179  if($values->isDisabled($def->getFieldId()))
180  {
181  $element->setDisabled(true);
182  }
183  }
184 
185  $adt_group_form->addToForm();
186 
187  $this->editor_form[$record_id] = array("values"=>$values, "form"=>$adt_group_form);
188  }
189  }
190 
196  public function importEditFormPostValues()
197  {
198  // #13774
199  if(!is_array($this->editor_form))
200  {
201  return false;
202  }
203 
204  $valid = true;
205 
206  foreach($this->editor_form as $item)
207  {
208  $item["form"]->importFromPost();
209  if(!$item["form"]->validate())
210  {
211  $valid = false;
212  }
213  }
214 
215  return $valid;
216  }
217 
225  public function writeEditForm($a_obj_id = null, $a_sub_id = null)
226  {
227  if(!sizeof($this->editor_form))
228  {
229  return false;
230  }
231 
232  // switch ids?
233  if($a_obj_id)
234  {
235  $this->obj_id = $a_obj_id;
236  }
237  if($a_sub_id)
238  {
239  $this->sub_id = $a_sub_id;
240  }
241 
242  foreach($this->editor_form as $item)
243  {
244  if($a_obj_id || $a_sub_id)
245  {
246  // switch active record to updated primary keys, e.g. after creation
247  $item["values"]->setActiveRecordPrimary($this->obj_id, $this->sub_type, $this->sub_id);
248  }
249 
250  $item["values"]->write();
251  }
252 
253  return true;
254  }
255 
256 
257  //
258  // search
259  //
260 
264  private function parseSearch()
265  {
266  // this is NOT used for the global search, see ilLuceneAdvancedSearchFields::getFormElement()
267  // (so searchable flag is NOT relevant)
268  //
269  // current usage: wiki page element "[amd] page list"
270 
271  $this->lng->loadLanguageModule('search');
272 
273  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
274 
275  $this->search_form = array();
276  foreach($this->getActiveRecords() as $record)
277  {
278  $fields = ilAdvancedMDFieldDefinition::getInstancesByRecordId($record->getRecordId(), true);
279 
280  // empty record?
281  if(!sizeof($fields))
282  {
283  continue;
284  }
285 
287  $section->setTitle($record->getTitle());
288  $section->setInfo($record->getDescription());
289  $this->form->addItem($section);
290 
291  foreach($fields as $field)
292  {
293  $field_form = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($field->getADTDefinition(), true, false);
294  $field_form->setForm($this->form);
295  $field_form->setElementId("advmd[".$field->getFieldId()."]");
296  $field_form->setTitle($field->getTitle());
297 
298  if(is_array($this->search_form_values) &&
299  isset($this->search_form_values[$field->getFieldId()]))
300  {
301  $field->setSearchValueSerialized($field_form, $this->search_form_values[$field->getFieldId()]);
302  }
303 
304  $field->prepareElementForSearch($field_form);
305 
306  $field_form->addToForm();
307 
308  $this->search_form[$field->getFieldId()] = array("def"=>$field, "value"=>$field_form);
309  }
310  }
311  }
312 
318  public function importSearchForm()
319  {
320  if(!sizeof($this->search_form))
321  {
322  return false;
323  }
324 
325  $valid = true;
326  $res = array();
327 
328  foreach($this->search_form as $field_id => $item)
329  {
330  $item["value"]->importFromPost();
331  if(!$item["value"]->validate())
332  {
333  $valid = false;
334  }
335  $value = $item["def"]->getSearchValueSerialized($item["value"]);
336  if($value !== null)
337  {
338  $res[$field_id] = $value;
339  }
340  }
341 
342  if($valid)
343  {
344  return $res;
345  }
346  }
347 
348  public function setSearchFormValues(array $a_values)
349  {
350  $this->search_form_values = $a_values;
351  }
352 
353 
354  //
355  // infoscreen
356  //
357 
358  private function parseInfoPage()
359  {
360  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
361  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
362  include_once('Services/ADT/classes/class.ilADTFactory.php');
363 
364  foreach(ilAdvancedMDValues::getInstancesForObjectId($this->obj_id, $this->obj_type, $this->sub_type, $this->sub_id) as $record_id => $a_values)
365  {
366  // this correctly binds group and definitions
367  $a_values->read();
368 
369  $this->info->addSection(ilAdvancedMDRecord::_lookupTitle($record_id));
370 
371  $defs = $a_values->getDefinitions();
372  foreach($a_values->getADTGroup()->getElements() as $element_id => $element)
373  {
374  if(!$element->isNull())
375  {
376  $this->info->addProperty($defs[$element_id]->getTitle(),
377  ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getHTML());
378  }
379  }
380  }
381  }
382 
383 
384  //
385  // :TODO: ECS
386  //
387 
395  private function handleECSDefinitions($a_definition)
396  {
397  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
398  include_once('./Services/WebServices/ECS/classes/class.ilECSServerSettings.php');
399 
400  if(ilECSServerSettings::getInstance()->activeServerExists() or
401  ($this->obj_type != 'crs' and $this->obj_type != 'rcrs')
402  )
403  {
404  return false;
405  }
406  return false;
407  /*
408  $mapping = ilECSDataMappingSettings::_getInstance();
409 
410  if($mapping->getMappingByECSName(ilECSDataMappingSetting::MAPPING_IMPORT_RCRS, 'begin') == $a_definition->getFieldId())
411  {
412  $this->showECSStart($a_definition);
413  return true;
414  }
415  if($mapping->getMappingByECSName(ilECSDataMappingSetting::MAPPING_IMPORT_RCRS, 'end') == $a_definition->getFieldId())
416  {
417  return true;
418  }
419  if($mapping->getMappingByECSName(ilECSDataMappingSetting::MAPPING_IMPORT_RCRS, 'cycle') == $a_definition->getFieldId())
420  {
421  return true;
422  }
423  if($mapping->getMappingByECSName(ilECSDataMappingSetting::MAPPING_IMPORT_RCRS, 'room') == $a_definition->getFieldId())
424  {
425  return true;
426  }
427  */
428  }
429 
436  private function showECSStart($def)
437  {
438  global $ilUser;
439 
440  $this->lng->loadLanguageModule('ecs');
441 
442  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValue.php');
443  $value_start = ilAdvancedMDValue::_getInstance($this->obj_id,$def->getFieldId());
444 
445  $unixtime = $value_start->getValue() ? $value_start->getValue() : mktime(8,0,0,date('m'),date('d'),date('Y'));
446 
447  $time = new ilDateTimeInputGUI($this->lng->txt('ecs_event_appointment'),'md['.$def->getFieldId().']');
448  $time->setShowTime(true);
449  $time->setDate(new ilDateTime($unixtime,IL_CAL_UNIX));
450  /*
451  $time->enableDateActivation($this->lng->txt('enabled'),
452  'md_activated['.$def->getFieldId().']',
453  $value_start->getValue() ? true : false);
454  */
455  $time->setDisabled($value_start->isDisabled());
456 
458  if($field_id = $mapping->getMappingByECSName(0,'end'))
459  {
460  $value_end = ilAdvancedMDValue::_getInstance($this->obj_id,$field_id);
461 
462  list($hours,$minutes) = $this->parseDuration($value_start->getValue(),$value_end->getValue());
463 
464  $duration = new ilDurationInputGUI($this->lng->txt('ecs_duration'),'ecs_duration');
465  $duration->setHours($hours);
466  $duration->setMinutes($minutes);
467  #$duration->setInfo($this->lng->txt('ecs_duration_info'));
468  $duration->setShowHours(true);
469  $duration->setShowMinutes(true);
470  $time->addSubItem($duration);
471  }
472 
473  if($field_id = $mapping->getMappingByECSName(0,'cycle'))
474  {
475  $value = ilAdvancedMDValue::_getInstance($this->obj_id,$field_id);
476  $cycle_def = ilAdvancedMDFieldDefinition::getInstance($field_id);
477  switch($cycle_def->getFieldType())
478  {
480  $text = new ilTextInputGUI($cycle_def->getTitle(),'md['.$cycle_def->getFieldId().']');
481  $text->setValue($value->getValue());
482  $text->setSize(20);
483  $text->setMaxLength(512);
484  $text->setDisabled($value->isDisabled());
485  $time->addSubItem($text);
486  break;
487 
489  $select = new ilSelectInputGUI($cycle_def->getTitle(),'md['.$cycle_def->getFieldId().']');
490  $select->setOptions($cycle_def->getFieldValuesForSelect());
491  $select->setValue($value->getValue());
492  $select->setDisabled($value->isDisabled());
493  $time->addSubItem($select);
494  break;
495  }
496  }
497  if($field_id = $mapping->getMappingByECSName(0,'room'))
498  {
499  $value = ilAdvancedMDValue::_getInstance($this->obj_id,$field_id);
500  $room_def = ilAdvancedMDFieldDefinition::getInstance($field_id);
501  switch($room_def->getFieldType())
502  {
504  $text = new ilTextInputGUI($room_def->getTitle(),'md['.$room_def->getFieldId().']');
505  $text->setValue($value->getValue());
506  $text->setSize(20);
507  $text->setMaxLength(512);
508  $text->setDisabled($value->isDisabled());
509  $time->addSubItem($text);
510  break;
511 
513  $select = new ilSelectInputGUI($room_def->getTitle(),'md['.$room_def->getFieldId().']');
514  $select->setOptions($cycle_def->getFieldValuesForSelect());
515  $select->setValue($value->getValue());
516  $select->setDisabled($value->isDisabled());
517  $time->addSubItem($select);
518  break;
519  }
520  }
521  $this->form->addItem($time);
522  }
523 
531  protected function parseDuration($u_start,$u_end)
532  {
533  if($u_start >= $u_end)
534  {
535  return array(0,0);
536  }
537  $diff = $u_end - $u_start;
538  $hours = (int) ($diff / (60 * 60));
539  $min = (int) (($diff % 3600) / 60);
540  return array($hours,$min);
541  }
542 
543 
544  //
545  // glossary
546  //
547 
554  public function parseRecordSelection($a_sec_head = "")
555  {
556  global $ilUser;
557 
558  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
559  $first = true;
560  foreach(ilAdvancedMDRecord::_getActivatedRecordsByObjectType($this->obj_type, $this->sub_type) as $record_obj)
561  {
562  $selected = ilAdvancedMDRecord::getObjRecSelection($this->obj_id, $this->sub_type);
563  if ($first)
564  {
565  $first = false;
567  $sec_tit = ($a_sec_head == "")
568  ? $this->lng->txt("meta_adv_records")
569  : $a_sec_head;
570  $section->setTitle($sec_tit);
571  $this->form->addItem($section);
572  }
573 
574  // checkbox for each active record
575  $cb = new ilCheckboxInputGUI($record_obj->getTitle(), "amet_use_rec[]");
576  $cb->setInfo($record_obj->getDescription());
577  $cb->setValue($record_obj->getRecordId());
578  if (in_array($record_obj->getRecordId(), $selected))
579  {
580  $cb->setChecked(true);
581  }
582  $this->form->addItem($cb);
583  }
584  }
585 
592  function saveSelection()
593  {
594  $sel = ilUtil::stripSlashesArray($_POST["amet_use_rec"]);
595  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
596  ilAdvancedMDRecord::saveObjRecSelection($this->obj_id, $this->sub_type, $sel);
597  }
598 
604  function setTableGUI($a_val)
605  {
606  $this->table_gui = $a_val;
607  }
608 
614  function getTableGUI()
615  {
616  return $this->table_gui;
617  }
618 
624  function setRowData($a_val)
625  {
626  $this->row_data = $a_val;
627  }
628 
634  function getRowData()
635  {
636  return $this->row_data;
637  }
638 
639  protected function getActiveRecords()
640  {
641  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
642  return ilAdvancedMDRecord::_getSelectedRecordsByObject($this->obj_type, $this->obj_id, $this->sub_type);
643  }
644 
651  private function parseFilter()
652  {
653  $this->adt_search = array();
654 
655  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
656  foreach($this->getActiveRecords() as $record_obj)
657  {
658  $record_id = $record_obj->getRecordId();
659 
661  foreach($defs as $def)
662  {
663  // some input GUIs do NOT support filter rendering yet
664  if(!$def->isFilterSupported())
665  {
666  continue;
667  }
668 
669  /* :TODO:
670  if($this->handleECSDefinitions($def))
671  {
672  continue;
673  }
674  */
675 
676  $this->adt_search[$def->getFieldId()] = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def->getADTDefinition(), true, false);
677  $this->adt_search[$def->getFieldId()]->setTableGUI($this->table_gui);
678  $this->adt_search[$def->getFieldId()]->setTitle($def->getTitle());
679  $this->adt_search[$def->getFieldId()]->setElementId('md_'.$def->getFieldId());
680 
681  $this->adt_search[$def->getFieldId()]->loadFilter();
682  $this->adt_search[$def->getFieldId()]->addToForm();
683  }
684  }
685  }
686 
690  public function importFilter()
691  {
692  if(!is_array($this->adt_search))
693  {
694  return;
695  }
696 
697  foreach($this->adt_search as $element)
698  {
699  $element->importFromPost();
700  }
701  }
702 
708  public function getFilterElements($a_only_non_empty = true)
709  {
710  if(!is_array($this->adt_search))
711  {
712  return;
713  }
714 
715  $res = array();
716 
717  foreach($this->adt_search as $def_id => $element)
718  {
719  if(!$element->isNull() ||
720  !(bool)$a_only_non_empty)
721  {
722  $res[$def_id] = $element;
723  }
724  }
725 
726  return $res;
727  }
728 
729 
730  //
731  // :TODO: OBSOLETE? not used in glossary
732  //
733 
737  private function parseTableHead()
738  {
739  foreach($this->getActiveRecords() as $record_obj)
740  {
741  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
742  foreach(ilAdvancedMDFieldDefinition::_getDefinitionsByRecordId($record_obj->getRecordId()) as $def)
743  {
744  if($this->handleECSDefinitions($def))
745  {
746  continue;
747  }
748 
749  $this->table_gui->addColumn($def->getTitle(),'md_'.$def->getFieldId());
750  }
751  }
752  }
753 
757  private function parseTableCells()
758  {
759  $data = $this->getRowData();
760 
761  $html = "";
762 
763  foreach($this->getActiveRecords() as $record_obj)
764  {
765  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
766  foreach(ilAdvancedMDFieldDefinition::_getDefinitionsByRecordId($record_obj->getRecordId()) as $def)
767  {
768  if($this->handleECSDefinitions($def))
769  {
770  continue;
771  }
772 
773  $html.= "<td class='std'>".$data['md_'.$def->getFieldId()]."</td>";
774  }
775  }
776  return $html;
777  }
778 
779 }
780 
781 
782 ?>
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.
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.
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
$info
Definition: example_052.php:80
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.
setInfoObject($info)
get info sections
__construct($a_mode, $a_obj_type='', $a_obj_id='', $a_sub_type='', $a_sub_id='')
Constructor.
static getInstance($a_field_id, $a_type=null)
Get definition instance by type.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
This class represents a text property in a property form.
Date and time handling
$ilUser
Definition: imgupload.php:18
static _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type="")
Get selected records by object.
setSearchValues($a_values)
Set values for search form.
static _lookupTitle($a_record_id)
Lookup title.
parseFilter()
Parse property for filter (table)
Create styles array
The data for the language used.
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.
$text
parseEditor()
Parse property form in editor mode.
static _getInstance()
Get Singleton instance.
$_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.