Esempio n. 1
0
 public function buildFormElement(\Symfony\Bundle\FrameworkBundle\Controller\Controller $controller, \Symfony\Component\Form\FormBuilder $builder, \Symforce\AdminBundle\Compiler\Cache\AdminCache $admin, \Symforce\AdminBundle\Compiler\Cache\ActionCache $action, $object, $property_name, $parent_property)
 {
     if ($object) {
         if (!$object instanceof $admin->class_name) {
             throw new \Exception();
         }
         if ($admin->workflow) {
             if ($admin !== $this) {
                 throw new \Exception();
             }
             $status = $admin->getObjectWorkflowStatus($object);
             if (!isset($status['properties'][$property_name])) {
                 return;
             }
             $flag = $status['properties'][$property_name];
             $readable = \Symforce\AdminBundle\Compiler\MetaType\Admin\Workflow::FLAG_VIEW & $flag;
             $editable = \Symforce\AdminBundle\Compiler\MetaType\Admin\Workflow::FLAG_EDIT & $flag;
             if (\Symforce\AdminBundle\Compiler\MetaType\Admin\Workflow::FLAG_AUTH & $flag) {
                 if ($readable || $editable) {
                     throw new \Exception();
                 }
                 $securityContext = $this->container->get('security.context');
                 $user = $securityContext->getToken()->getUser();
                 $group = $user->getUserGroup();
                 if ($group) {
                     $flag = $group->getWorkflowPropertyVisiable($this->name, $property_name, $status['name']);
                     if ('1' === $flag) {
                         $editable = true;
                     } else {
                         if ('2' === $flag) {
                             $readable = true;
                         }
                     }
                 }
             }
             if ($readable) {
                 $options = array();
                 $_options = $admin->getFormBuilderOption($property_name, $action, $object);
                 if ($_options) {
                     $options['label'] = $_options['label'];
                 }
                 $builder->add($property_name, 'sf_view', $options);
                 return;
             }
             if (!$editable) {
                 return;
             }
         }
     }
     $options = $admin->getFormBuilderOption($property_name, $action, $object);
     $type = $options['sf_form_type'];
     if (isset($options['read_only']) && $options['read_only']) {
         if (in_array($type, array('sf_owner', 'sf_entity', 'sf_workflow', 'choice', 'checkbox', 'sf_file', 'sf_image', 'sf_html', 'money'))) {
             $options = array();
             $_options = $admin->getFormBuilderOption($property_name, $action, $object);
             if ($_options) {
                 $options['label'] = $_options['label'];
             }
             $builder->add($property_name, 'sf_view', $options);
             return;
         }
     }
     if (isset($options['required']) && $options['required']) {
         if (!isset($options['constraints'])) {
             $options['constraints'] = array(new \Symfony\Component\Validator\Constraints\NotBlank());
         } else {
             $find_not_blank = false;
             foreach ($options['constraints'] as $_constraint) {
                 if ($_constraint instanceof \Symfony\Component\Validator\Constraints\NotBlank) {
                     $find_not_blank = true;
                 }
             }
             if (!$find_not_blank) {
                 $options['constraints'][] = new \Symfony\Component\Validator\Constraints\NotBlank();
             }
         }
     }
     /**
      * @FIXME since symfony 2.7, without this hack radio(value=0) will not get checked
      */
     if ('bool' == $options['sf_form_meta']) {
         if ($object) {
             $options['data'] = $admin->getReflectionProperty($property_name)->getValue($object) ? 1 : 0;
         } else {
             $options['data'] = 0;
         }
     }
     $this->adjustFormOptions($object, $property_name, $options);
     $subscribers = null;
     if (isset($options['subscribers'])) {
         $subscribers = $options['subscribers'];
         unset($options['subscribers']);
     }
     $builder->add($property_name, $type, $options);
     if ($subscribers) {
         $_builder = $builder->get($property_name);
         foreach ($subscribers as $subscriber) {
             $events = $subscriber->getSubscribedEvents();
             foreach ($events as $_event => $method) {
                 $_builder->addEventListener($_event, array($subscriber, $method));
             }
         }
     }
 }