![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 /*------------------------------------------------------------------------------- 00003 * Xataface Web Application Framework 00004 * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *------------------------------------------------------------------------------- 00020 */ 00021 00022 /****************************************************************************************************************** 00023 * File: Dataface/RecordGrid.php 00024 * Author: Steve Hannah <shannah@sfu.ca> 00025 * Created: December 4, 2005 00026 * 00027 * Description: 00028 * ------------- 00029 * Displays a database result or array of records as HTML. 00030 * 00031 * Usage: 00032 * ------ 00033 * <code> 00034 * // Simple scenario. Load an array of Dataface_Record objects and display them 00035 * // in a grid. This scenario displays all fields of each record. 00036 * $records =& getRecords(); // get an array of DatafaceRecord objects. 00037 * $grid = new Dataface_RecordGrid($records); 00038 * echo $grid->toHTML(); 00039 * 00040 * // More complex scenario: Same as simple scenario, except we only display certain 00041 * // fields of the records. 00042 * $records =& getRecords(); 00043 * $grid = new Dataface_RecordGrid($records, array('id','fname','lname')); 00044 * echo $grid->toHTML(); // displays 3 column table. 00045 * 00046 * // Using grid with normal associative arrays 00047 * $res = mysql_query("SELECT * FROM Students"); 00048 * $records = array(); 00049 * while ( $row = mysql_fetch_array($res) ){ 00050 * $record = array(); 00051 * foreach ( $row as $key=>$value){ 00052 * if ( is_int($key) ) continue; 00053 * // discard all of the numeric keys 00054 * $record[$key] = $value; 00055 * } 00056 * $records[] = $record; 00057 * } 00058 * $grid = new Dataface_RecordGrid($records); 00059 * echo $grid->toHTML(); 00060 * </code> 00061 ********************************************************************************************************************/ 00062 require_once 'Dataface/Record.php'; 00063 define('RecordGrid_ActionLabel', '____actions____'); 00064 00065 class Dataface_RecordGrid { 00066 var $records; 00067 var $columns; 00068 var $labels; 00069 var $id="sortable"; 00070 var $cssclass = ""; 00071 var $actionCellCallbacks = array(); 00072 var $cellFilters = array(); 00073 00074 function Dataface_RecordGrid(&$records, $columns=null, $labels=null){ 00075 $this->records =& $records; 00076 if ( !is_array($this->records) ){ 00077 throw new Exception('In Dataface_RecordGrid the first parameter is expected to be an array but received "'.get_class($records).'"', E_USER_ERROR); 00078 } 00079 00080 $this->columns = $columns; 00081 $this->labels = $labels; 00082 } 00083 00084 function addActionCellCallback($callback){ 00085 $this->actionCellCallbacks[] = $callback; 00086 } 00087 00088 function toHTML(){ 00089 import('Dataface/SkinTool.php'); 00090 $recKeys = array_keys($this->records); 00091 $sampleRecord =& $this->records[$recKeys[0]]; 00092 if ( $this->columns === null ){ 00093 $columns = array(); 00094 if ( is_a($sampleRecord, 'Dataface_Record') ){ 00095 $columns = array_keys($sampleRecord->_table->fields(false,true)); 00096 } else if ( is_a($sampleRecord, 'Dataface_RelatedRecord') ){ 00097 $columns = $sampleRecord->_relationship->_schema['short_columns']; 00098 } else { 00099 $columns = array_keys($sampleRecord); 00100 } 00101 } else { 00102 $columns =& $this->columns; 00103 } 00104 if ( count($this->actionCellCallbacks) > 0 ){ 00105 $hasCallbacks = true; 00106 array_unshift($columns, RecordGrid_ActionLabel); 00107 } else { 00108 $hasCallbacks = false; 00109 } 00110 //print_r($columns); 00111 00112 $gridContent = array(); 00113 foreach ($this->records as $record){ 00114 if ( $hasCallbacks ) $row[RecordGrid_ActionLabel] = ''; 00115 if ( is_a($record, 'Dataface_Record') or is_a($record, 'Dataface_RelatedRecord') ){ 00116 $row = array(); 00117 foreach ( $columns as $column){ 00118 if ( $column == RecordGrid_ActionLabel ) continue; 00119 $row[$column] = $record->printValue($column); 00120 if ( isset($this->cellFilters[$column]) ){ 00121 $row[$column] = call_user_func($this->cellFilters[$column], $record, $column, $row[$column]); 00122 00123 } 00124 } 00125 if ( $hasCallbacks ){ 00126 $cbout = array(); 00127 foreach ( $this->actionCellCallbacks as $cb ){ 00128 $cbout[] = call_user_func($cb, $row); 00129 } 00130 $row[RecordGrid_ActionLabel] =implode('', $cbout); 00131 } 00132 $gridContent[] =& $row; 00133 unset($row); 00134 } else if ( is_array($record) ){ 00135 $row = array(); 00136 foreach ( $columns as $column){ 00137 if ( $column == RecordGrid_ActionLabel ) continue; 00138 $row[$column] = @$record[$column]; 00139 if ( isset($this->cellFilters[$column]) ){ 00140 $row[$column] = call_user_func($this->cellFilters[$column], $row, $column, $row[$column]); 00141 } 00142 } 00143 if ( $hasCallbacks ){ 00144 $cbout = array(); 00145 foreach ( $this->actionCellCallbacks as $cb ){ 00146 $cbout[] = call_user_func($cb, $row); 00147 } 00148 $row[RecordGrid_ActionLabel] = implode('', $cbout); 00149 } 00150 00151 $gridContent[] =& $row; 00152 unset($row); 00153 } 00154 } 00155 00156 00157 if ( $this->labels === null ){ 00158 $this->labels = array(); 00159 foreach ($columns as $column){ 00160 if ( $column == RecordGrid_ActionLabel ){ 00161 $labels[$column] = ''; 00162 continue; 00163 } 00164 if ( is_a( $sampleRecord, 'Dataface_Record') ){ 00165 $field =& $sampleRecord->_table->getField($column); 00166 $labels[$column] = $field['widget']['label']; 00167 } else if ( is_a($sampleRecord, 'Dataface_RelatedRecord') ){ 00168 $table =& $sampleRecord->_relationship->getTable($column); 00169 $field =& $table->getField($column); 00170 $labels[$column] = $field['widget']['label']; 00171 } else { 00172 $labels[$column] = ucwords(str_replace('_',' ',$column)); 00173 } 00174 unset($field); 00175 unset($table); 00176 } 00177 00178 00179 } else { 00180 $labels =& $this->labels; 00181 } 00182 00183 00184 00185 $context = array( 'data'=> &$gridContent, 'labels'=>&$labels, 'columns'=>&$columns, 'id'=>$this->id, 'class'=>$this->cssclass); 00186 $skinTool =& Dataface_SkinTool::getInstance(); 00187 ob_start(); 00188 $skinTool->display($context, 'Dataface_RecordGrid.html'); 00189 $out = ob_get_contents(); 00190 ob_end_clean(); 00191 return $out; 00192 } 00193 }