src/AppBundle/Controller/FacilityDisplayBaseController.php line 13

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Component\Util\Hash;
  4. use Doctrine\ORM\EntityNotFoundException;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use AppBundle\Controller\BaseController;
  9. use AppBundle\Component\PageBuilder\PageBuilder;
  10. use AppBundle\Entity\Master\Facility;
  11. class FacilityDisplayBaseController extends BaseController
  12. {
  13.     public $facility null;
  14.     protected $entityClass null;
  15.     protected $categoryClass null;
  16.     protected $formClass null;
  17.     protected $redirect_route null;
  18.     protected $default_setting null;
  19.     protected $entity_setting null;
  20.     public $views = [
  21.         "top" => "",
  22.         "index" => "",
  23.         "detail" => "",
  24.         "facility_index" => "",
  25.         "facility_detail" => ""
  26.     ];
  27.     public $settings = [
  28.         "pageLimit" => 10,
  29.         "pagerNum" => 5,
  30.         "theme" => "pattern1"
  31.     ];
  32.     public function before(FilterControllerEvent $event)
  33.     {
  34.         parent::before($event);
  35.         $request $event->getRequest();
  36.         $facility_code $request->attributes->get('facility');
  37.         if (!is_null($facility_code)) {
  38.             $em $this->getDoctrine()->getManager();
  39.             $facility $this->getFacilityByCode($facility_code);
  40.             if(!$facility){
  41.                 throw $this->createNotFoundException('Facility "' $facility_code '" not found.');
  42.             }
  43.             $filter $em->getFilters()->enable('facility');
  44.             $filter->setParameter('facility_id'$facility->getId());
  45.             $this->facility $facility;
  46.         }
  47.     }
  48.     public function groupTopAction(Request $request)
  49.     {
  50.         return $this->renderFront($this->views["top"]);
  51.     }
  52.     public function groupListAction(Request $request)
  53.     {
  54.         // 記事
  55.         $listData $this->getList($request);
  56.         extract($listData);
  57.         $cms__categories $this->getCategories();
  58.         $cms__archives $this->getArchive();
  59.         $cms__is_facility $this->isFacility();
  60.         // リダイレクト処理
  61.         if (!is_null($this->redirect_route) && $cms__pager['is_out_of_range'] && !CMS__IS_FRONT_ROUTING) {
  62.             $params $cms__queryParams;
  63.             if ($cms__pager['last'] > 1) {
  64.                 $params['page'] = $cms__pager['last'];
  65.             }
  66.             return $this->redirectToRoute($this->redirect_route$params);
  67.         }
  68.         return $this->renderFront($this->views["index"], compact(
  69.             "cms__items",
  70.             "cms__pager",
  71.             "cms__queryParams",
  72.             "cms__categories",
  73.             "cms__archives",
  74.             "cms__is_facility"
  75.         ));
  76.     }
  77.     public function groupDetailAction(Request $request$id)
  78.     {
  79.         $detailData $this->getDetail($request$idnull$this->settings["theme"]);
  80.         extract($detailData);
  81.         $cms__categories $this->getCategories();
  82.         $cms__archives $this->getArchive();
  83.         $cms__is_facility $this->isFacility();
  84.         return $this->renderFront($this->views['detail'], compact(
  85.             "cms__item",
  86.             "cms__builder",
  87.             "cms__contents",
  88.             "cms__blocks",
  89.             "cms__isPreview",
  90.             "cms__detailPager",
  91.             "cms__queryParams",
  92.             "cms__categories",
  93.             "cms__archives",
  94.             "cms__is_facility"
  95.         ));
  96.     }
  97.     public function facilityTopAction(Request $request$facility)
  98.     {
  99.         return null;
  100.     }
  101.     
  102.     public function facilityListAction(Request $request$facility)
  103.     {
  104.         $facility_id null;
  105.         if (!is_null($this->facility)) {
  106.             $facility_id $this->facility->getId();
  107.         }
  108.         // 記事
  109.         $listData $this->getList($request$facility_id);
  110.         extract($listData);
  111.         $cms__categories $this->getCategories($facility_id);
  112.         $cms__archives $this->getArchive($facility_id);
  113.         $cms__is_facility $this->isFacility();
  114.         // リダイレクト処理
  115.         if (!is_null($this->redirect_route) && $cms__pager['is_out_of_range'] && !CMS__IS_FRONT_ROUTING) {
  116.             $params $cms__queryParams;
  117.             $params['facility'] = $facility;
  118.             if ($cms__pager['last'] > 1) {
  119.                 $params['page'] = $cms__pager['last'];
  120.             }
  121.             return $this->redirectToRoute($this->redirect_route$params);
  122.         }
  123.         return $this->renderFront("{$facility}/" $this->views["facility_index"], compact(
  124.             "cms__items",
  125.             "cms__pager",
  126.             "cms__queryParams",
  127.             "cms__categories",
  128.             "cms__archives",
  129.             "cms__is_facility"
  130.         ));
  131.     }
  132.     public function facilityDetailAction(Request $request$facility$id)
  133.     {
  134.         $facility_id null;
  135.         if (!is_null($this->facility)) {
  136.             $facility_id $this->facility->getId();
  137.         }
  138.         $detailData $this->getDetail($request$id$facility_id$this->settings["theme"]);
  139.         extract($detailData);
  140.         $cms__categories $this->getCategories($facility_id);
  141.         $cms__archives $this->getArchive($facility_id);
  142.         $cms__is_facility $this->isFacility();
  143.         return $this->renderFront("{$facility}/" $this->views['facility_detail'], compact(
  144.             "cms__item",
  145.             "cms__builder",
  146.             "cms__contents",
  147.             "cms__blocks",
  148.             "cms__isPreview",
  149.             "cms__detailPager",
  150.             "cms__queryParams",
  151.             "cms__categories",
  152.             "cms__archives",
  153.             "cms__is_facility"
  154.         ));
  155.     }
  156.     /**
  157.      * @param $request
  158.      * @param null $facility_id
  159.      * @return array
  160.      */
  161.     protected function getList(Request $request$facility_id null)
  162.     {
  163.         $items = [];
  164.         $pager null;
  165.         $repository $this->getDoctrine()->getRepository($this->entityClass);
  166.         $queryParams $request->query->all();
  167.         unset($queryParams["facility"]);
  168.         unset($queryParams["page"]);
  169.         if (method_exists($repository"getListQueryBuilder")) {
  170.             $query $queryParams;
  171.             if (!empty($query['category'])) {
  172.                 $categories_id = [];
  173.                 $categories_id[] = (int)$query['category'];
  174.                 // 子カテゴリのIDを取得
  175.                 $repo_category $this->getDoctrine()->getRepository($this->categoryClass);
  176.                 if ($repo_category) {
  177.                     $category $repo_category->findOneBy(['id' => $query['category']]);
  178.                     foreach ($category->getDescendants() as $descendant) {
  179.                         $categories_id[] = $descendant->getId();
  180.                     }
  181.                 }
  182.                 $query['category'] = $categories_id;
  183.             }
  184.             $qb $repository->getListQueryBuilder($query$facility_id);
  185.             $page $request->query->getInt('page'1);
  186.             if($page 1){
  187.                 $page 1;
  188.             }
  189.             $limit = !empty($this->settings['pageLimit']) ? $this->settings['pageLimit'] : 10;
  190.             $query $qb->getQuery();
  191.             // ページング生成
  192.             $pager $this->getListPager($query$page$limit);
  193.             $items $query->getResult();
  194.         }
  195.         return [
  196.             "cms__pager" => $pager,
  197.             "cms__items" => $items,
  198.             "cms__queryParams" => $queryParams
  199.         ];
  200.     }
  201.     /**
  202.      * @param $query
  203.      * @param $page
  204.      * @param $limit
  205.      * @return array|null
  206.      */
  207.     protected function getListPager($query$page$limit)
  208.     {
  209.         $paginator $this->paginate($query$page$limit);
  210.         $all_count count($paginator);
  211.         $is_out_of_range false;
  212.         if (!isset($this->settings["pagerNum"]) || !is_numeric($this->settings["pagerNum"])) {
  213.             return null;
  214.         }
  215.         $pager_num $this->settings["pagerNum"];
  216.         //ページャ生成
  217.         $first 1;
  218.         if($all_count 0){
  219.             $last intval(ceil($all_count/$limit));
  220.         }else{
  221.             $last 1;
  222.         }
  223.         if($pager_num $last){
  224.             $pager_num $last;
  225.         }
  226.         //指定ページ
  227.         $target_page $page;
  228.         if($target_page == OR $last $target_page) {
  229.             //範囲外
  230.             $is_out_of_range true;
  231.             return compact('last''is_out_of_range');
  232.         }
  233.         //Prev&Next
  234.         if($target_page 1) {
  235.             $offset = ($target_page-1) * $limit;
  236.             $prev $target_page 1;
  237.             $next $target_page 1;
  238.         }else{
  239.             $offset 0;
  240.             $prev 1;
  241.             $next 2;
  242.         }
  243.         if($last $next){
  244.             $next $last;
  245.         }
  246.         $start_page 1;
  247.         $end_page $pager_num;
  248.         if($last $pager_num){
  249.             $center_num intval(ceil($pager_num/2));
  250.             if($target_page $center_num) {
  251.                 if($last $center_num $target_page){
  252.                     $start_page $last $pager_num 1;
  253.                     $end_page $start_page $pager_num 1;
  254.                 } else {
  255.                     $start_page $target_page $center_num 1;
  256.                     $end_page $target_page $center_num 1;
  257.                 }
  258.             }
  259.         }
  260.         if($all_count == 0) {
  261.             $end_page = -1;
  262.         }
  263.         $end_view_count $offset $limit;
  264.         if($all_count $end_view_count) {
  265.             $end_view_count $all_count;
  266.         }
  267.         $pager = [
  268.             "active_page" => $target_page,
  269.             "first" => $first,
  270.             "prev" => $prev,
  271.             "next" => $next,
  272.             "last" => $last,
  273.             "start_page" => $start_page,
  274.             "end_page" => $end_page,
  275.             "start_view_count" => ($all_count 1)?0:$offset 1,
  276.             "end_view_count" => $end_view_count,
  277.             "all_count" => $all_count,
  278.             'is_out_of_range' => $is_out_of_range
  279.         ];
  280.         return $pager;
  281.     }
  282.     /**
  283.      * @param $request
  284.      * @param $id
  285.      * @param null $class
  286.      * @param bool $single
  287.      * @return object|null
  288.      */
  289.     protected function findItem($request$id$class=null$single false)
  290.     {
  291.         $item $this->findItemBy($request$class, ['id' => $id]);
  292.         if ($single && !$item) {
  293.             $item = new $class();
  294.             if (method_exists($item'setDefaultValues')) {
  295.                 $access_token md5(uniqid(mt_rand(), true));
  296.                 $item->setDefaultValues($this->facility$access_token);
  297.                 $em $this->getDoctrine()->getManager();
  298.                 $em->persist($item);
  299.                 $em->flush($item);
  300.             }
  301.         }
  302.         return $item;
  303.     }
  304.     /**
  305.      * @param $request
  306.      * @param null $id
  307.      * @param null $facility_id
  308.      * @param string $theme
  309.      * @return array
  310.      */
  311.     protected function getDetail($request$id null$facility_id null$theme "default"$is_only_one false)
  312.     {
  313.         $em $this->getDoctrine()->getManager();
  314.         $mode $request->query->get('cms_mode''default');
  315.         $isPreview in_array($mode, ['preview''tmp_preview']) ? true false;
  316.         $item $this->findItem($request$id$this->entityClass$is_only_one);
  317.         if (!$item) {
  318.             // 記事が存在しない場合
  319.             throw $this->createNotFoundException();
  320.         }
  321.         $block $this->getBlock($item$this->formClass$theme);
  322.         $queryParams $request->query->all();
  323.         if(!$em->getFilters()->isEnabled('public')) {
  324.             $em->getFilters()->enable('public');
  325.         }
  326.         $detailPager $this->getDetailPager($item->id$item->public_date$facility_id);
  327.         return [
  328.             "cms__item" => $item,
  329.             "cms__builder" => $block['builder'],
  330.             "cms__contents" => $block['contents'],
  331.             "cms__blocks" => $block['blocks'],
  332.             "cms__detailPager" => $detailPager,
  333.             "cms__isPreview" => $isPreview,
  334.             "cms__queryParams" => $queryParams
  335.         ];
  336.     }
  337.     protected function getBlock($item$form_class$theme 'default'$block_form_name 'customblock')
  338.     {
  339.         $contents null;
  340.         $blocks null;
  341.         $builder $this->createPageBuilder($form_class$item);
  342.         if (!is_null($form_class)) {
  343.             // テーマがあれば利用(ないものはdefault)
  344.             $builder->setTheme($theme);
  345.             $contents $builder->findBlockByName($block_form_name);
  346.             $blocks $builder->getBlocks();
  347.         }
  348.         return compact(
  349.             'contents',
  350.             'blocks',
  351.             'builder'
  352.         );
  353.     }
  354.     /***
  355.      * 詳細ページャー取得
  356.      * @param string $id
  357.      * @param string $date
  358.      * @return array
  359.      */
  360.     protected function getDetailPager($id$date$facility_id null)
  361.     {
  362.         $repository $this->getDoctrine()->getRepository($this->entityClass);
  363.         $pager = [];
  364.         $next = [];
  365.         $prev = [];
  366.         if (method_exists($repository"getDetailPagerQueryBuilder")) {
  367.             //前
  368.             $qb $repository->getDetailPagerQueryBuilder($id$date$facility_id);
  369.             $pager['prev'] = $qb->getQuery()->getResult();
  370.             if (count($pager['prev']) > 0) {
  371.                 foreach ($pager['prev'] as $p) {
  372.                     $prev $p;
  373.                 }
  374.             }
  375.             //次
  376.             $qb $repository->getDetailPagerQueryBuilder($id$date$facility_id"<""DESC");
  377.             $pager['next'] = $qb->getQuery()->getResult();
  378.             if (count($pager['next']) > 0) {
  379.                 foreach ($pager['next'] as $p) {
  380.                     $next $p;
  381.                 }
  382.             }
  383.         }
  384.         $detailPager = [
  385.             "prev" => $prev,
  386.             "next" => $next
  387.         ];
  388.         return $detailPager;
  389.     }
  390.     /***
  391.      * アーカイブ取得
  392.      * @return array            //投稿月をもとに記事数を集計する
  393.      */
  394.     protected function getArchive($facility_id null)
  395.     {
  396.         $archiveList = [];
  397.         $repository $this->getDoctrine()->getRepository($this->entityClass);
  398.         if (method_exists($repository"getArchiveQueryBuilder")) {
  399.             $qb $repository->getArchiveQueryBuilder($facility_id);
  400.             $result $qb->getQuery()->getResult();
  401.             if (count($result) > 0) {
  402.                 foreach ($result as $archive) {
  403.                     $year date('Y'strtotime($archive['public_year_month']));
  404.                     $month date('n'strtotime($archive['public_year_month']));
  405.                     if (!isset($archiveList[$year]['total'])) {
  406.                         $archiveList[$year]['total'] = 0;
  407.                     }
  408.                     $archiveList[$year]['month'][$month]['count'] = $archive['group_count'];
  409.                     $archiveList[$year]['total'] += $archive['group_count'];
  410.                 }
  411.             }
  412.         }
  413.         return $archiveList;
  414.     }
  415.     /***
  416.      * カテゴリ取得
  417.      * @return array            //各カテゴリごとに集計する
  418.      */
  419.     protected function getCategories($facility_id null)
  420.     {
  421.         $categoryList = [];
  422.         $categoryList['total'] = 0;
  423.         $categoryList['list'] = [];
  424.         if (!isset($this->categoryClass) || is_null($this->categoryClass)) {
  425.             return $categoryList;
  426.         }
  427.         // カテゴリごとのカウント
  428.         $categoryRepository $this->getDoctrine()->getRepository($this->categoryClass);
  429.         if (method_exists($categoryRepository"getArchivesCategoryList")) {
  430.             if($this->entity_setting){
  431.                 $multi_category Hash::get($this->entity_setting"item.multi_category"false);
  432.             } else {
  433.                 $multi_category false;
  434.             }
  435.             $categoryList $categoryRepository->getArchivesCategoryList(null$multi_category);
  436.         }
  437.         $repo $this->getDoctrine()->getRepository($this->entityClass);
  438.         if (method_exists($repo"getAllCountQueryBuilder")) {
  439.             $qb $repo->getAllCountQueryBuilder($facility_id);
  440.             $totalCount $qb->getQuery()->getResult();
  441.             $categoryList['total'] = $totalCount[0]['1'];
  442.         }
  443.         // カテゴリそのもののリスト
  444.         if (method_exists($categoryRepository"getCategoryList")) {
  445.             $list $categoryRepository->getCategoryList(null""false);
  446.             $list array_flip($list);
  447.             $categoryList['list'] = $list;
  448.         }
  449.         return $categoryList;
  450.     }
  451.     protected function getPreviewBy($criteria = [], $class null)
  452.     {
  453.         $em $this->getDoctrine()->getManager();
  454.         if(is_null($class)){
  455.             $class $this->entityClass;
  456.         }
  457.         $repository $this->getDoctrine()->getRepository($class);
  458.         $filters $em->getFilters();
  459.         if ($filters->isEnabled('public')) {
  460.             $filters->disable('public');
  461.         }
  462.         $filters->disable('post_type');
  463.         $parent $repository->findOneBy($criteria);
  464.         if($parent) {
  465.             $post $repository->findOneBy([
  466.                 'revision_parent' => $parent->getId(),
  467.                 'post_type' => 'preview'
  468.             ]);
  469.             if(!$post) {
  470.                 $post $parent;
  471.             }
  472.         } else {
  473.             $post null;
  474.         }
  475.         $filters->enable('public');
  476.         $filters->enable('post_type');
  477.         return $post;
  478.     }
  479.     protected function getPreview($id=null$class=null)
  480.     {
  481.         return $this->getPreviewBy(['id' => $id], $class);
  482.     }
  483.     /**
  484.      * 施設を取得する
  485.      *
  486.      * @param $code
  487.      * @return null|object
  488.      */
  489.     protected function getFacilityByCode($code)
  490.     {
  491.         if(!empty($this->facility)){
  492.             return $this->facility;
  493.         } else {
  494.             $repo $this->getDoctrine()->getRepository(Facility::class);
  495.             return $repo->findOneBy(['code' => $code]);
  496.         }
  497.     }
  498.     /**
  499.      * 施設かどうか
  500.      * @return bool
  501.      */
  502.     protected function isFacility()
  503.     {
  504.         return !is_null($this->facility);
  505.     }
  506. }