src/EventListener/NewsEventListener.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Event\Model\DataObjectEvent;
  4. use Pimcore\Model\DataObject\Comment;
  5. use Pimcore\Model\DataObject\News;
  6. use App\Services\NewsServices;
  7. class NewsEventListener
  8. {
  9.     public function postAdd(DataObjectEvent $e
  10.     {
  11.         $object $e->getObject();
  12.         self::coutingTotalComment($object);
  13.     }
  14.     public function postUpdate(DataObjectEvent $e)
  15.     {
  16.         $object $e->getObject();
  17.         self::coutingTotalComment($object);
  18.         if ($object instanceof News && !empty($object->getLocalizedfields()->getItems())) {
  19.             foreach ($object->getLocalizedfields()->getItems() as $key => $item) {
  20.                 if ($key == "vi") {
  21.                     NewsServices::deleteFolder($item['title']);
  22.                     $content $item['content'];
  23.                     preg_match_all('/<img\s+[^>]*src="([^"]*)"[^>]*>/i'$content$matches);
  24.                     if(empty($matches[0])){
  25.                         preg_match_all('/src=["\']?(data:image\/[^;]+;base64,[^"\']+)["\']?/i'$content$matches);
  26.                     }
  27.                     $allImgTags $matches[0];
  28.                     $allSrcValues $matches[1];
  29.                     if (!empty($allSrcValues)) {
  30.                         foreach ($allSrcValues as $imgData) {
  31.                             if (strpos($imgData'base64') !== false) {
  32.                                 $charactersToRemove = ['"'"'"'\\''/'':'];
  33.                                 $item['title'] = str_replace($charactersToRemove''$item['title']);
  34.                                 $fullpath NewsServices::saveBase64Image($imgData$item['title'], $item['title'] . rand(1100));
  35.                                 $content str_replace($imgData$fullpath$content);
  36.                             }
  37.                         }
  38.                     }
  39.                     $item['content'] = $content;
  40.                     $object->getLocalizedfields()->setLocalizedValue('content'$content'vi');
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     private static function coutingTotalComment($object)
  46.     {
  47.         if($object instanceof Comment){
  48.             if($object->getPublished()){
  49.                 // Lấy id bài viết
  50.                 $idNews $object->getCommentObject()->getId();
  51.                 $comment = new Comment\Listing();
  52.                 $comment->addConditionParam('commentObject__id = ?',$idNews);
  53.                 $news $object->getCommentObject();
  54.                 
  55.                 //Cộng thêm số comment
  56.                 $totalComment$comment->count();
  57.                 $news->setTotalComment($totalComment);
  58.                 $news->save();
  59.             }
  60.         }
  61.     }
  62.     public function postDelete(DataObjectEvent $e
  63.     {
  64.         $object $e->getObject();
  65.         if($object instanceof Comment){
  66.             if($object->getCommentObject()){
  67.                 $news $object->getCommentObject();
  68.                 //Trừ thêm số comment
  69.                 if($news->getTotalComment()||$news->getTotalComment() > 0){
  70.                     $totalComment$news->getTotalComment()-1;
  71.                     $news->setTotalComment($totalComment);
  72.                   
  73.                 }else{
  74.                     $news->setTotalComment(0); 
  75.                 }
  76.                 $news->save();
  77.                 
  78.             }
  79.         }
  80.     }
  81. }