Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
Utilities.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 
27 
38  public static function groupBy($fieldname, $records, $order=array(), $titles=array()){
39  import( 'PEAR.php');
40  if (!is_array($records) ){
41  return PEAR::raiseError("In Dataface_Utilities::groupBy() expected 2nd parameter to be an array, but received "+$records);
42  }
43 
44 
45  $out = array();
46  $unordered = array();
47  $ordered = array();
48 
49  foreach ($order as $orderKey){
50  $ordered[$orderKey] = array();
51  }
52 
53 
54  foreach (array_keys($records) as $i){
55  if ( is_a($records[$i], 'Dataface_Record') || is_a($records[$i], 'Dataface_RelatedRecord') ){
56  $key = $records[$i]->qq($fieldname);
57 
58  } else if (is_array($records[$i]) ) {
59  $key = $records[$i][$fieldname];
60  } else {
61  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]);
62  }
63  if ( !$key ){
64  continue;
65  }
66  if ( isset( $ordered[$key] ) ) $ordered[$key][] =& $records[$i];
67  else {
68  if ( !isset($unordered[$key]) ){
69  $unordered[$key] = array();
70  }
71 
72  $unordered[$key][] =& $records[$i];
73  }
74  }
75  $out = array_merge($ordered, $unordered);
76  $out2 = array();
77  foreach (array_keys($out) as $key){
78  if ( isset($titles[$key]) ){
79  $out2[$titles[$key]] =& $out[$key];
80  } else {
81  $out2[$key] =& $out[$key];
82  }
83  }
84 
85  return $out2;
86 
87  }
88 
89 
117  public static function query2html($query, $keyFilter=array()){
118  foreach ( $keyFilter as $bad ){
119  if ( isset($query[$bad]) ) unset($query[$bad]);
120  }
121  $qt = array();
122  //call_user_func(array(__CLASS__, 'flattenQuery'), $query, $qt);
124 
125  ob_start();
126  foreach ($qt as $key=>$value){
127  echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />\n";
128  }
129  $out = ob_get_contents();
130  ob_end_clean();
131  return $out;
132  }
133 
167  public static function flattenQuery($in,&$out, $path=array()){
168  $origPath = $path;
169  if ( !empty($path) ){
170  $prefix = array_shift($path);
171  if ( !empty($path) ){
172  $prefix .= '['.implode('][', $path).']';
173  }
174  } else {
175  $prefix = '';
176  }
177  $hasprefix = !empty($prefix);
178  foreach ($in as $key=>$value){
179  //if ( substr($key,0,2) == '--' ) continue;
180  if ( is_array($value) ){
181  $origPath[] = $key;
182  Dataface_Utilities::flattenQuery($value, $out, $origPath);
183  } else {
184  if ( $hasprefix ){
185  $out[$prefix.'['.$key.']'] = $value;
186  } else {
187  $out[$key] = $value;
188  }
189  }
190  }
191  }
192 
193 
202  public static function fireEvent($name, $params=array()){
204  $query =& $app->getQuery();
205  if ( isset($query['-table']) ){
206  $table =& Dataface_Table::loadTable($query['-table']);
207  $delegate =& $table->getDelegate();
208  if ( isset($delegate) && method_exists($delegate, $name) ){
209  //$res = call_user_func(array(&$delegate, $name));
210  $res = $delegate->$name($params);
211  return $res;
212  }
213  }
214 
215  $appDelegate =& $app->getDelegate();
216  if ( isset($appDelegate) && method_exists($appDelegate, $name) ){
217  //$res = call_user_func(array(&$appDelegate, $name));
218  $res = $appDelegate->$name($params);
219  return $res;
220  }
221  }
222 
223 
227  public static function quoteIdent($ident){
228  return str_replace('`','', $ident);
229  }
230 
231 
242  public static function redirect($msg=null, $error=null){
244  $query =& $app->getQuery();
245  $prefix = @$query['--prefix'];
246  if ( isset($query['--error_page']) and isset($error) ){
247  $page = $query['--error_page'];
248  } else if ( isset($query['--success_page']) and !isset($error) ){
249  $page = $query['--success_page'];
250  } else if ( isset($query['--redirect']) ){
251  $page = $query['--redirect'];
252  }
253 
254  if ( !isset($page) ) return;
255  if ( isset($error) ){
256  if ( strpos($page,'?') === false ) $page .= '?';
257  else $page .= '&';
258 
259  $page .= $prefix.'--error_message='.urlencode($res->getMessage()).'&'.$prefix.'--error_code='.urlencode($res->getCode());
260 
261  }
262 
263  if ( isset($msg) ){
264  if ( strpos($page,'?') === false ) $page .= '?';
265  $page .= $prefix.'--msg='.urlencode($msg);
266  }
267 
268  $app->redirect("$page");
269  }
270 
271 }
272