Example #1
0
 /**
  * Import one field from model into form.
  *
  * @param string $field
  * @param string $field_name
  *
  * @return void|Form_Field
  */
 public function importField($field, $field_name = null)
 {
     $field = $this->model->hasElement($field);
     if (!$field) {
         return;
     }
     /** @type Field $field */
     if (!$field->editable()) {
         return;
     }
     if ($field_name === null) {
         $field_name = $this->_unique($this->owner->elements, $field->short_name);
     }
     $field_type = $this->getFieldType($field);
     $field_caption = $field->caption();
     $this->field_associations[$field_name] = $field;
     if ($field->listData() || $field instanceof Field_Reference) {
         if ($field_type == 'Line') {
             $field_type = 'DropDown';
         }
     }
     $form_field = $this->owner->addField($field_type, $field_name, $field_caption);
     $form_field->set($field->get());
     $field_placeholder = $field->placeholder() ?: $field->emptyText() ?: null;
     if ($field_placeholder) {
         $form_field->setAttr('placeholder', $field_placeholder);
     }
     if ($field->hint()) {
         $form_field->setFieldHint($field->hint());
     }
     if ($field->listData()) {
         /** @type Form_Field_ValueList $form_field */
         $a = $field->listData();
         $form_field->setValueList($a);
     }
     if ($msg = $field->mandatory()) {
         $form_field->validateNotNULL($msg);
     }
     if ($field instanceof Field_Reference || $field_type == 'reference') {
         $form_field->setModel($field->getModel());
     }
     if ($field->theModel) {
         $form_field->setModel($field->theModel);
     }
     if ($form_field instanceof Form_Field_ValueList && !$field->mandatory()) {
         /** @type string $text */
         $text = $field->emptyText();
         $form_field->setEmptyText($text);
     }
     if ($field->onField()) {
         call_user_func($field->onField(), $form_field);
     }
     return $form_field;
 }
Example #2
0
 /**
  * Import one field from model into grid.
  *
  * @param string $field
  *
  * @return void|Grid|Controller_Grid_Format
  */
 public function importField($field)
 {
     $field = $this->model->hasElement($field);
     if (!$field) {
         return;
     }
     /** @type Field $field */
     $field_name = $field->short_name;
     if ($field instanceof Field_Reference) {
         $field_name = $field->getDereferenced();
     }
     $field_type = $this->getFieldType($field);
     /** @type string $field_caption */
     $field_caption = $field->caption();
     $this->field_associations[$field_name] = $field;
     $column = $this->owner->addColumn($field_type, $field_name, $field_caption);
     if ($field->sortable() && $column->hasMethod('makeSortable')) {
         $column->makeSortable();
     }
     return $column;
 }
Example #3
0
 /**
  * Will convert one row of data from Persistence-specific
  * types to PHP native types.
  *
  * NOTE: Please DO NOT perform "actual" field mapping here, because data
  * may be "aliased" from SQL persistences or mapped depending on persistence
  * driver.
  *
  * @param Model $m
  * @param array $row
  *
  * @return array
  */
 public function typecastLoadRow(Model $m, $row)
 {
     if (!$row) {
         return $row;
     }
     $result = [];
     foreach ($row as $key => &$value) {
         // Look up field object
         $f = $m->hasElement($key);
         // We have no knowledge of the field, it wasn't defined, so
         // we will leave it as-is.
         if (!$f) {
             $result[$key] = $value;
             continue;
         }
         // ignore null values
         if ($value === null) {
             continue;
         }
         // serialize if we explicitly want that
         if ($f->serialize) {
             $value = $this->serializeLoadField($f, $value);
         }
         // typecast if we explicitly want that or there is not serialization enabled
         if ($f->typecast || $f->typecast === null && $f->serialize === null) {
             $value = $this->typecastLoadField($f, $value);
         }
         // store converted value
         $result[$key] = $value;
     }
     return $result;
 }