app/Plugin/TabaCMS/Twig/Extension/TwigExtension.php line 303

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