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\Entity\Master\Disp; |
29
|
|
|
use Eccube\Entity\Product; |
30
|
|
|
use Eccube\Util\Str; |
31
|
|
|
use Silex\Application; |
32
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
33
|
|
|
|
34
|
|
|
class EccubeExtension extends \Twig_Extension |
|
|
|
|
35
|
80 |
|
{ |
36
|
|
|
private $app; |
37
|
80 |
|
|
38
|
|
|
public function __construct(Application $app) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->app = $app; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a list of functions to add to the existing list. |
45
|
43 |
|
* |
46
|
|
|
* @return array An array of functions |
47
|
|
|
*/ |
48
|
|
|
public function getFunctions() |
49
|
|
|
{ |
50
|
|
|
$RoutingExtension = $this->app['twig']->getExtension('routing'); |
51
|
43 |
|
|
52
|
|
|
return array( |
53
|
|
|
new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')), |
54
|
|
|
new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')), |
55
|
|
|
new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))), |
56
|
|
|
// Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url |
57
|
|
|
new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
58
|
|
|
// Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path |
59
|
43 |
|
new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
60
|
|
|
new \Twig_SimpleFunction('is_object', array($this, 'isObject')), |
61
|
|
|
new \Twig_SimpleFunction('get_product', array($this, 'getProduct')), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a list of filters. |
67
|
43 |
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getFilters() |
71
|
|
|
{ |
72
|
|
|
return array( |
73
|
|
|
new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')), |
74
|
|
|
new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')), |
75
|
62 |
|
new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')), |
76
|
|
|
new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')), |
77
|
62 |
|
new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')), |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Name of this extension |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getName() |
87
|
|
|
{ |
88
|
|
|
return 'eccube'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
|
|
|
|
92
|
|
|
* Name of this extension |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getCalcIncTax($price, $tax_rate, $tax_rule) |
97
|
|
|
{ |
98
|
|
|
return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Name of this extension |
103
|
|
|
* |
104
|
|
|
* @param array $menus |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getActiveMenus($menus = array()) |
108
|
|
|
{ |
109
|
|
|
$count = count($menus); |
110
|
|
|
for ($i = $count; $i <= 2; $i++) { |
111
|
6 |
|
$menus[] = ''; |
112
|
|
|
} |
113
|
|
|
|
114
|
6 |
|
return $menus; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Name of this extension |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getCsrfTokenForAnchor() |
123
|
|
|
{ |
124
|
|
|
$token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue(); |
125
|
|
|
|
126
|
|
|
return 'token-for-anchor=\''.$token.'\''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* bind から URL へ変換します。 |
131
|
|
|
* \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、 |
132
|
|
|
* RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
133
|
|
|
* 文字列 "/404?bind={bind}" を返します。 |
134
|
|
|
* |
135
|
|
|
* @param string $name |
|
|
|
|
136
|
|
|
* @param array $parameters |
|
|
|
|
137
|
|
|
* @param boolean $relative |
138
|
|
|
* @return string URL |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function getPath($name, $parameters = array(), $relative = false) |
141
|
|
|
{ |
142
|
|
|
$RoutingExtension = $this->app['twig']->getExtension('routing'); |
143
|
|
|
try { |
144
|
|
|
return $RoutingExtension->getPath($name, $parameters, $relative); |
145
|
|
|
} catch (RouteNotFoundException $e) { |
146
|
11 |
|
trigger_error($e->getMessage(), E_USER_WARNING); |
147
|
|
|
} |
148
|
|
|
|
149
|
11 |
|
return $RoutingExtension->getPath('homepage').'404?bind='.$name; |
150
|
|
|
} |
151
|
11 |
|
|
152
|
|
|
/** |
153
|
|
|
* bind から URL へ変換します。 |
154
|
|
|
* \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、 |
155
|
|
|
* RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
156
|
|
|
* 文字列 "/404?bind={bind}" を返します。 |
157
|
|
|
* |
158
|
|
|
* @param string $name |
|
|
|
|
159
|
|
|
* @param array $parameters |
|
|
|
|
160
|
|
|
* @param boolean $schemeRelative |
161
|
|
|
* @return string URL |
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function getUrl($name, $parameters = array(), $schemeRelative = false) |
164
|
|
|
{ |
165
|
|
|
$RoutingExtension = $this->app['twig']->getExtension('routing'); |
166
|
|
|
try { |
167
|
|
|
return $RoutingExtension->getUrl($name, $parameters, $schemeRelative); |
168
|
|
|
} catch (RouteNotFoundException $e) { |
169
|
|
|
trigger_error($e->getMessage(), E_USER_WARNING); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $RoutingExtension->getUrl('homepage').'404?bind='.$name; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check if the value is object |
177
|
|
|
* |
178
|
|
|
* @param object $value |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isObject($value) |
182
|
|
|
{ |
183
|
|
|
return is_object($value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
|
|
|
|
187
|
|
|
* product_idで指定したProductを取得 |
188
|
|
|
* Productが取得できない場合、または非公開の場合、商品情報は表示させない。 |
189
|
|
|
* デバッグ環境以外ではProductが取得できなくでもエラー画面は表示させず無視される。 |
190
|
|
|
* |
191
|
|
|
* @param $id |
|
|
|
|
192
|
|
|
* @return Product|null |
193
|
|
|
*/ |
194
|
|
|
public function getProduct($id) |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
|
|
$Product = $this->app['eccube.repository.product']->get($id); |
198
|
|
|
|
199
|
|
|
if ($Product->getStatus()->getId() == Disp::DISPLAY_SHOW) { |
200
|
|
|
return $Product; |
201
|
|
|
} |
202
|
|
|
} catch (\Exception $e) { |
203
|
|
|
return null; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
|
|
|
|
210
|
|
|
* return No Image filename |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getNoImageProduct($image) |
215
|
|
|
{ |
216
|
|
|
return empty($image) ? 'no_image_product.jpg' : $image; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
|
|
|
|
220
|
|
|
* Name of this extension |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
225
|
|
|
{ |
226
|
|
|
if (is_null($date)) { |
227
|
|
|
return $value; |
228
|
|
|
} else { |
229
|
|
|
return $date->format($format); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
|
|
|
|
234
|
|
|
* Name of this extension |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
239
|
|
|
{ |
240
|
|
|
$price = number_format($number, $decimals, $decPoint, $thousandsSep); |
241
|
|
|
$price = '¥ '.$price; |
242
|
|
|
|
243
|
|
|
return $price; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
|
|
|
|
247
|
|
|
* Name of this extension |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function getEllipsis($value, $length = 100, $end = '...') |
252
|
|
|
{ |
253
|
|
|
return Str::ellipsis($value, $length, $end); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
|
|
|
|
257
|
|
|
* Name of this extension |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public function getTimeAgo($date) |
262
|
|
|
{ |
263
|
|
|
return Str::timeAgo($date); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|