Esempio n. 1
0
 /**
  * Prepare the attribute form
  *
  * @param $attributes array           
  * @return \ProductAdmin\Form\ProductForm
  */
 public function createAttributesElements(array $attributes)
 {
     $customHydrator = new ClassMethods(true);
     $parentFilter = new \Zend\InputFilter\InputFilter();
     $fieldset = new \Zend\Form\Fieldset('attributes');
     //         $fieldset->setUseAsBaseFieldset(true);
     $fieldset->setObject(new \Zend\Stdlib\ArrayObject());
     $fieldset->setFormFactory($this->getFormFactory());
     // thanks to jurians #zftalk irc
     $fieldInput = null;
     $inputFilter = new \Zend\InputFilter\InputFilter();
     foreach ($attributes as $attribute) {
         $formitem = array();
         $filterChain = new \Zend\Filter\FilterChain();
         $name = $attribute->getName();
         $label = $attribute->getLabel() ? $attribute->getLabel() : "-";
         $type = $attribute->getType() ? $attribute->getType() : "string";
         $isRequired = $attribute->getIsRequired();
         $sourceModel = $attribute->getSourceModel();
         $filters = $attribute->getFilters();
         $validators = $attribute->getValidators();
         $cssStyles = $attribute->getCss();
         $validators = !empty($validators) ? json_decode($validators, true) : array();
         // create the new form element array structure
         $formitem['attributes'] = $attribute->getCustomAttributes() ? json_decode($attribute->getCustomAttributes(), true) : json_decode($attribute->attributes, true);
         $formitem['attributes']['id'] = $name;
         $formitem['attributes']['class'] = !empty($attribute->basecss) ? $attribute->basecss : null;
         $formitem['attributes']['name'] = $name;
         $formitem['type'] = !empty($sourceModel) ? $sourceModel : $attribute->input_type;
         $formitem['options']['label'] = $label;
         if ($attribute->getData()) {
             $formitem['options']['value_options'] = json_decode($attribute->getData(), true);
         }
         $filterChain->attachByName('null');
         // set as default
         // set the css style of the element
         if (!empty($cssStyles)) {
             $formitem['attributes']['class'] .= " " . $cssStyles;
         }
         // Handle the dates
         if (!empty($type) && $type == "date") {
             $customHydrator->addStrategy($name, new DateTimeStrategy());
             $typeSource = 'Zend\\Form\\Element\\Date';
             $formitem['type'] = "\\Zend\\Form\\Element\\Date";
             $formitem['options']['format'] = 'd/m/Y';
         }
         // handle the validators preferences of the attribute
         foreach ($validators as $validator) {
             $formitem['validators'] = $validator;
         }
         // var_dump($type);
         // var_dump($customHydrator);
         //             var_dump($formitem);
         // Attach the form item into the form
         $fieldset->add($formitem);
         $fieldInput = new \Zend\InputFilter\Input($name);
         $fieldInput->setRequired($isRequired);
         // handle the filters preferences of the attribute
         if (!empty($filters)) {
             // get the filters attached to the attribute
             $filters = json_decode($filters, true);
             foreach ($filters as $filter) {
                 // if the filter is an array check it by name
                 if (is_array($filter)) {
                     // If the filter is a ...
                     if ($filter['name'] == "File\\RenameUpload") {
                         // create the filter Zend\InputFilter\FileInput
                         $thefilter = new \Zend\InputFilter\FileInput($filter['options']);
                         // ... but how to attach the new filter to the
                         // chain?
                         $chain = new \Zend\Filter\FilterChain();
                         // ... in this way it doesn't work!!
                         $chain->attachByName("filerenameupload", $filter['options']);
                         $filterChain->merge($chain);
                         // just for debugging it ...
                         $filtersApplied = $filterChain->getFilters();
                         //                             var_dump($filtersApplied->toArray());
                     }
                 } elseif ($filter == "cleanurl") {
                     // custom filter
                     $filterChain->attach(new \ProductAdmin\Form\Filter\Cleanurl());
                 } elseif (is_string($filter)) {
                     $filterChain->attachByName($filter);
                 }
             }
         }
         $fieldInput->setFilterChain($filterChain);
         $inputFilter->add($fieldInput);
     }
     $fieldset->setHydrator($customHydrator);
     $this->add($fieldset);
     $parentFilter->add($inputFilter, 'attributes');
     // thanks to GeeH #zftalk irc
     $this->setInputFilter($parentFilter);
     return $this;
 }