Exemplo n.º 1
0
 /**
  * Create Form Objects for column and rowset index.
  *
  * @param \FSi\Component\DataGrid\Column\ColumnTypeInterface $column
  * @param mixed $index
  * @param mixed $object
  * @return FormInterface
  */
 private function createForm(ColumnTypeInterface $column, $index, $object)
 {
     $formId = implode(array($column->getName(), $column->getId(), $index));
     if (array_key_exists($formId, $this->forms)) {
         return $this->forms[$formId];
     }
     //Create fields array. There are column types like entity where field_mapping
     //should not be used to build field array.
     $fields = array();
     switch ($column->getId()) {
         case 'entity':
             $field = array('name' => $column->getOption('relation_field'), 'type' => $this->isSymfony3() ? $this->getEntityTypeName() : 'entity', 'options' => array());
             $fields[$column->getOption('relation_field')] = $field;
             break;
         default:
             foreach ($column->getOption('field_mapping') as $fieldName) {
                 $field = array('name' => $fieldName, 'type' => null, 'options' => array());
                 $fields[$fieldName] = $field;
             }
     }
     //Pass fields form options from column into $fields array.
     $fieldsOptions = $column->getOption('form_options');
     foreach ($fieldsOptions as $fieldName => $fieldOptions) {
         if (array_key_exists($fieldName, $fields)) {
             if (is_array($fieldOptions)) {
                 $fields[$fieldName]['options'] = $fieldOptions;
             }
         }
     }
     //Pass fields form type from column into $fields array.
     $fieldsTypes = $column->getOption('form_type');
     foreach ($fieldsTypes as $fieldName => $fieldType) {
         if (array_key_exists($fieldName, $fields)) {
             if (is_string($fieldType)) {
                 $fields[$fieldName]['type'] = $fieldType;
             }
         }
     }
     //Build data array, the data array holds data that should be passed into
     //form elements.
     switch ($column->getId()) {
         case 'datetime':
             foreach ($fields as &$field) {
                 $value = $column->getDataMapper()->getData($field['name'], $object);
                 if (!isset($field['type'])) {
                     $field['type'] = $this->isSymfony3() ? $this->getDateTimeTypeName() : 'datetime';
                 }
                 if (is_numeric($value) && !isset($field['options']['input'])) {
                     $field['options']['input'] = 'timestamp';
                 }
                 if (is_string($value) && !isset($field['options']['input'])) {
                     $field['options']['input'] = 'string';
                 }
                 if ($value instanceof \DateTime && !isset($field['options']['input'])) {
                     $field['options']['input'] = 'datetime';
                 }
             }
             break;
     }
     if ($this->isSymfony3()) {
         $formBuilderOptions = array('entry_type' => $this->getRowTypeName(), 'csrf_protection' => false);
     } else {
         $formBuilderOptions = array('type' => new RowType($fields), 'csrf_protection' => false);
     }
     if ($this->isSymfony3()) {
         $formBuilderOptions['entry_options']['fields'] = $fields;
     }
     $formData = [];
     foreach (array_keys($fields) as $fieldName) {
         $formData[$fieldName] = $column->getDataMapper()->getData($fieldName, $object);
     }
     //Create form builder.
     $formBuilder = $this->formFactory->createNamedBuilder($column->getDataGrid()->getName(), $this->isSymfony3() ? $this->getCollectionTypeName() : 'collection', array($index => $formData), $formBuilderOptions);
     //Create Form.
     $this->forms[$formId] = $formBuilder->getForm();
     return $this->forms[$formId];
 }