Xataface HTML Reports Module 0.2
HTML Reports Module for Xataface
|
00001 <?php 00002 /* 00003 * Xataface HTML Reports Module 00004 * Copyright (C) 2011 Steve Hannah <steve@weblite.ca> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library 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 GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public 00017 * License along with this library; if not, write to the 00018 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00202 class actions_htmlreports_schemabrowser_getschema { 00203 00204 function handle($params){ 00205 00206 session_write_close(); 00207 header('Connection:close'); 00208 require_once(dirname(__FILE__).'/../classes/XfHtmlReportBuilder.class.php'); 00209 $app = Dataface_Application::getInstance(); 00210 $query = $app->getQuery(); 00211 $table = Dataface_Table::loadTable($query['-table']); 00212 $perms = $table->getPermissions(array()); 00213 00214 00215 try { 00216 00217 if ( !@$perms['view schema'] ){ 00218 throw new Exception("You don't have permission to view this table's schema."); 00219 } 00220 00221 00222 $opts = array(); 00223 $opts[] = array( 00224 'data'=>df_translate('xataface.modules.htmlreports.schemabrowser.fields_label','Fields'), 00225 'children'=>array() 00226 ); 00227 00228 00229 // First do the regular fields 00230 $fields =& $table->fields(); 00231 00232 $fieldOpts =& $opts[0]['children']; 00233 foreach ($fields as $field){ 00234 $fperms = $table->getPermissions(array('field'=>$field['name'])); 00235 if ( !@$fperms['view schema'] ) continue; 00236 $myopt= array( 00237 'data'=>$field['widget']['label'], 00238 'attr'=> array( 00239 'xf-htmlreports-fieldname'=>$field['name'], 00240 'xf-htmlreports-macro'=>'{$'.$field['name'].'}' 00241 ) 00242 ); 00243 00244 $children = array(); 00245 foreach ( XfHtmlReportBuilder::$SUMMARY_FUNCTIONS as $func ){ 00246 $children[] = array( 00247 'data'=> $func.'('.$field['widget']['label'].')', 00248 'attr'=>array( 00249 'xf-htmlreports-macro'=>'{@'.$func.'('.$field['name'].')}' 00250 ) 00251 ); 00252 } 00253 00254 $myopt['children'] = $children; 00255 $fieldOpts[] = $myopt; 00256 } 00257 00258 unset($fieldOpts); 00259 00260 00261 // Next we do the grafted fields 00262 $fieldOpts = array(); 00263 $graftedOpt = array( 00264 'data'=>df_translate('xataface.modules.htmlreports.schemabrowser.grafted_fields_label', 'Grafted Fields') 00265 ); 00266 $graftedOpt['children'] =& $fieldOpts; 00267 00268 unset($fields); 00269 $fields =& $table->graftedfields(); 00270 foreach ($fields as $field){ 00271 $fperms = $table->getPermissions(array('field'=>$field['name'])); 00272 if ( !@$fperms['view'] or !@$fperms['view schema'] ) continue; 00273 $myopt = array( 00274 'data'=>$field['widget']['label'], 00275 'attr'=>array( 00276 'xf-htmlreports-fieldname'=>$field['name'], 00277 'xf-htmlreports-macro'=> '{$'.$field['name'].'}' 00278 ) 00279 ); 00280 00281 $children = array(); 00282 foreach ( XfHtmlReportBuilder::$SUMMARY_FUNCTIONS as $func ){ 00283 $children[] = array( 00284 'data'=> $func.'('.$field['widget']['label'].')', 00285 'attr'=>array( 00286 'xf-htmlreports-macro'=>'{@'.$func.'('.$field['name'].')}' 00287 ) 00288 ); 00289 } 00290 00291 $myopt['children'] = $children; 00292 $fieldOpts[] = $myopt; 00293 } 00294 00295 $opts[] = $graftedOpt; 00296 00297 00298 unset($fieldOpts); 00299 unset($graftedOpt); 00300 unset($fields); 00301 00302 00303 // Next we do the calculated fields 00304 $fieldOpts = array(); 00305 $calcOpt = array( 00306 'data'=>df_translate('xataface.modules.htmlreports.schemabrowser.calculated_fields_label', 'Calculated Fields') 00307 ); 00308 $calcOpt['children'] =& $fieldOpts; 00309 $fields =& $table->delegateFields(); 00310 00311 foreach ($fields as $field){ 00312 $fperms = $table->getPermissions(array('field'=>$field['name'])); 00313 if ( !@$fperms['view schema'] ) continue; 00314 $myopt = array( 00315 'data'=>$field['widget']['label'], 00316 'attr'=>array( 00317 'xf-htmlreports-fieldname'=>$field['name'], 00318 'xf-htmlreports-macro'=> '{$'.$field['name'].'}' 00319 ) 00320 ); 00321 00322 $children = array(); 00323 foreach ( XfHtmlReportBuilder::$SUMMARY_FUNCTIONS as $func ){ 00324 $children[] = array( 00325 'data'=> $func.'('.$field['widget']['label'].')', 00326 'attr'=>array( 00327 'xf-htmlreports-macro'=>'{@'.$func.'('.$field['name'].')}' 00328 ) 00329 ); 00330 } 00331 00332 $myopt['children'] = $children; 00333 00334 $fieldOpts[] = $myopt; 00335 } 00336 00337 $opts[] = $calcOpt; 00338 00339 unset($calcOpt); 00340 unset($fields); 00341 unset($fieldOpts); 00342 00343 00344 // Now we do the relationships 00345 00346 $rOpt = array( 00347 'data'=>df_translate('xataface.modules.htmlreports.schemabrowser.relationships_label', 'Relationships'), 00348 'data-key'=>'relationships' 00349 ); 00350 $rOptOpts = array(); 00351 $rOpt['children'] =& $rOptOpts; 00352 $opts[] = $rOpt; 00353 00354 $relationships =& $table->relationships(); 00355 foreach ($relationships as $rname=>$r){ 00356 $rperms = $table->getPermissions(array('relationship'=>$rname)); 00357 if ( !@$rperms['view schema'] ) continue; 00358 00359 $rfieldOpts = array(); 00360 $rfields = $r->fields(true); 00361 $thisrOpt = array( 00362 'data'=>$r->getLabel(), 00363 'attr'=>array( 00364 'xf-htmlreports-relationshipname'=>$rname 00365 ) 00366 ); 00367 $thisrOpt['children'] =& $rfieldOpts; 00368 00369 foreach ($rfields as $rfield){ 00370 $fieldDef = $r->getField($rfield); 00371 $ftable = Dataface_Table::loadTable($fieldDef['tablename']); 00372 $fperms = $table->getPermissions(array('field'=>$fieldDef['name'])); 00373 if ( !@$fperms['view schema'] ){ 00374 continue; 00375 } 00376 00377 $rfieldOpts[] = array( 00378 'data' => $fieldDef['widget']['label'], 00379 'attr'=>array( 00380 'xf-htmlreports-macro' => '{$'.$rname.'.'.$fieldDef['name'].'}', 00381 'xf-htmlreports-fieldname'=>$fieldDef['name'] 00382 ) 00383 ); 00384 00385 00386 00387 00388 } 00389 00390 unset($rfieldOpts); 00391 $rOptOpts[] = $thisrOpt; 00392 00393 } 00394 00395 00396 00397 00398 $root = array( 00399 'data'=>$table->getLabel(), 00400 'children'=>$opts 00401 ); 00402 00403 $this->out(array( 00404 'code'=>200, 00405 'schema'=>$opts 00406 )); 00407 exit; 00408 00409 00410 00411 } catch (Exception $ex){ 00412 $this->out(array( 00413 'code'=>$ex->getCode(), 00414 'message'=>$ex->getMessage() 00415 )); 00416 exit; 00417 } 00418 00419 } 00420 00421 function out($params){ 00422 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"'); 00423 echo json_encode($params); 00424 } 00425 00426 00427 }