Browse Source

Evolution #151: Evenements

bastien 13 years ago
parent
commit
687b52467f

+ 8 - 1
src/Muzich/CommentBundle/Controller/CommentController.php View File

@@ -4,6 +4,7 @@ namespace Muzich\CommentBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Muzich\CoreBundle\Managers\CommentsManager;
7
+use Muzich\CoreBundle\Propagator\EventElementComment;
7 8
 
8 9
 class CommentController extends Controller
9 10
 {
@@ -54,11 +55,17 @@ class CommentController extends Controller
54 55
       )));
55 56
     }
56 57
     
57
-    
58 58
     // On met a jour les commentaires
59 59
     $cm = new CommentsManager($element->getComments());
60 60
     $cm->add($this->getUser(), $comment);
61 61
     $element->setComments($cm->get());
62
+    $event = new EventElementComment($this->container);
63
+    
64
+    // Event pour user d'un nouveau comment
65
+    if ($this->getUserId() != $element->getOwner()->getId())
66
+    {
67
+      $event->commentAdded($element);
68
+    }
62 69
 
63 70
     $this->getDoctrine()->getEntityManager()->persist($element);
64 71
     $this->getDoctrine()->getEntityManager()->flush();

+ 41 - 0
src/Muzich/CoreBundle/Actions/User/Event.php View File

@@ -0,0 +1,41 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Actions\User;
4
+
5
+use Muzich\CoreBundle\Entity\User;
6
+use Muzich\CoreBundle\Entity\Event as EventEntity;
7
+
8
+/**
9
+ * Description of Reputation
10
+ *
11
+ * @author bux
12
+ */
13
+class Event
14
+{
15
+  
16
+  /**
17
+   *
18
+   * @var User 
19
+   */
20
+  protected $user;
21
+  protected $event;
22
+  
23
+  public function __construct(User $user, EventEntity $event)
24
+  {
25
+    $this->user = $user;
26
+    $this->event = $event;
27
+  }
28
+  
29
+  public function updateEvent($element_id)
30
+  {
31
+    $this->event->addId($element_id);
32
+  }
33
+  
34
+  public function createEvent($type, $element_id)
35
+  {
36
+    $this->event->addId($element_id);
37
+    $this->event->setType($type);
38
+    $this->event->setView(false);
39
+    $this->event->setUser($this->user);
40
+  }
41
+}

+ 189 - 0
src/Muzich/CoreBundle/Entity/Event.php View File

@@ -0,0 +1,189 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Entity;
4
+
5
+use Doctrine\ORM\Mapping as ORM;
6
+use \Doctrine\Common\Collections\ArrayCollection;
7
+use Gedmo\Mapping\Annotation as Gedmo;
8
+
9
+/**
10
+ * L'Element est l'Element central de l'application. C'est cet
11
+ * entité qui stocke le media partagé sur le réseau.
12
+ * 
13
+ * @ORM\Entity
14
+ * @ORM\Table(name="event")
15
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\EventRepository")
16
+ * 
17
+ */
18
+class Event
19
+{
20
+  
21
+  const TYPE_COMMENT_ADDED_OWNED_ELEMENT = "com.add.ow.e";
22
+  
23
+  /**
24
+   * @ORM\Id
25
+   * @ORM\Column(type="integer")
26
+   * @ORM\GeneratedValue(strategy="AUTO")
27
+   * @var type int
28
+   */
29
+  protected $id;
30
+  
31
+  /**
32
+   * array json des id d'éléments/users/...
33
+   * 
34
+   * @ORM\Column(type="text", nullable=true)
35
+   * @var string 
36
+   */
37
+  protected $ids;
38
+  
39
+  /**
40
+   * Compteur d'élément/users concernés
41
+   * 
42
+   * @ORM\Column(type="integer", nullable=true)
43
+   * @var int 
44
+   */
45
+  protected $count;
46
+  
47
+  /**
48
+   * Type (constitué de la valeur d'une des
49
+   * 
50
+   * @ORM\Column(type = "string", length = 12)
51
+   * @var type string
52
+   */
53
+  protected $type;
54
+  
55
+  /**
56
+   * Propriétaire de l'event
57
+   * 
58
+   * @ORM\ManyToOne(targetEntity="User", inversedBy="events")
59
+   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
60
+   */
61
+  protected $user;
62
+  
63
+  /**
64
+   * Si $view est a vrai, cela se traduit par le fait que l'user a vue l'événement
65
+   * ou qu'il l'a supprimé.
66
+   * 
67
+   * @ORM\Column(type="boolean")
68
+   * @var type string
69
+   */
70
+  protected $view;
71
+  
72
+  /*
73
+   * 
74
+   * getters / setters
75
+   * 
76
+   */
77
+  
78
+  public function getId()
79
+  {
80
+    return $this->id;
81
+  }
82
+  
83
+  public function getIds()
84
+  {
85
+    return json_decode($this->ids, true);
86
+  }
87
+  
88
+  public function setIds($ids)
89
+  {
90
+    $this->ids = json_encode($ids);
91
+  }
92
+  
93
+  public function getCount()
94
+  {
95
+    return $this->count;
96
+  }
97
+  
98
+  public function setCount($count)
99
+  {
100
+    $this->count = $count;
101
+  }
102
+  
103
+  public function getType()
104
+  {
105
+    return $this->type;
106
+  }
107
+  
108
+  public function setType($type)
109
+  {
110
+    $this->type = $type;
111
+  }
112
+  
113
+  public function getUser()
114
+  {
115
+    return $this->user;
116
+  }
117
+  
118
+  public function setUser($user)
119
+  {
120
+    $this->user = $user;
121
+  }
122
+  
123
+  public function getView()
124
+  {
125
+    return $this->view;
126
+  }
127
+  
128
+  public function setView($view)
129
+  {
130
+    $this->view = $view;
131
+  }
132
+  
133
+  /*
134
+   * other methods
135
+   * 
136
+   */
137
+  
138
+  public function addId($id)
139
+  {
140
+    $ids = $this->getIds();
141
+    if (!count($ids))
142
+    {
143
+      $ids = array();
144
+    }
145
+    
146
+    if (!$this->hasId($id))
147
+    {
148
+      $ids[] = (string)$id;
149
+      $this->setCount($this->getCount()+1);
150
+    }
151
+    $this->setIds($ids);
152
+  }
153
+  
154
+  public function removeVoteGood($id_remove)
155
+  {
156
+    if (count($ids = $this->getIds()))
157
+    {
158
+      $ids_n = array();
159
+      foreach ($ids as $id)
160
+      {
161
+        if ($id != $id_remove)
162
+        {
163
+          $ids_n[] = (string)$id;
164
+        }
165
+        else
166
+        {
167
+          $this->setCount($this->getCount()-1);
168
+        }
169
+      }
170
+      $this->setIds($ids_n);
171
+    }
172
+  }
173
+  
174
+  public function hasId($id_check)
175
+  {
176
+    if (count($ids = $this->getIds()))
177
+    {
178
+      foreach ($ids as $id)
179
+      {
180
+        if ($id == $id_check)
181
+        {
182
+          return true;
183
+        }
184
+      }
185
+    }
186
+    return false;
187
+  }
188
+  
189
+}

+ 17 - 0
src/Muzich/CoreBundle/Entity/User.php View File

@@ -127,6 +127,13 @@ class User extends BaseUser
127 127
   protected $reputation;
128 128
   
129 129
   /**
130
+   * Liste des Events appartenant a cet utilisateur.
131
+   * 
132
+   * @ORM\OneToMany(targetEntity="Event", mappedBy="user")
133
+   */
134
+  protected $events;
135
+  
136
+  /**
130 137
    * 
131 138
    */
132 139
   public function __construct()
@@ -386,6 +393,16 @@ class User extends BaseUser
386 393
     return $this->reputation;
387 394
   }
388 395
   
396
+  public function getEvents()
397
+  {
398
+    return $this->events;
399
+  }
400
+  
401
+  public function setEvents($events)
402
+  {
403
+    $this->events = $events;
404
+  }
405
+  
389 406
   /*
390 407
    * 
391 408
    * 

+ 57 - 0
src/Muzich/CoreBundle/Propagator/EventElementComment.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Propagator;
4
+
5
+use Muzich\CoreBundle\Propagator\EventPropagator;
6
+use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8
+use Muzich\CoreBundle\Entity\Event;
9
+
10
+/**
11
+ * Description of EventElementScore
12
+ *
13
+ * @author bux
14
+ */
15
+class EventElementComment extends EventPropagator
16
+{  
17
+  
18
+  public function commentAdded(Element $element)
19
+  {    
20
+    $em = $this->container->get('doctrine')->getEntityManager();
21
+    
22
+    try
23
+    {
24
+      $event = $em->createQuery(
25
+        'SELECT e FROM MuzichCoreBundle:Event e
26
+        WHERE e.user = :uid AND e.type = :type AND 
27
+          (e.view = \'FALSE\' OR e.view = \'0\')'
28
+      )->setParameters(array(
29
+        'uid' => $element->getOwner()->getId(),
30
+        'type' => Event::TYPE_COMMENT_ADDED_OWNED_ELEMENT
31
+      ))->getSingleResult()
32
+      ;
33
+      $new = false;
34
+    } 
35
+    catch (\Doctrine\ORM\NoResultException $e)
36
+    {
37
+      $event = new Event();
38
+      $new = true;
39
+    }
40
+    
41
+    $uea = new UserEventAction($element->getOwner(), $event);
42
+    if ($new)
43
+    {
44
+      $uea->createEvent(
45
+        Event::TYPE_COMMENT_ADDED_OWNED_ELEMENT,
46
+        $element->getId()
47
+      );
48
+    }
49
+    else
50
+    {
51
+      $uea->updateEvent($element->getId());
52
+    }
53
+    
54
+    $em->persist($event);
55
+  }
56
+  
57
+}

+ 1 - 1
src/Muzich/CoreBundle/Propagator/EventElementScore.php View File

@@ -43,4 +43,4 @@ class EventElementScore extends EventPropagator
43 43
     );
44 44
   }
45 45
   
46
-}
46
+}

+ 10 - 0
src/Muzich/CoreBundle/Repository/EventRepository.php View File

@@ -0,0 +1,10 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Repository;
4
+
5
+use Doctrine\ORM\EntityRepository;
6
+
7
+class ElementRepository extends EntityRepository
8
+{
9
+    
10
+}