Framework  3.9
grouped_list.inc
Go to the documentation of this file.
1 <?php
5 /**************************************************************
6 
7  Copyright (c) 2007-2010 Sonjara, Inc
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  Except as contained in this notice, the name(s) of the above
22  copyright holders shall not be used in advertising or otherwise
23  to promote the sale, use or other dealings in this Software
24  without prior written authorization.
25 
26  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  OTHER DEALINGS IN THE SOFTWARE.
34 
35 *****************************************************************/
36 
37 require_once realpath(dirname(__FILE__))."/grouped_data_view.inc";
38 
39 /*
40  * Similar to PagedList class but provides a one-level
41  * expandable/collapsable style list, given an array of items
42  * that are grouped by a field.
43  *
44  * For example, projects grouped by type. Outputs the project
45  * type as a heading followed by all projects that are categorized
46  * as that type. The project type heading can be expanded/collapsed.
47  *
48  * Can be used for Blogs and other formatted list displays.
49  */
50 
61 {
62  function GroupListGroup($parent, $title, $key, $expanded = false, $fixed = false)
63  {
64  parent::DataGroup($parent, $title, $key, $expanded, $fixed);
65  }
66 
67  function draw()
68  {
69  $attrs = "";
70  if ($this->cssClass) $attrs .= " class='{$this->cssClass}'";
71  if ($this->groupStyles) $attrs .= " style='{$this->groupStyles}'";
72 
73  $cssClass = ($this->fixed || $this->parent->mode == 'fixed') ? "fixed" : ($this->expanded) ? "expanded" : "collapsed";
74 
75  echo "<h2 id=\"{$this->parent->group_id}[{$this->key}]\" $attrs>\n";
76  // Output the section title
77  echo "{$this->title}\n";
78  echo "</h2>\n";
79  }
80 }
81 
82 
83 class GroupedList implements FacetFilterable
84 {
85  var $items;
86  var $id;
87  var $format;
88  var $mode;
89  var $groups;
90  var $cssClass = null;
91  var $styles = null;
92  var $groupStyles = "";
93  var $groupClass = "subheading";
95  var $group_id;
97 
98  function GroupedList($items, $id = "grouped_list", $format, $cssClass = "article_list", $groupClass = "subheading")
99  {
100  $this->items = $items;
101  $this->id = $id;
102  $this->format = $format;
103  $this->cssClass = $cssClass;
104  $this->groupClass = $groupClass;
105  $this->cssStyle = null;
106  $this->mode = "accordian";
107  $this->tagRowCallbacks = array();
108  }
109 
117  function group($title, $key, $expanded = false, $fixed = false)
118  {
119  $this->groups[] = new GroupedListGroup($this, $title, $key, $expanded);
120  return $this;
121  }
122 
129  function groupBy($groups, $format = null, $field = "")
130  {
131  if (count($groups) == 0) return;
132  if (!$field) $field = $groups[0]->getPrimaryKey();
133 
134  $this->group_id = $field;
135 
136  foreach($groups as $group)
137  {
138  $this->group($this->format($group, $format), $group->$field);
139  }
140  }
141 
142  function getID()
143  {
144  return $this->id;
145  }
146 
151  function addFacetTaggingHandler($handler)
152  {
153  $this->tagRowCallbacks[] = $handler;
154  }
155 
164  function format($group, $template)
165  {
166  if(!is_callable($template))
167  return $group->format($template);
168  else
169  return call_user_func($template, $group);
170  }
171 
172 
173  function writeScript()
174  {
175  if(count($this->items) == 0) return;
176 
177  $script = "<script type='text/javascript' src='/fakoli/js/grouping_list.js'></script>\n";
178 
179  $constructor .= "\n\t\tvar {$this->id} = new GroupingList('{$this->id}', {mode: '{$this->mode}'});";
180 
181  if ($constructor)
182  {
183  $script .= "\t<script type='text/javascript'>\n\twindow.addEvent('domready', function()\n\t{";
184  $script .= $constructor;
185  $script .= "\n\t});\n\t</script>\n";
186  }
187 
188  return $script;
189  }
190 
191  function drawList()
192  {
193  $attrs = "";
194  if ($this->cssClass) $attrs .= " class='{$this->cssClass}'";
195  if ($this->styles) $attrs .= " style='{$this->styles}'";
196 
197  echo "<div id='{$this->id}' class='{$this->groupedListClass}'>\n";
198 
199  if(count($this->groups) == 0)
200  {
201  foreach(array_keys($this->items) as $group)
202  {
203  $this->group($group, $group);
204  }
205  }
206 
207  if(count($this->groups) == 0) return;
208 
209  foreach($this->groups as $group)
210  {
211  if (array_key_exists($group->key, $this->items))
212  {
213  echo "<div id='content_{$group->key}'>\n";
214 
215  $group->draw();
216 
217  $itemsInGroup = $this->items[$group->key];
218 
219  echo "<ul id=\"subheading[{$group->key}]\" class=\"{$this->groupClass}\" $attrs>\n";
220 
221  foreach($itemsInGroup as $item)
222  {
223  $css = "";
224 
225  if ($this->onStartRow)
226  {
227  $css = call_user_func($this->onStartRow, $item);
228  if ($css === false) continue; // Skip this record if false returned
229  }
230 
231  if ($css) $css = " class='$css'";
232 
233  $dataAttrs = array();
234  foreach($this->tagRowCallbacks as $cb)
235  {
236  $dataAttrs = call_user_func($cb, $item, $dataAttrs);
237  }
238 
239  $rowAttrs = "";
240  foreach($dataAttrs as $name => $value)
241  {
242  $rowAttrs .= " ".$name."='".$value."'";
243  }
244 
245  echo "<li {$css}{$rowAttrs}>";
246 
247  if (is_callable($this->format))
248  {
249  echo call_user_func($this->format, $item);
250  }
251  else echo $item->format($this->format);
252  echo "</li>\n";
253  }
254 
255  echo "</ul>\n";
256  echo "</div>\n";
257  }
258  } // end for each groups as group
259 
260  echo "</div>\n";
261  }
262 
269  function drawListToString()
270  {
271  ob_start();
272  $this->drawList();
273  $out = ob_get_contents();
274  ob_end_clean();
275  return $out;
276  }
277 
278 }
Represents a grouped collection of data within a table.
$fixed
Specifies whether this group should be fixed (i.e. exempt from expand/collapse toggling).
$key
The key value for this data group.
$title
The text to display in the title for this data group.
$expanded
Boolean indicating whether this group should initially be expanded.
$parent
The parent GroupedDataListView object.
Represents a grouped collection of data within a table.
draw()
Draws the data group subheading within the table.
GroupListGroup($parent, $title, $key, $expanded=false, $fixed=false)
group($title, $key, $expanded=false, $fixed=false)
Adds a group to the view.
$groupedListClass
The css class for the entire grouped list div.
GroupedList($items, $id="grouped_list", $format, $cssClass="article_list", $groupClass="subheading")
$groupClass
Class for the ul that links the li list.
drawListToString()
Helpful utility function - generates the list HTML in an output buffer and returns the string represe...
addFacetTaggingHandler($handler)
Adds a row tagging handler.
$groupStyles
Styles for the group headings.
format($group, $template)
Outputs the text for the groupedBy title bar using the given group dataitem object.
$groups
The group definitions.
$tagRowCallbacks
Array of callbacks for adding extra attributes to each row.
groupBy($groups, $format=null, $field="")
Add groups based on the supplied list of DataItems.
$mode
The display mode for the table: fixed, tree, or accordian.