|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
namespace Eccube\Twig\Extension; |
|
26
|
|
|
|
|
27
|
|
|
use Eccube\Common\Constant; |
|
28
|
|
|
use Eccube\Util\Str; |
|
29
|
|
|
use Silex\Application; |
|
30
|
|
|
use Symfony\Bridge\Twig\Extension\RoutingExtension; |
|
31
|
|
|
use Symfony\Component\Form\FormView; |
|
32
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
|
33
|
|
|
|
|
34
|
|
|
class EccubeExtension extends \Twig_Extension |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
private $app; |
|
37
|
1178 |
|
|
|
38
|
|
|
public function __construct(Application $app) |
|
|
|
|
|
|
39
|
1178 |
|
{ |
|
40
|
|
|
$this->app = $app; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns a list of functions to add to the existing list. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array An array of functions |
|
47
|
96 |
|
*/ |
|
48
|
|
|
public function getFunctions() |
|
49
|
96 |
|
{ |
|
50
|
|
|
$RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class); |
|
51
|
|
|
|
|
52
|
96 |
|
$app = $this->app; |
|
53
|
96 |
|
return array( |
|
|
|
|
|
|
54
|
96 |
|
new \Twig_SimpleFunction('has_errors', array($this, 'hasErrors')), |
|
55
|
96 |
|
new \Twig_SimpleFunction('is_object', array($this, 'isObject')), |
|
56
|
|
|
new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')), |
|
57
|
|
|
new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')), |
|
58
|
96 |
|
new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))), |
|
59
|
|
|
|
|
60
|
96 |
|
// Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url |
|
61
|
|
|
new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
|
62
|
|
|
// Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path |
|
63
|
|
|
new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
|
64
|
|
|
|
|
65
|
|
|
new \Twig_SimpleFunction('php_*', function() { |
|
|
|
|
|
|
66
|
|
|
$arg_list = func_get_args(); |
|
67
|
|
|
$function = array_shift($arg_list); |
|
68
|
|
|
if (is_callable($function)) { |
|
69
|
96 |
|
return call_user_func_array($function, $arg_list); |
|
70
|
|
|
} |
|
71
|
|
|
trigger_error('Called to an undefined function : php_'. $function, E_USER_WARNING); |
|
|
|
|
|
|
72
|
96 |
|
|
|
73
|
96 |
|
}, ['pre_escape' => 'html', 'is_safe' => ['html']]), |
|
74
|
96 |
|
|
|
75
|
96 |
|
new \Twig_SimpleFunction('eccube_block_*', function() use ($app) { |
|
|
|
|
|
|
76
|
96 |
|
$sources = $app['eccube.twig.block.templates']; |
|
77
|
|
|
$arg_list = func_get_args(); |
|
78
|
|
|
$block_name = array_shift($arg_list); |
|
79
|
|
|
foreach ($sources as $source) { |
|
80
|
|
|
$template = $app['twig']->loadTemplate($source); |
|
81
|
|
|
if (!isset($arg_list[0])) { |
|
82
|
|
|
$arg_list[0] = []; |
|
83
|
|
|
} |
|
84
|
|
|
if ($template->hasBlock($block_name, $arg_list[0])) { |
|
85
|
1178 |
|
echo $result = $template->renderBlock($block_name, $arg_list[0]); |
|
86
|
|
|
return; |
|
|
|
|
|
|
87
|
1178 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
trigger_error($block_name.' block is not found', E_USER_WARNING); |
|
90
|
|
|
}, ['pre_escape' => 'html', 'is_safe' => ['html']]) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
10 |
|
* Returns a list of filters. |
|
96
|
|
|
* |
|
97
|
10 |
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getFilters() |
|
100
|
|
|
{ |
|
101
|
|
|
return array( |
|
102
|
|
|
new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')), |
|
103
|
|
|
new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')), |
|
104
|
|
|
new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')), |
|
105
|
|
|
new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')), |
|
106
|
142 |
|
new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')), |
|
107
|
|
|
); |
|
108
|
142 |
|
} |
|
109
|
142 |
|
|
|
110
|
87 |
|
public function getTokenParsers() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
return $this->app['twig_parsers']; |
|
113
|
142 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Name of this extension |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getName() |
|
121
|
27 |
|
{ |
|
122
|
|
|
return 'eccube'; |
|
123
|
27 |
|
} |
|
124
|
27 |
|
|
|
125
|
|
|
/** |
|
|
|
|
|
|
126
|
|
|
* Name of this extension |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getCalcIncTax($price, $tax_rate, $tax_rule) |
|
131
|
|
|
{ |
|
132
|
8 |
|
return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule); |
|
133
|
|
|
} |
|
134
|
8 |
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Name of this extension |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $menus |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getActiveMenus($menus = array()) |
|
142
|
14 |
|
{ |
|
143
|
|
|
$count = count($menus); |
|
144
|
14 |
|
for ($i = $count; $i <= 2; $i++) { |
|
145
|
10 |
|
$menus[] = ''; |
|
146
|
|
|
} |
|
147
|
4 |
|
|
|
148
|
|
|
return $menus; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Name of this extension |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
87 |
|
public function getCsrfTokenForAnchor() |
|
157
|
|
|
{ |
|
158
|
87 |
|
$token = $this->app['csrf.token_manager']->getToken(Constant::TOKEN_NAME)->getValue(); |
|
159
|
87 |
|
return 'token-for-anchor=\'' . $token . '\''; |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
87 |
|
|
|
162
|
|
|
/** |
|
|
|
|
|
|
163
|
|
|
* return No Image filename |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getNoImageProduct($image) |
|
168
|
|
|
{ |
|
169
|
|
|
return empty($image) ? 'no_image_product.jpg' : $image; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
|
|
|
|
|
173
|
|
|
* Name of this extension |
|
174
|
|
|
* |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
|
178
|
|
|
{ |
|
179
|
|
|
if (is_null($date)) { |
|
180
|
|
|
return $value; |
|
181
|
|
|
} else { |
|
182
|
|
|
return $date->format($format); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
|
|
|
|
|
187
|
|
|
* Name of this extension |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
|
192
|
|
|
{ |
|
193
|
|
|
$price = number_format($number, $decimals, $decPoint, $thousandsSep); |
|
194
|
|
|
$price = '¥ ' . $price; |
|
|
|
|
|
|
195
|
225 |
|
|
|
196
|
|
|
return $price; |
|
197
|
225 |
|
} |
|
198
|
|
|
|
|
199
|
225 |
|
/** |
|
|
|
|
|
|
200
|
|
|
* Name of this extension |
|
201
|
|
|
* |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getEllipsis($value, $length = 100, $end = '...') |
|
205
|
|
|
{ |
|
206
|
|
|
return Str::ellipsis($value, $length, $end); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
|
|
|
|
|
210
|
|
|
* Name of this extension |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getTimeAgo($date) |
|
215
|
|
|
{ |
|
216
|
|
|
return Str::timeAgo($date); |
|
217
|
|
|
} |
|
218
|
216 |
|
|
|
219
|
|
|
/** |
|
220
|
216 |
|
* bind から URL へ変換します。 |
|
221
|
|
|
* \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、 |
|
222
|
216 |
|
* RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
|
223
|
|
|
* 文字列 "/404?bind={bind}" を返します。 |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $name |
|
|
|
|
|
|
226
|
|
|
* @param array $parameters |
|
|
|
|
|
|
227
|
|
|
* @param boolean $relative |
|
228
|
|
|
* @return string URL |
|
229
|
|
|
*/ |
|
230
|
|
View Code Duplication |
public function getPath($name, $parameters = array(), $relative = false) |
|
|
|
|
|
|
231
|
|
|
{ |
|
232
|
|
|
$RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class); |
|
233
|
|
|
try { |
|
234
|
|
|
return $RoutingExtension->getPath($name, $parameters, $relative); |
|
235
|
|
|
} catch (RouteNotFoundException $e) { |
|
236
|
2 |
|
trigger_error($e->getMessage(), E_USER_WARNING); |
|
237
|
|
|
} |
|
238
|
2 |
|
|
|
239
|
|
|
return $RoutingExtension->getPath('homepage').'404?bind='.$name; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* bind から URL へ変換します。 |
|
244
|
|
|
* \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、 |
|
245
|
|
|
* RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
|
246
|
|
|
* 文字列 "/404?bind={bind}" を返します。 |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $name |
|
|
|
|
|
|
249
|
|
|
* @param array $parameters |
|
|
|
|
|
|
250
|
|
|
* @param boolean $schemeRelative |
|
251
|
|
|
* @return string URL |
|
252
|
|
|
*/ |
|
253
|
|
View Code Duplication |
public function getUrl($name, $parameters = array(), $schemeRelative = false) |
|
|
|
|
|
|
254
|
|
|
{ |
|
255
|
|
|
$RoutingExtension = $this->app['twig']->getExtension(RoutingExtension::class); |
|
256
|
|
|
try { |
|
257
|
|
|
return $RoutingExtension->getUrl($name, $parameters, $schemeRelative); |
|
258
|
|
|
} catch (RouteNotFoundException $e) { |
|
259
|
|
|
trigger_error($e->getMessage(), E_USER_WARNING); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $RoutingExtension->getUrl('homepage').'404?bind='.$name; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Check if the value is object |
|
267
|
|
|
* |
|
268
|
|
|
* @param object $value |
|
269
|
|
|
* @return bool |
|
270
|
|
|
*/ |
|
271
|
|
|
public function isObject($value) |
|
272
|
|
|
{ |
|
273
|
|
|
return is_object($value); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* FormView にエラーが含まれるかを返す. |
|
278
|
|
|
* |
|
279
|
|
|
* @return bool |
|
280
|
|
|
*/ |
|
281
|
|
|
public function hasErrors() |
|
282
|
|
|
{ |
|
283
|
|
|
$hasErrors = false; |
|
284
|
|
|
|
|
285
|
|
|
$views = func_get_args(); |
|
286
|
|
|
foreach ($views as $view) { |
|
287
|
|
|
if (!$view instanceof FormView) { |
|
288
|
|
|
throw new \InvalidArgumentException(); |
|
289
|
|
|
} |
|
290
|
|
|
if (count($view->vars['errors'])) { |
|
291
|
|
|
$hasErrors = true; |
|
292
|
|
|
break; |
|
293
|
|
|
}; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return $hasErrors; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|