Xataface Summary Module 0.1
Summary Reports for Xataface Apps
/Applications/XAMPP/xamppfiles/htdocs/researchdb/modules/summary/summary.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Summary Module
00004  * 
00005  * Copyright (C) 2011  Steve Hannah <shannah@sfu.ca>
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00020  * Boston, MA  02110-1301, USA.
00021  */
00022  
00034 class modules_summary {
00035 
00039         private $cache = array();
00040         
00044         private $baseURL=null;
00045 
00046         public function __construct(){
00047         
00048                 
00049         }
00050         
00051         
00061         public function decorateSummaryField($key, &$field){
00062                 
00063                 if ( !isset($field['widget']) ) $field['widget'] = array();
00064                 if ( !isset($field['widget']['label']) ){
00065                         if ( isset($field['widget:label']) ){
00066                                 $field['widget']['label'] = $field['widget:label'];
00067                         } else {
00068                                 $field['widget']['label'] = $field['widget:label'];
00069                         }//$key;
00070                 }
00071         }
00072         
00073         
00081         public function getBaseURL(){
00082                 if ( !isset($this->baseURL) ){
00083                         $this->baseURL = Dataface_ModuleTool::getInstance()->getModuleURL(__FILE__);
00084                 }
00085                 return $this->baseURL;
00086         }
00087         
00104         public function &getSummaryFields(Dataface_Table $table){
00105                 $tablename = $table->tablename;
00106                 
00107                 if ( !isset($this->cache[$tablename]) ){
00108                         $this->cache[$tablename] = array();
00109                 }
00110                 
00111                 $cache =& $this->cache[$tablename];
00112                 if ( !isset($cache['summary_fields']) ){
00113                         $cache['summary_fields'] = array();
00114                         $summary_fields =& $cache['summary_fields'];
00115                         
00116                         foreach ( $table->attributes() as $fieldname=>$field ){
00117                                 if ( !is_array($field) ) continue;
00118                                 if ( @$field['summary'] ){
00119                                         $summary_fields[$fieldname] = $field;
00120                                         $this->decorateSummaryField($fieldname, $summary_fields[$fieldname]);
00121                                 }
00122                         }
00123                 } else {
00124                         $summary_fields =& $cache['summary_fields'];
00125                 }
00126                 return $summary_fields;
00127         
00128         }
00129         
00130         
00139         public function getSummaryField(Dataface_Table $table, $fieldname){
00140                 $fields =& $this->getSummaryFields($table);
00141                 if ( !isset($fields[$fieldname]) ){
00142                         return null;
00143                 }
00144                 
00145                 return $fields[$fieldname];
00146         }
00147         
00148         
00160         public function getSummarySQL(Dataface_Table $table, $query, $groupBy=null, $summaryFields=null, $graftedSummaryFields=null){
00161                 
00162                 $qb = new Dataface_QueryBuilder($table->tablename, $query);
00163                 
00164                 if ( !$groupBy ){
00165                         $groupBy = '';
00166                 }
00167                 
00168                 if ( !isset($summaryFields) ){
00169                         $summaryFields = array_keys($this->getSummaryFields($table));
00170                 }
00171                 
00172                 $sql = $qb->select($groupBy, $query);
00173                 if ( $groupBy ){
00174                         $gbc = ' group by `'.implode('`,`', $groupBy).'`';
00175                         if ( preg_match('/( LIMIT [\s\S]*)$/i', $sql, $matches) ){
00176                                 $sql = preg_replace('/( LIMIT [\s\S]*)$/i', $gbc.'$1', $sql);
00177                         } else {
00178                                 $sql .= $gbc;
00179                         }
00180                 } else {
00181                         $sql = preg_replace('/select (.*?) from /i', 'select  from ', $sql);
00182                 }
00183                 //echo $sql;
00184                 if ( $summaryFields){
00185                         $pos = stripos($sql, ' from ');
00186                         $sql1 = substr($sql, 0, $pos);
00187                         $sql2 = substr($sql, $pos);
00188                         
00189                         $summaryStr = array();
00190                         foreach ($summaryFields as $fname){
00191                                 $field = $this->getSummaryField($table, $fname);
00192                                 if ( !$field  and is_array($graftedSummaryFields) ){
00193                                         $field = @$graftedSummaryFields[$fname];
00194                                 }
00195                                 if ( !$field ) continue;
00196                                 if ( !@$field['formula'] ) continue;
00197                                 
00198                                 $summaryStr[] = $field['formula'].' as `'.$fname.'`';
00199                         }
00200                         $summaryStr = implode(', ', $summaryStr);
00201                         $sql = $sql1.($groupBy?',':'').' '.$summaryStr.' '.$sql2;
00202                 }
00203                 return $sql;
00204         
00205         }
00206         
00207         
00208         
00209         
00210 }
 All Data Structures Files Functions