|
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
|
|
|
|
|
31
|
|
|
class EccubeExtension extends \Twig_Extension |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
private $app; |
|
34
|
|
|
|
|
35
|
162 |
|
public function __construct(Application $app) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
162 |
|
$this->app = $app; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns a list of functions to add to the existing list. |
|
42
|
|
|
* |
|
43
|
|
|
* @return array An array of functions |
|
44
|
|
|
*/ |
|
45
|
74 |
|
public function getFunctions() |
|
46
|
|
|
{ |
|
47
|
|
|
return array( |
|
48
|
|
|
new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')), |
|
49
|
|
|
new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')), |
|
50
|
|
|
new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))), |
|
51
|
74 |
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns a list of filters. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
74 |
|
public function getFilters() |
|
60
|
|
|
{ |
|
61
|
|
|
return array( |
|
62
|
|
|
new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')), |
|
63
|
|
|
new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')), |
|
64
|
|
|
new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')), |
|
65
|
|
|
new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')), |
|
66
|
|
|
new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')), |
|
67
|
74 |
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Name of this extension |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
138 |
|
public function getName() |
|
76
|
|
|
{ |
|
77
|
138 |
|
return 'eccube'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
|
|
|
|
|
81
|
|
|
* Name of this extension |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getCalcIncTax($price, $tax_rate, $tax_rule) |
|
86
|
|
|
{ |
|
87
|
|
|
return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Name of this extension |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $menus |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getActiveMenus($menus = array()) |
|
97
|
|
|
{ |
|
98
|
|
|
$count = count($menus); |
|
99
|
|
|
for ($i = $count; $i <= 2; $i++) { |
|
100
|
|
|
$menus[] = ''; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $menus; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Name of this extension |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
7 |
|
public function getCsrfTokenForAnchor() |
|
112
|
|
|
{ |
|
113
|
|
|
$token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue(); |
|
114
|
7 |
|
return 'token-for-anchor=\'' . $token . '\''; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
|
|
|
|
|
118
|
|
|
* return No Image filename |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function getNoImageProduct($image) |
|
123
|
|
|
{ |
|
124
|
1 |
|
return empty($image) ? 'no_image_product.jpg' : $image; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
|
|
|
|
|
128
|
|
|
* Name of this extension |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
|
133
|
|
|
{ |
|
134
|
|
|
if (is_null($date)) { |
|
135
|
1 |
|
return $value; |
|
136
|
|
|
} else { |
|
137
|
|
|
return $date->format($format); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
|
|
|
|
|
142
|
|
|
* Name of this extension |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
31 |
|
public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
|
147
|
|
|
{ |
|
148
|
|
|
$price = number_format($number, $decimals, $decPoint, $thousandsSep); |
|
149
|
31 |
|
$price = '¥ ' . $price; |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
31 |
|
return $price; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
|
|
|
|
|
155
|
|
|
* Name of this extension |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getEllipsis($value, $length = 100, $end = '...') |
|
160
|
|
|
{ |
|
161
|
|
|
return Str::ellipsis($value, $length, $end); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
|
|
|
|
|
165
|
|
|
* Name of this extension |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getTimeAgo($date) |
|
170
|
|
|
{ |
|
171
|
|
|
return Str::timeAgo($date); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|