Failed Conditions
Pull Request — experimental/sf (#3225)
by Kentaro
58:20 queued 49:13
created

EccubeExtension::getActiveMenus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
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\Product;
18
use Eccube\Service\TaxRuleService;
19
use Eccube\Util\StringUtil;
20
use Symfony\Component\Form\FormView;
21
use Twig\Extension\AbstractExtension;
22
use Twig\TwigFilter;
23
use Twig\TwigFunction;
24
25
class EccubeExtension extends AbstractExtension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
26
{
27
    /**
28
     * @var EccubeConfig
29
     */
30
    protected $eccubeConfig;
31
32
    /**
33
     * @var TaxRuleService
34
     */
35
    protected $TaxRuleService;
36
37 463
    public function __construct(TaxRuleService $TaxRuleService, EccubeConfig $eccubeConfig)
38
    {
39 463
        $this->TaxRuleService = $TaxRuleService;
40 463
        $this->eccubeConfig = $eccubeConfig;
41
    }
42
43
    /**
44
     * Returns a list of functions to add to the existing list.
45
     *
46
     * @return array An array of functions
47
     */
48 243
    public function getFunctions()
49
    {
50
        return [
51 243
            new TwigFunction('has_errors', [$this, 'hasErrors']),
52 243
            new TwigFunction('is_object', [$this, 'isObject']),
53 243
            new TwigFunction('calc_inc_tax', [$this, 'getCalcIncTax']),
54 243
            new TwigFunction('active_menus', [$this, 'getActiveMenus']),
55 243
            new TwigFunction('class_categories_as_json', [$this, 'getClassCategoriesAsJson']),
56 243 View Code Duplication
            new TwigFunction('php_*', function () {
57 12
                $arg_list = func_get_args();
58 12
                $function = array_shift($arg_list);
59 12
                if (is_callable($function)) {
60 12
                    return call_user_func_array($function, $arg_list);
61
                }
62
                trigger_error('Called to an undefined function : php_'.$function, E_USER_WARNING);
63 243
            }, ['pre_escape' => 'html', 'is_safe' => ['html']]),
64
        ];
65
    }
66
67
    /**
68
     * Returns a list of filters.
69
     *
70
     * @return array
71
     */
72 243
    public function getFilters()
73
    {
74
        return [
75 243
            new TwigFilter('no_image_product', [$this, 'getNoImageProduct']),
76 243
            new TwigFilter('date_format', [$this, 'getDateFormatFilter']),
77 243
            new TwigFilter('price', [$this, 'getPriceFilter']),
78 243
            new TwigFilter('ellipsis', [$this, 'getEllipsis']),
79 243
            new TwigFilter('time_ago', [$this, 'getTimeAgo']),
80 243
            new TwigFilter('file_ext_icon', [$this, 'getExtensionIcon'], ['is_safe' => ['html']]),
81
        ];
82
    }
83
84
    /**
85
     * Name of this extension
86
     *
87
     * @return string
88
     */
89
    public function getName()
90
    {
91
        return 'eccube';
92
    }
93
94
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$price" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rate" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rule" missing
Loading history...
95
     * Name of this extension
96
     *
97
     * @return string
98
     */
99 11
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
100
    {
101 11
        return $price + $this->TaxRuleService->calcTax($price, $tax_rate, $tax_rule);
102
    }
103
104
    /**
105
     * Name of this extension
106
     *
107
     * @param array $menus
108
     *
109
     * @return array
110
     */
111 147
    public function getActiveMenus($menus = [])
112
    {
113 147
        $count = count($menus);
114 147
        for ($i = $count; $i <= 2; $i++) {
115 105
            $menus[] = '';
116
        }
117
118 147
        return $menus;
119
    }
120
121
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
122
     * return No Image filename
123
     *
124
     * @return string
125
     */
126 61
    public function getNoImageProduct($image)
127
    {
128 61
        return empty($image) ? 'no_image_product.jpg' : $image;
129
    }
130
131
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$format" missing
Loading history...
132
     * Name of this extension
133
     *
134
     * @return string
135
     */
136 142
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
137
    {
138 142
        if (is_null($date)) {
139 142
            return $value;
140
        } else {
141 1
            return $date->format($format);
142
        }
143
    }
144
145
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$number" missing
Loading history...
introduced by
Doc comment for parameter "$decimals" missing
Loading history...
introduced by
Doc comment for parameter "$decPoint" missing
Loading history...
introduced by
Doc comment for parameter "$thousandsSep" missing
Loading history...
146
     * Name of this extension
147
     *
148
     * @return string
149
     */
150 95
    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...
151
    {
152 95
        $locale = $this->eccubeConfig['locale'];
153 95
        $currency = $this->eccubeConfig['currency'];
154 95
        $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
155
156 95
        return $formatter->formatCurrency($number, $currency);
157
    }
158
159
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$value" missing
Loading history...
introduced by
Doc comment for parameter "$length" missing
Loading history...
introduced by
Doc comment for parameter "$end" missing
Loading history...
160
     * Name of this extension
161
     *
162
     * @return string
163
     */
164
    public function getEllipsis($value, $length = 100, $end = '...')
165
    {
166
        return StringUtil::ellipsis($value, $length, $end);
167
    }
168
169
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
170
     * Name of this extension
171
     *
172
     * @return string
173
     */
174
    public function getTimeAgo($date)
175
    {
176
        return StringUtil::timeAgo($date);
177
    }
178
179
    /**
180
     * Check if the value is object
181
     *
182
     * @param object $value
183
     *
184
     * @return bool
185
     */
186
    public function isObject($value)
187
    {
188
        return is_object($value);
189
    }
190
191
    /**
192
     * FormView にエラーが含まれるかを返す.
193
     *
194
     * @return bool
195
     */
196 13
    public function hasErrors()
197
    {
198 13
        $hasErrors = false;
199
200 13
        $views = func_get_args();
201 13
        foreach ($views as $view) {
202 13
            if (!$view instanceof FormView) {
203
                throw new \InvalidArgumentException();
204
            }
205 13
            if (count($view->vars['errors'])) {
206 1
                $hasErrors = true;
207 13
                break;
208
            }
209
        }
210
211 13
        return $hasErrors;
212
    }
213
214
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
215
     * product_idで指定したProductを取得
216
     * Productが取得できない場合、または非公開の場合、商品情報は表示させない。
217
     * デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。
218
     *
219
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
220
     *
221
     * @return Product|null
222
     */
223
    public function getProduct($id)
224
    {
225
        try {
226
            $Product = $this->app['eccube.repository.product']->get($id);
227
228
            if ($Product->getStatus()->getId() == Disp::DISPLAY_SHOW) {
229
                return $Product;
230
            }
231
        } catch (\Exception $e) {
232
            return null;
233
        }
234
235
        return null;
236
    }
237
238
    /**
239
     * Twigでphp関数を使用できるようにする。
240
     *
241
     * @return mixed|null
242
     */
243 View Code Duplication
    public function getPhpFunctions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        $arg_list = func_get_args();
246
        $function = array_shift($arg_list);
247
248
        if (is_callable($function)) {
249
            return call_user_func_array($function, $arg_list);
250
        }
251
252
        trigger_error('Called to an undefined function : php_'.$function, E_USER_WARNING);
253
254
        return null;
255
    }
256
257
    /**
258
     * Get the ClassCategories as JSON.
259
     *
260
     * @param Product $Product
261
     *
262
     * @return string
263
     */
264 15
    public function getClassCategoriesAsJson(Product $Product)
265
    {
266 15
        $Product->_calc();
267
        $class_categories = [
268
            '__unselected' => [
269
                '__unselected' => [
270 15
                    'name' => trans('product.text.please_select'),
271 15
                    'product_class_id' => '',
272
                ],
273
            ],
274
        ];
275 15
        foreach ($Product->getProductClasses() as $ProductClass) {
276
            /* @var $ProductClass \Eccube\Entity\ProductClass */
277 15
            $ClassCategory1 = $ProductClass->getClassCategory1();
278 15
            $ClassCategory2 = $ProductClass->getClassCategory2();
279 15
            if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
280
                continue;
281
            }
282 15
            $class_category_id1 = $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
283 15
            $class_category_id2 = $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
284 15
            $class_category_name2 = $ClassCategory2 ? $ClassCategory2->getName().($ProductClass->getStockFind() ? '' : trans('product.text.out_of_stock')) : '';
285
286 15
            $class_categories[$class_category_id1]['#'] = [
287 15
                'classcategory_id2' => '',
288 15
                'name' => trans('product.text.please_select'),
289 15
                'product_class_id' => '',
290
            ];
291 15
            $class_categories[$class_category_id1]['#'.$class_category_id2] = [
292 15
                'classcategory_id2' => $class_category_id2,
293 15
                'name' => $class_category_name2,
294 15
                'stock_find' => $ProductClass->getStockFind(),
295 15
                'price01' => $ProductClass->getPrice01() === null ? '' : number_format($ProductClass->getPrice01()),
296 15
                'price02' => number_format($ProductClass->getPrice02()),
297 15
                'price01_inc_tax' => $ProductClass->getPrice01() === null ? '' : number_format($ProductClass->getPrice01IncTax()),
298 15
                'price02_inc_tax' => number_format($ProductClass->getPrice02IncTax()),
299 15
                'product_class_id' => (string) $ProductClass->getId(),
300 15
                'product_code' => $ProductClass->getCode() === null ? '' : $ProductClass->getCode(),
301 15
                'sale_type' => (string) $ProductClass->getSaleType()->getId(),
302
            ];
303
        }
304
305 15
        return json_encode($class_categories);
306
    }
307
308
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$ext" missing
Loading history...
introduced by
Doc comment for parameter "$attr" missing
Loading history...
309
     * Display file extension icon
310
     *
311
     * @param $ext
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
312
     * @param $attr
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
313
     *
314
     * @return string
315
     */
316 3
    public function getExtensionIcon($ext, $attr = [])
317
    {
318
        $classes = [
319 3
            'txt' => 'fa-file-text-o',
320
            'rtf' => 'fa-file-text-o',
321
            'pdf' => 'fa-file-pdf-o',
322
            'doc' => 'fa-file-word-o',
323
            'docx' => 'fa-file-word-o',
324
            'csv' => 'fa-file-excel-o',
325
            'xls' => 'fa-file-excel-o',
326
            'xlsx' => 'fa-file-excel-o',
327
            'ppt' => 'fa-file-powerpoint-o',
328
            'pptx' => 'fa-file-powerpoint-o',
329
            'png' => 'fa-file-image-o',
330
            'jpg' => 'fa-file-image-o',
331
            'jpeg' => 'fa-file-image-o',
332
            'bmp' => 'fa-file-image-o',
333
            'gif' => 'fa-file-image-o',
334
            'zip' => 'fa-file-archive-o',
335
            'tar' => 'fa-file-archive-o',
336
            'gz' => 'fa-file-archive-o',
337
            'rar' => 'fa-file-archive-o',
338
            '7zip' => 'fa-file-archive-o',
339
            'mp3' => 'fa-file-audio-o',
340
            'm4a' => 'fa-file-audio-o',
341
            'wav' => 'fa-file-audio-o',
342
            'mp4' => 'fa-file-video-o',
343
            'wmv' => 'fa-file-video-o',
344
            'mov' => 'fa-file-video-o',
345
            'mkv' => 'fa-file-video-o',
346
        ];
347 3
        $class = isset($classes[$ext]) ? $classes[$ext] : 'fa-file-o';
348 3
        $attr['class'] = isset($attr['class'])
349 3
            ? $attr['class']." fa {$class}"
350
            : "fa {$class}";
351
352 3
        $html = '<i ';
353 3
        foreach ($attr as $name => $value) {
354 3
            $html .= "{$name}=\"$value\" ";
355
        }
356 3
        $html .= '></i>';
357
358 3
        return $html;
359
    }
360
}
361