|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\View; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\Deprecation; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Defines an extra set of basic methods that can be used in templates |
|
9
|
|
|
* that are not defined on sub-classes of {@link ViewableData}. |
|
10
|
|
|
*/ |
|
11
|
|
|
class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $iteratorPos; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $iteratorTotalItems; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function get_template_iterator_variables() |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
'IsFirst', |
|
30
|
|
|
'IsLast', |
|
31
|
|
|
'First', |
|
32
|
|
|
'Last', |
|
33
|
|
|
'FirstLast', |
|
34
|
|
|
'Middle', |
|
35
|
|
|
'MiddleString', |
|
36
|
|
|
'Even', |
|
37
|
|
|
'Odd', |
|
38
|
|
|
'EvenOdd', |
|
39
|
|
|
'Pos', |
|
40
|
|
|
'FromEnd', |
|
41
|
|
|
'TotalItems', |
|
42
|
|
|
'Modulus', |
|
43
|
|
|
'MultipleOf', |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set the current iterator properties - where we are on the iterator. |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $pos position in iterator |
|
51
|
|
|
* @param int $totalItems total number of items |
|
52
|
|
|
*/ |
|
53
|
|
|
public function iteratorProperties($pos, $totalItems) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->iteratorPos = $pos; |
|
56
|
|
|
$this->iteratorTotalItems = $totalItems; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns true if this object is the first in a set. |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function IsFirst() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->iteratorPos == 0; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @deprecated 5.0.0 Use IsFirst() to avoid clashes with SS_Lists |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function First() |
|
74
|
|
|
{ |
|
75
|
|
|
Deprecation::notice('5.0.0', 'Use IsFirst() to avoid clashes with SS_Lists'); |
|
76
|
|
|
return $this->IsFirst(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns true if this object is the last in a set. |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function IsLast() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->iteratorPos == $this->iteratorTotalItems - 1; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @deprecated 5.0.0 Use IsLast() to avoid clashes with SS_Lists |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function Last() |
|
94
|
|
|
{ |
|
95
|
|
|
Deprecation::notice('5.0.0', 'Use IsLast() to avoid clashes with SS_Lists'); |
|
96
|
|
|
return $this->IsLast(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns 'first' or 'last' if this is the first or last object in the set. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string|null |
|
103
|
|
|
*/ |
|
104
|
|
|
public function FirstLast() |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->IsFirst() && $this->IsLast()) { |
|
107
|
|
|
return 'first last'; |
|
108
|
|
|
} |
|
109
|
|
|
if ($this->IsFirst()) { |
|
110
|
|
|
return 'first'; |
|
111
|
|
|
} |
|
112
|
|
|
if ($this->IsLast()) { |
|
113
|
|
|
return 'last'; |
|
114
|
|
|
} |
|
115
|
|
|
return null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return true if this object is between the first & last objects. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function Middle() |
|
124
|
|
|
{ |
|
125
|
|
|
return !$this->IsFirst() && !$this->IsLast(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Return 'middle' if this object is between the first & last objects. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function MiddleString() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->Middle()) { |
|
136
|
|
|
return 'middle'; |
|
137
|
|
|
} |
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Return true if this object is an even item in the set. |
|
143
|
|
|
* The count starts from $startIndex, which defaults to 1. |
|
144
|
|
|
* |
|
145
|
|
|
* @param int $startIndex Number to start count from. |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function Even($startIndex = 1) |
|
149
|
|
|
{ |
|
150
|
|
|
return !$this->Odd($startIndex); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Return true if this is an odd item in the set. |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $startIndex Number to start count from. |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function Odd($startIndex = 1) |
|
160
|
|
|
{ |
|
161
|
|
|
return (bool)(($this->iteratorPos + $startIndex) % 2); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Return 'even' or 'odd' if this object is in an even or odd position in the set respectively. |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $startIndex Number to start count from. |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
public function EvenOdd($startIndex = 1) |
|
171
|
|
|
{ |
|
172
|
|
|
return ($this->Even($startIndex)) ? 'even' : 'odd'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return the numerical position of this object in the container set. The count starts at $startIndex. |
|
177
|
|
|
* The default is the give the position using a 1-based index. |
|
178
|
|
|
* |
|
179
|
|
|
* @param int $startIndex Number to start count from. |
|
180
|
|
|
* @return int |
|
181
|
|
|
*/ |
|
182
|
|
|
public function Pos($startIndex = 1) |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->iteratorPos + $startIndex; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Return the position of this item from the last item in the list. The position of the final |
|
189
|
|
|
* item is $endIndex, which defaults to 1. |
|
190
|
|
|
* |
|
191
|
|
|
* @param int $endIndex Value of the last item |
|
192
|
|
|
* @return int |
|
193
|
|
|
*/ |
|
194
|
|
|
public function FromEnd($endIndex = 1) |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->iteratorTotalItems - $this->iteratorPos + $endIndex - 1; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Return the total number of "sibling" items in the dataset. |
|
201
|
|
|
* |
|
202
|
|
|
* @return int |
|
203
|
|
|
*/ |
|
204
|
|
|
public function TotalItems() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->iteratorTotalItems; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns the modulus of the numerical position of the item in the data set. |
|
211
|
|
|
* The count starts from $startIndex, which defaults to 1. |
|
212
|
|
|
* |
|
213
|
|
|
* @param int $mod The number to perform Mod operation to. |
|
214
|
|
|
* @param int $startIndex Number to start count from. |
|
215
|
|
|
* @return int |
|
216
|
|
|
*/ |
|
217
|
|
|
public function Modulus($mod, $startIndex = 1) |
|
218
|
|
|
{ |
|
219
|
|
|
return ($this->iteratorPos + $startIndex) % $mod; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Returns true or false depending on if the pos of the iterator is a multiple of a specific number. |
|
224
|
|
|
* So, <% if MultipleOf(3) %> would return true on indexes: 3,6,9,12,15, etc. |
|
225
|
|
|
* The count starts from $offset, which defaults to 1. |
|
226
|
|
|
* |
|
227
|
|
|
* @param int $factor The multiple of which to return |
|
228
|
|
|
* @param int $offset Number to start count from. |
|
229
|
|
|
* @return bool |
|
230
|
|
|
*/ |
|
231
|
|
|
public function MultipleOf($factor, $offset = 1) |
|
232
|
|
|
{ |
|
233
|
|
|
return (bool)($this->Modulus($factor, $offset) == 0); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|