Completed
Push — sf/composer-install-update ( a1e834 )
by Kiyotaka
10:44
created

EccubeExtension::isAuthorizedUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Twig\Extension;
15
16
use Eccube\Common\EccubeConfig;
17
use Eccube\Entity\Master\ProductStatus;
18
use Eccube\Entity\Product;
19
use Eccube\Repository\ProductRepository;
20
use Eccube\Util\StringUtil;
21
use Symfony\Component\Form\FormView;
22
use Twig\Extension\AbstractExtension;
23
use Twig\TwigFilter;
24
use Twig\TwigFunction;
25
26
class EccubeExtension extends AbstractExtension
27
{
28
    /**
29
     * @var EccubeConfig
30
     */
31
    protected $eccubeConfig;
32
33
    /**
34
     * @var ProductRepository
35
     */
36
    private $productRepository;
37
38
    /**
39
     * EccubeExtension constructor.
40
     * @param EccubeConfig $eccubeConfig
41
     * @param ProductRepository $productRepository
42
     */
43
    public function __construct(EccubeConfig $eccubeConfig, ProductRepository $productRepository)
44
    {
45
        $this->eccubeConfig = $eccubeConfig;
46
        $this->productRepository = $productRepository;
47
    }
48
49
    /**
50
     * Returns a list of functions to add to the existing list.
51
     *
52
     * @return TwigFunction[] An array of functions
53
     */
54
    public function getFunctions()
55
    {
56
        return [
57
            new TwigFunction('has_errors', [$this, 'hasErrors']),
58
            new TwigFunction('active_menus', [$this, 'getActiveMenus']),
59
            new TwigFunction('class_categories_as_json', [$this, 'getClassCategoriesAsJson']),
60
            new TwigFunction('product', [$this, 'getProduct']),
61
            new TwigFunction('php_*', [$this, 'getPhpFunctions'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
62
        ];
63
    }
64
65
    /**
66
     * Returns a list of filters.
67
     *
68
     * @return TwigFilter[]
69
     */
70
    public function getFilters()
71
    {
72
        return [
73
            new TwigFilter('no_image_product', [$this, 'getNoImageProduct']),
74
            new TwigFilter('date_format', [$this, 'getDateFormatFilter']),
75
            new TwigFilter('price', [$this, 'getPriceFilter']),
76
            new TwigFilter('ellipsis', [$this, 'getEllipsis']),
77
            new TwigFilter('time_ago', [$this, 'getTimeAgo']),
78
            new TwigFilter('file_ext_icon', [$this, 'getExtensionIcon'], ['is_safe' => ['html']]),
79
        ];
80
    }
81
82
    /**
83
     * Name of this extension
84
     *
85
     * @return string
86
     */
87
    public function getName()
88
    {
89
        return 'eccube';
90
    }
91
92
    /**
93
     * Name of this extension
94
     *
95
     * @param array $menus
96
     *
97
     * @return array
98
     */
99
    public function getActiveMenus($menus = [])
100
    {
101
        $count = count($menus);
102
        for ($i = $count; $i <= 2; $i++) {
103
            $menus[] = '';
104
        }
105
106
        return $menus;
107
    }
108
109
    /**
110
     * return No Image filename
111
     *
112
     * @return string
113
     */
114
    public function getNoImageProduct($image)
115
    {
116
        return empty($image) ? 'no_image_product.jpg' : $image;
117
    }
118
119
    /**
120
     * Name of this extension
121
     *
122
     * @return string
123
     */
124
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
125
    {
126
        if (is_null($date)) {
127
            return $value;
128
        } else {
129
            return $date->format($format);
130
        }
131
    }
132
133
    /**
134
     * Name of this extension
135
     *
136
     * @return string
137
     */
138
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
0 ignored issues
show
Unused Code introduced by
The parameter $decimals is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $decPoint is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $thousandsSep is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        $locale = $this->eccubeConfig['locale'];
141
        $currency = $this->eccubeConfig['currency'];
142
        $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
143
144
        return $formatter->formatCurrency($number, $currency);
145
    }
146
147
    /**
148
     * Name of this extension
149
     *
150
     * @return string
151
     */
152
    public function getEllipsis($value, $length = 100, $end = '...')
153
    {
154
        return StringUtil::ellipsis($value, $length, $end);
155
    }
156
157
    /**
158
     * Name of this extension
159
     *
160
     * @return string
161
     */
162
    public function getTimeAgo($date)
163
    {
164
        return StringUtil::timeAgo($date);
165
    }
166
167
    /**
168
     * FormView にエラーが含まれるかを返す.
169
     *
170
     * @return bool
171
     */
172
    public function hasErrors()
173
    {
174
        $hasErrors = false;
175
176
        $views = func_get_args();
177
        foreach ($views as $view) {
178
            if (!$view instanceof FormView) {
179
                throw new \InvalidArgumentException();
180
            }
181
            if (count($view->vars['errors'])) {
182
                $hasErrors = true;
183
                break;
184
            }
185
        }
186
187
        return $hasErrors;
188
    }
189
190
    /**
191
     * product_idで指定したProductを取得
192
     * Productが取得できない場合、または非公開の場合、商品情報は表示させない。
193
     * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。
194
     *
195
     * @param $id
196
     *
197
     * @return Product|null
198
     */
199
    public function getProduct($id)
200
    {
201
        try {
202
            $Product = $this->productRepository->findWithSortedClassCategories($id);
203
204
            if ($Product->getStatus()->getId() == ProductStatus::DISPLAY_SHOW) {
205
                return $Product;
206
            }
207
        } catch (\Exception $e) {
208
            return null;
209
        }
210
211
        return null;
212
    }
213
214
    /**
215
     * Twigでphp関数を使用できるようにする。
216
     *
217
     * @return mixed|null
218
     */
219
    public function getPhpFunctions()
220
    {
221
        $arg_list = func_get_args();
222
        $function = array_shift($arg_list);
223
224
        if (is_callable($function)) {
225
            return call_user_func_array($function, $arg_list);
226
        }
227
228
        trigger_error('Called to an undefined function : php_'.$function, E_USER_WARNING);
229
230
        return null;
231
    }
232
233
    /**
234
     * Get the ClassCategories as JSON.
235
     *
236
     * @param Product $Product
237
     *
238
     * @return string
239
     */
240
    public function getClassCategoriesAsJson(Product $Product)
241
    {
242
        $Product->_calc();
243
        $class_categories = [
244
            '__unselected' => [
245
                '__unselected' => [
246
                    'name' => trans('common.select'),
247
                    'product_class_id' => '',
248
                ],
249
            ],
250
        ];
251
        foreach ($Product->getProductClasses() as $ProductClass) {
252
            /* @var $ProductClass \Eccube\Entity\ProductClass */
253
            $ClassCategory1 = $ProductClass->getClassCategory1();
254
            $ClassCategory2 = $ProductClass->getClassCategory2();
255
            if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
256
                continue;
257
            }
258
            $class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
259
            $class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
260
            $class_category_name2 = $ClassCategory2 ? $ClassCategory2->getName().($ProductClass->getStockFind() ? '' : trans('front.product.out_of_stock_label')) : '';
261
262
            $class_categories[$class_category_id1]['#'] = [
263
                'classcategory_id2' => '',
264
                'name' => trans('common.select'),
265
                'product_class_id' => '',
266
            ];
267
            $class_categories[$class_category_id1]['#'.$class_category_id2] = [
268
                'classcategory_id2' => $class_category_id2,
269
                'name' => $class_category_name2,
270
                'stock_find' => $ProductClass->getStockFind(),
271
                'price01' => $ProductClass->getPrice01() === null ? '' : number_format($ProductClass->getPrice01()),
272
                'price02' => number_format($ProductClass->getPrice02()),
273
                'price01_inc_tax' => $ProductClass->getPrice01() === null ? '' : number_format($ProductClass->getPrice01IncTax()),
274
                'price02_inc_tax' => number_format($ProductClass->getPrice02IncTax()),
275
                'product_class_id' => (string) $ProductClass->getId(),
276
                'product_code' => $ProductClass->getCode() === null ? '' : $ProductClass->getCode(),
277
                'sale_type' => (string) $ProductClass->getSaleType()->getId(),
278
            ];
279
        }
280
281
        return json_encode($class_categories);
282
    }
283
284
    /**
285
     * Display file extension icon
286
     *
287
     * @param $ext
288
     * @param $attr
289
     *
290
     * @return string
291
     */
292
    public function getExtensionIcon($ext, $attr = [])
293
    {
294
        $classes = [
295
            'txt' => 'fa-file-text-o',
296
            'rtf' => 'fa-file-text-o',
297
            'pdf' => 'fa-file-pdf-o',
298
            'doc' => 'fa-file-word-o',
299
            'docx' => 'fa-file-word-o',
300
            'csv' => 'fa-file-excel-o',
301
            'xls' => 'fa-file-excel-o',
302
            'xlsx' => 'fa-file-excel-o',
303
            'ppt' => 'fa-file-powerpoint-o',
304
            'pptx' => 'fa-file-powerpoint-o',
305
            'png' => 'fa-file-image-o',
306
            'jpg' => 'fa-file-image-o',
307
            'jpeg' => 'fa-file-image-o',
308
            'bmp' => 'fa-file-image-o',
309
            'gif' => 'fa-file-image-o',
310
            'zip' => 'fa-file-archive-o',
311
            'tar' => 'fa-file-archive-o',
312
            'gz' => 'fa-file-archive-o',
313
            'rar' => 'fa-file-archive-o',
314
            '7zip' => 'fa-file-archive-o',
315
            'mp3' => 'fa-file-audio-o',
316
            'm4a' => 'fa-file-audio-o',
317
            'wav' => 'fa-file-audio-o',
318
            'mp4' => 'fa-file-video-o',
319
            'wmv' => 'fa-file-video-o',
320
            'mov' => 'fa-file-video-o',
321
            'mkv' => 'fa-file-video-o',
322
        ];
323
        $class = isset($classes[$ext]) ? $classes[$ext] : 'fa-file-o';
324
        $attr['class'] = isset($attr['class'])
325
            ? $attr['class']." fa {$class}"
326
            : "fa {$class}";
327
328
        $html = '<i ';
329
        foreach ($attr as $name => $value) {
330
            $html .= "{$name}=\"$value\" ";
331
        }
332
        $html .= '></i>';
333
334
        return $html;
335
    }
336
}
337