Framework  3.9
paged_list.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
43 class PagedList implements FacetFilterable
44 {
45  var $items;
46  var $id;
47  var $format;
48  var $pageSize = 10;
49  var $listTag = "ul";
50  var $CSSclass = null;
51  var $styles = null;
52  var $emptyList = "No items in the list";
53  var $paginate = true;
54  var $writeIdTag = false;
57  var $displayMode = "list-item";
58 
65  function PagedList($items, $id = "paged_list", $format = null)
66  {
67  $this->items = $items;
68  $this->id = $id;
69  $this->format = $format;
70  $this->tagRowCallbacks = array();
71  }
72 
73  function getID()
74  {
75  return $this->id;
76  }
77 
82  function addFacetTaggingHandler($handler)
83  {
84  $this->tagRowCallbacks[] = $handler;
85  }
86 
87  function writeScript()
88  {
89  if(!$this->paginate && count($this->tagRowCallbacks) == 0) return "";
90 
91  ob_start();
92 ?>
93  <script type="text/javascript">
94  window.addEvent('domready', function()
95  {
96  var <?echo $this->id?> = new PaginatingList('<?echo $this->id ?>', '<?echo $this->id?>_paginator', {per_page: <?echo $this->pageSize ?>, display_mode: '<?echo $this->displayMode?>'});
97  });
98  </script>
99 <?
100  $script = ob_get_contents();
101  ob_end_clean();
102  return $script;
103  }
104 
105  function drawList()
106  {
107  if (!count($this->items))
108  {
109  echo "<p>{$this->emptyList}</p>";
110  return;
111  }
112 
113  $attrs = "";
114  if ($this->CSSclass) $attrs .= " class='{$this->CSSclass}'";
115  if ($this->styles) $attrs .= " style='{$this->styles}'";
116  if($this->paginate)
117  {
118 ?>
119  <div style="float: right;" class="paginator">
120  <ul id="<?echo $this->id?>_paginator"></ul>
121  </div>
122  <?php
123  }
124  ?>
125  <<?echo $this->listTag?> id="<?echo $this->id?>"<?echo $attrs?>>
126 <?
127  foreach($this->items as $item)
128  {
129  $css = "";
130 
131 
132  if ($this->onStartRow)
133  {
134  $css = call_user_func($this->onStartRow, $item);
135  if ($css === false) continue; // Skip this record if false returned
136  }
137 
138  if ($css) $css = " class='$css'";
139 
140  if($this->writeIdTag)
141  {
142  $idTag = "id='".$this->formatIdTag($item)."'";
143  }
144 
145  $dataAttrs = array();
146  foreach($this->tagRowCallbacks as $cb)
147  {
148  $dataAttrs = call_user_func($cb, $item, $dataAttrs);
149  }
150 
151  $rowAttrs = "";
152  foreach($dataAttrs as $name => $value)
153  {
154  $rowAttrs .= " ".$name."='".$value."'";
155  }
156 
157  echo "<li {$idTag}{$css}{$rowAttrs}>";
158 
159  if (is_callable($this->format))
160  {
161  echo call_user_func($this->format, $item);
162  }
163  else echo $item->format($this->format);
164 
165  echo "</li>\n";
166  }
167  echo "</{$this->listTag}>\n";
168  }
169 
170  function formatIdTag($item)
171  {
172  $pk = $item->getPrimaryKey();
173  return "{$pk}_{$item->get($pk)}";
174  }
175 
182  function drawListToString()
183  {
184  ob_start();
185  $this->drawList();
186  $out = ob_get_contents();
187  ob_end_clean();
188  return $out;
189  }
190 }
191 
201 {
202  var $items;
203  var $id;
204  var $format;
205  var $pageSize = 10;
206  var $listTag = "ul";
207  var $CSSclass = null;
208  var $styles = null;
209  var $emptyList = "No items in the list";
210  var $paginate = true;
211  var $page = -1;
212  var $writeIdTag = false;
213 
214  function __construct($items, $id = "paged_list", $format = null)
215  {
216  $this->items = $items;
217  $this->id = $id;
218  $this->format = $format;
219  }
220 
221 
222  function writeScript()
223  {
224  }
225 
226  function drawList()
227  {
228  if (!count($this->items))
229  {
230  echo "<p>{$this->emptyList}</p>";
231  return;
232  }
233 
234  if ($this->page == -1)
235  {
236  $this->page = checkNumeric($_GET["page"]);
237  if (!$this->page) $this->page = 1;
238  }
239 
240  if ($this->paginate)
241  {
242  $start = ($this->page - 1) * $this->pageSize;
243  if ($start < 0) $start = 0;
244 
245  $end = $start + $this->pageSize;
246  if ($end > count($this->items)) $end = count($this->items);
247  }
248  else
249  {
250  $start = 0;
251  $end = count($this->items);
252  }
253 
254  $attrs = "";
255  if ($this->CSSclass) $attrs .= " class='{$this->CSSclass}'";
256  if ($this->styles) $attrs .= " style='{$this->styles}'";
257 
258  if($this->paginate)
259  {
260 
261  $prev = $this->page - 1;
262  if ($prev < 1) $prev = 1;
263  $next = $this->page + 1;
264  $total = intval((count($this->items) - 1) / $this->pageSize + 1);
265  if ($next > $total) $next = $total;
266  ?>
267  <div style="float: right;" class="paginator">
268  <ul id="<?echo $this->id?>_paginator">
269  <li><a href="?page=<?echo $prev?>" class="paginate"><span>&lt;&lt; Prev</span></a></li>
270  <li class="pager"><a href="#" class="goto-page">Page <?echo $this->page?> of <?echo $total?></a></li>
271  <li><a href="?page=<?echo $next?>" class="paginate"><span>Next &gt;&gt;</span></a></li>
272  </ul>
273  </div>
274  <?php
275  }
276  ?>
277  <<?echo $this->listTag?> id="<?echo $this->id?>"<?echo $attrs?>>
278 <?
279  for($i = $start; $i < $end; ++$i)
280  {
281  $item = $this->items[$i];
282 
283  if($this->writeIdTag)
284  {
285  $idTag = " id='".$this->formatIdTag($item)."'";
286  }
287 
288  echo "<li{$idTag}>";
289 
290  if (is_callable($this->format))
291  {
292  echo call_user_func($this->format, $item);
293  }
294  else echo $item->format($this->format);
295 
296  echo "</li>\n";
297  }
298  echo "</{$this->listTag}>\n";
299  }
300 
301 
302  function formatIdTag($item)
303  {
304  $pk = $item->getPrimaryKey();
305  return "{$pk}_{$item->get($pk)}";
306  }
307 }
308 ?>
The PagedList class provides a simple User Interface control to allow pagination of HTML lists of Dat...
Definition: paged_list.inc:44
ServerPagedList provides the same interface and functionality as PagedList, with the difference that ...
Definition: paged_list.inc:201
$styles
Specific CSS styles to be applied to the list.
Definition: paged_list.inc:208
$paginate
Specifies whether list should be paginated.
Definition: paged_list.inc:210
__construct($items, $id="paged_list", $format=null)
Definition: paged_list.inc:214
$tagRowCallbacks
Array of callbacks for adding extra attributes to each row.
Definition: paged_list.inc:55
$writeIdTag
whether to include an id tag for each li item
Definition: paged_list.inc:54
$writeIdTag
whether to include an id tag for each li item
Definition: paged_list.inc:212
$format
The formatter used to display the list items (either a format string or a callback function or method...
Definition: paged_list.inc:47
$pageSize
The number of items per page.
Definition: paged_list.inc:205
$items
The items to be displayed in the list.
Definition: paged_list.inc:45
$emptyList
Message to display when the list is empty.
Definition: paged_list.inc:209
$listTag
The HTML list tag type.
Definition: paged_list.inc:49
PagedList($items, $id="paged_list", $format=null)
Creates a new PagedList control.
Definition: paged_list.inc:65
$CSSclass
CSS class(es) to be applied to the list.
Definition: paged_list.inc:207
addFacetTaggingHandler($handler)
Adds a row tagging handler.
Definition: paged_list.inc:82
$emptyList
Message to display when the list is empty.
Definition: paged_list.inc:52
$CSSclass
CSS class(es) to be applied to the list.
Definition: paged_list.inc:50
formatIdTag($item)
Definition: paged_list.inc:302
$paginate
Specifies whether list should be paginated (false for view only pages)
Definition: paged_list.inc:53
drawListToString()
Helpful utility function - generates the list HTML in an output buffer and returns the string represe...
Definition: paged_list.inc:182
$pageSize
The number of items per page.
Definition: paged_list.inc:48
formatIdTag($item)
Definition: paged_list.inc:170
$listTag
The HTML list tag type.
Definition: paged_list.inc:206
$onStartRow
Callback for setting the CSS class / visibility of each item in the list.
Definition: paged_list.inc:56
$styles
Specific CSS styles to be applied to the list.
Definition: paged_list.inc:51
$id
The HTML id of the list.
Definition: paged_list.inc:46
$displayMode
CSS display type for visible items.
Definition: paged_list.inc:57
checkNumeric($p)
Security helper function.
Definition: functions.inc:630