/**
  * Generate tokens for a math expression
  *
  * @param string $math_expression Expression of type math
  *
  * @return array
  */
 public static function getMathTokens($math_expression)
 {
     $operators = array_keys(ilDclExpressionParser::getOperators());
     $pattern = '#((^\\[\\[)[\\d\\.]+)|(\\(|\\)|\\' . implode("|\\", $operators) . ')#';
     $tokens = preg_split($pattern, $math_expression, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     return array_map('trim', $tokens);
 }
 /**
  * initEditCustomForm
  *
  * @param string $a_mode values: create | edit
  */
 public function initForm($a_mode = "create")
 {
     global $ilCtrl, $lng;
     include_once "./Services/Form/classes/class.ilPropertyFormGUI.php";
     $this->form = new ilPropertyFormGUI();
     if ($a_mode == "edit") {
         $this->form->setTitle($lng->txt('dcl_edit_field'));
         $hidden_prop = new ilHiddenInputGUI("field_id");
         $this->form->addItem($hidden_prop);
         $this->form->setFormAction($ilCtrl->getFormAction($this), "update");
         $this->form->addCommandButton('update', $lng->txt('dcl_update_field'));
     } else {
         $this->form->setTitle($lng->txt('dcl_new_field'));
         $hidden_prop = new ilHiddenInputGUI("table_id");
         $hidden_prop->setValue($this->field_obj->getTableId());
         $this->form->addItem($hidden_prop);
         $this->form->setFormAction($ilCtrl->getFormAction($this), "save");
         $this->form->addCommandButton('save', $lng->txt('dcl_create_field'));
     }
     $this->form->addCommandButton('cancel', $lng->txt('cancel'));
     $text_prop = new ilTextInputGUI($lng->txt("title"), "title");
     $text_prop->setRequired(true);
     $text_prop->setInfo(sprintf($lng->txt('fieldtitle_allow_chars'), ilDataCollectionField::_getTitleValidChars(false)));
     $text_prop->setValidationRegexp(ilDataCollectionField::_getTitleValidChars(true));
     $this->form->addItem($text_prop);
     $edit_datatype = new ilRadioGroupInputGUI($lng->txt('dcl_datatype'), 'datatype');
     foreach (ilDataCollectionDatatype::getAllDatatypes() as $datatype) {
         $opt = new ilRadioOption($lng->txt('dcl_' . $datatype['title']), $datatype['id']);
         foreach (ilDataCollectionDatatype::getProperties($datatype['id']) as $property) {
             //Type Reference: List Tabels
             if ($datatype['id'] == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE and $property['id'] == ilDataCollectionField::PROPERTYID_REFERENCE) {
                 $options = array();
                 // Get Tables
                 require_once "./Modules/DataCollection/classes/class.ilDataCollectionTable.php";
                 $tables = $this->parent_obj->getDataCollectionObject()->getTables();
                 foreach ($tables as $table) {
                     foreach ($table->getRecordFields() as $field) {
                         //referencing references may lead to endless loops.
                         if ($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
                             $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
                         }
                     }
                 }
                 $table_selection = new ilSelectInputGUI('', 'prop_' . $property['id']);
                 $table_selection->setOptions($options);
                 //$table_selection->setValue($this->table_id);
                 $opt->addSubItem($table_selection);
             } elseif ($datatype['id'] == ilDataCollectionDatatype::INPUTFORMAT_REFERENCELIST and $property['id'] == ilDataCollectionField::PROPERTYID_N_REFERENCE) {
                 // Get Tables
                 require_once "./Modules/DataCollection/classes/class.ilDataCollectionTable.php";
                 $tables = $this->parent_obj->getDataCollectionObject()->getTables();
                 foreach ($tables as $table) {
                     foreach ($table->getRecordFields() as $field) {
                         //referencing references may lead to endless loops.
                         if ($field->getDatatypeId() != ilDataCollectionDatatype::INPUTFORMAT_REFERENCELIST) {
                             $options[$field->getId()] = $table->getTitle() . self::REFERENCE_SEPARATOR . $field->getTitle();
                         }
                     }
                 }
                 $table_selection = new ilSelectInputGUI('', 'prop_' . $property['id']);
                 $table_selection->setOptions($options);
                 $opt->addSubItem($table_selection);
             } elseif ($property['id'] == ilDataCollectionField::PROPERTYID_FORMULA_EXPRESSION) {
                 $table = ilDataCollectionCache::getTableCache((int) $_GET['table_id']);
                 $fields = array();
                 foreach ($table->getFieldsForFormula() as $f) {
                     $placeholder = $f->isStandardField() ? $f->getId() : $f->getTitle();
                     $fields[] = '<a class="dclPropExpressionField" data-placeholder="' . $placeholder . '">' . $f->getTitle() . '</a>';
                 }
                 $subitem = new ilTextAreaInputGUI($lng->txt('dcl_prop_expression'), 'prop_' . $property['id']);
                 $operators = implode(', ', array_keys(ilDclExpressionParser::getOperators()));
                 $functions = implode(', ', ilDclExpressionParser::getFunctions());
                 $subitem->setInfo(sprintf($lng->txt('dcl_prop_expression_info'), $operators, $functions, implode('<br>', $fields)));
                 $opt->addSubItem($subitem);
             } elseif ($property['datatype_id'] == $datatype['id']) {
                 //All other Types: List properties saved in propertie definition table
                 if ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_BOOLEAN) {
                     $subitem = new ilCheckboxInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
                     $opt->addSubItem($subitem);
                 } elseif ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_NUMBER) {
                     $subitem = new ilNumberInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
                     // TODO: Nicer way to add additional info to fields (need changes in language-logic)
                     /*if($lng->txt('dcl_'.$property['title'].'_info') != '-dcl_'.$property['title'].'_info-') {
                     			$subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
                     		}*/
                     $subitem->setSize(5);
                     if ($property['title'] == 'length') {
                         $subitem->setMaxValue(4000);
                         $subitem->setInfo($lng->txt('dcl_' . $property['title'] . '_info'));
                     }
                     $opt->addSubItem($subitem);
                 } elseif ($property['inputformat'] == ilDataCollectionDatatype::INPUTFORMAT_NON_EDITABLE_VALUE) {
                     $subitem = new ilNonEditableValueGUI($lng->txt('dcl_' . $property['title']));
                     $subitem->setValue(implode(', ', ilDataCollectionDatatype::$mob_suffixes));
                     $opt->addSubItem($subitem);
                 } else {
                     $subitem = new ilTextInputGUI($lng->txt('dcl_' . $property['title']), 'prop_' . $property['id']);
                     // TODO: Nicer way to add additional info to fields (need changes in language-logic)
                     /*if($lng->txt('dcl_'.$property['title'].'_info') != '-dcl_'.$property['title'].'_info-') {
                     			$subitem->setInfo($lng->txt('dcl_'.$property['title'].'_info'));
                     		}*/
                     if ($property['title'] == 'regex') {
                         $subitem->setInfo($lng->txt('dcl_' . $property['title'] . '_info'));
                     }
                     $opt->addSubItem($subitem);
                 }
             }
         }
         $edit_datatype->addOption($opt);
     }
     $edit_datatype->setRequired(true);
     //you can't change type but we still need it in POST
     if ($a_mode == "edit") {
         $edit_datatype->setDisabled(true);
     }
     $this->form->addItem($edit_datatype);
     // Description
     $text_prop = new ilTextAreaInputGUI($lng->txt("dcl_field_description"), "description");
     $this->form->addItem($text_prop);
     // Required
     $cb = new ilCheckboxInputGUI($lng->txt("dcl_field_required"), "required");
     $this->form->addItem($cb);
     //Unique
     $cb = new ilCheckboxInputGUI($lng->txt("dcl_unique"), "unique");
     $this->form->addItem($cb);
 }
 /**
  * Parse expression
  *
  * @return string
  */
 protected function parse()
 {
     if (!$this->parsed_value and $this->expression) {
         $parser = new ilDclExpressionParser($this->expression, $this->record, $this->field);
         try {
             $this->parsed_value = $parser->parse();
         } catch (ilException $e) {
             return $this->lng->txt('dcl_error_parsing_expression') . ' (' . $e->getMessage() . ')';
         }
     }
     return $this->parsed_value;
 }