1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Brickrouge;
13
14 class Ranger extends Element
15 {
16 const T_START = '#ranger-start';
17 const T_LIMIT = '#ranger-limit';
18 const T_COUNT = '#ranger-count';
19 const T_WITH = '#ranger-with';
20 const T_EDITABLE = '#ranger-editable';
21 const T_NO_ARROWS = '#ranger-no-arrows';
22
23 public function __construct($type, $tags)
24 {
25 parent::__construct($type, $tags + array('class' => 'wdranger'));
26 }
27
28 protected function render_inner_html()
29 {
30 $start = max(1, $this[self::T_START]);
31 $limit = $this[self::T_LIMIT] ?: 10;
32 $count = $this[self::T_COUNT];
33
34 $start_final = $start;
35
36 if ($this[self::T_EDITABLE] && $count > $limit)
37 {
38 $start_final = (string) new Text
39 (
40 array
41 (
42 'name' => 'start',
43 'value' => $start,
44 'size' => 4,
45 'class' => 'measure'
46 )
47 );
48 }
49
50 $rc = t
51 (
52 'From :start to :finish on :max', array
53 (
54 ':start' => $start_final,
55 ':finish' => $start + $limit > $count ? $count : $start + $limit - 1,
56 ':max' => $count
57 ),
58
59 array('scope' => 'ranger.element')
60 );
61
62 if ($count > $limit && !$this[self::T_NO_ARROWS])
63 {
64 $url = $this->getURLBase();
65
66 $rc .= '<a href="' . $url . ($start - $limit < 1 ? $count - $limit + 1 + ($count % $limit ? $limit - ($count % $limit) : 0) : $start - $limit) . '" class="browse previous"><</a>';
67 $rc .= '<a href="' . $url . ($start + $limit >= $count ? 1 : $start + $limit) . '" class="browse next">></a>';
68 }
69
70 return $rc;
71 }
72
73 protected function getURLBase()
74 {
75 $with = $this[self::T_WITH];
76
77 if ($with)
78 {
79 if (is_string($with))
80 {
81 $parts = explode(',', $with);
82 $parts = array_map('trim', $parts);
83 $parts = array_flip($parts);
84
85 foreach ($parts as $name => &$part)
86 {
87 $part = isset($_REQUEST[$name]) ? $_REQUEST[$name] : null;
88 }
89 }
90 else
91 {
92 $parts = (array) $with;
93 }
94 }
95 else
96 {
97 $parts = array();
98 }
99
100
101
102
103
104 $using = 'start';
105
106 unset($parts[$using]);
107
108 $parts['limit'] = $this[self::T_LIMIT] ?: 10;
109 $parts[$using] = '';
110
111
112
113
114
115 $rc = '';
116
117 $rc .= '?' . http_build_query
118 (
119 $parts, '', '&'
120 );
121
122 return $rc;
123 }
124 }