src/Eccube/Twig/Extension/EccubeExtension.php line 364

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Twig\Extension;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\Master\ProductStatus;
  15. use Eccube\Entity\Product;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Repository\ProductRepository;
  18. use Eccube\Util\StringUtil;
  19. use Symfony\Component\Form\FormView;
  20. use Symfony\Component\Intl\Intl;
  21. use Twig\Extension\AbstractExtension;
  22. use Twig\TwigFilter;
  23. use Twig\TwigFunction;
  24. class EccubeExtension extends AbstractExtension
  25. {
  26.     /**
  27.      * @var EccubeConfig
  28.      */
  29.     protected $eccubeConfig;
  30.     /**
  31.      * @var ProductRepository
  32.      */
  33.     private $productRepository;
  34.     /**
  35.      * EccubeExtension constructor.
  36.      *
  37.      * @param EccubeConfig $eccubeConfig
  38.      * @param ProductRepository $productRepository
  39.      */
  40.     public function __construct(EccubeConfig $eccubeConfigProductRepository $productRepository)
  41.     {
  42.         $this->eccubeConfig $eccubeConfig;
  43.         $this->productRepository $productRepository;
  44.     }
  45.     /**
  46.      * Returns a list of functions to add to the existing list.
  47.      *
  48.      * @return TwigFunction[] An array of functions
  49.      */
  50.     public function getFunctions()
  51.     {
  52.         return [
  53.             new TwigFunction('has_errors', [$this'hasErrors']),
  54.             new TwigFunction('active_menus', [$this'getActiveMenus']),
  55.             new TwigFunction('class_categories_as_json', [$this'getClassCategoriesAsJson']),
  56.             new TwigFunction('product', [$this'getProduct']),
  57.             new TwigFunction('php_*', [$this'getPhpFunctions'], ['pre_escape' => 'html''is_safe' => ['html']]),
  58.             new TwigFunction('currency_symbol', [$this'getCurrencySymbol']),
  59.         ];
  60.     }
  61.     /**
  62.      * Returns a list of filters.
  63.      *
  64.      * @return TwigFilter[]
  65.      */
  66.     public function getFilters()
  67.     {
  68.         return [
  69.             new TwigFilter('no_image_product', [$this'getNoImageProduct']),
  70.             new TwigFilter('date_format', [$this'getDateFormatFilter']),
  71.             new TwigFilter('price', [$this'getPriceFilter']),
  72.             new TwigFilter('ellipsis', [$this'getEllipsis']),
  73.             new TwigFilter('time_ago', [$this'getTimeAgo']),
  74.             new TwigFilter('file_ext_icon', [$this'getExtensionIcon'], ['is_safe' => ['html']]),
  75.         ];
  76.     }
  77.     /**
  78.      * Name of this extension
  79.      *
  80.      * @return string
  81.      */
  82.     public function getName()
  83.     {
  84.         return 'eccube';
  85.     }
  86.     /**
  87.      * Name of this extension
  88.      *
  89.      * @param array $menus
  90.      *
  91.      * @return array
  92.      */
  93.     public function getActiveMenus($menus = [])
  94.     {
  95.         $count count($menus);
  96.         for ($i $count$i <= 2$i++) {
  97.             $menus[] = '';
  98.         }
  99.         return $menus;
  100.     }
  101.     /**
  102.      * return No Image filename
  103.      *
  104.      * @return string
  105.      */
  106.     public function getNoImageProduct($image)
  107.     {
  108.         return empty($image) ? 'no_image_product.png' $image;
  109.     }
  110.     /**
  111.      * Name of this extension
  112.      *
  113.      * @return string
  114.      */
  115.     public function getDateFormatFilter($date$value ''$format 'Y/m/d')
  116.     {
  117.         if (is_null($date)) {
  118.             return $value;
  119.         } else {
  120.             return $date->format($format);
  121.         }
  122.     }
  123.     /**
  124.      * Name of this extension
  125.      *
  126.      * @return string
  127.      */
  128.     public function getPriceFilter($number$decimals 0$decPoint '.'$thousandsSep ',')
  129.     {
  130.         $locale $this->eccubeConfig['locale'];
  131.         $currency $this->eccubeConfig['currency'];
  132.         $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
  133.         return $formatter->formatCurrency($number$currency);
  134.     }
  135.     /**
  136.      * Name of this extension
  137.      *
  138.      * @return string
  139.      */
  140.     public function getEllipsis($value$length 100$end '...')
  141.     {
  142.         return StringUtil::ellipsis($value$length$end);
  143.     }
  144.     /**
  145.      * Name of this extension
  146.      *
  147.      * @return string
  148.      */
  149.     public function getTimeAgo($date)
  150.     {
  151.         return StringUtil::timeAgo($date);
  152.     }
  153.     /**
  154.      * FormView にエラーが含まれるかを返す.
  155.      *
  156.      * @return bool
  157.      */
  158.     public function hasErrors()
  159.     {
  160.         $hasErrors false;
  161.         $views func_get_args();
  162.         foreach ($views as $view) {
  163.             if (!$view instanceof FormView) {
  164.                 throw new \InvalidArgumentException();
  165.             }
  166.             if (count($view->vars['errors'])) {
  167.                 $hasErrors true;
  168.                 break;
  169.             }
  170.         }
  171.         return $hasErrors;
  172.     }
  173.     /**
  174.      * product_idで指定したProductを取得
  175.      * Productが取得できない場合、または非公開の場合、商品情報は表示させない。
  176.      * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。
  177.      *
  178.      * @param $id
  179.      *
  180.      * @return Product|null
  181.      */
  182.     public function getProduct($id)
  183.     {
  184.         try {
  185.             $Product $this->productRepository->findWithSortedClassCategories($id);
  186.             if ($Product->getStatus()->getId() == ProductStatus::DISPLAY_SHOW) {
  187.                 return $Product;
  188.             }
  189.         } catch (\Exception $e) {
  190.             return null;
  191.         }
  192.         return null;
  193.     }
  194.     /**
  195.      * Twigでphp関数を使用できるようにする。
  196.      *
  197.      * @return mixed|null
  198.      */
  199.     public function getPhpFunctions()
  200.     {
  201.         $arg_list func_get_args();
  202.         $function array_shift($arg_list);
  203.         if (is_callable($function)) {
  204.             return call_user_func_array($function$arg_list);
  205.         }
  206.         trigger_error('Called to an undefined function : php_'.$functionE_USER_WARNING);
  207.         return null;
  208.     }
  209.     /**
  210.      * Get the ClassCategories as JSON.
  211.      *
  212.      * @param Product $Product
  213.      *
  214.      * @return string
  215.      */
  216.     public function getClassCategoriesAsJson(Product $Product)
  217.     {
  218.         $Product->_calc();
  219.         $class_categories = [
  220.             '__unselected' => [
  221.                 '__unselected' => [
  222.                     'name' => trans('common.select'),
  223.                     'product_class_id' => '',
  224.                 ],
  225.             ],
  226.         ];
  227.         foreach ($Product->getProductClasses() as $ProductClass) {
  228.             /** @var ProductClass $ProductClass */
  229.             if (!$ProductClass->isVisible()) {
  230.                 continue;
  231.             }
  232.             /* @var $ProductClass \Eccube\Entity\ProductClass */
  233.             $ClassCategory1 $ProductClass->getClassCategory1();
  234.             $ClassCategory2 $ProductClass->getClassCategory2();
  235.             if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  236.                 continue;
  237.             }
  238.             $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  239.             $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  240.             $class_category_name2 $ClassCategory2 $ClassCategory2->getName().($ProductClass->getStockFind() ? '' trans('front.product.out_of_stock_label')) : '';
  241.             $class_categories[$class_category_id1]['#'] = [
  242.                 'classcategory_id2' => '',
  243.                 'name' => trans('common.select'),
  244.                 'product_class_id' => '',
  245.             ];
  246.             $class_categories[$class_category_id1]['#'.$class_category_id2] = [
  247.                 'classcategory_id2' => $class_category_id2,
  248.                 'name' => $class_category_name2,
  249.                 'stock_find' => $ProductClass->getStockFind(),
  250.                 'price01' => $ProductClass->getPrice01() === null '' number_format($ProductClass->getPrice01()),
  251.                 'price02' => number_format($ProductClass->getPrice02()),
  252.                 'price01_inc_tax' => $ProductClass->getPrice01() === null '' number_format($ProductClass->getPrice01IncTax()),
  253.                 'price02_inc_tax' => number_format($ProductClass->getPrice02IncTax()),
  254.                 'price01_with_currency' => $ProductClass->getPrice01() === null '' $this->getPriceFilter($ProductClass->getPrice01()),
  255.                 'price02_with_currency' => $this->getPriceFilter($ProductClass->getPrice02()),
  256.                 'price01_inc_tax_with_currency' => $ProductClass->getPrice01() === null '' $this->getPriceFilter($ProductClass->getPrice01IncTax()),
  257.                 'price02_inc_tax_with_currency' => $this->getPriceFilter($ProductClass->getPrice02IncTax()),
  258.                 'product_class_id' => (string) $ProductClass->getId(),
  259.                 'product_code' => $ProductClass->getCode() === null '' $ProductClass->getCode(),
  260.                 'sale_type' => (string) $ProductClass->getSaleType()->getId(),
  261.             ];
  262.         }
  263.         return json_encode($class_categories);
  264.     }
  265.     /**
  266.      * Display file extension icon
  267.      *
  268.      * @param $ext
  269.      * @param $attr
  270.      * @param $iconOnly アイコンのクラス名のみ返す場合はtrue
  271.      *
  272.      * @return string
  273.      */
  274.     public function getExtensionIcon($ext$attr = [], $iconOnly false)
  275.     {
  276.         $classes = [
  277.             'txt' => 'fa-file-text-o',
  278.             'rtf' => 'fa-file-text-o',
  279.             'pdf' => 'fa-file-pdf-o',
  280.             'doc' => 'fa-file-word-o',
  281.             'docx' => 'fa-file-word-o',
  282.             'csv' => 'fa-file-excel-o',
  283.             'xls' => 'fa-file-excel-o',
  284.             'xlsx' => 'fa-file-excel-o',
  285.             'ppt' => 'fa-file-powerpoint-o',
  286.             'pptx' => 'fa-file-powerpoint-o',
  287.             'png' => 'fa-file-image-o',
  288.             'jpg' => 'fa-file-image-o',
  289.             'jpeg' => 'fa-file-image-o',
  290.             'bmp' => 'fa-file-image-o',
  291.             'gif' => 'fa-file-image-o',
  292.             'zip' => 'fa-file-archive-o',
  293.             'tar' => 'fa-file-archive-o',
  294.             'gz' => 'fa-file-archive-o',
  295.             'rar' => 'fa-file-archive-o',
  296.             '7zip' => 'fa-file-archive-o',
  297.             'mp3' => 'fa-file-audio-o',
  298.             'm4a' => 'fa-file-audio-o',
  299.             'wav' => 'fa-file-audio-o',
  300.             'mp4' => 'fa-file-video-o',
  301.             'wmv' => 'fa-file-video-o',
  302.             'mov' => 'fa-file-video-o',
  303.             'mkv' => 'fa-file-video-o',
  304.         ];
  305.         $ext strtolower($ext);
  306.         $class = isset($classes[$ext]) ? $classes[$ext] : 'fa-file-o';
  307.         if ($iconOnly) {
  308.             return $class;
  309.         }
  310.         $attr['class'] = isset($attr['class'])
  311.             ? $attr['class']." fa {$class}"
  312.             "fa {$class}";
  313.         $html '<i ';
  314.         foreach ($attr as $name => $value) {
  315.             $html .= "{$name}=\"$value\" ";
  316.         }
  317.         $html .= '></i>';
  318.         return $html;
  319.     }
  320.     /**
  321.      * Get currency symbol
  322.      *
  323.      * @param null $currency
  324.      *
  325.      * @return bool|string
  326.      */
  327.     public function getCurrencySymbol($currency null)
  328.     {
  329.         if (is_null($currency)) {
  330.             $currency $this->eccubeConfig->get('currency');
  331.         }
  332.         $symbol Intl::getCurrencyBundle()->getCurrencySymbol($currency);
  333.         return $symbol;
  334.     }
  335. }