@@ -1,14 +1,14 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Created by PhpStorm. |
|
| 4 | - * User: Rafidion Michael |
|
| 5 | - * Date: 03/09/2015 |
|
| 6 | - * Time: 12:00 |
|
| 7 | - */ |
|
| 3 | + * Created by PhpStorm. |
|
| 4 | + * User: Rafidion Michael |
|
| 5 | + * Date: 03/09/2015 |
|
| 6 | + * Time: 12:00 |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | namespace Mykees\CommentBundle\Interfaces; |
| 10 | 10 | |
| 11 | 11 | |
| 12 | 12 | interface IsCommentable { |
| 13 | - public function getId(); |
|
| 13 | + public function getId(); |
|
| 14 | 14 | } |
@@ -11,40 +11,40 @@ |
||
| 11 | 11 | class CommentListener{ |
| 12 | 12 | |
| 13 | 13 | |
| 14 | - public $container; |
|
| 15 | - public $entity; |
|
| 16 | - public $fos_user; |
|
| 17 | - public $class; |
|
| 18 | - public $managerRegistry; |
|
| 19 | - public $depth; |
|
| 20 | - |
|
| 21 | - |
|
| 22 | - public function __construct(ManagerRegistry $managerRegistry, $class, $fos_user_class, $depth) |
|
| 23 | - { |
|
| 24 | - $this->managerRegistry = $managerRegistry; |
|
| 25 | - $this->class = $class; |
|
| 26 | - $this->fos_user = $fos_user_class; |
|
| 27 | - $this->depth = $depth; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - public function preRemove(LifecycleEventArgs $args) |
|
| 32 | - { |
|
| 33 | - $model = $args->getEntity(); |
|
| 34 | - |
|
| 35 | - if($model instanceof IsCommentable) |
|
| 36 | - { |
|
| 37 | - $manager = new CommentQueryManager($this->managerRegistry,$this->class,$this->depth); |
|
| 38 | - $manager->preDeleteComment($model); |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - public function getSubscribedEvents() |
|
| 44 | - { |
|
| 45 | - return [ |
|
| 46 | - Events::preRemove |
|
| 47 | - ]; |
|
| 48 | - } |
|
| 14 | + public $container; |
|
| 15 | + public $entity; |
|
| 16 | + public $fos_user; |
|
| 17 | + public $class; |
|
| 18 | + public $managerRegistry; |
|
| 19 | + public $depth; |
|
| 20 | + |
|
| 21 | + |
|
| 22 | + public function __construct(ManagerRegistry $managerRegistry, $class, $fos_user_class, $depth) |
|
| 23 | + { |
|
| 24 | + $this->managerRegistry = $managerRegistry; |
|
| 25 | + $this->class = $class; |
|
| 26 | + $this->fos_user = $fos_user_class; |
|
| 27 | + $this->depth = $depth; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + public function preRemove(LifecycleEventArgs $args) |
|
| 32 | + { |
|
| 33 | + $model = $args->getEntity(); |
|
| 34 | + |
|
| 35 | + if($model instanceof IsCommentable) |
|
| 36 | + { |
|
| 37 | + $manager = new CommentQueryManager($this->managerRegistry,$this->class,$this->depth); |
|
| 38 | + $manager->preDeleteComment($model); |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + public function getSubscribedEvents() |
|
| 44 | + { |
|
| 45 | + return [ |
|
| 46 | + Events::preRemove |
|
| 47 | + ]; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | } |
@@ -18,82 +18,82 @@ |
||
| 18 | 18 | |
| 19 | 19 | class CommentManager extends Manager{ |
| 20 | 20 | |
| 21 | - protected $formFactory; |
|
| 22 | - protected $router; |
|
| 23 | - protected $comment_class; |
|
| 24 | - protected $repository; |
|
| 25 | - protected $session; |
|
| 26 | - protected $formType; |
|
| 27 | - |
|
| 28 | - public function __construct(FormFactory $formFactory, Router $router, Session $session, CommentFormType $formType, $class) |
|
| 29 | - { |
|
| 30 | - $this->formFactory = $formFactory; |
|
| 31 | - $this->router = $router; |
|
| 32 | - $this->comment_class = $class; |
|
| 33 | - $this->formType = $formType; |
|
| 34 | - $this->session = $session; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create Comment Form |
|
| 40 | - * @param IsCommentable $model |
|
| 41 | - * @param array $labels |
|
| 42 | - * @param null $success_message |
|
| 43 | - * @return \Symfony\Component\Form\Form |
|
| 44 | - */ |
|
| 45 | - public function createForm(IsCommentable $model, $labels = [], $success_message=null) |
|
| 46 | - { |
|
| 47 | - $dataForm = $this->session->get('form_comment_data'); |
|
| 48 | - $model_name = $this->getClassShortName($model); |
|
| 49 | - $route = $this->router->generate('mykees_comment_manage'); |
|
| 50 | - $comment = new $this->comment_class; |
|
| 51 | - $comment->setModel($model_name); |
|
| 52 | - $comment->setModelId($model->getId()); |
|
| 53 | - |
|
| 54 | - $this->formType->username = !empty($labels['username']) ? $labels['username'] : false; |
|
| 55 | - $this->formType->email = !empty($labels['email']) ? $labels['email'] : false; |
|
| 56 | - $this->formType->content = !empty($labels['content']) ? $labels['content'] : false; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - if($this->session->has('success_message')) { $this->session->remove('success_message'); } |
|
| 60 | - if($success_message) { $this->session->set('success_message',$success_message); } |
|
| 61 | - |
|
| 62 | - $form = $this->formFactory->create( |
|
| 63 | - $this->formType, |
|
| 64 | - $comment, |
|
| 65 | - ['action'=> $route,'method'=> 'POST'] |
|
| 66 | - ); |
|
| 67 | - |
|
| 68 | - if( !empty($dataForm) ){ |
|
| 69 | - $form->submit($dataForm); |
|
| 70 | - $this->session->remove('form_comment_data'); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - return $form; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Verify if the comment is a spam |
|
| 78 | - * @param $comment |
|
| 79 | - * @param Request $request |
|
| 80 | - * @param $akismetInit |
|
| 81 | - * @return bool |
|
| 82 | - * @throws \Symfony\Component\Config\Definition\Exception\Exception |
|
| 83 | - */ |
|
| 84 | - public function isSpam($comment, Request $request, $akismetInit) |
|
| 85 | - { |
|
| 86 | - if($akismetInit){ |
|
| 87 | - $akismet = new Akismet($akismetInit['website'],$akismetInit['api_key'],$request); |
|
| 88 | - |
|
| 89 | - $akismet->setCommentAuthor($comment->getUsername()); |
|
| 90 | - $akismet->setCommentAuthorEmail($comment->getEmail()); |
|
| 91 | - $akismet->setCommentContent($comment->getContent()); |
|
| 92 | - $akismet->setUserIP($comment->getIp()); |
|
| 93 | - |
|
| 94 | - return $akismet->isCommentSpam($request); |
|
| 95 | - } |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 21 | + protected $formFactory; |
|
| 22 | + protected $router; |
|
| 23 | + protected $comment_class; |
|
| 24 | + protected $repository; |
|
| 25 | + protected $session; |
|
| 26 | + protected $formType; |
|
| 27 | + |
|
| 28 | + public function __construct(FormFactory $formFactory, Router $router, Session $session, CommentFormType $formType, $class) |
|
| 29 | + { |
|
| 30 | + $this->formFactory = $formFactory; |
|
| 31 | + $this->router = $router; |
|
| 32 | + $this->comment_class = $class; |
|
| 33 | + $this->formType = $formType; |
|
| 34 | + $this->session = $session; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create Comment Form |
|
| 40 | + * @param IsCommentable $model |
|
| 41 | + * @param array $labels |
|
| 42 | + * @param null $success_message |
|
| 43 | + * @return \Symfony\Component\Form\Form |
|
| 44 | + */ |
|
| 45 | + public function createForm(IsCommentable $model, $labels = [], $success_message=null) |
|
| 46 | + { |
|
| 47 | + $dataForm = $this->session->get('form_comment_data'); |
|
| 48 | + $model_name = $this->getClassShortName($model); |
|
| 49 | + $route = $this->router->generate('mykees_comment_manage'); |
|
| 50 | + $comment = new $this->comment_class; |
|
| 51 | + $comment->setModel($model_name); |
|
| 52 | + $comment->setModelId($model->getId()); |
|
| 53 | + |
|
| 54 | + $this->formType->username = !empty($labels['username']) ? $labels['username'] : false; |
|
| 55 | + $this->formType->email = !empty($labels['email']) ? $labels['email'] : false; |
|
| 56 | + $this->formType->content = !empty($labels['content']) ? $labels['content'] : false; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + if($this->session->has('success_message')) { $this->session->remove('success_message'); } |
|
| 60 | + if($success_message) { $this->session->set('success_message',$success_message); } |
|
| 61 | + |
|
| 62 | + $form = $this->formFactory->create( |
|
| 63 | + $this->formType, |
|
| 64 | + $comment, |
|
| 65 | + ['action'=> $route,'method'=> 'POST'] |
|
| 66 | + ); |
|
| 67 | + |
|
| 68 | + if( !empty($dataForm) ){ |
|
| 69 | + $form->submit($dataForm); |
|
| 70 | + $this->session->remove('form_comment_data'); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + return $form; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Verify if the comment is a spam |
|
| 78 | + * @param $comment |
|
| 79 | + * @param Request $request |
|
| 80 | + * @param $akismetInit |
|
| 81 | + * @return bool |
|
| 82 | + * @throws \Symfony\Component\Config\Definition\Exception\Exception |
|
| 83 | + */ |
|
| 84 | + public function isSpam($comment, Request $request, $akismetInit) |
|
| 85 | + { |
|
| 86 | + if($akismetInit){ |
|
| 87 | + $akismet = new Akismet($akismetInit['website'],$akismetInit['api_key'],$request); |
|
| 88 | + |
|
| 89 | + $akismet->setCommentAuthor($comment->getUsername()); |
|
| 90 | + $akismet->setCommentAuthorEmail($comment->getEmail()); |
|
| 91 | + $akismet->setCommentContent($comment->getContent()); |
|
| 92 | + $akismet->setUserIP($comment->getIp()); |
|
| 93 | + |
|
| 94 | + return $akismet->isCommentSpam($request); |
|
| 95 | + } |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | 99 | } |
@@ -13,333 +13,333 @@ |
||
| 13 | 13 | |
| 14 | 14 | class CommentQueryManager extends Manager{ |
| 15 | 15 | |
| 16 | - private $repository; |
|
| 17 | - private $comment_class; |
|
| 18 | - private $depth; |
|
| 19 | - private $depth_reached; |
|
| 20 | - |
|
| 21 | - public function __construct(ManagerRegistry $managerRegistry, $class, $depth) |
|
| 22 | - { |
|
| 23 | - $this->comment_class = $class; |
|
| 24 | - $this->repository = $managerRegistry->getRepository($this->comment_class); |
|
| 25 | - $this->depth = $depth; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Find one comment |
|
| 31 | - * @param $id |
|
| 32 | - * @param null $model_name |
|
| 33 | - * @param int $model_id |
|
| 34 | - * @return mixed |
|
| 35 | - */ |
|
| 36 | - public function findOneComment($id, $model_name=null,$model_id=0) |
|
| 37 | - { |
|
| 38 | - $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 39 | - $qb = $this->repository->createQueryBuilder('c'); |
|
| 40 | - |
|
| 41 | - return $this->oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Find a comment by model referer |
|
| 47 | - * @param $id |
|
| 48 | - * @param IsCommentable $model |
|
| 49 | - * @return mixed |
|
| 50 | - */ |
|
| 51 | - public function findOneCommentByReferer($id,IsCommentable $model) |
|
| 52 | - { |
|
| 53 | - $model_name = $this->getClassShortName( $model ); |
|
| 54 | - $model_id = $model->getId(); |
|
| 55 | - $qb = $this->repository->createQueryBuilder('c'); |
|
| 56 | - $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 57 | - |
|
| 58 | - return $this->oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Find all comments for an entity |
|
| 63 | - * @param IsCommentable $model |
|
| 64 | - * @param bool $get_by_id |
|
| 65 | - * @return mixed |
|
| 66 | - */ |
|
| 67 | - public function findComments(IsCommentable $model, $get_by_id=false) |
|
| 68 | - { |
|
| 69 | - $model_name = $this->getClassShortName( $model ); |
|
| 70 | - $comments = []; |
|
| 71 | - $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 72 | - $qb = $this->repository->createQueryBuilder('c'); |
|
| 73 | - |
|
| 74 | - $comments['comments'] = $this->commentQueryResult($qb, $model_name, $model->getId(), $is_join); |
|
| 75 | - $comments['count'] = count($comments['comments']); |
|
| 76 | - |
|
| 77 | - return $this->buildCommentStructure($comments,$get_by_id); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Find all comments for an entity |
|
| 83 | - * @param $model_name |
|
| 84 | - * @param $model_id |
|
| 85 | - * @param bool $get_by_id |
|
| 86 | - * @return mixed |
|
| 87 | - */ |
|
| 88 | - public function findCommentsByModelAndId($model_name, $model_id, $get_by_id=false) |
|
| 89 | - { |
|
| 90 | - $comments = []; |
|
| 91 | - $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 92 | - $qb = $this->repository->createQueryBuilder('c'); |
|
| 93 | - |
|
| 94 | - $comments['comments'] = $this->commentQueryResult($qb, $model_name, $model_id, $is_join); |
|
| 95 | - $comments['count'] = count($comments['comments']); |
|
| 96 | - |
|
| 97 | - return $this->buildCommentStructure($comments,$get_by_id); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Build Comment Structure |
|
| 103 | - * @param $comments |
|
| 104 | - * @param bool $get_by_id |
|
| 105 | - * @return mixed |
|
| 106 | - */ |
|
| 107 | - private function buildCommentStructure($comments, $get_by_id=false) |
|
| 108 | - { |
|
| 109 | - $comments_by_id = []; |
|
| 110 | - return $this->build($comments,$comments_by_id,$get_by_id); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - private function build($comments,$comments_by_id, $get_by_id) |
|
| 115 | - { |
|
| 116 | - foreach($comments['comments'] as $comment) |
|
| 117 | - { |
|
| 118 | - $comments_by_id[$comment->getId()] = $comment; |
|
| 119 | - $comment->setDepth($this->depth); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - //Comment parent and children |
|
| 123 | - foreach($comments['comments'] as $k=>$comment) |
|
| 124 | - { |
|
| 125 | - if($this->depth > 0 && $comment->getParentId() > 0) |
|
| 126 | - { |
|
| 127 | - $parent_id = $this->depthComment($comments_by_id,$comment->getId()); |
|
| 128 | - |
|
| 129 | - //Si la pronfondeur atteinte est plus petite que la profondeur definis |
|
| 130 | - //alors la réponse aura l'id du commentaire |
|
| 131 | - //Sinon |
|
| 132 | - //la réponse aura l'id du parent |
|
| 133 | - if($this->depth_reached < $this->depth) |
|
| 134 | - { |
|
| 135 | - $comment->setDepth($comment->getId()); |
|
| 136 | - }else{ |
|
| 137 | - $comment->setDepth($parent_id); |
|
| 138 | - } |
|
| 139 | - $comment->setDepthReached($this->depth_reached); |
|
| 140 | - $comments_by_id[$parent_id]->setChildren($comment); |
|
| 141 | - unset($comments['comments'][$k]); |
|
| 142 | - |
|
| 143 | - }else if($this->depth == 0){ |
|
| 144 | - $comment->setDepth(0); |
|
| 145 | - }else{ |
|
| 146 | - $comment->setDepth($comment->getId()); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if($get_by_id) |
|
| 152 | - { |
|
| 153 | - return $comments_by_id; |
|
| 154 | - }else{ |
|
| 155 | - return $comments; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * config depth of comments |
|
| 161 | - * @param $comments_by_id |
|
| 162 | - * @param $comment_id |
|
| 163 | - * @return mixed |
|
| 164 | - */ |
|
| 165 | - private function depthComment($comments_by_id,$comment_id) |
|
| 166 | - { |
|
| 167 | - $parent_id=[]; |
|
| 168 | - $index = $comment_id; |
|
| 169 | - $parent = true; |
|
| 170 | - $depth_reached = 0; |
|
| 171 | - |
|
| 172 | - while($parent) |
|
| 173 | - { |
|
| 174 | - $comment = $comments_by_id[$index]; |
|
| 175 | - if($comment->getParentId()) |
|
| 176 | - { |
|
| 177 | - $parent_id[] = $comments_by_id[$index]->getParentId(); |
|
| 178 | - $index = $comments_by_id[$index]->getParentId(); |
|
| 179 | - $depth_reached++; |
|
| 180 | - }else{ |
|
| 181 | - $parent = false; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - $this->depth_reached = $depth_reached; |
|
| 185 | - $array_reverse = array_reverse($parent_id); |
|
| 186 | - |
|
| 187 | - return $this->depth < count($array_reverse) ? $array_reverse[$this->depth-1] : end($array_reverse); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Retrieve all comments |
|
| 193 | - * @param array $criteria |
|
| 194 | - * @param null $orderBy |
|
| 195 | - * @param null $limit |
|
| 196 | - * @param null $offset |
|
| 197 | - * @return array |
|
| 198 | - */ |
|
| 199 | - public function findAllComments(array $criteria=[],$orderBy=null,$limit=null,$offset=null) |
|
| 200 | - { |
|
| 201 | - if(!empty($criteria) || isset($orderBy) || isset($limit) || isset($offset)) |
|
| 202 | - { |
|
| 203 | - return $this->repository->findBy($criteria,$orderBy,$limit,$offset); |
|
| 204 | - }else{ |
|
| 205 | - return $this->repository->findAll(); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * @param $qb |
|
| 212 | - * @param $model_name |
|
| 213 | - * @param $model_id |
|
| 214 | - * @param $is_join |
|
| 215 | - * @return mixed |
|
| 216 | - */ |
|
| 217 | - private function commentQueryResult($qb, $model_name, $model_id, $is_join) |
|
| 218 | - { |
|
| 219 | - if($is_join) |
|
| 220 | - { |
|
| 221 | - $qb |
|
| 222 | - ->leftJoin('c.user','u') |
|
| 223 | - ->addSelect('u') |
|
| 224 | - ; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return $qb |
|
| 228 | - ->where("c.model = :ref") |
|
| 229 | - ->setParameter('ref',$model_name) |
|
| 230 | - ->andWhere("c.modelId = :ref_id") |
|
| 231 | - ->setParameter('ref_id',$model_id) |
|
| 232 | - ->orderBy('c.createdAt', 'DESC') |
|
| 233 | - ->getQuery() |
|
| 234 | - ->getResult() |
|
| 235 | - ; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - private function oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join) |
|
| 239 | - { |
|
| 240 | - if($is_join) |
|
| 241 | - { |
|
| 242 | - $qb |
|
| 243 | - ->leftJoin('c.user','u') |
|
| 244 | - ->addSelect('u') |
|
| 245 | - ; |
|
| 246 | - } |
|
| 247 | - $qb->where('c.id = :id') |
|
| 248 | - ->setParameter('id',$id) |
|
| 249 | - ; |
|
| 250 | - |
|
| 251 | - if($model_name) |
|
| 252 | - { |
|
| 253 | - $qb->andWhere("c.model = :ref") |
|
| 254 | - ->setParameter('ref',$model_name) |
|
| 255 | - ; |
|
| 256 | - } |
|
| 257 | - if($model_id > 0) |
|
| 258 | - { |
|
| 259 | - $qb->andWhere("c.modelId = :ref_id") |
|
| 260 | - ->setParameter('ref_id',$model_id) |
|
| 261 | - ; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return $qb->getQuery()->getOneOrNullResult(); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param $ids |
|
| 270 | - * @return mixed |
|
| 271 | - */ |
|
| 272 | - public function deleteByCommentIds($ids) |
|
| 273 | - { |
|
| 274 | - $comment_class = $this->classRepository($this->comment_class); |
|
| 275 | - |
|
| 276 | - return $this->repository |
|
| 277 | - ->createQueryBuilder('c') |
|
| 278 | - ->delete($comment_class,'c') |
|
| 279 | - ->where("c.id IN(:ids)") |
|
| 280 | - ->setParameter('ids', array_values($ids)) |
|
| 281 | - ->getQuery() |
|
| 282 | - ->execute() |
|
| 283 | - ; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Delete a comment by id |
|
| 289 | - * @param $id |
|
| 290 | - * @return mixed |
|
| 291 | - */ |
|
| 292 | - public function deleteComment($id) |
|
| 293 | - { |
|
| 294 | - $comment_class = $this->classRepository($this->comment_class); |
|
| 295 | - |
|
| 296 | - return $this->repository |
|
| 297 | - ->createQueryBuilder('c') |
|
| 298 | - ->delete($comment_class,'c') |
|
| 299 | - ->where("c.id = id") |
|
| 300 | - ->setParameter('id', $id) |
|
| 301 | - ->getQuery() |
|
| 302 | - ->execute() |
|
| 303 | - ; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Remove ccomment children |
|
| 310 | - * @param $comment |
|
| 311 | - * @return array |
|
| 312 | - */ |
|
| 313 | - public function getChildren($comment) |
|
| 314 | - { |
|
| 315 | - $children_ids = []; |
|
| 316 | - foreach($comment->getChildren() as $child) |
|
| 317 | - { |
|
| 318 | - $children_ids[] = $child->getId(); |
|
| 319 | - if($child->getChildren()) |
|
| 320 | - { |
|
| 321 | - $children_ids = array_merge($children_ids, $this->getChildren($child)); |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return $children_ids; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Pre delete comment |
|
| 330 | - * @param IsCommentable $referer |
|
| 331 | - * @return bool |
|
| 332 | - */ |
|
| 333 | - public function preDeleteComment(IsCommentable $referer) |
|
| 334 | - { |
|
| 335 | - $comments = $this->findComments($referer, true); |
|
| 336 | - foreach($comments as $k=>$comment) |
|
| 337 | - { |
|
| 338 | - $children_ids = $this->getChildren($comment); |
|
| 339 | - array_push($children_ids,$k); |
|
| 340 | - |
|
| 341 | - $this->deleteByCommentIds($children_ids); |
|
| 342 | - } |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 16 | + private $repository; |
|
| 17 | + private $comment_class; |
|
| 18 | + private $depth; |
|
| 19 | + private $depth_reached; |
|
| 20 | + |
|
| 21 | + public function __construct(ManagerRegistry $managerRegistry, $class, $depth) |
|
| 22 | + { |
|
| 23 | + $this->comment_class = $class; |
|
| 24 | + $this->repository = $managerRegistry->getRepository($this->comment_class); |
|
| 25 | + $this->depth = $depth; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Find one comment |
|
| 31 | + * @param $id |
|
| 32 | + * @param null $model_name |
|
| 33 | + * @param int $model_id |
|
| 34 | + * @return mixed |
|
| 35 | + */ |
|
| 36 | + public function findOneComment($id, $model_name=null,$model_id=0) |
|
| 37 | + { |
|
| 38 | + $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 39 | + $qb = $this->repository->createQueryBuilder('c'); |
|
| 40 | + |
|
| 41 | + return $this->oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Find a comment by model referer |
|
| 47 | + * @param $id |
|
| 48 | + * @param IsCommentable $model |
|
| 49 | + * @return mixed |
|
| 50 | + */ |
|
| 51 | + public function findOneCommentByReferer($id,IsCommentable $model) |
|
| 52 | + { |
|
| 53 | + $model_name = $this->getClassShortName( $model ); |
|
| 54 | + $model_id = $model->getId(); |
|
| 55 | + $qb = $this->repository->createQueryBuilder('c'); |
|
| 56 | + $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 57 | + |
|
| 58 | + return $this->oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Find all comments for an entity |
|
| 63 | + * @param IsCommentable $model |
|
| 64 | + * @param bool $get_by_id |
|
| 65 | + * @return mixed |
|
| 66 | + */ |
|
| 67 | + public function findComments(IsCommentable $model, $get_by_id=false) |
|
| 68 | + { |
|
| 69 | + $model_name = $this->getClassShortName( $model ); |
|
| 70 | + $comments = []; |
|
| 71 | + $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 72 | + $qb = $this->repository->createQueryBuilder('c'); |
|
| 73 | + |
|
| 74 | + $comments['comments'] = $this->commentQueryResult($qb, $model_name, $model->getId(), $is_join); |
|
| 75 | + $comments['count'] = count($comments['comments']); |
|
| 76 | + |
|
| 77 | + return $this->buildCommentStructure($comments,$get_by_id); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Find all comments for an entity |
|
| 83 | + * @param $model_name |
|
| 84 | + * @param $model_id |
|
| 85 | + * @param bool $get_by_id |
|
| 86 | + * @return mixed |
|
| 87 | + */ |
|
| 88 | + public function findCommentsByModelAndId($model_name, $model_id, $get_by_id=false) |
|
| 89 | + { |
|
| 90 | + $comments = []; |
|
| 91 | + $is_join = method_exists($this->comment_class,'getUser') ? true : false; |
|
| 92 | + $qb = $this->repository->createQueryBuilder('c'); |
|
| 93 | + |
|
| 94 | + $comments['comments'] = $this->commentQueryResult($qb, $model_name, $model_id, $is_join); |
|
| 95 | + $comments['count'] = count($comments['comments']); |
|
| 96 | + |
|
| 97 | + return $this->buildCommentStructure($comments,$get_by_id); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Build Comment Structure |
|
| 103 | + * @param $comments |
|
| 104 | + * @param bool $get_by_id |
|
| 105 | + * @return mixed |
|
| 106 | + */ |
|
| 107 | + private function buildCommentStructure($comments, $get_by_id=false) |
|
| 108 | + { |
|
| 109 | + $comments_by_id = []; |
|
| 110 | + return $this->build($comments,$comments_by_id,$get_by_id); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + private function build($comments,$comments_by_id, $get_by_id) |
|
| 115 | + { |
|
| 116 | + foreach($comments['comments'] as $comment) |
|
| 117 | + { |
|
| 118 | + $comments_by_id[$comment->getId()] = $comment; |
|
| 119 | + $comment->setDepth($this->depth); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //Comment parent and children |
|
| 123 | + foreach($comments['comments'] as $k=>$comment) |
|
| 124 | + { |
|
| 125 | + if($this->depth > 0 && $comment->getParentId() > 0) |
|
| 126 | + { |
|
| 127 | + $parent_id = $this->depthComment($comments_by_id,$comment->getId()); |
|
| 128 | + |
|
| 129 | + //Si la pronfondeur atteinte est plus petite que la profondeur definis |
|
| 130 | + //alors la réponse aura l'id du commentaire |
|
| 131 | + //Sinon |
|
| 132 | + //la réponse aura l'id du parent |
|
| 133 | + if($this->depth_reached < $this->depth) |
|
| 134 | + { |
|
| 135 | + $comment->setDepth($comment->getId()); |
|
| 136 | + }else{ |
|
| 137 | + $comment->setDepth($parent_id); |
|
| 138 | + } |
|
| 139 | + $comment->setDepthReached($this->depth_reached); |
|
| 140 | + $comments_by_id[$parent_id]->setChildren($comment); |
|
| 141 | + unset($comments['comments'][$k]); |
|
| 142 | + |
|
| 143 | + }else if($this->depth == 0){ |
|
| 144 | + $comment->setDepth(0); |
|
| 145 | + }else{ |
|
| 146 | + $comment->setDepth($comment->getId()); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if($get_by_id) |
|
| 152 | + { |
|
| 153 | + return $comments_by_id; |
|
| 154 | + }else{ |
|
| 155 | + return $comments; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * config depth of comments |
|
| 161 | + * @param $comments_by_id |
|
| 162 | + * @param $comment_id |
|
| 163 | + * @return mixed |
|
| 164 | + */ |
|
| 165 | + private function depthComment($comments_by_id,$comment_id) |
|
| 166 | + { |
|
| 167 | + $parent_id=[]; |
|
| 168 | + $index = $comment_id; |
|
| 169 | + $parent = true; |
|
| 170 | + $depth_reached = 0; |
|
| 171 | + |
|
| 172 | + while($parent) |
|
| 173 | + { |
|
| 174 | + $comment = $comments_by_id[$index]; |
|
| 175 | + if($comment->getParentId()) |
|
| 176 | + { |
|
| 177 | + $parent_id[] = $comments_by_id[$index]->getParentId(); |
|
| 178 | + $index = $comments_by_id[$index]->getParentId(); |
|
| 179 | + $depth_reached++; |
|
| 180 | + }else{ |
|
| 181 | + $parent = false; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + $this->depth_reached = $depth_reached; |
|
| 185 | + $array_reverse = array_reverse($parent_id); |
|
| 186 | + |
|
| 187 | + return $this->depth < count($array_reverse) ? $array_reverse[$this->depth-1] : end($array_reverse); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Retrieve all comments |
|
| 193 | + * @param array $criteria |
|
| 194 | + * @param null $orderBy |
|
| 195 | + * @param null $limit |
|
| 196 | + * @param null $offset |
|
| 197 | + * @return array |
|
| 198 | + */ |
|
| 199 | + public function findAllComments(array $criteria=[],$orderBy=null,$limit=null,$offset=null) |
|
| 200 | + { |
|
| 201 | + if(!empty($criteria) || isset($orderBy) || isset($limit) || isset($offset)) |
|
| 202 | + { |
|
| 203 | + return $this->repository->findBy($criteria,$orderBy,$limit,$offset); |
|
| 204 | + }else{ |
|
| 205 | + return $this->repository->findAll(); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * @param $qb |
|
| 212 | + * @param $model_name |
|
| 213 | + * @param $model_id |
|
| 214 | + * @param $is_join |
|
| 215 | + * @return mixed |
|
| 216 | + */ |
|
| 217 | + private function commentQueryResult($qb, $model_name, $model_id, $is_join) |
|
| 218 | + { |
|
| 219 | + if($is_join) |
|
| 220 | + { |
|
| 221 | + $qb |
|
| 222 | + ->leftJoin('c.user','u') |
|
| 223 | + ->addSelect('u') |
|
| 224 | + ; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + return $qb |
|
| 228 | + ->where("c.model = :ref") |
|
| 229 | + ->setParameter('ref',$model_name) |
|
| 230 | + ->andWhere("c.modelId = :ref_id") |
|
| 231 | + ->setParameter('ref_id',$model_id) |
|
| 232 | + ->orderBy('c.createdAt', 'DESC') |
|
| 233 | + ->getQuery() |
|
| 234 | + ->getResult() |
|
| 235 | + ; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + private function oneCommentQueryResult($qb, $id, $model_name, $model_id, $is_join) |
|
| 239 | + { |
|
| 240 | + if($is_join) |
|
| 241 | + { |
|
| 242 | + $qb |
|
| 243 | + ->leftJoin('c.user','u') |
|
| 244 | + ->addSelect('u') |
|
| 245 | + ; |
|
| 246 | + } |
|
| 247 | + $qb->where('c.id = :id') |
|
| 248 | + ->setParameter('id',$id) |
|
| 249 | + ; |
|
| 250 | + |
|
| 251 | + if($model_name) |
|
| 252 | + { |
|
| 253 | + $qb->andWhere("c.model = :ref") |
|
| 254 | + ->setParameter('ref',$model_name) |
|
| 255 | + ; |
|
| 256 | + } |
|
| 257 | + if($model_id > 0) |
|
| 258 | + { |
|
| 259 | + $qb->andWhere("c.modelId = :ref_id") |
|
| 260 | + ->setParameter('ref_id',$model_id) |
|
| 261 | + ; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return $qb->getQuery()->getOneOrNullResult(); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param $ids |
|
| 270 | + * @return mixed |
|
| 271 | + */ |
|
| 272 | + public function deleteByCommentIds($ids) |
|
| 273 | + { |
|
| 274 | + $comment_class = $this->classRepository($this->comment_class); |
|
| 275 | + |
|
| 276 | + return $this->repository |
|
| 277 | + ->createQueryBuilder('c') |
|
| 278 | + ->delete($comment_class,'c') |
|
| 279 | + ->where("c.id IN(:ids)") |
|
| 280 | + ->setParameter('ids', array_values($ids)) |
|
| 281 | + ->getQuery() |
|
| 282 | + ->execute() |
|
| 283 | + ; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Delete a comment by id |
|
| 289 | + * @param $id |
|
| 290 | + * @return mixed |
|
| 291 | + */ |
|
| 292 | + public function deleteComment($id) |
|
| 293 | + { |
|
| 294 | + $comment_class = $this->classRepository($this->comment_class); |
|
| 295 | + |
|
| 296 | + return $this->repository |
|
| 297 | + ->createQueryBuilder('c') |
|
| 298 | + ->delete($comment_class,'c') |
|
| 299 | + ->where("c.id = id") |
|
| 300 | + ->setParameter('id', $id) |
|
| 301 | + ->getQuery() |
|
| 302 | + ->execute() |
|
| 303 | + ; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Remove ccomment children |
|
| 310 | + * @param $comment |
|
| 311 | + * @return array |
|
| 312 | + */ |
|
| 313 | + public function getChildren($comment) |
|
| 314 | + { |
|
| 315 | + $children_ids = []; |
|
| 316 | + foreach($comment->getChildren() as $child) |
|
| 317 | + { |
|
| 318 | + $children_ids[] = $child->getId(); |
|
| 319 | + if($child->getChildren()) |
|
| 320 | + { |
|
| 321 | + $children_ids = array_merge($children_ids, $this->getChildren($child)); |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return $children_ids; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Pre delete comment |
|
| 330 | + * @param IsCommentable $referer |
|
| 331 | + * @return bool |
|
| 332 | + */ |
|
| 333 | + public function preDeleteComment(IsCommentable $referer) |
|
| 334 | + { |
|
| 335 | + $comments = $this->findComments($referer, true); |
|
| 336 | + foreach($comments as $k=>$comment) |
|
| 337 | + { |
|
| 338 | + $children_ids = $this->getChildren($comment); |
|
| 339 | + array_push($children_ids,$k); |
|
| 340 | + |
|
| 341 | + $this->deleteByCommentIds($children_ids); |
|
| 342 | + } |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | 345 | } |
@@ -1,10 +1,10 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Created by PhpStorm. |
|
| 4 | - * User: Rafidion Michael |
|
| 5 | - * Date: 03/09/2015 |
|
| 6 | - * Time: 12:02 |
|
| 7 | - */ |
|
| 3 | + * Created by PhpStorm. |
|
| 4 | + * User: Rafidion Michael |
|
| 5 | + * Date: 03/09/2015 |
|
| 6 | + * Time: 12:02 |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | namespace Mykees\CommentBundle\Manager; |
| 10 | 10 | |
@@ -12,75 +12,75 @@ discard block |
||
| 12 | 12 | abstract class Manager { |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param $ref |
|
| 17 | - * @return string |
|
| 18 | - */ |
|
| 19 | - public function getBundleShortName ( $ref ){ |
|
| 20 | - $explode = explode('\\', $this->getClassName($ref)); |
|
| 21 | - return $explode[0].$explode[1]; |
|
| 22 | - } |
|
| 15 | + /** |
|
| 16 | + * @param $ref |
|
| 17 | + * @return string |
|
| 18 | + */ |
|
| 19 | + public function getBundleShortName ( $ref ){ |
|
| 20 | + $explode = explode('\\', $this->getClassName($ref)); |
|
| 21 | + return $explode[0].$explode[1]; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param $ref |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function getBundlePath ( $ref ){ |
|
| 30 | - $explode = explode('\\', $this->getClassName($ref)); |
|
| 31 | - return $explode[0].'\\'.$explode[1]; |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * @param $ref |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function getBundlePath ( $ref ){ |
|
| 30 | + $explode = explode('\\', $this->getClassName($ref)); |
|
| 31 | + return $explode[0].'\\'.$explode[1]; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param $ref |
|
| 37 | - * @param bool $withouSlash |
|
| 38 | - * @return string |
|
| 39 | - */ |
|
| 40 | - public function getFullBundlePath($ref,$withouSlash = false) |
|
| 41 | - { |
|
| 42 | - $explode = explode('\\', $this->getClassName($ref)); |
|
| 43 | - if($withouSlash) |
|
| 44 | - { |
|
| 45 | - return $explode[0].$explode[1].':'.$explode[3]; |
|
| 46 | - }else{ |
|
| 47 | - return $explode[0].'\\'.$explode[1].'\\'.$explode[2].'\\'.$explode[3]; |
|
| 48 | - } |
|
| 49 | - } |
|
| 35 | + /** |
|
| 36 | + * @param $ref |
|
| 37 | + * @param bool $withouSlash |
|
| 38 | + * @return string |
|
| 39 | + */ |
|
| 40 | + public function getFullBundlePath($ref,$withouSlash = false) |
|
| 41 | + { |
|
| 42 | + $explode = explode('\\', $this->getClassName($ref)); |
|
| 43 | + if($withouSlash) |
|
| 44 | + { |
|
| 45 | + return $explode[0].$explode[1].':'.$explode[3]; |
|
| 46 | + }else{ |
|
| 47 | + return $explode[0].'\\'.$explode[1].'\\'.$explode[2].'\\'.$explode[3]; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param $ref |
|
| 54 | - * @return string |
|
| 55 | - */ |
|
| 56 | - public function getClassShortName( $ref ) { |
|
| 57 | - $reflection = new \ReflectionClass( $ref ); |
|
| 58 | - if( $reflection->getParentClass() ) { |
|
| 59 | - $reflection = $reflection->getParentClass(); |
|
| 60 | - } |
|
| 61 | - return $reflection->getShortName(); |
|
| 62 | - } |
|
| 52 | + /** |
|
| 53 | + * @param $ref |
|
| 54 | + * @return string |
|
| 55 | + */ |
|
| 56 | + public function getClassShortName( $ref ) { |
|
| 57 | + $reflection = new \ReflectionClass( $ref ); |
|
| 58 | + if( $reflection->getParentClass() ) { |
|
| 59 | + $reflection = $reflection->getParentClass(); |
|
| 60 | + } |
|
| 61 | + return $reflection->getShortName(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param $ref |
|
| 67 | - * @return string |
|
| 68 | - */ |
|
| 69 | - public function getClassName ( $ref ) { |
|
| 70 | - $reflection = new \ReflectionClass( $ref ); |
|
| 71 | - return $reflection->getName(); |
|
| 72 | - } |
|
| 65 | + /** |
|
| 66 | + * @param $ref |
|
| 67 | + * @return string |
|
| 68 | + */ |
|
| 69 | + public function getClassName ( $ref ) { |
|
| 70 | + $reflection = new \ReflectionClass( $ref ); |
|
| 71 | + return $reflection->getName(); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Retrieve class repository name |
|
| 76 | - * @param $comment_class |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function classRepository($comment_class) |
|
| 80 | - { |
|
| 81 | - $comment_class_name = $comment_class; |
|
| 82 | - $explode = explode('\\',$comment_class_name); |
|
| 74 | + /** |
|
| 75 | + * Retrieve class repository name |
|
| 76 | + * @param $comment_class |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function classRepository($comment_class) |
|
| 80 | + { |
|
| 81 | + $comment_class_name = $comment_class; |
|
| 82 | + $explode = explode('\\',$comment_class_name); |
|
| 83 | 83 | |
| 84 | - return $explode[0] . $explode[1] . ":" . $explode[3]; |
|
| 85 | - } |
|
| 84 | + return $explode[0] . $explode[1] . ":" . $explode[3]; |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -12,14 +12,14 @@ |
||
| 12 | 12 | |
| 13 | 13 | class CommentRepository extends EntityRepository |
| 14 | 14 | { |
| 15 | - public function deleteByCommentIds(array $ids) |
|
| 16 | - { |
|
| 17 | - return $this->_em->createQueryBuilder() |
|
| 18 | - ->delete("MykeesCommentBundle:Comment",'c') |
|
| 19 | - ->where("c.id IN(:ids)") |
|
| 20 | - ->setParameter('ids', array_values($ids)) |
|
| 21 | - ->getQuery() |
|
| 22 | - ->execute() |
|
| 23 | - ; |
|
| 24 | - } |
|
| 15 | + public function deleteByCommentIds(array $ids) |
|
| 16 | + { |
|
| 17 | + return $this->_em->createQueryBuilder() |
|
| 18 | + ->delete("MykeesCommentBundle:Comment",'c') |
|
| 19 | + ->where("c.id IN(:ids)") |
|
| 20 | + ->setParameter('ids', array_values($ids)) |
|
| 21 | + ->getQuery() |
|
| 22 | + ->execute() |
|
| 23 | + ; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -1,10 +1,10 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Created by PhpStorm. |
|
| 4 | - * User: Rafidion Michael |
|
| 5 | - * Date: 23/04/2015 |
|
| 6 | - * Time: 02:28 |
|
| 7 | - */ |
|
| 3 | + * Created by PhpStorm. |
|
| 4 | + * User: Rafidion Michael |
|
| 5 | + * Date: 23/04/2015 |
|
| 6 | + * Time: 02:28 |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | namespace Mykees\CommentBundle\Tests\Controller; |
| 10 | 10 | |
@@ -15,32 +15,32 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | protected $client; |
| 17 | 17 | protected $container; |
| 18 | - protected $manager; |
|
| 19 | - protected $query_manager; |
|
| 18 | + protected $manager; |
|
| 19 | + protected $query_manager; |
|
| 20 | 20 | protected $em; |
| 21 | 21 | protected $commentClass; |
| 22 | 22 | protected $request; |
| 23 | 23 | |
| 24 | - public function setUp() |
|
| 25 | - { |
|
| 26 | - |
|
| 24 | + public function setUp() |
|
| 25 | + { |
|
| 27 | 26 | |
| 28 | - $this->client = static::createClient(); |
|
| 29 | - $this->container = $this->client->getContainer(); |
|
| 30 | - $this->manager = $this->container->get('mykees.comment.manager'); |
|
| 31 | - $this->query_manager = $this->container->get('mykees.comment.query.manager'); |
|
| 32 | - $this->em = static::$kernel->getContainer() |
|
| 33 | - ->get('doctrine') |
|
| 34 | - ->getManager() |
|
| 35 | - ; |
|
| 36 | 27 | |
| 37 | - $fixtures = [ |
|
| 38 | - 'Mykees\CommentBundle\DataFixtures\ORM\LoadCommentData', |
|
| 39 | - 'Mvc\BlogBundle\DataFixtures\ORM\LoadPostsData', |
|
| 40 | - ]; |
|
| 41 | - $this->loadFixtures($fixtures); |
|
| 42 | - parent::setUp(); |
|
| 43 | - } |
|
| 28 | + $this->client = static::createClient(); |
|
| 29 | + $this->container = $this->client->getContainer(); |
|
| 30 | + $this->manager = $this->container->get('mykees.comment.manager'); |
|
| 31 | + $this->query_manager = $this->container->get('mykees.comment.query.manager'); |
|
| 32 | + $this->em = static::$kernel->getContainer() |
|
| 33 | + ->get('doctrine') |
|
| 34 | + ->getManager() |
|
| 35 | + ; |
|
| 36 | + |
|
| 37 | + $fixtures = [ |
|
| 38 | + 'Mykees\CommentBundle\DataFixtures\ORM\LoadCommentData', |
|
| 39 | + 'Mvc\BlogBundle\DataFixtures\ORM\LoadPostsData', |
|
| 40 | + ]; |
|
| 41 | + $this->loadFixtures($fixtures); |
|
| 42 | + parent::setUp(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | public function testCountCommentHtmlList() |
| 46 | 46 | { |
@@ -49,150 +49,150 @@ discard block |
||
| 49 | 49 | $this->assertEquals(3,$crawler->filter('.comment-list')->count()); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - public function testRemoveAssociateComment() |
|
| 53 | - { |
|
| 54 | - $this->client->request('GET', '/admin/delete/5'); |
|
| 55 | - $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 52 | + public function testRemoveAssociateComment() |
|
| 53 | + { |
|
| 54 | + $this->client->request('GET', '/admin/delete/5'); |
|
| 55 | + $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 56 | + |
|
| 57 | + $count = count($this->query_manager->findAllComments()); |
|
| 58 | + |
|
| 59 | + $this->assertEquals(4, $count); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function testAddComment() |
|
| 63 | + { |
|
| 64 | + $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 65 | + $this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction', $this->client->getRequest()->attributes->get('_controller')); |
|
| 66 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 67 | + 'mykees_comment[username]'=>'Mykees', |
|
| 68 | + 'mykees_comment[email]'=>'[email protected]', |
|
| 69 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 70 | + 'mykees_comment[parentId]'=>0, |
|
| 71 | + 'mykees_comment[model]'=>'Post', |
|
| 72 | + 'mykees_comment[modelId]'=>1, |
|
| 73 | + ]); |
|
| 74 | + |
|
| 75 | + $this->client->submit($form); |
|
| 76 | + |
|
| 77 | + $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 78 | + |
|
| 79 | + $count = count($this->query_manager->findAllComments()); |
|
| 80 | + |
|
| 81 | + $this->assertEquals(6, $count); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function testAddCommentWithEmptyName() |
|
| 85 | + { |
|
| 86 | + $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 87 | + $this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction', $this->client->getRequest()->attributes->get('_controller')); |
|
| 88 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 89 | + 'mykees_comment[username]'=>'', |
|
| 90 | + 'mykees_comment[email]'=>'[email protected]', |
|
| 91 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 92 | + 'mykees_comment[parentId]'=>0, |
|
| 93 | + 'mykees_comment[model]'=>'Post', |
|
| 94 | + 'mykees_comment[modelId]'=>1, |
|
| 95 | + ]); |
|
| 96 | + |
|
| 97 | + $this->client->submit($form); |
|
| 98 | + |
|
| 99 | + $count = count($this->query_manager->findAllComments()); |
|
| 100 | + |
|
| 101 | + $this->assertEquals(5, $count); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function testAddCommentWithWrongEmailFormat() |
|
| 105 | + { |
|
| 106 | + |
|
| 107 | + $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 108 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 109 | + 'mykees_comment[username]'=>'Mykees', |
|
| 110 | + 'mykees_comment[email]'=>'contact.fr', |
|
| 111 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 112 | + 'mykees_comment[parentId]'=>0, |
|
| 113 | + 'mykees_comment[model]'=>'Post', |
|
| 114 | + 'mykees_comment[modelId]'=>1, |
|
| 115 | + ]); |
|
| 116 | + |
|
| 117 | + $this->client->submit($form); |
|
| 118 | + |
|
| 119 | + $count = count($this->query_manager->findAllComments()); |
|
| 120 | + |
|
| 121 | + $this->assertEquals(5, $count); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + public function testAddCommmentWithAChild() |
|
| 126 | + { |
|
| 127 | + $crawler = $this->client->request('GET','/blog/title-2'); |
|
| 128 | + |
|
| 129 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 130 | + 'mykees_comment[username]'=>'Mykees', |
|
| 131 | + 'mykees_comment[email]'=>'[email protected]', |
|
| 132 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 133 | + 'mykees_comment[parentId]'=>0, |
|
| 134 | + 'mykees_comment[model]'=>'Post', |
|
| 135 | + 'mykees_comment[modelId]'=>1, |
|
| 136 | + ]); |
|
| 137 | + $this->client->submit($form); |
|
| 138 | + |
|
| 139 | + $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 140 | + |
|
| 141 | + $count = count($this->query_manager->findAllComments()); |
|
| 142 | + |
|
| 143 | + $this->assertEquals(6, $count); |
|
| 144 | + |
|
| 145 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 146 | + 'mykees_comment[username]'=>'Marion', |
|
| 147 | + 'mykees_comment[email]'=>'[email protected]', |
|
| 148 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 149 | + 'mykees_comment[parentId]'=>27, |
|
| 150 | + 'mykees_comment[model]'=>'Post', |
|
| 151 | + 'mykees_comment[modelId]'=>1, |
|
| 152 | + ]); |
|
| 153 | + $this->client->submit($form); |
|
| 56 | 154 | |
| 57 | - $count = count($this->query_manager->findAllComments()); |
|
| 58 | - |
|
| 59 | - $this->assertEquals(4, $count); |
|
| 60 | - } |
|
| 155 | + $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 61 | 156 | |
| 62 | - public function testAddComment() |
|
| 63 | - { |
|
| 64 | - $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 65 | - $this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction', $this->client->getRequest()->attributes->get('_controller')); |
|
| 66 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 67 | - 'mykees_comment[username]'=>'Mykees', |
|
| 68 | - 'mykees_comment[email]'=>'[email protected]', |
|
| 69 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 70 | - 'mykees_comment[parentId]'=>0, |
|
| 71 | - 'mykees_comment[model]'=>'Post', |
|
| 72 | - 'mykees_comment[modelId]'=>1, |
|
| 73 | - ]); |
|
| 74 | - |
|
| 75 | - $this->client->submit($form); |
|
| 76 | - |
|
| 77 | - $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 78 | - |
|
| 79 | - $count = count($this->query_manager->findAllComments()); |
|
| 80 | - |
|
| 81 | - $this->assertEquals(6, $count); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function testAddCommentWithEmptyName() |
|
| 85 | - { |
|
| 86 | - $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 87 | - $this->assertEquals('Mvc\BlogBundle\Controller\BlogController::showAction', $this->client->getRequest()->attributes->get('_controller')); |
|
| 88 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 89 | - 'mykees_comment[username]'=>'', |
|
| 90 | - 'mykees_comment[email]'=>'[email protected]', |
|
| 91 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 92 | - 'mykees_comment[parentId]'=>0, |
|
| 93 | - 'mykees_comment[model]'=>'Post', |
|
| 94 | - 'mykees_comment[modelId]'=>1, |
|
| 95 | - ]); |
|
| 96 | - |
|
| 97 | - $this->client->submit($form); |
|
| 98 | - |
|
| 99 | - $count = count($this->query_manager->findAllComments()); |
|
| 100 | - |
|
| 101 | - $this->assertEquals(5, $count); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public function testAddCommentWithWrongEmailFormat() |
|
| 105 | - { |
|
| 106 | - |
|
| 107 | - $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 108 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 109 | - 'mykees_comment[username]'=>'Mykees', |
|
| 110 | - 'mykees_comment[email]'=>'contact.fr', |
|
| 111 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 112 | - 'mykees_comment[parentId]'=>0, |
|
| 113 | - 'mykees_comment[model]'=>'Post', |
|
| 114 | - 'mykees_comment[modelId]'=>1, |
|
| 115 | - ]); |
|
| 116 | - |
|
| 117 | - $this->client->submit($form); |
|
| 118 | - |
|
| 119 | - $count = count($this->query_manager->findAllComments()); |
|
| 120 | - |
|
| 121 | - $this->assertEquals(5, $count); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - public function testAddCommmentWithAChild() |
|
| 126 | - { |
|
| 127 | - $crawler = $this->client->request('GET','/blog/title-2'); |
|
| 128 | - |
|
| 129 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 130 | - 'mykees_comment[username]'=>'Mykees', |
|
| 131 | - 'mykees_comment[email]'=>'[email protected]', |
|
| 132 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 133 | - 'mykees_comment[parentId]'=>0, |
|
| 134 | - 'mykees_comment[model]'=>'Post', |
|
| 135 | - 'mykees_comment[modelId]'=>1, |
|
| 136 | - ]); |
|
| 137 | - $this->client->submit($form); |
|
| 138 | - |
|
| 139 | - $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 140 | - |
|
| 141 | - $count = count($this->query_manager->findAllComments()); |
|
| 142 | - |
|
| 143 | - $this->assertEquals(6, $count); |
|
| 144 | - |
|
| 145 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 146 | - 'mykees_comment[username]'=>'Marion', |
|
| 147 | - 'mykees_comment[email]'=>'[email protected]', |
|
| 148 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 149 | - 'mykees_comment[parentId]'=>27, |
|
| 150 | - 'mykees_comment[model]'=>'Post', |
|
| 151 | - 'mykees_comment[modelId]'=>1, |
|
| 152 | - ]); |
|
| 153 | - $this->client->submit($form); |
|
| 154 | - |
|
| 155 | - $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 156 | - |
|
| 157 | - $count = count($this->query_manager->findAllComments()); |
|
| 158 | - $this->assertEquals(7, $count); |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - $count = count($this->query_manager->findAllComments(['parentId'=>27])); |
|
| 162 | - $this->assertEquals(1, $count); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - public function testPreDeleteComment() |
|
| 167 | - { |
|
| 168 | - $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 169 | - $form = $crawler->selectButton('Poster')->form([ |
|
| 170 | - 'mykees_comment[username]'=>'Mykees', |
|
| 171 | - 'mykees_comment[email]'=>'[email protected]', |
|
| 172 | - 'mykees_comment[content]'=>'Salut les guedins', |
|
| 173 | - 'mykees_comment[parentId]'=>0, |
|
| 174 | - 'mykees_comment[model]'=>'Post', |
|
| 175 | - 'mykees_comment[modelId]'=>25, |
|
| 176 | - ]); |
|
| 177 | - $this->client->submit($form); |
|
| 178 | - $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 179 | - $count = count($this->query_manager->findAllComments()); |
|
| 180 | - $this->assertEquals(6, $count); |
|
| 181 | - |
|
| 182 | - |
|
| 183 | - $post = $this->em->getRepository('MvcBlogBundle:Post')->find(25); |
|
| 184 | - $this->em->remove($post); |
|
| 185 | - $this->em->flush(); |
|
| 186 | - |
|
| 187 | - $count = count($this->query_manager->findAllComments()); |
|
| 188 | - $this->assertEquals(5, $count); |
|
| 189 | - } |
|
| 157 | + $count = count($this->query_manager->findAllComments()); |
|
| 158 | + $this->assertEquals(7, $count); |
|
| 190 | 159 | |
| 191 | 160 | |
| 192 | - public function testWithCommentDepthToZero(){ |
|
| 193 | - $crawler = $this->client->request('GET', '/blog/title-1'); |
|
| 194 | - $this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
|
| 195 | - $this->assertEquals(0,$crawler->filter('.reply')->count()); |
|
| 196 | - } |
|
| 161 | + $count = count($this->query_manager->findAllComments(['parentId'=>27])); |
|
| 162 | + $this->assertEquals(1, $count); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + public function testPreDeleteComment() |
|
| 167 | + { |
|
| 168 | + $crawler = $this->client->request('GET','/blog/title-1'); |
|
| 169 | + $form = $crawler->selectButton('Poster')->form([ |
|
| 170 | + 'mykees_comment[username]'=>'Mykees', |
|
| 171 | + 'mykees_comment[email]'=>'[email protected]', |
|
| 172 | + 'mykees_comment[content]'=>'Salut les guedins', |
|
| 173 | + 'mykees_comment[parentId]'=>0, |
|
| 174 | + 'mykees_comment[model]'=>'Post', |
|
| 175 | + 'mykees_comment[modelId]'=>25, |
|
| 176 | + ]); |
|
| 177 | + $this->client->submit($form); |
|
| 178 | + $this->assertEquals(302,$this->client->getResponse()->getStatusCode()); |
|
| 179 | + $count = count($this->query_manager->findAllComments()); |
|
| 180 | + $this->assertEquals(6, $count); |
|
| 181 | + |
|
| 182 | + |
|
| 183 | + $post = $this->em->getRepository('MvcBlogBundle:Post')->find(25); |
|
| 184 | + $this->em->remove($post); |
|
| 185 | + $this->em->flush(); |
|
| 186 | + |
|
| 187 | + $count = count($this->query_manager->findAllComments()); |
|
| 188 | + $this->assertEquals(5, $count); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + public function testWithCommentDepthToZero(){ |
|
| 193 | + $crawler = $this->client->request('GET', '/blog/title-1'); |
|
| 194 | + $this->assertEquals(200,$this->client->getResponse()->getStatusCode()); |
|
| 195 | + $this->assertEquals(0,$crawler->filter('.reply')->count()); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | 198 | } |
| 199 | 199 | \ No newline at end of file |
@@ -19,9 +19,9 @@ |
||
| 19 | 19 | new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), |
| 20 | 20 | |
| 21 | 21 | new Mvc\BlogBundle\MvcBlogBundle(), |
| 22 | - new Mykees\CommentBundle\MykeesCommentBundle(), |
|
| 23 | - new Mykees\MediaBundle\MykeesMediaBundle(), |
|
| 24 | - new Mykees\TagBundle\MykeesTagBundle(), |
|
| 22 | + new Mykees\CommentBundle\MykeesCommentBundle(), |
|
| 23 | + new Mykees\MediaBundle\MykeesMediaBundle(), |
|
| 24 | + new Mykees\TagBundle\MykeesTagBundle(), |
|
| 25 | 25 | new Liip\FunctionalTestBundle\LiipFunctionalTestBundle(), |
| 26 | 26 | ); |
| 27 | 27 | |
@@ -13,115 +13,115 @@ |
||
| 13 | 13 | |
| 14 | 14 | class CommentExtension extends \Twig_Extension{ |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Returns a list of functions to add to the existing list. |
|
| 18 | - * |
|
| 19 | - * @return array An array of functions |
|
| 20 | - */ |
|
| 21 | - public function getFunctions() |
|
| 22 | - { |
|
| 23 | - return array( |
|
| 24 | - new \Twig_SimpleFunction('helper_comment_form', [$this, 'commentForm'], [ |
|
| 25 | - 'is_safe'=>array('html'), |
|
| 26 | - 'needs_environment'=>true |
|
| 27 | - ]), |
|
| 28 | - new \Twig_SimpleFunction('helper_comments_list', [$this, 'commentsList'], [ |
|
| 29 | - 'is_safe'=>['html'], |
|
| 30 | - 'needs_environment'=>true |
|
| 31 | - ]), |
|
| 32 | - new \Twig_SimpleFunction('date_interval', [$this, 'dateInterval'], [ |
|
| 33 | - 'is_safe'=>array('html') |
|
| 34 | - ]), |
|
| 35 | - ); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function getFilters() { |
|
| 39 | - return array( |
|
| 40 | - 'url_decode' => new \Twig_Filter_Method($this, 'urlDecode') |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - public function commentForm(\Twig_Environment $env, $form, $ajax=false, $options=[]) |
|
| 46 | - { |
|
| 47 | - return $env->render('MykeesCommentBundle:Comments:form.html.twig',[ |
|
| 48 | - 'form'=>$form, |
|
| 49 | - 'ajax'=>$ajax, |
|
| 50 | - 'options'=>$options |
|
| 51 | - ]); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param \Twig_Environment $env |
|
| 56 | - * @param $comments |
|
| 57 | - * @param array $options |
|
| 58 | - * @param bool $canAdminComment |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public function commentsList(\Twig_Environment $env,$comments, $canAdminComment=false, $options=[]) |
|
| 62 | - { |
|
| 63 | - return $env->render('MykeesCommentBundle:Comments:comments.html.twig',[ |
|
| 64 | - 'comments'=>$comments, |
|
| 65 | - "canAdminComment"=>$canAdminComment |
|
| 66 | - ]); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - public function dateInterval($date,$locale) |
|
| 71 | - { |
|
| 72 | - $comment_date = new \DateTime($date); |
|
| 73 | - $dateNow = new \DateTime('NOW'); |
|
| 74 | - $interval = $dateNow->diff($comment_date); |
|
| 75 | - |
|
| 76 | - $periodes = [ |
|
| 77 | - $interval->format('%y'), |
|
| 78 | - $interval->format('%m'), |
|
| 79 | - $interval->format('%d'), |
|
| 80 | - $interval->format('%h'), |
|
| 81 | - $interval->format('%i') |
|
| 82 | - ]; |
|
| 83 | - $unity_en = [ |
|
| 84 | - ['year','years'], |
|
| 85 | - ['month','months'], |
|
| 86 | - ['day','days'], |
|
| 87 | - ['hour','hours'], |
|
| 88 | - ['minute','minutes'] |
|
| 89 | - ]; |
|
| 90 | - $unity_fr = [ |
|
| 91 | - ['an','ans'], |
|
| 92 | - ['mois','mois'], |
|
| 93 | - ['jour','jours'], |
|
| 94 | - ['heure','heures'], |
|
| 95 | - ['minute','minutes'] |
|
| 96 | - ]; |
|
| 97 | - |
|
| 98 | - $periodesLength = count($periodes); |
|
| 99 | - |
|
| 100 | - for($i=0; $i < $periodesLength; $i++) |
|
| 101 | - { |
|
| 102 | - if(intval($periodes[$i]) >= 1 && $i < count($periodes)) |
|
| 103 | - { |
|
| 104 | - $locale_unity = $locale == "fr" ? $unity_fr : $unity_en; |
|
| 105 | - $u = $periodes[$i] > 1 ? $locale_unity[$i][1] : $locale_unity[$i][0]; |
|
| 106 | - |
|
| 107 | - return $periodes[$i].' '.$u; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - if($locale == 'fr') |
|
| 111 | - { |
|
| 112 | - return "moins d'une minute"; |
|
| 113 | - }else{ |
|
| 114 | - return 'less than a minute'; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Returns the name of the extension. |
|
| 120 | - * |
|
| 121 | - * @return string The extension name |
|
| 122 | - */ |
|
| 123 | - public function getName() |
|
| 124 | - { |
|
| 125 | - return 'mykees_helper_comment'; |
|
| 126 | - } |
|
| 16 | + /** |
|
| 17 | + * Returns a list of functions to add to the existing list. |
|
| 18 | + * |
|
| 19 | + * @return array An array of functions |
|
| 20 | + */ |
|
| 21 | + public function getFunctions() |
|
| 22 | + { |
|
| 23 | + return array( |
|
| 24 | + new \Twig_SimpleFunction('helper_comment_form', [$this, 'commentForm'], [ |
|
| 25 | + 'is_safe'=>array('html'), |
|
| 26 | + 'needs_environment'=>true |
|
| 27 | + ]), |
|
| 28 | + new \Twig_SimpleFunction('helper_comments_list', [$this, 'commentsList'], [ |
|
| 29 | + 'is_safe'=>['html'], |
|
| 30 | + 'needs_environment'=>true |
|
| 31 | + ]), |
|
| 32 | + new \Twig_SimpleFunction('date_interval', [$this, 'dateInterval'], [ |
|
| 33 | + 'is_safe'=>array('html') |
|
| 34 | + ]), |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function getFilters() { |
|
| 39 | + return array( |
|
| 40 | + 'url_decode' => new \Twig_Filter_Method($this, 'urlDecode') |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + public function commentForm(\Twig_Environment $env, $form, $ajax=false, $options=[]) |
|
| 46 | + { |
|
| 47 | + return $env->render('MykeesCommentBundle:Comments:form.html.twig',[ |
|
| 48 | + 'form'=>$form, |
|
| 49 | + 'ajax'=>$ajax, |
|
| 50 | + 'options'=>$options |
|
| 51 | + ]); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param \Twig_Environment $env |
|
| 56 | + * @param $comments |
|
| 57 | + * @param array $options |
|
| 58 | + * @param bool $canAdminComment |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public function commentsList(\Twig_Environment $env,$comments, $canAdminComment=false, $options=[]) |
|
| 62 | + { |
|
| 63 | + return $env->render('MykeesCommentBundle:Comments:comments.html.twig',[ |
|
| 64 | + 'comments'=>$comments, |
|
| 65 | + "canAdminComment"=>$canAdminComment |
|
| 66 | + ]); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + public function dateInterval($date,$locale) |
|
| 71 | + { |
|
| 72 | + $comment_date = new \DateTime($date); |
|
| 73 | + $dateNow = new \DateTime('NOW'); |
|
| 74 | + $interval = $dateNow->diff($comment_date); |
|
| 75 | + |
|
| 76 | + $periodes = [ |
|
| 77 | + $interval->format('%y'), |
|
| 78 | + $interval->format('%m'), |
|
| 79 | + $interval->format('%d'), |
|
| 80 | + $interval->format('%h'), |
|
| 81 | + $interval->format('%i') |
|
| 82 | + ]; |
|
| 83 | + $unity_en = [ |
|
| 84 | + ['year','years'], |
|
| 85 | + ['month','months'], |
|
| 86 | + ['day','days'], |
|
| 87 | + ['hour','hours'], |
|
| 88 | + ['minute','minutes'] |
|
| 89 | + ]; |
|
| 90 | + $unity_fr = [ |
|
| 91 | + ['an','ans'], |
|
| 92 | + ['mois','mois'], |
|
| 93 | + ['jour','jours'], |
|
| 94 | + ['heure','heures'], |
|
| 95 | + ['minute','minutes'] |
|
| 96 | + ]; |
|
| 97 | + |
|
| 98 | + $periodesLength = count($periodes); |
|
| 99 | + |
|
| 100 | + for($i=0; $i < $periodesLength; $i++) |
|
| 101 | + { |
|
| 102 | + if(intval($periodes[$i]) >= 1 && $i < count($periodes)) |
|
| 103 | + { |
|
| 104 | + $locale_unity = $locale == "fr" ? $unity_fr : $unity_en; |
|
| 105 | + $u = $periodes[$i] > 1 ? $locale_unity[$i][1] : $locale_unity[$i][0]; |
|
| 106 | + |
|
| 107 | + return $periodes[$i].' '.$u; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + if($locale == 'fr') |
|
| 111 | + { |
|
| 112 | + return "moins d'une minute"; |
|
| 113 | + }else{ |
|
| 114 | + return 'less than a minute'; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Returns the name of the extension. |
|
| 120 | + * |
|
| 121 | + * @return string The extension name |
|
| 122 | + */ |
|
| 123 | + public function getName() |
|
| 124 | + { |
|
| 125 | + return 'mykees_helper_comment'; |
|
| 126 | + } |
|
| 127 | 127 | } |