| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id$
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Pager_Layout
|
|
|
24 |
*
|
|
|
25 |
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
26 |
* @package Doctrine
|
|
|
27 |
* @subpackage Pager
|
|
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
29 |
* @version $Revision$
|
|
|
30 |
* @link www.doctrine-project.org
|
|
|
31 |
* @since 0.9
|
|
|
32 |
*/
|
|
|
33 |
class Doctrine_Pager_Layout
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* @var Doctrine_Pager $_pager Doctrine_Pager object related to the pager layout
|
|
|
37 |
*/
|
|
|
38 |
private $_pager;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @var Doctrine_Pager_Range $_pagerRange Doctrine_Pager_Range object related to the pager layout
|
|
|
42 |
*/
|
|
|
43 |
private $_pagerRange;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @var string $_template Template to be applied for inactive pages
|
|
|
47 |
* (and also active is selected template is not defined)
|
|
|
48 |
*/
|
|
|
49 |
private $_template;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* @var string $_selectedTemplate Template to be applied for active page
|
|
|
53 |
*/
|
|
|
54 |
private $_selectedTemplate;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @var string $_separatorTemplate Separator template, applied between each page
|
|
|
58 |
*/
|
|
|
59 |
private $_separatorTemplate;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @var string $_urlMask URL to be assigned for each page. Masks are used as: {%var_name}
|
|
|
63 |
*/
|
|
|
64 |
private $_urlMask;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @var array $_maskReplacements Stores references of masks and their correspondent
|
|
|
68 |
* (replaces defined masks with new masks or values)
|
|
|
69 |
*/
|
|
|
70 |
private $_maskReplacements = array();
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* __construct
|
|
|
75 |
*
|
|
|
76 |
* @param Doctrine_Pager $pager Doctrine_Pager object related to the pager layout
|
|
|
77 |
* @param Doctrine_Pager_Range $pagerRange Doctrine_Pager_Range object related to the pager layout
|
|
|
78 |
* @param string $urlMask URL to be assigned for each page
|
|
|
79 |
* @return void
|
|
|
80 |
*/
|
|
|
81 |
public function __construct($pager, $pagerRange, $urlMask)
|
|
|
82 |
{
|
|
|
83 |
$this->_setPager($pager);
|
|
|
84 |
$this->_setPagerRange($pagerRange);
|
|
|
85 |
$this->_setUrlMask($urlMask);
|
|
|
86 |
|
|
|
87 |
$this->setTemplate('[<a href="{%url}">{%page}</a>]');
|
|
|
88 |
$this->setSelectedTemplate('');
|
|
|
89 |
$this->setSeparatorTemplate('');
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* getPager
|
|
|
94 |
*
|
|
|
95 |
* Returns the Doctrine_Pager object related to the pager layout
|
|
|
96 |
*
|
|
|
97 |
* @return Doctrine_Pager Doctrine_Pager object related to the pager range
|
|
|
98 |
*/
|
|
|
99 |
public function getPager()
|
|
|
100 |
{
|
|
|
101 |
return $this->_pager;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* _setPager
|
|
|
106 |
*
|
|
|
107 |
* Defines the Doctrine_Pager object related to the pager layout
|
|
|
108 |
*
|
|
|
109 |
* @param $pager Doctrine_Pager object related to the pager range
|
|
|
110 |
* @return void
|
|
|
111 |
*/
|
|
|
112 |
protected function _setPager($pager)
|
|
|
113 |
{
|
|
|
114 |
$this->_pager = $pager;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* execute
|
|
|
119 |
*
|
|
|
120 |
* Handy method to execute the query without need to retrieve the Pager instance
|
|
|
121 |
*
|
|
|
122 |
* @param $params Optional parameters to Doctrine_Query::execute
|
|
|
123 |
* @param $hydrationMode Hydration Mode of Doctrine_Query::execute returned ResultSet.
|
|
|
124 |
* @return Doctrine_Collection The root collection
|
|
|
125 |
*/
|
|
|
126 |
public function execute($params = array(), $hydrationMode = null)
|
|
|
127 |
{
|
|
|
128 |
return $this->getPager()->execute($params, $hydrationMode);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* getPagerRange
|
|
|
133 |
*
|
|
|
134 |
* Returns the Doctrine_Pager_Range subclass object related to the pager layout
|
|
|
135 |
*
|
|
|
136 |
* @return Doctrine_Pager_Range Doctrine_Pager_Range subclass object related to the pager range
|
|
|
137 |
*/
|
|
|
138 |
public function getPagerRange()
|
|
|
139 |
{
|
|
|
140 |
return $this->_pagerRange;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* _setPagerRange
|
|
|
145 |
*
|
|
|
146 |
* Defines the Doctrine_Pager_Range subclass object related to the pager layout
|
|
|
147 |
*
|
|
|
148 |
* @param $pagerRange Doctrine_Pager_Range subclass object related to the pager range
|
|
|
149 |
* @return void
|
|
|
150 |
*/
|
|
|
151 |
protected function _setPagerRange($pagerRange)
|
|
|
152 |
{
|
|
|
153 |
$this->_pagerRange = $pagerRange;
|
|
|
154 |
$this->getPagerRange()->setPager($this->getPager());
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* getUrlMask
|
|
|
159 |
*
|
|
|
160 |
* Returns the URL to be assigned for each page
|
|
|
161 |
*
|
|
|
162 |
* @return string URL to be assigned for each page
|
|
|
163 |
*/
|
|
|
164 |
public function getUrlMask()
|
|
|
165 |
{
|
|
|
166 |
return $this->_urlMask;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* _setUrlMask
|
|
|
171 |
*
|
|
|
172 |
* Defines the URL to be assigned for each page
|
|
|
173 |
*
|
|
|
174 |
* @param $urlMask URL to be assigned for each page
|
|
|
175 |
* @return void
|
|
|
176 |
*/
|
|
|
177 |
protected function _setUrlMask($urlMask)
|
|
|
178 |
{
|
|
|
179 |
$this->_urlMask = $urlMask;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* getTemplate
|
|
|
185 |
*
|
|
|
186 |
* Returns the Template to be applied for inactive pages
|
|
|
187 |
*
|
|
|
188 |
* @return string Template to be applied for inactive pages
|
|
|
189 |
*/
|
|
|
190 |
public function getTemplate()
|
|
|
191 |
{
|
|
|
192 |
return $this->_template;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* setTemplate
|
|
|
197 |
*
|
|
|
198 |
* Defines the Template to be applied for inactive pages
|
|
|
199 |
* (also active page if selected template not defined)
|
|
|
200 |
*
|
|
|
201 |
* @param $template Template to be applied for inactive pages
|
|
|
202 |
* @return void
|
|
|
203 |
*/
|
|
|
204 |
public function setTemplate($template)
|
|
|
205 |
{
|
|
|
206 |
$this->_template = $template;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* getSelectedTemplate
|
|
|
211 |
*
|
|
|
212 |
* Returns the Template to be applied for active page
|
|
|
213 |
*
|
|
|
214 |
* @return string Template to be applied for active page
|
|
|
215 |
*/
|
|
|
216 |
public function getSelectedTemplate()
|
|
|
217 |
{
|
|
|
218 |
return $this->_selectedTemplate;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* setSelectedTemplate
|
|
|
223 |
*
|
|
|
224 |
* Defines the Template to be applied for active page
|
|
|
225 |
*
|
|
|
226 |
* @param $selectedTemplate Template to be applied for active page
|
|
|
227 |
* @return void
|
|
|
228 |
*/
|
|
|
229 |
public function setSelectedTemplate($selectedTemplate)
|
|
|
230 |
{
|
|
|
231 |
$this->_selectedTemplate = $selectedTemplate;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* getSeparatorTemplate
|
|
|
236 |
*
|
|
|
237 |
* Returns the Separator template, applied between each page
|
|
|
238 |
*
|
|
|
239 |
* @return string Separator template, applied between each page
|
|
|
240 |
*/
|
|
|
241 |
public function getSeparatorTemplate()
|
|
|
242 |
{
|
|
|
243 |
return $this->_separatorTemplate;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* setSeparatorTemplate
|
|
|
248 |
*
|
|
|
249 |
* Defines the Separator template, applied between each page
|
|
|
250 |
*
|
|
|
251 |
* @param $separatorTemplate Separator template, applied between each page
|
|
|
252 |
* @return void
|
|
|
253 |
*/
|
|
|
254 |
public function setSeparatorTemplate($separatorTemplate)
|
|
|
255 |
{
|
|
|
256 |
$this->_separatorTemplate = $separatorTemplate;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* addMaskReplacement
|
|
|
261 |
*
|
|
|
262 |
* Defines a mask replacement. When parsing template, it converts replacement
|
|
|
263 |
* masks into new ones (or values), allowing to change masks behavior on the fly
|
|
|
264 |
*
|
|
|
265 |
* @param $oldMask Mask to be replaced
|
|
|
266 |
* @param $newMask Mask or Value that will be defined after replacement
|
|
|
267 |
* @param $asValue Optional value (default false) that if defined as true,
|
|
|
268 |
* changes the bahavior of replacement mask to replacement
|
|
|
269 |
* value
|
|
|
270 |
* @return void
|
|
|
271 |
*/
|
|
|
272 |
public function addMaskReplacement($oldMask, $newMask, $asValue = false)
|
|
|
273 |
{
|
|
|
274 |
if (($oldMask = trim($oldMask)) != 'page_number') {
|
|
|
275 |
$this->_maskReplacements[$oldMask] = array(
|
|
|
276 |
'newMask' => $newMask,
|
|
|
277 |
'asValue' => ($asValue === false) ? false : true
|
|
|
278 |
);
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* removeMaskReplacement
|
|
|
284 |
*
|
|
|
285 |
* Remove a mask replacement
|
|
|
286 |
*
|
|
|
287 |
* @param $oldMask Replacement Mask to be removed
|
|
|
288 |
* @return void
|
|
|
289 |
*/
|
|
|
290 |
public function removeMaskReplacement($oldMask)
|
|
|
291 |
{
|
|
|
292 |
if (isset($this->_maskReplacements[$oldMask])) {
|
|
|
293 |
$this->_maskReplacements[$oldMask] = null;
|
|
|
294 |
unset($this->_maskReplacements[$oldMask]);
|
|
|
295 |
}
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* cleanMaskReplacements
|
|
|
301 |
*
|
|
|
302 |
* Remove all mask replacements
|
|
|
303 |
*
|
|
|
304 |
* @return void
|
|
|
305 |
*/
|
|
|
306 |
public function cleanMaskReplacements()
|
|
|
307 |
{
|
|
|
308 |
$this->_maskReplacements = null;
|
|
|
309 |
$this->_maskReplacements = array();
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* display
|
|
|
314 |
*
|
|
|
315 |
* Displays the pager on screen based on templates and options defined
|
|
|
316 |
*
|
|
|
317 |
* @param $options Optional parameters to be applied in template and url mask
|
|
|
318 |
* @param $return Optional parameter if you want to capture the output of this method call
|
|
|
319 |
* (Default value is false), instead of printing it
|
|
|
320 |
* @return void If you would like to capture the output of Doctrine_Pager_Layout::display(),
|
|
|
321 |
* use the $return parameter. If this parameter is set to TRUE, this method
|
|
|
322 |
* will return its output, instead of printing it (which it does by default)
|
|
|
323 |
*/
|
|
|
324 |
public function display($options = array(), $return = false)
|
|
|
325 |
{
|
|
|
326 |
$range = $this->getPagerRange()->rangeAroundPage();
|
|
|
327 |
$str = '';
|
|
|
328 |
|
|
|
329 |
// For each page in range
|
|
|
330 |
for ($i = 0, $l = count($range); $i < $l; $i++) {
|
|
|
331 |
// Define some optional mask values
|
|
|
332 |
$options['page_number'] = $range[$i];
|
|
|
333 |
|
|
|
334 |
$str .= $this->processPage($options);
|
|
|
335 |
|
|
|
336 |
// Apply separator between pages
|
|
|
337 |
if ($i < $l - 1) {
|
|
|
338 |
$str .= $this->getSeparatorTemplate();
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
// Possible wish to return value instead of print it on screen
|
|
|
343 |
if ($return) {
|
|
|
344 |
return $str;
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
echo $str;
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
/**
|
|
|
351 |
* processPage
|
|
|
352 |
*
|
|
|
353 |
* Parses the template and returns the string of a processed page
|
|
|
354 |
*
|
|
|
355 |
* @param array Optional parameters to be applied in template and url mask
|
|
|
356 |
* @return string Processed template for the given page
|
|
|
357 |
*/
|
|
|
358 |
public function processPage($options = array())
|
|
|
359 |
{
|
|
|
360 |
// Check if at least basic options are defined
|
|
|
361 |
if ( !isset($options['page_number'])) {
|
|
|
362 |
throw new Doctrine_Pager_Exception(
|
|
|
363 |
'Cannot process template of the given page. ' .
|
|
|
364 |
'Missing at least one of needed parameters: \'page\' or \'page_number\''
|
|
|
365 |
);
|
|
|
366 |
|
|
|
367 |
// Should never reach here
|
|
|
368 |
return '';
|
|
|
369 |
}
|
|
|
370 |
|
|
|
371 |
// Assign "page" options index if not defined yet
|
|
|
372 |
if ( !isset($this->_maskReplacements['page']) && !isset($options['page'])) {
|
|
|
373 |
$options['page'] = $options['page_number'];
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
return $this->_parseTemplate($options);
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
/**
|
|
|
380 |
* Simply calls display, and returns the output.
|
|
|
381 |
*/
|
|
|
382 |
public function __toString()
|
|
|
383 |
{
|
|
|
384 |
return $this->display(array(), true);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* _parseTemplate
|
|
|
389 |
*
|
|
|
390 |
* Parse the template of a given page and return the processed template
|
|
|
391 |
*
|
|
|
392 |
* @param array Optional parameters to be applied in template and url mask
|
|
|
393 |
* @return string
|
|
|
394 |
*/
|
|
|
395 |
protected function _parseTemplate($options = array())
|
|
|
396 |
{
|
|
|
397 |
$str = $this->_parseUrlTemplate($options);
|
|
|
398 |
$replacements = $this->_parseReplacementsTemplate($options);
|
|
|
399 |
|
|
|
400 |
return strtr($str, $replacements);
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
/**
|
|
|
404 |
* _parseUrlTemplate
|
|
|
405 |
*
|
|
|
406 |
* Parse the url mask to return the correct template depending of the options sent.
|
|
|
407 |
* Already process the mask replacements assigned.
|
|
|
408 |
*
|
|
|
409 |
* @param $options Optional parameters to be applied in template and url mask
|
|
|
410 |
* @return string
|
|
|
411 |
*/
|
|
|
412 |
protected function _parseUrlTemplate($options = array())
|
|
|
413 |
{
|
|
|
414 |
$str = '';
|
|
|
415 |
|
|
|
416 |
// If given page is the current active one
|
|
|
417 |
if ($options['page_number'] == $this->getPager()->getPage()) {
|
|
|
418 |
$str = $this->_parseMaskReplacements($this->getSelectedTemplate());
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
// Possible attempt where Selected == Template
|
|
|
422 |
if ($str == '') {
|
|
|
423 |
$str = $this->_parseMaskReplacements($this->getTemplate());
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
return $str;
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
/**
|
|
|
430 |
* _parseUrl
|
|
|
431 |
*
|
|
|
432 |
* Parse the mask replacements of a given page
|
|
|
433 |
*
|
|
|
434 |
* @param $options Optional parameters to be applied in template and url mask
|
|
|
435 |
* @return string
|
|
|
436 |
*/
|
|
|
437 |
protected function _parseReplacementsTemplate($options = array())
|
|
|
438 |
{
|
|
|
439 |
// Defining "url" options index to allow {%url} mask
|
|
|
440 |
$options['url'] = $this->_parseUrl($options);
|
|
|
441 |
|
|
|
442 |
$replacements = array();
|
|
|
443 |
|
|
|
444 |
foreach ($options as $k => $v) {
|
|
|
445 |
$replacements['{%'.$k.'}'] = $v;
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
return $replacements;
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
/**
|
|
|
452 |
* _parseUrl
|
|
|
453 |
*
|
|
|
454 |
* Parse the url mask of a given page and return the processed url
|
|
|
455 |
*
|
|
|
456 |
* @param $options Optional parameters to be applied in template and url mask
|
|
|
457 |
* @return string
|
|
|
458 |
*/
|
|
|
459 |
protected function _parseUrl($options = array())
|
|
|
460 |
{
|
|
|
461 |
$str = $this->_parseMaskReplacements($this->getUrlMask());
|
|
|
462 |
|
|
|
463 |
$replacements = array();
|
|
|
464 |
|
|
|
465 |
foreach ($options as $k => $v) {
|
|
|
466 |
$replacements['{%'.$k.'}'] = $v;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
return strtr($str, $replacements);
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* _parseMaskReplacements
|
|
|
474 |
*
|
|
|
475 |
* Parse the mask replacements, changing from to-be replaced mask with new masks/values
|
|
|
476 |
*
|
|
|
477 |
* @param $str String to have masks replaced
|
|
|
478 |
* @return string
|
|
|
479 |
*/
|
|
|
480 |
protected function _parseMaskReplacements($str)
|
|
|
481 |
{
|
|
|
482 |
$replacements = array();
|
|
|
483 |
|
|
|
484 |
foreach ($this->_maskReplacements as $k => $v) {
|
|
|
485 |
$replacements['{%'.$k.'}'] = ($v['asValue'] === true) ? $v['newMask'] : '{%'.$v['newMask'].'}';
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
return strtr($str, $replacements);
|
|
|
489 |
}
|
|
|
490 |
}
|