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

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\Currencies;
  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('currency_symbol', [$this'getCurrencySymbol']),
  58.         ];
  59.     }
  60.     /**
  61.      * Returns a list of filters.
  62.      *
  63.      * @return TwigFilter[]
  64.      */
  65.     public function getFilters()
  66.     {
  67.         return [
  68.             new TwigFilter('no_image_product', [$this'getNoImageProduct']),
  69.             new TwigFilter('date_format', [$this'getDateFormatFilter']),
  70.             new TwigFilter('price', [$this'getPriceFilter']),
  71.             new TwigFilter('ellipsis', [$this'getEllipsis']),
  72.             new TwigFilter('time_ago', [$this'getTimeAgo']),
  73.             new TwigFilter('file_ext_icon', [$this'getExtensionIcon'], ['is_safe' => ['html']]),
  74.         ];
  75.     }
  76.     /**
  77.      * Name of this extension
  78.      *
  79.      * @return string
  80.      */
  81.     public function getName()
  82.     {
  83.         return 'eccube';
  84.     }
  85.     /**
  86.      * Name of this extension
  87.      *
  88.      * @param array $menus
  89.      *
  90.      * @return array
  91.      */
  92.     public function getActiveMenus($menus = [])
  93.     {
  94.         $count count($menus);
  95.         for ($i $count$i <= 2$i++) {
  96.             $menus[] = '';
  97.         }
  98.         return $menus;
  99.     }
  100.     /**
  101.      * return No Image filename
  102.      *
  103.      * @return string
  104.      */
  105.     public function getNoImageProduct($image)
  106.     {
  107.         return empty($image) ? 'no_image_product.png' $image;
  108.     }
  109.     /**
  110.      * Name of this extension
  111.      *
  112.      * @return string
  113.      */
  114.     public function getDateFormatFilter($date$value ''$format 'Y/m/d')
  115.     {
  116.         if (is_null($date)) {
  117.             return $value;
  118.         } else {
  119.             return $date->format($format);
  120.         }
  121.     }
  122.     /**
  123.      * Name of this extension
  124.      *
  125.      * @return string
  126.      */
  127.     public function getPriceFilter($number$decimals 0$decPoint '.'$thousandsSep ',')
  128.     {
  129.         $locale $this->eccubeConfig['locale'];
  130.         $currency $this->eccubeConfig['currency'];
  131.         $formatter = new \NumberFormatter($locale\NumberFormatter::CURRENCY);
  132.         return $formatter->formatCurrency($number$currency);
  133.     }
  134.     /**
  135.      * Name of this extension
  136.      *
  137.      * @return string
  138.      */
  139.     public function getEllipsis($value$length 100$end '...')
  140.     {
  141.         return StringUtil::ellipsis($value$length$end);
  142.     }
  143.     /**
  144.      * Name of this extension
  145.      *
  146.      * @return string
  147.      */
  148.     public function getTimeAgo($date)
  149.     {
  150.         return StringUtil::timeAgo($date);
  151.     }
  152.     /**
  153.      * FormView にエラーが含まれるかを返す.
  154.      *
  155.      * @return bool
  156.      */
  157.     public function hasErrors()
  158.     {
  159.         $hasErrors false;
  160.         $views func_get_args();
  161.         foreach ($views as $view) {
  162.             if (!$view instanceof FormView) {
  163.                 throw new \InvalidArgumentException();
  164.             }
  165.             if (count($view->vars['errors'])) {
  166.                 $hasErrors true;
  167.                 break;
  168.             }
  169.         }
  170.         return $hasErrors;
  171.     }
  172.     /**
  173.      * product_idで指定したProductを取得
  174.      * Productが取得できない場合、または非公開の場合、商品情報は表示させない。
  175.      * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。
  176.      *
  177.      * @param $id
  178.      *
  179.      * @return Product|null
  180.      */
  181.     public function getProduct($id)
  182.     {
  183.         try {
  184.             $Product $this->productRepository->findWithSortedClassCategories($id);
  185.             if ($Product->getStatus()->getId() == ProductStatus::DISPLAY_SHOW) {
  186.                 return $Product;
  187.             }
  188.         } catch (\Exception $e) {
  189.             return null;
  190.         }
  191.         return null;
  192.     }
  193.     /**
  194.      * Get the ClassCategories as JSON.
  195.      *
  196.      * @param Product $Product
  197.      *
  198.      * @return string
  199.      */
  200.     public function getClassCategoriesAsJson(Product $Product)
  201.     {
  202.         $Product->_calc();
  203.         $class_categories = [
  204.             '__unselected' => [
  205.                 '__unselected' => [
  206.                     'name' => trans('common.select'),
  207.                     'product_class_id' => '',
  208.                 ],
  209.             ],
  210.         ];
  211.         foreach ($Product->getProductClasses() as $ProductClass) {
  212.             /** @var ProductClass $ProductClass */
  213.             if (!$ProductClass->isVisible()) {
  214.                 continue;
  215.             }
  216.             /* @var $ProductClass \Eccube\Entity\ProductClass */
  217.             $ClassCategory1 $ProductClass->getClassCategory1();
  218.             $ClassCategory2 $ProductClass->getClassCategory2();
  219.             if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  220.                 continue;
  221.             }
  222.             $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  223.             $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  224.             $class_category_name2 $ClassCategory2 $ClassCategory2->getName().($ProductClass->getStockFind() ? '' trans('front.product.out_of_stock_label')) : '';
  225.             $class_categories[$class_category_id1]['#'] = [
  226.                 'classcategory_id2' => '',
  227.                 'name' => trans('common.select'),
  228.                 'product_class_id' => '',
  229.             ];
  230.             $class_categories[$class_category_id1]['#'.$class_category_id2] = [
  231.                 'classcategory_id2' => $class_category_id2,
  232.                 'name' => $class_category_name2,
  233.                 'stock_find' => $ProductClass->getStockFind(),
  234.                 'price01' => $ProductClass->getPrice01() === null '' number_format($ProductClass->getPrice01()),
  235.                 'price02' => number_format($ProductClass->getPrice02()),
  236.                 'price01_inc_tax' => $ProductClass->getPrice01() === null '' number_format($ProductClass->getPrice01IncTax()),
  237.                 'price02_inc_tax' => number_format($ProductClass->getPrice02IncTax()),
  238.                 'price01_with_currency' => $ProductClass->getPrice01() === null '' $this->getPriceFilter($ProductClass->getPrice01()),
  239.                 'price02_with_currency' => $this->getPriceFilter($ProductClass->getPrice02()),
  240.                 'price01_inc_tax_with_currency' => $ProductClass->getPrice01() === null '' $this->getPriceFilter($ProductClass->getPrice01IncTax()),
  241.                 'price02_inc_tax_with_currency' => $this->getPriceFilter($ProductClass->getPrice02IncTax()),
  242.                 'product_class_id' => (string) $ProductClass->getId(),
  243.                 'product_code' => $ProductClass->getCode() === null '' $ProductClass->getCode(),
  244.                 'sale_type' => (string) $ProductClass->getSaleType()->getId(),
  245.             ];
  246.         }
  247.         return json_encode($class_categories);
  248.     }
  249.     /**
  250.      * Display file extension icon
  251.      *
  252.      * @param $ext
  253.      * @param $attr
  254.      * @param $iconOnly アイコンのクラス名のみ返す場合はtrue
  255.      *
  256.      * @return string
  257.      */
  258.     public function getExtensionIcon($ext$attr = [], $iconOnly false)
  259.     {
  260.         $classes = [
  261.             'txt' => 'fa-file-text-o',
  262.             'rtf' => 'fa-file-text-o',
  263.             'pdf' => 'fa-file-pdf-o',
  264.             'doc' => 'fa-file-word-o',
  265.             'docx' => 'fa-file-word-o',
  266.             'csv' => 'fa-file-excel-o',
  267.             'xls' => 'fa-file-excel-o',
  268.             'xlsx' => 'fa-file-excel-o',
  269.             'ppt' => 'fa-file-powerpoint-o',
  270.             'pptx' => 'fa-file-powerpoint-o',
  271.             'png' => 'fa-file-image-o',
  272.             'jpg' => 'fa-file-image-o',
  273.             'jpeg' => 'fa-file-image-o',
  274.             'bmp' => 'fa-file-image-o',
  275.             'gif' => 'fa-file-image-o',
  276.             'zip' => 'fa-file-archive-o',
  277.             'tar' => 'fa-file-archive-o',
  278.             'gz' => 'fa-file-archive-o',
  279.             'rar' => 'fa-file-archive-o',
  280.             '7zip' => 'fa-file-archive-o',
  281.             'mp3' => 'fa-file-audio-o',
  282.             'm4a' => 'fa-file-audio-o',
  283.             'wav' => 'fa-file-audio-o',
  284.             'mp4' => 'fa-file-video-o',
  285.             'wmv' => 'fa-file-video-o',
  286.             'mov' => 'fa-file-video-o',
  287.             'mkv' => 'fa-file-video-o',
  288.         ];
  289.         $ext strtolower($ext);
  290.         $class = isset($classes[$ext]) ? $classes[$ext] : 'fa-file-o';
  291.         if ($iconOnly) {
  292.             return $class;
  293.         }
  294.         $attr['class'] = isset($attr['class'])
  295.             ? $attr['class']." fa {$class}"
  296.             "fa {$class}";
  297.         $html '<i ';
  298.         foreach ($attr as $name => $value) {
  299.             $html .= "{$name}=\"$value\" ";
  300.         }
  301.         $html .= '></i>';
  302.         return $html;
  303.     }
  304.     /**
  305.      * Get currency symbol
  306.      *
  307.      * @param null $currency
  308.      *
  309.      * @return bool|string
  310.      */
  311.     public function getCurrencySymbol($currency null)
  312.     {
  313.         if (is_null($currency)) {
  314.             $currency $this->eccubeConfig->get('currency');
  315.         }
  316.         $symbol Currencies::getSymbol($currency);
  317.         return $symbol;
  318.     }
  319. }