Browse Source

Evolution #143: Commentaires sur un élément

bastien 13 years ago
parent
commit
78e6b8ffac

+ 4 - 0
app/Resources/translations/elements.fr.yml View File

32
     thereare:           Afficher les <strong>%count%</strong> commentaires
32
     thereare:           Afficher les <strong>%count%</strong> commentaires
33
     hideis:             Cacher le commentaire
33
     hideis:             Cacher le commentaire
34
     hideare:            Cacher les commentaires
34
     hideare:            Cacher les commentaires
35
+    add_submit:         Commenter
36
+    add_cancel:         Annuler
37
+    add_error:
38
+      min:              Le commentaire doit être de %limit% caractéres minimum
35
   
39
   
36
 elements:
40
 elements:
37
   ajax:
41
   ajax:

+ 6 - 0
app/config/routing.yml View File

63
   resource: "@MuzichFavoriteBundle/Resources/config/routing.yml"
63
   resource: "@MuzichFavoriteBundle/Resources/config/routing.yml"
64
   prefix:   /{_locale}/
64
   prefix:   /{_locale}/
65
   defaults:
65
   defaults:
66
+    _locale: en|fr
67
+  
68
+MuzichCommentBundle:
69
+  resource: "@MuzichCommentBundle/Resources/config/routing.yml"
70
+  prefix:   /{_locale}/
71
+  defaults:
66
     _locale: en|fr
72
     _locale: en|fr

+ 55 - 0
src/Muzich/CommentBundle/Controller/CommentController.php View File

3
 namespace Muzich\CommentBundle\Controller;
3
 namespace Muzich\CommentBundle\Controller;
4
 
4
 
5
 use Muzich\CoreBundle\lib\Controller;
5
 use Muzich\CoreBundle\lib\Controller;
6
+use Muzich\CoreBundle\Managers\CommentsManager;
6
 
7
 
7
 class CommentController extends Controller
8
 class CommentController extends Controller
8
 {
9
 {
9
   
10
   
11
+  public function addAction($element_id)
12
+  {
13
+    if (($response = $this->mustBeConnected(true)))
14
+    {
15
+      return $response;
16
+    }
17
+    
18
+    if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
19
+      ->findOneById($element_id)))
20
+    {
21
+      throw $this->createNotFoundException('Not found');
22
+    }
23
+        
24
+    // TODO: Faire un objet pour le formulaire ajout de commentaire
25
+    if (
26
+      strlen((($comment = $this->getRequest()->request->get('comment'))))
27
+      >= $this->container->getParameter('comment_add_min_length')
28
+    )
29
+    {
30
+      
31
+      // On met a jour les commentaires
32
+      $cm = new CommentsManager($element->getComments());
33
+      $cm->add($this->getUser(), $comment);
34
+      $element->setComments($cm->get());
35
+      
36
+      $this->getDoctrine()->getEntityManager()->persist($element);
37
+      $this->getDoctrine()->getEntityManager()->flush();
38
+      
39
+      // On récupère le html du li avec le comment pour la réponse
40
+      $html = $this->render('MuzichCommentBundle:Comment:comment.html.twig', array(
41
+        'comment'     => $cm->getLast()
42
+      ))->getContent();
43
+      
44
+      return $this->jsonResponse(array(
45
+        'status' => 'success',
46
+        'html'   => $html
47
+      ));
48
+      
49
+    }
50
+    else
51
+    {
52
+      return $this->jsonResponse(array(
53
+        'status' => 'error',
54
+        'errors' => array($this->trans(
55
+          'element.comments.add_error.min', 
56
+          array(
57
+            '%limit%' => $this->container->getParameter('comment_add_min_length')
58
+          ), 
59
+          'elements'
60
+        )
61
+      )));
62
+    }
63
+    
64
+  }
10
   
65
   
11
 }
66
 }

+ 4 - 0
src/Muzich/CommentBundle/Resources/config/routing.yml View File

1
+
2
+ajax_add_comment:
3
+  pattern: /ajax/comment/add/{element_id}
4
+  defaults: { _controller: MuzichCommentBundle:Comment:add }

+ 5 - 0
src/Muzich/CommentBundle/Resources/views/Comment/comment.html.twig View File

1
+<li class="comment">
2
+  <a href="{{ path('show_user', {'slug': comment.u.s}) }}" >{{ comment.u.n }}</a>: 
3
+  {{ comment.c }}
4
+  <span class="datesince">({{ comment.d|date_or_relative_date }})</span>
5
+</li>

+ 1 - 1
src/Muzich/CoreBundle/Controller/ElementController.php View File

33
    * 
33
    * 
34
    */
34
    */
35
   public function editAction($element_id)
35
   public function editAction($element_id)
36
-  {    
36
+  {
37
     if (($response = $this->mustBeConnected()))
37
     if (($response = $this->mustBeConnected()))
38
     {
38
     {
39
       return $response;
39
       return $response;

+ 1 - 1
src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementData.php View File

200
     
200
     
201
     $cm = new CommentsManager();
201
     $cm = new CommentsManager();
202
     $cm->add($joelle, "J'aime bien quand ça tape. Ca rapelle ".
202
     $cm->add($joelle, "J'aime bien quand ça tape. Ca rapelle ".
203
-      "le grincement sinistre des volets de vieilles".
203
+      "le grincement sinistre des volets de vieilles ".
204
       "maisons. D'ailleur j'ai repeint mon mur des shiots !", $this->dateD(180));
204
       "maisons. D'ailleur j'ai repeint mon mur des shiots !", $this->dateD(180));
205
     
205
     
206
     $this->createElement('azyd_azylum_1', 'AZYD AZYLUM Live au Café Provisoire', 
206
     $this->createElement('azyd_azylum_1', 'AZYD AZYLUM Live au Café Provisoire', 

+ 17 - 2
src/Muzich/CoreBundle/Managers/CommentsManager.php View File

40
   }
40
   }
41
   
41
   
42
   /**
42
   /**
43
+   * Retourne le dernier enregistrement commentaire
44
+   * 
45
+   * @return array
46
+   */
47
+  public function getLast()
48
+  {
49
+    return $this->get(count($this->comments)-1);
50
+  }
51
+  
52
+  /**
43
    *
53
    *
44
    * @return array
54
    * @return array
45
    */
55
    */
46
-  public function get()
56
+  public function get($index = null)
47
   {
57
   {
48
-    return $this->comments;
58
+    if ($index === null)
59
+    {
60
+      return $this->comments;
61
+    }
62
+    
63
+    return $this->comments[$index];
49
   }
64
   }
50
   
65
   
51
 }
66
 }

+ 33 - 12
src/Muzich/CoreBundle/Resources/views/SearchElement/element.html.twig View File

116
         {% if element.comments|length > 1 %}
116
         {% if element.comments|length > 1 %}
117
           <a href="#hide_comments_{{ element.id }}" class="hide_comments" style="display: none;">
117
           <a href="#hide_comments_{{ element.id }}" class="hide_comments" style="display: none;">
118
             {{ 'element.comments.hideare'|trans({}, 'elements') }}
118
             {{ 'element.comments.hideare'|trans({}, 'elements') }}
119
-          </a>   
119
+          </a>  
120
           <a href="#comments_{{ element.id }}" class="display_comments">
120
           <a href="#comments_{{ element.id }}" class="display_comments">
121
             {{ 'element.comments.thereare'|trans({'%count%':element.comments|length}, 'elements') }}
121
             {{ 'element.comments.thereare'|trans({'%count%':element.comments|length}, 'elements') }}
122
-          </a> -
122
+          </a>
123
         {% elseif element.comments|length == 1 %}
123
         {% elseif element.comments|length == 1 %}
124
           <a href="#hide_comments_{{ element.id }}" class="hide_comments" style="display: none;">
124
           <a href="#hide_comments_{{ element.id }}" class="hide_comments" style="display: none;">
125
             {{ 'element.comments.hideis'|trans({}, 'elements') }}
125
             {{ 'element.comments.hideis'|trans({}, 'elements') }}
126
-          </a>
126
+          </a> 
127
           <a href="#comments_{{ element.id }}" class="display_comments">
127
           <a href="#comments_{{ element.id }}" class="display_comments">
128
             {{ 'element.comments.thereis'|trans({}, 'elements') }}
128
             {{ 'element.comments.thereis'|trans({}, 'elements') }}
129
-          </a> -
129
+          </a>
130
         {%endif %}
130
         {%endif %}
131
       {% endautoescape %}
131
       {% endautoescape %}
132
                 
132
                 
146
   {% endautoescape %}
146
   {% endautoescape %}
147
 {% endif %}
147
 {% endif %}
148
 
148
 
149
-{% if element.comments|length %}
150
   <div class="comments" style="display: none;">
149
   <div class="comments" style="display: none;">
151
     <ul class="comments">
150
     <ul class="comments">
151
+    {% if element.comments|length %}
152
       {% for comment in element.comments %}
152
       {% for comment in element.comments %}
153
-        <li class="comment">
154
-          <a href="{{ path('show_user', {'slug': comment.u.s}) }}" >{{ comment.u.n }}</a>: 
155
-          {{ comment.c }}
156
-          <span class="datesince">({{ comment.d|date_or_relative_date }})</span>
157
-        </li>
153
+        {% include "MuzichCommentBundle:Comment:comment.html.twig" %}
158
       {% endfor %}
154
       {% endfor %}
155
+    {% endif %}
159
     </ul>
156
     </ul>
160
       
157
       
161
-    
158
+    <div class="comments_loader">
159
+      <img class="comments_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading"/>
160
+    </div>
161
+      
162
+    <form 
163
+      action="{{ path('ajax_add_comment', {'element_id':element.id}) }}" 
164
+      method="post" 
165
+      name="add_comment"
166
+      style="display: none;"
167
+      class="add_comment"
168
+    >
169
+      <textarea name="comment"></textarea>
170
+      <div class="buttons">
171
+        <input 
172
+          type="submit" 
173
+          value="{{ 'element.comments.add_submit'|trans({}, 'elements') }}" 
174
+          class="button"
175
+        />
176
+        <br />
177
+        <input 
178
+          type="button" 
179
+          value="{{ 'element.comments.add_cancel'|trans({}, 'elements') }}" 
180
+          class="button cancel"
181
+        />
182
+      </div>
183
+    </form>
162
       
184
       
163
     <a href="#add_comment_{{ element.id }}" class="add_comment">
185
     <a href="#add_comment_{{ element.id }}" class="add_comment">
164
       {{ 'element.comments.add'|trans({}, 'elements') }}
186
       {{ 'element.comments.add'|trans({}, 'elements') }}
165
     </a>
187
     </a>
166
       
188
       
167
   </div>
189
   </div>
168
-{% endif %}

+ 6 - 1
src/Muzich/CoreBundle/lib/Controller.php View File

316
    *
316
    *
317
    * @return Response
317
    * @return Response
318
    */
318
    */
319
-  protected function mustBeConnected()
319
+  protected function mustBeConnected($and_ajax = false)
320
   {
320
   {
321
+    if ($and_ajax && !$this->getRequest()->isXmlHttpRequest())
322
+    {
323
+      throw $this->createNotFoundException('Ressource ajax uniquement.');
324
+    }
325
+    
321
     if ($this->getUser() == 'anon.')
326
     if ($this->getUser() == 'anon.')
322
     {
327
     {
323
       if ($this->getRequest()->isXmlHttpRequest())
328
       if ($this->getRequest()->isXmlHttpRequest())

+ 24 - 1
web/bundles/muzichcore/css/main.css View File

782
 	opacity: 0;
782
 	opacity: 0;
783
 }
783
 }
784
 
784
 
785
-div.question .yes, .cancel {
785
+div.question .yes, div.question .cancel {
786
 	margin-top: .5em;
786
 	margin-top: .5em;
787
 	margin-right: .5em;
787
 	margin-right: .5em;
788
 	cursor: pointer;
788
 	cursor: pointer;
960
   color: #868686;
960
   color: #868686;
961
 }
961
 }
962
 
962
 
963
+div.comments form.add_comment textarea
964
+{
965
+  width: 80%;
966
+  height: 48px;
967
+}
968
+
969
+div.comments form.add_comment div.buttons
970
+{
971
+  float: right;
972
+  text-align: center;
973
+}
974
+
975
+div.comments form.add_comment div.buttons input
976
+{
977
+  margin-top: 1px;
978
+  margin-bottom: 1px;
979
+}
980
+
981
+div.comments_loader
982
+{
983
+  text-align: center;
984
+}
985
+
963
 /* END commentaires */
986
 /* END commentaires */

+ 49 - 1
web/bundles/muzichcore/js/muzich.js View File

1305
       li_element.find('a.hide_comments').show();
1305
       li_element.find('a.hide_comments').show();
1306
     }
1306
     }
1307
   
1307
   
1308
-    function hide_comments(li)
1308
+    function hide_comments(li_element)
1309
     {
1309
     {
1310
       li_element.find('div.comments').slideUp();
1310
       li_element.find('div.comments').slideUp();
1311
       li_element.find('a.display_comments').show();
1311
       li_element.find('a.display_comments').show();
1319
       ));
1319
       ));
1320
     });
1320
     });
1321
     
1321
     
1322
+    $('form.add_comment input[type="submit"]').live('click', function(){
1323
+      $(this).parent('div').parent('form').parent('div.comments').find('img.comments_loader').show();
1324
+    });
1325
+        
1322
     function display_add_comment(li_element)
1326
     function display_add_comment(li_element)
1323
     {
1327
     {
1328
+      display_comments(li_element);
1324
       li_element.find('a.add_comment').hide();
1329
       li_element.find('a.add_comment').hide();
1330
+      li_element.find('form.add_comment').show();
1325
       
1331
       
1332
+      li_element.find('form.add_comment').ajaxForm(function(response) {
1333
+        if (response.status == 'mustbeconnected')
1334
+        {
1335
+          $(location).attr('href', url_index);
1336
+        }
1337
+
1338
+        li_element.find('img.comments_loader').hide();
1339
+        
1340
+        if (response.status == 'success')
1341
+        {
1342
+          li_element.find('form.add_comment').find('ul.error_list').remove();
1343
+          li_element.find('div.comments ul.comments').append(response.html);
1344
+          hide_add_comment(li_element);
1345
+        }
1346
+        else if (response.status == 'error')
1347
+        {
1348
+          li_element.find('form.add_comment').find('ul.error_list').remove();
1349
+          ul_errors = $('<ul>').addClass('error_list');
1350
+
1351
+          for (i in response.errors)
1352
+          {
1353
+            ul_errors.append($('<li>').append(response.errors[i]));
1354
+          }
1355
+
1356
+          li_element.find('form.add_comment').prepend(ul_errors);
1357
+        }
1358
+
1359
+        return false;
1360
+      });
1361
+      
1362
+    }
1363
+    
1364
+    $('form.add_comment input.cancel').live('click', function(){
1365
+      li_element = $(this).parent('div').parent('form').parent('div.comments').parent('li.element');
1366
+      hide_add_comment(li_element);
1367
+    });
1368
+    
1369
+    function hide_add_comment(li_element)
1370
+    {
1371
+      li_element.find('a.add_comment').show();
1372
+      li_element.find('form.add_comment').hide();
1373
+      li_element.find('form.add_comment textarea').val('');
1326
     }
1374
     }
1327
    
1375
    
1328
  });
1376
  });