public function init(array $options = array())
 {
     $itemModel = \Eva\Api::_()->getModel('User\\Model\\Field');
     $items = $itemModel->setItemList(array('applyToAll' => 1))->getFieldList();
     $items = $items->toArray(array('self' => array(), 'join' => array('Fieldoption' => array('*'))));
     foreach ($items as $item) {
         $this->mergeElements[$item['id']] = $itemModel->fieldToElement($item);
         $this->mergeFilters[$item['id']] = $itemModel->fieldToFilter($item);
     }
     return parent::init();
 }
 public function init(array $options = array())
 {
     $roleKey = $this->getRole();
     $itemModel = \Eva\Api::_()->getModel('User\\Model\\Role');
     $item = $itemModel->getRole($roleKey);
     $item = $item->toArray(array('self' => array('*'), 'join' => array('RoleFields' => array('self' => array('*'), 'join' => array('Fieldoption' => array('self' => array('*')))))));
     if (isset($item['Fields'])) {
         $fieldModel = \Eva\Api::_()->getModel('User\\Model\\Field');
         $elements = array();
         foreach ($item['Fields'] as $field) {
             $this->mergeElements[$field['id']] = $fieldModel->fieldToElement($field);
             $this->mergeFilters[$field['id']] = $fieldModel->fieldToFilter($field);
         }
     }
     parent::init();
 }
Beispiel #3
0
 /**
  * @param \Eva\Form\Form $form
  */
 public function prepare($form)
 {
     $elements = $form->getElements();
     $preparedElements = array();
     foreach ($elements as $element) {
         if ($element->getElements()->count()) {
             $this->prepare($element);
         }
         /** @var \Eva\Form\ElementInterface $element */
         $type = $element->getAttribute('type');
         if ($element instanceof Input) {
             if (in_array($type, ['text', 'password'])) {
                 $element->appendAttribute('class', 'form-control');
                 $element->setAttribute('value', $element->getValue());
             } elseif ($type == 'hidden') {
                 $element->setAttribute('value', $element->getValue());
             } elseif ($type == 'submit') {
                 $element->appendAttribute('class', 'btn btn-primary');
             }
         } elseif ($element instanceof Select) {
             /** @var Select $element */
             $element->appendAttribute('class', 'form-control');
             $value = $element->getValue();
             if ($value instanceof Entity) {
                 $value = $value->getId();
             }
             if ($element->getEmptyOption() !== null) {
                 $optionElement = new Option();
                 $optionElement->setText($element->getEmptyOption());
                 $element->append($optionElement);
             }
             foreach ($element->getOptions() as $key => $option) {
                 $optionAttributes = [];
                 if (is_array($option)) {
                     $optionAttributes = $option['attributes'];
                     $option = $option['value'];
                 }
                 $optionElement = new Option();
                 $optionElement->setAttributes($optionAttributes);
                 if ($option instanceof Entity) {
                     $property = $element->getProperty() ?: 'name';
                     $getter = 'get' . ucfirst($property);
                     if (!is_callable(array($option, $getter))) {
                         throw new \RuntimeException(sprintf('Метода нет "%s::%s"', get_class($option), $getter));
                     }
                     $label = $option->{$getter}();
                     $optionValue = $option->getId();
                     $optionElement->setText($label);
                 } else {
                     $optionValue = $key;
                     $optionElement->setText($option);
                 }
                 $optionElement->setAttribute('value', $optionValue);
                 if ($optionValue == $value) {
                     $optionElement->setAttribute('selected', 'selected');
                 }
                 $element->append($optionElement);
             }
         } elseif ($element instanceof Textarea) {
             $element->appendAttribute('class', 'form-control');
             $element->setText($element->getValue());
         }
         if ($element instanceof ElementInterface) {
             $div = new Div();
             $label = $element->getLabel();
             $label->setAttribute('class', 'control-label');
             if ($type == 'checkbox') {
                 $clone = clone $element;
                 $clone->setAttribute('value', '1');
                 $element->setAttribute('type', 'hidden');
                 $element->setAttribute('value', '0');
                 $div->append($element);
                 $div->setAttributes(array('class' => 'checkbox'));
                 $label->append(clone $clone);
                 $div->append($label);
             } elseif ($type == 'hidden') {
                 $div->append($element);
             } else {
                 $div->setAttributes(['class' => 'form-group']);
                 if ($element->getLabel()->getText()) {
                     $div->append($element->getLabel());
                 }
                 $div->append($element);
             }
             if (!$element->isValid()) {
                 $div->appendAttribute('class', 'has-error');
                 $p = new P();
                 $p->setAttribute('class', 'help-block');
                 $p->setText($element->getErrorMessage());
                 $div->append($p);
             }
             $preparedElements[] = $div;
         } else {
             $preparedElements[] = $element;
         }
     }
     $form->getElements()->exchangeArray($preparedElements);
 }
Beispiel #4
0
 public function isValid()
 {
     if (!$this->getFileTransfer()) {
         return parent::isValid();
     }
     $elementValid = parent::isValid();
     $fileValid = $this->fileTransfer->isValid();
     $result = $elementValid && $fileValid;
     $this->isValid = $result;
     if (!$result) {
         $fileTransferMessages = $this->fileTransfer->getMessages();
         if ($fileTransferMessages) {
             $this->fileTransferMessages = $fileTransferMessages = $this->resortFileTransferMessages($fileTransferMessages);
         }
         $this->setMessages($fileTransferMessages);
     }
     return $result;
 }