Explorar el Código

Création de l'usine a Element, et mise ne place de l'ajout d'element.

bastien hace 12 años
padre
commit
8ac9c8bd6d

+ 48 - 0
src/Muzich/CoreBundle/Controller/CoreController.php Ver fichero

@@ -7,6 +7,9 @@ use Muzich\CoreBundle\lib\Controller;
7 7
 use Muzich\CoreBundle\Entity\FollowUser;
8 8
 use Muzich\CoreBundle\Entity\FollowGroup;
9 9
 //use Doctrine\ORM\Query;
10
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
11
+use Muzich\CoreBundle\ElementFactory\ElementFactory;
12
+use Muzich\CoreBundle\Entity\Element;
10 13
 
11 14
 class CoreController extends Controller
12 15
 {
@@ -73,4 +76,49 @@ class CoreController extends Controller
73 76
     }
74 77
   }
75 78
   
79
+  public function elementAddAction()
80
+  {
81
+    $user = $this->getUser();
82
+    $em = $this->getDoctrine()->getEntityManager();
83
+    
84
+    $form = $this->createForm(
85
+      new ElementAddForm(),
86
+      array(),
87
+      array('tags' => $this->getTagsArray())
88
+    );
89
+    
90
+    if ($this->getRequest()->getMethod() == 'POST')
91
+    {
92
+      $form->bindRequest($this->getRequest());
93
+      if ($form->isValid())
94
+      {
95
+        $data = $form->getData();
96
+        $element = new Element();
97
+        
98
+        $factory = new ElementFactory($element, $em);
99
+        $factory->proceed($data, $user);
100
+        
101
+        $em->persist($element);
102
+        $em->flush();
103
+      }
104
+      
105
+    }
106
+    
107
+    if ($this->getRequest()->isXmlHttpRequest())
108
+    {
109
+      
110
+    }
111
+    else
112
+    {
113
+      return $this->redirect($this->generateUrl('home'));
114
+    }
115
+    
116
+  }
117
+  
118
+//  protected function proceedElement(Element $element)
119
+//  {
120
+//    $factory = new ElementFactory();
121
+//    $factory->proceed($element, $form->getData());
122
+//  }
123
+  
76 124
 }

+ 96 - 0
src/Muzich/CoreBundle/ElementFactory/ElementFactory.php Ver fichero

@@ -0,0 +1,96 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\ElementFactory;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+use Muzich\CoreBundle\Entity\User;
7
+use Doctrine\ORM\EntityManager;
8
+
9
+/**
10
+ * 
11
+ *
12
+ * @author bux
13
+ */
14
+class ElementFactory
15
+{
16
+  
17
+  const TYPE_UNKNOW = 'unknow';
18
+  
19
+  protected $types = array(
20
+    'youtube', 'soundclound', 'son2teuf', 'jamendo'
21
+  );
22
+  
23
+  protected $em;
24
+  protected $element;
25
+  
26
+  /**
27
+   * Procédure chargé de retourner des information destiné au 
28
+   * formulaire d'ajout d'un element.
29
+   * 
30
+   * @param string $url
31
+   * @return array
32
+   */
33
+  public static function collect($url)
34
+  {
35
+    
36
+    return array(
37
+      'name' => null,
38
+      'tags' => array()
39
+    );
40
+  }
41
+  
42
+  public function __construct(Element $element, EntityManager $em)
43
+  {
44
+    $this->element = $element;
45
+    $this->em = $em;
46
+    
47
+    $evm = new \Doctrine\Common\EventManager();
48
+    $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
49
+    $evm->addEventSubscriber($timestampableListener);
50
+    
51
+    $this->em->getEventManager()->addEventSubscriber($timestampableListener);
52
+  }
53
+  
54
+  /**
55
+   * Procédure chargé de construire le contenu de l'élément.
56
+   *  nom, Code d'embed, [...]
57
+   * 
58
+   * @param Element $element
59
+   * @param array $params
60
+   * @param User $owner
61
+   * @return Element 
62
+   */
63
+  public function proceed($params, User $owner)
64
+  {
65
+    $this->element->setName($params['name']);
66
+    $this->element->setUrl($params['url']);
67
+    $this->element->setOwner($owner);
68
+    $this->element->setTagsWithIds($this->em, $params['tags']);
69
+    $this->determineType();
70
+    $this->proceedExtraFields();
71
+  }
72
+  
73
+  /**
74
+   * Determine le type d'objet auquel on a affaire.
75
+   */
76
+  protected function determineType()
77
+  {
78
+    $this->element->setType(null);
79
+  }
80
+  
81
+  /**
82
+   * Construction des autres champs tel que embed.
83
+   * 
84
+   * 
85
+   */
86
+  protected function proceedExtraFields()
87
+  {
88
+   
89
+    // Instanciation d'un objet factory correspondant au type, par exemple
90
+    // YoutubeFactory, qui répondant a une implementation retournera ces infos.
91
+  
92
+  }
93
+    
94
+}
95
+
96
+?>

+ 28 - 3
src/Muzich/CoreBundle/Entity/Element.php Ver fichero

@@ -5,7 +5,7 @@ namespace Muzich\CoreBundle\Entity;
5 5
 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 9
 
10 10
 /**
11 11
  * L'Element est l'Element central de l'application. C'est cet
@@ -84,6 +84,14 @@ class Element
84 84
   protected $name;
85 85
   
86 86
   /**
87
+   * Code d'embed
88
+   * 
89
+   * @ORM\Column(type="text", nullable=true)
90
+   * @var type string
91
+   */
92
+  protected $embed;
93
+  
94
+  /**
87 95
    * @var datetime $created
88 96
    *
89 97
    * @Gedmo\Timestampable(on="create")
@@ -155,7 +163,7 @@ class Element
155 163
    *
156 164
    * @param ElementType $type
157 165
    */
158
-  public function setType(ElementType $type)
166
+  public function setType(ElementType $type = null)
159 167
   {
160 168
     $this->type = $type;
161 169
   }
@@ -171,9 +179,10 @@ class Element
171 179
   }
172 180
   
173 181
   
174
-  public function __construct()
182
+  public function __construct($url = null)
175 183
   {
176 184
     $this->tags = new ArrayCollection();
185
+    $this->url = $url;
177 186
   }
178 187
   
179 188
   /**
@@ -296,4 +305,20 @@ class Element
296 305
   {
297 306
       return $this->updated;
298 307
   }
308
+  
309
+  /**
310
+   * Ajoute des relation vers des tags.
311
+   * 
312
+   * @param array $ids 
313
+   */
314
+  public function setTagsWithIds(EntityManager $em, $ids)
315
+  {
316
+    $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
317
+
318
+    // Pour les nouveaux ids restants
319
+    foreach ($tags as $tag)
320
+    {      
321
+      $this->addTag($tag);
322
+    }
323
+  }
299 324
 }

+ 41 - 0
src/Muzich/CoreBundle/Form/Element/ElementAddForm.php Ver fichero

@@ -0,0 +1,41 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\Element;
4
+
5
+use Symfony\Component\Form\AbstractType;
6
+use Symfony\Component\Form\FormBuilder;
7
+
8
+class ElementAddForm extends AbstractType
9
+{
10
+  public function buildForm(FormBuilder $builder, array $options)
11
+  {
12
+    $builder->add('name', 'text', array(
13
+      'required' => true,
14
+    ));
15
+    
16
+    $builder->add('url', 'text', array(
17
+      'required' => true,
18
+    ));
19
+        
20
+    $builder->add('tags', 'choice', array(
21
+      'choices'           => $options['tags'],
22
+      'expanded'          => true,
23
+      'multiple'          => true
24
+    ));
25
+  }
26
+
27
+  public function getName()
28
+  {
29
+    return 'element_add';
30
+  }
31
+  
32
+  public function getDefaultOptions(array $options)
33
+  {
34
+    return array(
35
+      'name' => '',
36
+      'url' => '',
37
+      'tags' => array(),
38
+      'data_class' => 'Muzich\CoreBundle\Entity\Element'
39
+    );
40
+  }
41
+}

+ 1 - 1
src/Muzich/CoreBundle/Repository/ElementRepository.php Ver fichero

@@ -78,7 +78,7 @@ class ElementRepository extends EntityRepository
78 78
     $query_string = "SELECT e, et, t2, eu, g
79 79
       FROM MuzichCoreBundle:Element e 
80 80
       LEFT JOIN e.group g 
81
-      JOIN e.type et 
81
+      LEFT JOIN e.type et 
82 82
       LEFT JOIN e.tags t 
83 83
       LEFT JOIN e.tags t2 
84 84
       JOIN e.owner eu $join_personal

+ 8 - 5
src/Muzich/CoreBundle/Resources/config/routing.yml Ver fichero

@@ -1,11 +1,14 @@
1 1
 
2 2
 
3 3
 search_elements:
4
-   pattern:  /search-elements
5
-   defaults: { _controller: MuzichCoreBundle:Search:searchElements }
4
+  pattern:  /search-elements
5
+  defaults: { _controller: MuzichCoreBundle:Search:searchElements }
6 6
    
7 7
 follow:
8
-   pattern:  /follow/{type}/{id}/{token}
9
-   defaults: { _controller: MuzichCoreBundle:Core:follow }
8
+  pattern:  /follow/{type}/{id}/{token}
9
+  defaults: { _controller: MuzichCoreBundle:Core:follow }
10 10
    
11
-     
11
+element_add:
12
+  pattern:  /element/add
13
+  defaults: { _controller: MuzichCoreBundle:Core:elementAdd }
14
+  

+ 9 - 1
src/Muzich/HomeBundle/Controller/HomeController.php Ver fichero

@@ -7,6 +7,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 
8 8
 use Doctrine\ORM\Query;
9 9
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
10
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
10 11
 
11 12
 class HomeController extends Controller
12 13
 {
@@ -21,10 +22,17 @@ class HomeController extends Controller
21 22
     $search_form = $this->createForm(
22 23
       new ElementSearchForm(), 
23 24
       $search_object->getParams(),
24
-      array('tags' => $this->getTagsArray())
25
+      array('tags' => $tags = $this->getTagsArray())
26
+    );
27
+    
28
+    $add_form = $this->createForm(
29
+      new ElementAddForm(),
30
+      array(),
31
+      array('tags' => $tags)
25 32
     );
26 33
         
27 34
     return array(
35
+      'add_form' => $add_form->createView(),
28 36
       'search_object' => $search_object,
29 37
       'search_form'   => $search_form->createView()
30 38
     );

+ 14 - 0
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig Ver fichero

@@ -4,6 +4,20 @@
4 4
 
5 5
 {% block content %}
6 6
 
7
+  <form action="{{ path('element_add') }}" method="post" {{ form_enctype(add_form) }}>
8
+    {{ form_errors(add_form) }}
9
+
10
+    {{ form_row(add_form.name) }}
11
+      
12
+    {{ form_row(add_form.url) }}
13
+      
14
+    {{ form_row(add_form.tags) }}
15
+
16
+    {{ form_rest(add_form) }}
17
+
18
+    <input type="submit" />
19
+  </form>
20
+
7 21
   {% include "MuzichCoreBundle:SearchElement:form.html.twig" %}
8 22
 
9 23
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}