app/Plugin/TabaCMS2/Twig/Extension/TwigExtension.php line 504

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\Twig\Extension;
  9. use Eccube\Request\Context;
  10. use Eccube\Common\EccubeConfig;
  11. use Knp\Component\Pager\Paginator;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Plugin\TabaCMS2\Common\Constants;
  14. use Plugin\TabaCMS2\Common\DataHolder;
  15. use Plugin\TabaCMS2\Util\Util;
  16. use Plugin\TabaCMS2\Repository\TypeRepository;
  17. use Plugin\TabaCMS2\Repository\PostRepository;
  18. use Plugin\TabaCMS2\Repository\CategoryRepository;
  19. use Plugin\TabaCMS2\Repository\TagRepository;
  20. use Plugin\TabaCMS2\Form\Type\FrontPostSearchType;
  21. use Symfony\Component\Asset\Packages;
  22. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  23. use Symfony\Component\Routing\RouterInterface;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Symfony\Component\Form\FormFactoryInterface;
  26. use Twig\Extension\AbstractExtension;
  27. use Twig\Environment;
  28. use Twig\TwigFunction;
  29. class TwigExtension extends AbstractExtension
  30. {
  31.     /**
  32.      * @var RequestStack
  33.      */
  34.     private $requestStack;
  35.     /**
  36.      * @var Paginator
  37.      */
  38.     private $paginator;
  39.     /**
  40.      * @var array
  41.      */
  42.     private $cached;
  43.     /**
  44.      * @var Context
  45.      */
  46.     private $requestContext;
  47.     /**
  48.      * @var TypeRepository
  49.      */
  50.     private $typeRepo;
  51.     /**
  52.      * @var PostRepository
  53.      */
  54.     private $postRepo;
  55.     /**
  56.      * @var CategoryRepository
  57.      */
  58.     private $categoryRepo;
  59.     /**
  60.      * @var TagRepository
  61.      */
  62.     private $tagRepo;
  63.     /**
  64.      * @var Packages
  65.      */
  66.     private $assetPackage;
  67.     /**
  68.      * @var Environment
  69.      */
  70.     private $twig;
  71.     /**
  72.      * @var CsrfTokenManagerInterface
  73.      */
  74.     private $csrfTokenManager;
  75.     /**
  76.      * @var EccubeConfig
  77.      */
  78.     private $eccubeConfig;
  79.     /**
  80.      * @var RouterInterface
  81.      */
  82.     private $router;
  83.     /**
  84.      * @var FormFactoryInterface
  85.      */
  86.     private $formFactory;
  87.     /**
  88.      * コンストラクタ
  89.      *
  90.      * @param Environment $twig
  91.      * @param RequestStack $requestStack
  92.      * @param PaginatorInterface $paginator
  93.      * @param Context $requestContext
  94.      * @param TypeRepository $typeRepo
  95.      * @param PostRepository $postRepo
  96.      * @param CategoryRepository $categoryRepo
  97.      * @param Packages $assetPackages
  98.      * @param CsrfTokenManagerInterface $csrfTokenManager
  99.      * @param EccubeConfig $eccubeConfig
  100.      * @param RouterInterface $router
  101.      */
  102.     public function __construct(
  103.         Environment $twig,
  104.         RequestStack $requestStack,
  105.         PaginatorInterface $paginator,
  106.         Context $requestContext,
  107.         TypeRepository $typeRepo,
  108.         PostRepository $postRepo,
  109.         CategoryRepository $categoryRepo,
  110.         TagRepository $tagRepo,
  111.         Packages $assetPackages,
  112.         CsrfTokenManagerInterface $csrfTokenManager,
  113.         EccubeConfig $eccubeConfig,
  114.         RouterInterface $router,
  115.         FormFactoryInterface $formFactory
  116.     ) {
  117.         $this->twig $twig;
  118.         $this->requestStack $requestStack;
  119.         $this->paginator $paginator;
  120.         $this->requestContext $requestContext;
  121.         $this->typeRepo $typeRepo;
  122.         $this->postRepo $postRepo;
  123.         $this->categoryRepo $categoryRepo;
  124.         $this->tagRepo $tagRepo;
  125.         $this->assetPackage $assetPackages;
  126.         $this->csrfTokenManager $csrfTokenManager;
  127.         $this->eccubeConfig $eccubeConfig;
  128.         $this->router $router;
  129.         $this->formFactory $formFactory;
  130.         $this->twig->addGlobal(Constants::PLUGIN_CATEGORY_NAME.'Status', new Constants());
  131.     }
  132.     /**
  133.      * Returns a list of functions to add to the existing list.
  134.      *
  135.      * @return array An array of functions
  136.      */
  137.     public function getFunctions()
  138.     {
  139.         return array(
  140.             new TwigFunction(Constants::PLUGIN_NAME 'Type', array($this'type')),
  141.             new TwigFunction(Constants::PLUGIN_NAME 'Post', array($this'post')),
  142.             new TwigFunction(Constants::PLUGIN_NAME 'PostList', array($this'postList')),
  143.             new TwigFunction(Constants::PLUGIN_NAME 'Category', array($this'category')),
  144.             new TwigFunction(Constants::PLUGIN_NAME 'CategoryList', array($this'categoryList')),
  145.             new TwigFunction(Constants::PLUGIN_NAME 'Asset', array($this'asset')),
  146.             new TwigFunction(Constants::PLUGIN_NAME 'IsAssetLoad', array($this'isAssetLoad')),
  147.             new TwigFunction(Constants::PLUGIN_NAME 'Widget', array($this'widget')),
  148.         );
  149.     }
  150.     /**
  151.      * Name of this extension
  152.      *
  153.      * @return string
  154.      */
  155.     public function getName()
  156.     {
  157.         return Constants::PLUGIN_CODE_LC;
  158.     }
  159.     /**
  160.      * ウィジェットを出力します。
  161.      *
  162.      * @param unknown $widget_name
  163.      * @param array $options
  164.      * @return void|string
  165.      */
  166.     public function widget($widget_name,$options = array()) {
  167.         $data_holder DataHolder::getInstance();
  168.         // オプション
  169.         $options array_merge(array(
  170.             'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  171.         ),$options);
  172.         //
  173.         // カテゴリーリスト
  174.         //
  175.         if ($widget_name == "category") {
  176.             // 投稿タイプの取得
  177.             $type null;
  178.             if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  179.                 return;
  180.             }
  181.             // カテゴリーリストの取得
  182.             $conditions = array();
  183.             $conditions['is_public'] = true;
  184.             if (!empty($type)) $conditions['type_id'] = $type->getTypeId();
  185.             $category_list $this->categoryRepo->getList($conditions);
  186.             $file_name 'widget_' $widget_name '.twig';
  187.             return $this->twig->render(Util::getTemplatePath($file_name,[$options['type_data_key']],$this->eccubeConfig),[
  188.                 'type' => $type,
  189.                 'options' => $options,
  190.                 'category_list' => $category_list,
  191.             ]);
  192.         }
  193.         //
  194.         // タグリスト
  195.         //
  196.         if ($widget_name == "tag") {
  197.             // 投稿タイプの取得
  198.             $type null;
  199.             if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  200.                 return;
  201.             }
  202.             // タグリストの取得
  203.             $conditions = array();
  204.             $conditions['is_public'] = true;
  205.             if (!empty($type)) $conditions['type_id'] = $type->getTypeId();
  206.             $tag_list $this->tagRepo->getList($conditions);
  207.             $file_name 'widget_' $widget_name '.twig';
  208.             return $this->twig->render(Util::getTemplatePath($file_name,[$options['type_data_key']],$this->eccubeConfig),[
  209.                 'type' => $type,
  210.                 'options' => $options,
  211.                 'tag_list' => $tag_list,
  212.             ]);
  213.         }
  214.         //
  215.         // 検索フォーム
  216.         //
  217.         else if ($widget_name == "search") {
  218.             // 投稿タイプの取得
  219.             $type null;
  220.             if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  221.                 return;
  222.             }
  223.             // フォームの生成
  224.             $builder $this->formFactory->createBuilder(FrontPostSearchType::class,['type_id' => $type->getTypeId()]);
  225.             $post_search_form $builder->getForm();
  226.             $post_search_form->handleRequest($this->requestStack->getCurrentRequest());
  227.             $file_name 'widget_' $widget_name '.twig';
  228.             return $this->twig->render(Util::getTemplatePath($file_name,[$options['type_data_key']],$this->eccubeConfig),[
  229.                 'type' => $type,
  230.                 'options' => $options,
  231.                 'post_search_form' => $post_search_form->createView(),
  232.             ]);
  233.         }
  234.         //
  235.         // 投稿リスト , 投稿
  236.         //
  237.         else if ($widget_name == "list") {
  238.             // 投稿タイプの取得
  239.             $type null;
  240.             if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  241.                 return;
  242.             }
  243.             $file_name 'widget_' $widget_name '.twig';
  244.             return $this->twig->render(Util::getTemplatePath($file_name,[$options['type_data_key']],$this->eccubeConfig),[
  245.                 'type' => $type,
  246.                 'options' => $options,
  247.             ]);
  248.         }
  249.     }
  250.     public function isAssetLoad($key) {
  251.         $data_holder DataHolder::getInstance();
  252.         if ($data_holder->getData("IS_LOADED_" $key)) {
  253.             return true;
  254.         } else {
  255.             $data_holder->setData("IS_LOADED_" $key,true);
  256.             return false;
  257.         }
  258.     }
  259.     /**
  260.      *
  261.      * @param string $file_name
  262.      * @param string $asset_type script|style|img
  263.      * @param boolean $once
  264.      * @param array $attributes tag attribute
  265.      * @return string|NULL
  266.      */
  267.     public function asset($file_name,$asset_type null,$once true,$attributes = []) {
  268.         if (!$once || ($once && !$this->isAssetLoad($file_name))) {
  269.             $uri $this->router->generate(Constants::FRONT_BIND_PREFIX '_assets',['file' => $file_name]);
  270.             if ($asset_type == "script") {
  271.                 return '<script src="' $uri '"></script>';
  272.             } else if ($asset_type == "style") {
  273.                 return '<link rel="stylesheet" href="' $uri '">';
  274.             } else if ($asset_type == "img") {
  275.                 return '<img src="' $uri '">';
  276.             } else {
  277.                 return $uri;
  278.             }
  279.         } else {
  280.             return null;
  281.         }
  282.     }
  283.     /**
  284.      * カテゴリーを取得します。
  285.      *
  286.      * @param array $options
  287.      * @return Post
  288.      */
  289.     public function category($options = array()) {
  290.         $data_holder DataHolder::getInstance();
  291.         // オプション
  292.         $options array_merge(array(
  293.             'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  294.             'data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_CATEGORY_DK),
  295.         ),$options);
  296.         // 投稿タイプの取得
  297.         $type null;
  298.         if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  299.             return;
  300.         }
  301.         // カテゴリーデータ
  302.         $category $this->categoryRepo->get([
  303.             'type_id' => $type->getTypeId(),
  304.             'data_key' => $options['data_key']
  305.         ]);
  306.         return $category;
  307.     }
  308.     /**
  309.      * カテゴリーリストを取得します。
  310.      *
  311.      * @param array $options
  312.      * @return Post
  313.      */
  314.     public function categoryList($options = array()) {
  315.         $data_holder DataHolder::getInstance();
  316.         $options array_merge(array(
  317.             'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  318.             'data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_CATEGORY_DK),
  319.         ),$options);
  320.         // 投稿タイプの取得
  321.         $type null;
  322.         if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  323.             return;
  324.         }
  325.         // カテゴリーリストの取得
  326.         $conditions = array();
  327.         $conditions['is_public'] = true;
  328.         if (!empty($type)) $conditions['type_id'] = $type->getTypeId();
  329.         $category_list $this->categoryRepo->getList($conditions);
  330.         return $category_list;
  331.     }
  332.     /**
  333.      * 投稿タイプを取得します。
  334.      *
  335.      * @param array $options
  336.      * @return Post
  337.      */
  338.     public function type($options = array()) {
  339.         $data_holder DataHolder::getInstance();
  340.         // オプション
  341.         if ($data_holder) {
  342.             $options array_merge(array(
  343.                 'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  344.             ),$options);
  345.         }
  346.         // 投稿タイプ
  347.         $type $this->typeRepo->get($options['type_data_key']);
  348.         if ($type && $type->getPublicDiv()) {
  349.             return $type;
  350.         }
  351.         return null;
  352.     }
  353.     /**
  354.      * 投稿データを取得します。
  355.      *
  356.      * @param array $options
  357.      * @return Post
  358.      */
  359.     public function post($options = array()) {
  360.         $data_holder DataHolder::getInstance();
  361.         // プレビュー
  362.         if ($data_holder->getData(Constants::DATA_HOLDER_KEY_PREVIEW)) {
  363.             $post $data_holder->getData(Constants::DATA_HOLDER_KEY_POST);
  364.         }
  365.         // 通常
  366.         else {
  367.             // オプション
  368.             $options array_merge(array(
  369.                 'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  370.                 'data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_POST_DK),
  371.             ),$options);
  372.             // 投稿データ
  373.             $post $this->postRepo->get([
  374.                 "data_key" => $options['data_key'],
  375.                 "is_public" => true
  376.             ]);
  377.         }
  378.         return $post;
  379.     }
  380.     /**
  381.      * 投稿リストを取得します。
  382.      *
  383.      * @param array $options
  384.      * @return void|\Knp\Component\Pager\Pagination\PaginationInterface
  385.      */
  386.     public function postList($options = array()) {
  387.         $data_holder DataHolder::getInstance();
  388.         if (!$options || !isset($options['overwrite_query']) || $options['overwrite_query'] !== false) {
  389.             $params $this->requestStack->getCurrentRequest()->query->all();
  390.             $options array_merge($params,$options);
  391.         }
  392.         $options array_merge(array(
  393.             'type_data_key' => $data_holder->getData(Constants::DATA_HOLDER_KEY_TYPE_DK),
  394.             'page_count' => Constants::DEFAULT_PAGE_COUNT,
  395.             'pageno' => 1,
  396.         ),$options);
  397.         // 投稿タイプの取得
  398.         $type null;
  399.         if (!$options['type_data_key'] || !($type $this->typeRepo->get($options['type_data_key']))) {
  400.             return;
  401.         }
  402.         //
  403.         // 抽出条件
  404.         //
  405.         $where = array(
  406.             "type_id" => $type->getTypeId(),
  407.             "is_public" => true,
  408.         );
  409.         // カテゴリー
  410.         if (isset($options["category_data_key"]) && !empty($options["category_data_key"])) {
  411.             $category $this->categoryRepo->findOneBy(['type' => $type,'dataKey' => $options["category_data_key"]]);
  412.             if ($category) {
  413.                 $where['category_id'] = $category->getCategoryId();
  414.             } else {
  415.                 $where['category_id'] = -1;
  416.             }
  417.         } else if (isset($options["category_id"]) && !empty($options["category_id"])) {
  418.             $where['category_id'] = $options["category_id"];
  419.         }
  420.         // タグ
  421.         if (isset($options["tag_data_key"]) && !empty($options["tag_data_key"])) {
  422.             if (is_array($options["tag_data_key"])) {
  423.                 $tags $this->tagRepo->findBy(['type' => $type,'dataKey' => $options["tag_data_key"]]);
  424.                 foreach ($tags as $tag) {
  425.                     $where['tag_id'][] = $tag->getTagId();
  426.                 }
  427.             } else {
  428.                 $tag $this->tagRepo->findOneBy(['type' => $type,'dataKey' => $options["tag_data_key"]]);
  429.                 if ($tag$where['tag_id'] = $tag->getTagId();
  430.             }
  431.         } else if (isset($options["tag_id"]) && !empty($options["tag_id"])) {
  432.             $where['tag_id'] = $options["tag_id"];
  433.         }
  434.         // キーワード
  435.         if (isset($options["keyword"])) {
  436.             $where['keyword'] = $options["keyword"];
  437.         }
  438.         // 投稿リストの取得
  439.         $qb_post_list $this->postRepo->createListQueryBuilder($where);
  440.         // ページネーション
  441.         $post_list $this->paginator->paginate(
  442.             $qb_post_list,
  443.             $options['pageno'],
  444.             $options['page_count'],
  445.             array('wrap-queries' => true)
  446.         );
  447.         return $post_list;
  448.     }
  449. }