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