Xataface 2.0
Xataface Application Framework
Dataface/Utilities.php
Go to the documentation of this file.
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 
00026 class Dataface_Utilities {
00027         
00038         public static function groupBy($fieldname, $records, $order=array(), $titles=array()){
00039                 import( 'PEAR.php');
00040                 if (!is_array($records) ){
00041                         return PEAR::raiseError("In Dataface_Utilities::groupBy() expected 2nd parameter to be an array, but received "+$records);
00042                 }
00043                 
00044                 
00045                 $out = array();
00046                 $unordered = array();
00047                 $ordered = array();
00048                 
00049                 foreach ($order as $orderKey){
00050                         $ordered[$orderKey] = array();
00051                 }
00052                 
00053                 
00054                 foreach (array_keys($records) as $i){
00055                         if ( is_a($records[$i], 'Dataface_Record') || is_a($records[$i], 'Dataface_RelatedRecord') ){
00056                                 $key = $records[$i]->qq($fieldname);
00057                                 
00058                         } else if (is_array($records[$i]) ) {
00059                                 $key = $records[$i][$fieldname];
00060                         } else {
00061                                 return PEAR::raiseError("In Dataface_Utilities::groupBy() each of the elements in the list of records should be either an array or a Dataface_Record object (or a Dataface_RelatedRecord object), but received: ".$records[$i]);
00062                         }
00063                         if ( !$key ){
00064                                 continue;
00065                         }
00066                         if ( isset( $ordered[$key] ) ) $ordered[$key][] =& $records[$i];
00067                         else {
00068                                 if ( !isset($unordered[$key]) ){
00069                                         $unordered[$key] = array();
00070                                 }
00071                         
00072                                 $unordered[$key][] =& $records[$i];
00073                         }
00074                 }
00075                 $out = array_merge($ordered, $unordered);
00076                 $out2 = array();
00077                 foreach (array_keys($out) as $key){
00078                         if ( isset($titles[$key]) ){
00079                                 $out2[$titles[$key]] =& $out[$key];
00080                         } else {
00081                                 $out2[$key] =& $out[$key];
00082                         }
00083                 }
00084                 
00085                 return $out2;
00086         
00087         }
00088         
00089         
00117         public static function query2html($query, $keyFilter=array()){
00118                 foreach ( $keyFilter as $bad ){
00119                         if ( isset($query[$bad]) ) unset($query[$bad]);
00120                 }
00121                 $qt = array();
00122                 //call_user_func(array(__CLASS__, 'flattenQuery'), $query, $qt);
00123                 Dataface_Utilities::flattenQuery($query, $qt);
00124                 
00125                 ob_start();
00126                 foreach ($qt as $key=>$value){
00127                         echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />\n";
00128                 }       
00129                 $out = ob_get_contents();
00130                 ob_end_clean();
00131                 return $out;
00132         }
00133         
00167         public static function flattenQuery($in,&$out, $path=array()){
00168                 $origPath = $path;
00169                 if ( !empty($path) ){
00170                         $prefix = array_shift($path);
00171                         if ( !empty($path) ){
00172                                 $prefix .= '['.implode('][', $path).']';
00173                         }
00174                 } else {
00175                         $prefix = '';
00176                 }
00177                 $hasprefix = !empty($prefix);
00178                 foreach ($in as $key=>$value){
00179                         //if ( substr($key,0,2) == '--' ) continue;
00180                         if ( is_array($value) ){
00181                                 $origPath[] = $key;
00182                                 Dataface_Utilities::flattenQuery($value, $out, $origPath);
00183                         } else {
00184                                 if ( $hasprefix ){
00185                                         $out[$prefix.'['.$key.']'] = $value;
00186                                 } else {
00187                                         $out[$key] = $value;
00188                                 }
00189                         }
00190                 }
00191         }
00192         
00193         
00202         public static function fireEvent($name, $params=array()){
00203                 $app =& Dataface_Application::getInstance();
00204                 $query =& $app->getQuery();
00205                 if ( isset($query['-table']) ){
00206                         $table =& Dataface_Table::loadTable($query['-table']);
00207                         $delegate =& $table->getDelegate();
00208                         if ( isset($delegate) && method_exists($delegate, $name) ){
00209                                 //$res = call_user_func(array(&$delegate, $name));
00210                                 $res = $delegate->$name($params);
00211                                 return $res;
00212                         }
00213                 }
00214                 
00215                 $appDelegate =& $app->getDelegate();
00216                 if ( isset($appDelegate) && method_exists($appDelegate, $name) ){
00217                         //$res = call_user_func(array(&$appDelegate, $name));
00218                         $res = $appDelegate->$name($params);
00219                         return $res;
00220                 }
00221         }
00222         
00223         
00227         public static function quoteIdent($ident){
00228                 return str_replace('`','', $ident);
00229         }
00230         
00231         
00242         public static function redirect($msg=null, $error=null){
00243                 $app =& Dataface_Application::getInstance();
00244                 $query =& $app->getQuery();
00245                 $prefix = @$query['--prefix'];
00246                 if ( isset($query['--error_page']) and isset($error) ){
00247                         $page = $query['--error_page'];
00248                 } else if ( isset($query['--success_page']) and !isset($error) ){
00249                         $page = $query['--success_page'];
00250                 } else if ( isset($query['--redirect']) ){
00251                         $page = $query['--redirect'];
00252                 }
00253                 
00254                 if ( !isset($page) ) return;
00255                 if ( isset($error) ){
00256                         if ( strpos($page,'?') === false ) $page .= '?';
00257                         else $page .= '&';
00258                                 
00259                         $page .= $prefix.'--error_message='.urlencode($res->getMessage()).'&'.$prefix.'--error_code='.urlencode($res->getCode());
00260                         
00261                 }
00262                         
00263                 if ( isset($msg) ){
00264                         if ( strpos($page,'?') === false ) $page .= '?';
00265                         $page .= $prefix.'--msg='.urlencode($msg);
00266                 }
00267                 
00268                 $app->redirect("$page");
00269         }
00270 
00271 }
00272  
 All Data Structures Namespaces Files Functions Variables Enumerations