app/Customize/Twig/Extension/EccubeExtension.php line 160

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