Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
RecordGrid.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 
22 /******************************************************************************************************************
23  * File: Dataface/RecordGrid.php
24  * Author: Steve Hannah <shannah@sfu.ca>
25  * Created: December 4, 2005
26  *
27  * Description:
28  * -------------
29  * Displays a database result or array of records as HTML.
30  *
31  * Usage:
32  * ------
33  * <code>
34  * // Simple scenario. Load an array of Dataface_Record objects and display them
35  * // in a grid. This scenario displays all fields of each record.
36  * $records =& getRecords(); // get an array of DatafaceRecord objects.
37  * $grid = new Dataface_RecordGrid($records);
38  * echo $grid->toHTML();
39  *
40  * // More complex scenario: Same as simple scenario, except we only display certain
41  * // fields of the records.
42  * $records =& getRecords();
43  * $grid = new Dataface_RecordGrid($records, array('id','fname','lname'));
44  * echo $grid->toHTML(); // displays 3 column table.
45  *
46  * // Using grid with normal associative arrays
47  * $res = mysql_query("SELECT * FROM Students");
48  * $records = array();
49  * while ( $row = mysql_fetch_array($res) ){
50  * $record = array();
51  * foreach ( $row as $key=>$value){
52  * if ( is_int($key) ) continue;
53  * // discard all of the numeric keys
54  * $record[$key] = $value;
55  * }
56  * $records[] = $record;
57  * }
58  * $grid = new Dataface_RecordGrid($records);
59  * echo $grid->toHTML();
60  * </code>
61  ********************************************************************************************************************/
62 require_once 'Dataface/Record.php';
63 define('RecordGrid_ActionLabel', '____actions____');
64 
66  var $records;
67  var $columns;
68  var $labels;
69  var $id="sortable";
70  var $cssclass = "";
71  var $actionCellCallbacks = array();
72  var $cellFilters = array();
73 
74  function Dataface_RecordGrid(&$records, $columns=null, $labels=null){
75  $this->records =& $records;
76  if ( !is_array($this->records) ){
77  throw new Exception('In Dataface_RecordGrid the first parameter is expected to be an array but received "'.get_class($records).'"', E_USER_ERROR);
78  }
79 
80  $this->columns = $columns;
81  $this->labels = $labels;
82  }
83 
84  function addActionCellCallback($callback){
85  $this->actionCellCallbacks[] = $callback;
86  }
87 
88  function toHTML(){
89  import('Dataface/SkinTool.php');
90  $recKeys = array_keys($this->records);
91  $sampleRecord =& $this->records[$recKeys[0]];
92  if ( $this->columns === null ){
93  $columns = array();
94  if ( is_a($sampleRecord, 'Dataface_Record') ){
95  $columns = array_keys($sampleRecord->_table->fields(false,true));
96  } else if ( is_a($sampleRecord, 'Dataface_RelatedRecord') ){
97  $columns = $sampleRecord->_relationship->_schema['short_columns'];
98  } else {
99  $columns = array_keys($sampleRecord);
100  }
101  } else {
103  }
104  if ( count($this->actionCellCallbacks) > 0 ){
105  $hasCallbacks = true;
106  array_unshift($columns, RecordGrid_ActionLabel);
107  } else {
108  $hasCallbacks = false;
109  }
110  //print_r($columns);
111 
112  $gridContent = array();
113  foreach ($this->records as $record){
114  if ( $hasCallbacks ) $row[RecordGrid_ActionLabel] = '';
115  if ( is_a($record, 'Dataface_Record') or is_a($record, 'Dataface_RelatedRecord') ){
116  $row = array();
117  foreach ( $columns as $column){
118  if ( $column == RecordGrid_ActionLabel ) continue;
119  $row[$column] = $record->printValue($column);
120  if ( isset($this->cellFilters[$column]) ){
121  $row[$column] = call_user_func($this->cellFilters[$column], $record, $column, $row[$column]);
122 
123  }
124  }
125  if ( $hasCallbacks ){
126  $cbout = array();
127  foreach ( $this->actionCellCallbacks as $cb ){
128  $cbout[] = call_user_func($cb, $row);
129  }
130  $row[RecordGrid_ActionLabel] =implode('', $cbout);
131  }
132  $gridContent[] =& $row;
133  unset($row);
134  } else if ( is_array($record) ){
135  $row = array();
136  foreach ( $columns as $column){
137  if ( $column == RecordGrid_ActionLabel ) continue;
138  $row[$column] = @$record[$column];
139  if ( isset($this->cellFilters[$column]) ){
140  $row[$column] = call_user_func($this->cellFilters[$column], $row, $column, $row[$column]);
141  }
142  }
143  if ( $hasCallbacks ){
144  $cbout = array();
145  foreach ( $this->actionCellCallbacks as $cb ){
146  $cbout[] = call_user_func($cb, $row);
147  }
148  $row[RecordGrid_ActionLabel] = implode('', $cbout);
149  }
150 
151  $gridContent[] =& $row;
152  unset($row);
153  }
154  }
155 
156 
157  if ( $this->labels === null ){
158  $this->labels = array();
159  foreach ($columns as $column){
160  if ( $column == RecordGrid_ActionLabel ){
161  $labels[$column] = '';
162  continue;
163  }
164  if ( is_a( $sampleRecord, 'Dataface_Record') ){
165  $field =& $sampleRecord->_table->getField($column);
166  $labels[$column] = $field['widget']['label'];
167  } else if ( is_a($sampleRecord, 'Dataface_RelatedRecord') ){
168  $table =& $sampleRecord->_relationship->getTable($column);
169  $field =& $table->getField($column);
170  $labels[$column] = $field['widget']['label'];
171  } else {
172  $labels[$column] = ucwords(str_replace('_',' ',$column));
173  }
174  unset($field);
175  unset($table);
176  }
177 
178 
179  } else {
181  }
182 
183 
184 
185  $context = array( 'data'=> &$gridContent, 'labels'=>&$labels, 'columns'=>&$columns, 'id'=>$this->id, 'class'=>$this->cssclass);
186  $skinTool =& Dataface_SkinTool::getInstance();
187  ob_start();
188  $skinTool->display($context, 'Dataface_RecordGrid.html');
189  $out = ob_get_contents();
190  ob_end_clean();
191  return $out;
192  }
193 }