Browse Source

Evolution #28: Forms: Contraintes de validités personalisés

bastien 13 years ago
parent
commit
5e0d41c8b7

+ 22 - 2
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -11,6 +11,7 @@ use Muzich\CoreBundle\Form\Element\ElementAddForm;
11 11
 use Muzich\CoreBundle\ElementFactory\ElementManager;
12 12
 use Muzich\CoreBundle\Entity\Element;
13 13
 use Symfony\Component\HttpFoundation\RedirectResponse;
14
+use Muzich\CoreBundle\Form\Search\ElementSearchForm;
14 15
 
15 16
 class CoreController extends Controller
16 17
 {
@@ -137,7 +138,6 @@ class CoreController extends Controller
137 138
       $form->bindRequest($this->getRequest());
138 139
       if ($form->isValid())
139 140
       {
140
-
141 141
         // On utilise le gestionnaire d'élément
142 142
         $factory = new ElementManager($element, $em, $this->container);
143 143
         $factory->proceedFill($user);
@@ -183,7 +183,27 @@ class CoreController extends Controller
183 183
         else
184 184
         {
185 185
           $this->setFlash('error', 'element.add.error');
186
-          return $this->redirect($redirect_url);
186
+          
187
+          $search_object = $this->getElementSearcher();
188
+          $user = $this->getUser(true, array('join' => array(
189
+            'groups_owned'
190
+          )), true);
191
+
192
+          $search_form = $this->createForm(
193
+            new ElementSearchForm(), 
194
+            $search_object->getParams(),
195
+            array(
196
+              'tags' => $tags = $this->getTagsArray()
197
+            )
198
+          );
199
+
200
+          return $this->render('MuzichHomeBundle:Home:index.html.twig', array(
201
+            'user'        => $this->getUser(),
202
+            'add_form'    => $form->createView(),
203
+            'search_form' => $search_form->createView(),
204
+            'elements'    => $search_object->getElements($this->getDoctrine(), $this->getUserId())
205
+          ));
206
+          
187 207
         }
188 208
         
189 209
       }

+ 3 - 0
src/Muzich/CoreBundle/Entity/Element.php View File

@@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM;
6 6
 use \Doctrine\Common\Collections\ArrayCollection;
7 7
 use Gedmo\Mapping\Annotation as Gedmo;
8 8
 use Doctrine\ORM\EntityManager;
9
+use Muzich\CoreBundle\Validator as MuzichAssert;
9 10
 
10 11
 /**
11 12
  * L'Element est l'Element central de l'application. C'est cet
@@ -40,6 +41,7 @@ class Element
40 41
    * 
41 42
    * @ORM\ManyToMany(targetEntity="Tag", inversedBy="elements")
42 43
    * @ORM\JoinTable(name="elements_tag")
44
+   * @MuzichAssert\Tags()
43 45
    */
44 46
   private $tags;
45 47
 
@@ -56,6 +58,7 @@ class Element
56 58
    * 
57 59
    * @ORM\ManyToOne(targetEntity="Group", inversedBy="elements")
58 60
    * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
61
+   * @MuzichAssert\GroupOwnedOrPublic()
59 62
    */
60 63
   protected $group = null;
61 64
   

+ 30 - 0
src/Muzich/CoreBundle/Validator/GroupOwnedOrPublic.php View File

@@ -0,0 +1,30 @@
1
+<?php
2
+// AlwaysFail.php
3
+namespace Muzich\CoreBundle\Validator;
4
+
5
+use Symfony\Component\Validator\Constraint;
6
+
7
+/**
8
+ * @Annotation
9
+ */
10
+class GroupOwnedOrPublic extends Constraint
11
+{
12
+  public $message = 'Le groupe est mal choisis';
13
+  public $entity;
14
+  public $property;
15
+
16
+  public function validatedBy()
17
+  {
18
+      return 'validator.groupownedorpublic';
19
+  }
20
+
21
+  public function requiredOptions()
22
+  {
23
+      return array('entity', 'property');
24
+  }
25
+  
26
+  public function getTargets()
27
+  {
28
+    return Constraint::PROPERTY_CONSTRAINT;
29
+  }
30
+}

+ 54 - 0
src/Muzich/CoreBundle/Validator/GroupOwnedOrPublicValidator.php View File

@@ -0,0 +1,54 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Validator;
4
+
5
+use Symfony\Component\Validator\ConstraintValidator;
6
+use Symfony\Component\Validator\Constraint;
7
+use Doctrine\ORM\EntityManager;
8
+use Symfony\Component\Security\Core\SecurityContext;
9
+
10
+class GroupOwnedOrPublicValidator extends ConstraintValidator
11
+{
12
+  
13
+  private $entityManager;
14
+  private $security_context;
15
+
16
+  public function __construct(EntityManager $entityManager, SecurityContext $security_context)
17
+  {
18
+    $this->entityManager = $entityManager;
19
+    $this->security_context = $security_context;
20
+  }
21
+  
22
+  /**
23
+   * Ce n'est valide que si
24
+   *  * Le groupe est open
25
+   *  * Le groupe appartient a l'user
26
+   *  * On a pas précisé de groupe
27
+   * 
28
+   * @param int $value
29
+   * @param Constraint $constraint
30
+   * @return boolean
31
+   */
32
+  public function isValid($value, Constraint $constraint)
33
+  {
34
+    if ($value)
35
+    {
36
+      $user = $user = $this->security_context->getToken()->getUser();
37
+      $group = $this->entityManager->getRepository('MuzichCoreBundle:Group')
38
+        ->findOneById($value)
39
+      ;
40
+      if (!$group)
41
+      {
42
+        $this->setMessage('Le groupe est invalide');
43
+        return false;
44
+      }
45
+      
46
+      if (!$group->userCanAddElement($user))
47
+      {
48
+        $this->setMessage('Le groupe est invalide');
49
+        return false;
50
+      }
51
+    }
52
+    return true;
53
+  }
54
+}

+ 30 - 0
src/Muzich/CoreBundle/Validator/Tags.php View File

@@ -0,0 +1,30 @@
1
+<?php
2
+// AlwaysFail.php
3
+namespace Muzich\CoreBundle\Validator;
4
+
5
+use Symfony\Component\Validator\Constraint;
6
+
7
+/**
8
+ * @Annotation
9
+ */
10
+class Tags extends Constraint
11
+{
12
+  public $message = 'Les tags sont mal configurés';
13
+  public $entity;
14
+  public $property;
15
+
16
+  public function validatedBy()
17
+  {
18
+      return 'validator.tags';
19
+  }
20
+
21
+  public function requiredOptions()
22
+  {
23
+      return array('entity', 'property');
24
+  }
25
+  
26
+  public function getTargets()
27
+  {
28
+    return Constraint::PROPERTY_CONSTRAINT;
29
+  }
30
+}

+ 42 - 0
src/Muzich/CoreBundle/Validator/TagsValidator.php View File

@@ -0,0 +1,42 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Validator;
4
+
5
+use Symfony\Component\Validator\ConstraintValidator;
6
+use Symfony\Component\Validator\Constraint;
7
+use Doctrine\ORM\EntityManager;
8
+
9
+class TagsValidator extends ConstraintValidator
10
+{
11
+  
12
+  private $entityManager;
13
+
14
+  public function __construct(EntityManager $entityManager)
15
+  {
16
+    $this->entityManager = $entityManager;
17
+  }
18
+  
19
+  public function isValid($value, Constraint $constraint)
20
+  {
21
+    if (array_diff($value, array_unique($value)))
22
+    {
23
+      $this->setMessage('Tags saisies incorrects');
24
+      return false;
25
+    }
26
+    
27
+    $count = $this->entityManager
28
+      ->createQuery("SELECT COUNT(t)
29
+        FROM MuzichCoreBundle:Tag t
30
+        WHERE t IN (:tids)")
31
+      ->setParameter('tids', $value)
32
+    ->getSingleScalarResult();
33
+    
34
+    if ($count != count ($value))
35
+    {
36
+      $this->setMessage('Tags saisies incorrects');
37
+      return false;
38
+    }
39
+    
40
+    return true;
41
+  }
42
+}