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