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

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