app/Plugin/TabaCMS2/Repository/PostRepository.php line 152

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) SPREAD WORKS Inc. All Rights Reserved.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Plugin\TabaCMS2\Repository;
  9. use Plugin\TabaCMS2\Common\Constants;
  10. use Plugin\TabaCMS2\Entity\Post;
  11. use Eccube\Repository\AbstractRepository;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. class PostRepository extends AbstractRepository
  14. {
  15.     private $diff_time "";
  16.     /**
  17.      *
  18.      * @param ManagerRegistry $registry
  19.      * @param string $entityClass
  20.      */
  21.     public function __construct(ManagerRegistry $registry$entityClass Post::class)
  22.     {
  23.         parent::__construct($registry$entityClass);
  24.         // datetime型はDB登録時に強制的にUTCに変更される。
  25.         // そのため、SQL発行時に、時間を指定する場合は
  26.         // ズレれている分を増減する必要がある。
  27.         if (($diff date("P")) != "+00:00") {
  28.             $sign = (strstr($diff,"+") ? "-" "+");
  29.             $diff str_replace(array("+","-"),"",$diff);
  30.             $hour = (int) strstr($diff,":",true);
  31.             $minute = (int) substr(strrchr($diff,":"),1);
  32.             $this->diff_time " " $sign $hour " hour";
  33.             if ($minute$this->diff_time .= " " $sign $minute " minute";
  34.         }
  35.     }
  36.     /**
  37.      * 投稿リストのQueryBuilderを生成します。
  38.      *
  39.      * @param integer $type_id
  40.      *            投稿タイプID
  41.      * @return mixed Doctrine\ORM\Query::getResult()
  42.      */
  43.     public function createListQueryBuilder($condition null$sort null)
  44.     {
  45.         $qb_post $this->getEntityManager()->createQueryBuilder();
  46.         $qb_post->from(Constants::$ENTITY['POST'], 'p');
  47.         $qb_post->select('p');
  48.         // 抽出条件
  49.         if (! empty($condition)) {
  50.             // 投稿タイプ
  51.             if (! empty($condition['type_id'])) {
  52.                 $qb_post->andWhere('p.typeId = :type_id')->setParameter('type_id'$condition['type_id']);
  53.             }
  54.             // データキー
  55.             if (! empty($condition['data_key'])) {
  56.                 $qb_post->andWhere('p.dataKey = :data_key')->setParameter('data_key'$condition['data_key']);
  57.             }
  58.             // 公開区分
  59.             if (isset($condition['public_div']) && ($condition['public_div'] || $condition['public_div'] === 0)) {
  60.                 $qb_post->andWhere('p.publicDiv = :publicDiv')->setParameter('publicDiv'$condition['public_div']);
  61.             }
  62.             // 公開状態
  63.             if (!empty($condition['is_public']) && $condition['is_public']) {
  64.                 if ($this->diff_time) {
  65.                     $now_date date('Y-m-d H:i:s',strtotime($this->diff_time));
  66.                 } else {
  67.                     $now_date date('Y-m-d H:i:s');
  68.                 }
  69.                 $qb_post->andWhere('p.publicDiv = :publicDiv')->setParameter('publicDiv'Post::PUBLIC_DIV_PUBLIC);
  70.                 $qb_post->andWhere('p.publicDate <= :publicDate')->setParameter('publicDate',$now_date);
  71.             }
  72.             // カテゴリー
  73.             if (isset($condition['category_id']) && !empty($condition['category_id'])) {
  74.                 $qb_post->andWhere('p.categoryId = :category_id')->setParameter('category_id'$condition['category_id']);
  75.             }
  76.             // タグ
  77.             if (isset($condition['tag_id']) && !empty($condition['tag_id'])) {
  78.                 if (is_array($condition['tag_id'])) {
  79.                     $joinOn 'p.postId = pt.postId';
  80.                     // foreach ($condition['tag_id'] as $tag_id) {
  81.                     //     $joinOn .= ' AND pt.tagId = :tag_id_' . $tag_id;
  82.                     // }
  83.                     $qb_post->join(Constants::$ENTITY['POST_TAG'],'pt','WITH',$joinOn);
  84.                     // foreach ($condition['tag_id'] as $tag_id) {
  85.                     //     $qb_post->setParameter('tag_id_' . $tag_id,$tag_id);
  86.                     // }
  87.                     // OR 検索
  88.                     $qb_post->andWhere($qb_post->expr()->in('pt.tagId',$condition['tag_id']));
  89.                 } else {
  90.                     $qb_post->join(Constants::$ENTITY['POST_TAG'],'pt','WITH','p.postId = pt.postId AND pt.tagId = :tagId');
  91.                     $qb_post->setParameter('tagId'$condition['tag_id']);
  92.                 }
  93.             }
  94.             // キーワード
  95.             if (isset($condition['keyword']) && !empty($condition['keyword'])) {
  96.                 $condition['keyword'] = preg_replace('/ {2,}/',' ',trim(preg_replace('/( | {2,})/',' ',$condition['keyword'])));
  97.                 $keywords explode(' ',$condition['keyword']);
  98.                 $i 0;
  99.                 foreach ($keywords as $keyword) {
  100.                     $qb_post->andWhere('p.title LIKE :title'.$i.' OR p.body LIKE :body'.$i.'');
  101.                     $qb_post->setParameter('title'.$i,'%' $keyword '%');
  102.                     $qb_post->setParameter('body'.$i,'%' $keyword '%');
  103.                     $i++;
  104.                 }
  105.             }
  106.             // ルーティング名が設定されているデータを抽出
  107.             if (! empty($condition['is_overwrite_route'])) {
  108.                 $qb_post->andWhere('p.overwriteRoute IS NOT NULL');
  109.             }
  110.         }
  111.         // ソート
  112.         $qb_post->addOrderBy('p.priorityFlg''DESC');
  113.         if (! empty($sort) && ! empty($sort['key'])) {
  114.             if (! empty($sort['order'])) {
  115.                 $qb_post->addOrderBy('p.' $sort['key'], $sort['order']);
  116.             } else {
  117.                 $qb_post->addOrderBy('p.' $sort['key'], 'DESC');
  118.             }
  119.         } else {
  120.             $qb_post->addOrderBy('p.publicDate''DESC');
  121.         }
  122.         return $qb_post;
  123.     }
  124.     /**
  125.      * 投稿リストデータを取得します。
  126.      *
  127.      * @param integer $type_id 投稿タイプID
  128.      * @return mixed Doctrine\ORM\Query::getResult()
  129.      */
  130.     public function getList($condition null$sort null)
  131.     {
  132.         $qb_post $this->createListQueryBuilder($condition$sort);
  133.         $query_post $qb_post->getQuery();
  134.         $res_post $query_post->getResult();
  135.         return $res_post;
  136.     }
  137.     /**
  138.      * @param string|array $data_key データキー|検索条件配列
  139.      */
  140.     public function get($data_key)
  141.     {
  142.         $list null;
  143.         if (is_array($data_key)) {
  144.             if (($list $this->getList($data_key)) && count($list) >= 1) {
  145.                 return $list[0];
  146.             }
  147.         } else {
  148.             if (($list $this->getList(array(
  149.                 "data_key" => $data_key
  150.             ))) && count($list) >= 1) {
  151.                 return $list[0];
  152.             }
  153.         }
  154.         return null;
  155.     }
  156.     /**
  157.      * 保存
  158.      *
  159.      * @param \Plugin\TabaCMS2\Entity\Post $entity
  160.      * @return boolean 成功した場合 true
  161.      */
  162.     public function save($entity)
  163.     {
  164.         $em $this->getEntityManager();
  165.         $em->getConnection()->beginTransaction();
  166.         try {
  167.             $em->persist($entity);
  168.             $em->flush();
  169.             $em->getConnection()->commit();
  170.         } catch (\Exception $e) {
  171.             log_error('投稿登録エラー', array(
  172.                 $e->getMessage()
  173.             ));
  174.             $em->getConnection()->rollback();
  175.             return false;
  176.         }
  177.         return true;
  178.     }
  179.     /**
  180.      * 削除
  181.      *
  182.      * @param \Plugin\TabaCMS2\Entity\Post $entity
  183.      * @return boolean
  184.      */
  185.     public function delete($entity)
  186.     {
  187.         $em $this->getEntityManager();
  188.         $em->getConnection()->beginTransaction();
  189.         try {
  190.             $em->remove($entity);
  191.             $em->flush();
  192.             $em->getConnection()->commit();
  193.         } catch (\Exception $e) {
  194.             log_error('投稿削除エラー', array(
  195.                 $e->getMessage()
  196.             ));
  197.             $em->getConnection()->rollback();
  198.             return false;
  199.         }
  200.         return true;
  201.     }
  202. }