コード例 #1
0
 /**
  * Prepares the form
  * @param \ride\library\form\FormBuilder $builder
  * @param array $options
  * @return null
  */
 public function prepareForm(FormBuilder $builder, array $options)
 {
     $translator = $options['translator'];
     $asset = $options['data'];
     $builder->addRow('resource', 'option', array('label' => $translator->translate('label.resource'), 'attributes' => array('data-toggle-dependant' => 'option-resource'), 'options' => array('file' => $translator->translate('label.file'), 'url' => $translator->translate('label.url')), 'default' => 'file'));
     $builder->addRow('file', 'file', array('label' => $translator->translate('label.file'), 'attributes' => array('class' => 'option-resource option-resource-file'), 'path' => $this->directory));
     $builder->addRow('url', 'website', array('label' => $translator->translate('label.url'), 'attributes' => array('class' => 'option-resource option-resource-url')));
     $builder->addRow('name', 'string', array('label' => $translator->translate('label.name'), 'filters' => array('trim' => array())));
     $builder->addRow('description', 'wysiwyg', array('label' => $translator->translate('label.description')));
     $builder->addRow('copyright', 'string', array('label' => $translator->translate('label.copyright'), 'filters' => array('trim' => array())));
     $requiredValidator = $this->validationFactory->createValidator('required', array());
     $urlRequired = new ConditionalConstraint();
     $urlRequired->addValueCondition('resource', 'url');
     $urlRequired->addValidator($requiredValidator, 'url');
     $fileRequired = new ConditionalConstraint();
     $fileRequired->addValueCondition('resource', 'file');
     $fileRequired->addValidator($requiredValidator, 'file');
     $builder->addValidationConstraint($urlRequired);
     $builder->addValidationConstraint($fileRequired);
 }
コード例 #2
0
 /**
  * Action to handle and show the properties of this widget
  * @return null
  */
 public function propertiesAction(ValidationFactory $validationFactory)
 {
     $translator = $this->getTranslator();
     $node = $this->properties->getNode();
     $rootNodeId = $node->getRootNodeId();
     $site = $this->cms->getNode($rootNodeId, $node->getRevision(), $rootNodeId, null, true);
     $levels = $this->cms->getChildrenLevels($site) - 1;
     $nodeList = $this->cms->getNodeList($site, $this->locale, true, true, false);
     $nodeList[self::PARENT_CURRENT] = $translator->translate('label.menu.parent.current');
     for ($i = 1; $i <= $levels; $i++) {
         $nodeList[self::PARENT_ABSOLUTE . $i] = $translator->translate('label.menu.parent.absolute', array('level' => $i));
     }
     for ($i = 0; $i < $levels; $i++) {
         $level = $i + 1;
         $nodeList[self::PARENT_RELATIVE . $level] = $translator->translate('label.menu.parent.relative', array('level' => '-' . $level));
     }
     $depths = array();
     for ($i = 1, $j = $levels + 1; $i <= $j; $i++) {
         $depths[$i] = $i;
     }
     $title = $this->properties->getWidgetProperty(self::PROPERTY_TITLE);
     if ($title) {
         $showTitle = true;
         $title = $title == 1 ? '' : $title;
     } else {
         $showTitle = false;
         $title = null;
     }
     $data = array(self::PROPERTY_PARENT => $this->getParent(false), self::PROPERTY_NODES => $this->getNodeIds(), self::PROPERTY_DEPTH => $this->getDepth(), self::PROPERTY_TITLE . '-show' => $showTitle, self::PROPERTY_TITLE => $title, self::PROPERTY_TEMPLATE => $this->getTemplate(static::TEMPLATE_NAMESPACE . '/default'));
     if ($data[self::PROPERTY_NODES]) {
         $data[self::PROPERTY_PARENT . '-select'] = self::PROPERTY_NODES;
     } else {
         $data[self::PROPERTY_PARENT . '-select'] = self::PROPERTY_PARENT;
     }
     $form = $this->createFormBuilder($data);
     $form->addRow(self::PROPERTY_PARENT . '-select', 'option', array('label' => $translator->translate('label.menu.base'), 'default' => 'parent', 'options' => array(self::PROPERTY_PARENT => $translator->translate('label.menu.parent'), self::PROPERTY_NODES => $translator->translate('label.menu.nodes')), 'attributes' => array('data-toggle-dependant' => 'option-node-select'), 'validators' => array('required' => array())));
     $form->addRow(self::PROPERTY_PARENT, 'select', array('label' => $translator->translate('label.menu.parent'), 'description' => $translator->translate('label.menu.parent.description'), 'options' => $nodeList, 'attributes' => array('class' => 'option-node-select option-node-select-' . self::PROPERTY_PARENT), 'hide-optional' => true));
     $form->addRow(self::PROPERTY_NODES, 'select', array('label' => $translator->translate('label.menu.nodes'), 'description' => $translator->translate('label.menu.nodes.description'), 'options' => $nodeList, 'attributes' => array('class' => 'option-node-select option-node-select-' . self::PROPERTY_NODES), 'multiple' => true, 'hide-optional' => true));
     $form->addRow(self::PROPERTY_DEPTH, 'select', array('label' => $translator->translate('label.menu.depth'), 'description' => $translator->translate('label.menu.depth.description'), 'options' => $depths));
     $form->addRow(self::PROPERTY_TITLE . '-show', 'option', array('label' => $translator->translate('label.title.show'), 'description' => $translator->translate('label.menu.title.show.description'), 'attributes' => array('data-toggle-dependant' => 'option-title')));
     $form->addRow(self::PROPERTY_TITLE, 'string', array('label' => $translator->translate('label.title'), 'description' => $translator->translate('label.menu.title.description'), 'attributes' => array('class' => 'option-title option-title-1')));
     $form->addRow(self::PROPERTY_TEMPLATE, 'select', array('label' => $translator->translate('label.template'), 'options' => $this->getAvailableTemplates(static::TEMPLATE_NAMESPACE), 'validators' => array('required' => array())));
     $requiredValidator = $validationFactory->createValidator('required', array());
     $urlRequired = new ConditionalConstraint();
     $urlRequired->addValueCondition(self::PROPERTY_PARENT . '-select', self::PROPERTY_PARENT);
     $urlRequired->addValidator($requiredValidator, self::PROPERTY_PARENT);
     $fileRequired = new ConditionalConstraint();
     $fileRequired->addValueCondition(self::PROPERTY_PARENT . '-select', self::PROPERTY_NODES);
     $fileRequired->addValidator($requiredValidator, self::PROPERTY_NODES);
     $form->addValidationConstraint($urlRequired);
     $form->addValidationConstraint($fileRequired);
     $form = $form->build();
     if ($form->isSubmitted()) {
         if ($this->request->getBodyParameter('cancel')) {
             return false;
         }
         try {
             $form->validate();
             $data = $form->getData();
             if ($data[self::PROPERTY_TITLE]) {
                 $title = $data[self::PROPERTY_TITLE];
             } elseif ($data[self::PROPERTY_TITLE . '-show']) {
                 $title = '1';
             } else {
                 $title = null;
             }
             if ($data[self::PROPERTY_PARENT . '-select'] == self::PROPERTY_PARENT) {
                 $this->properties->setWidgetProperty(self::PROPERTY_PARENT, $data[self::PROPERTY_PARENT]);
                 $this->properties->setWidgetProperty(self::PROPERTY_NODES);
             } else {
                 $this->properties->setWidgetProperty(self::PROPERTY_PARENT);
                 $this->properties->setWidgetProperty(self::PROPERTY_NODES, implode(',', $data[self::PROPERTY_NODES]));
             }
             $this->properties->setWidgetProperty(self::PROPERTY_DEPTH, $data[self::PROPERTY_DEPTH]);
             $this->properties->setWidgetProperty(self::PROPERTY_TITLE, $title);
             $this->setTemplate($data[self::PROPERTY_TEMPLATE]);
             return true;
         } catch (ValidationException $exception) {
             $this->setValidationException($exception, $form);
         }
     }
     $view = $this->setTemplateView(static::TEMPLATE_NAMESPACE . '/properties', array('form' => $form->getView()));
     $view->addJavascript('js/form.js');
     $form->processView($view);
     return false;
 }