Xataface 2.0
Xataface Application Framework
public-api.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2011 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  * @file dataface-public-api.php
00021  * Author:      Steve Hannah <shannah@sfu.ca>
00022  * Created:     December 14, 2005
00023  *
00024  * Description:
00025  * ------------
00026  * A Procedural API to the Dataface framework.  This is designed to same developers
00027  * time trying to figure out what all the classes are and what they do.  This api
00028  * provides functions to access all importand aspects of the framework.
00029  *
00030  */
00031 
00032 
00033 if ( !defined( 'DATAFACE_PUBLIC_API_LOADED' ) ){
00034 define('DATAFACE_PUBLIC_API_LOADED', true);
00035 
00050 function df_init($site_path, $dataface_url, $conf=null){
00051         require_once dirname(__FILE__).'/init.php';
00052         init($site_path, $dataface_url);
00053 
00054         import( 'PEAR.php');
00055         import( 'Dataface/Application.php'); 
00056                         
00057         
00058         $app = Dataface_Application::getInstance($conf);
00059         if ( df_get_file_system_version() != df_get_database_version() ){
00060                 $res = df_update();
00061                 if (PEAR::isError($res) ){
00062                         throw new Exception($res->getMessage(), E_USER_ERROR);
00063                 }
00064         }
00065         return $app;
00066 
00067 }
00068 /* @} */
00069 
00070 function df_secure(&$records, $secure=true){
00071         foreach ($records as $record){
00072                 $record->secureDisplay = $secure;
00073         }
00074 }
00075 
00076 if ( !function_exists('xmlentities') ){
00077         function xmlentities($string) {
00078                 return str_replace ( array ( '&', '"', "'", '<', '>', '' ), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string );
00079         }
00080 }
00081 
00082 function df_update(){
00083         import('actions/install.php');
00084         $action = new dataface_actions_install;
00085         $params = array();
00086         $res = $action->handle($params);
00087         if ( PEAR::isError($res) ){
00088                 return $res;
00089         }
00090 
00091 }
00092 
00093 
00094 
00095 function &df_create_new_record_form($table, $fields=null){
00096         import( 'Dataface/QuickForm.php');
00097         $form = Dataface_QuickForm::createNewRecordForm($table, $fields);
00098         return $form;
00099 }
00100 
00101 function &df_create_edit_record_form(&$table, $fields=null){
00102         import('Dataface/QuickForm.php');
00103         $form = Dataface_QuickForm::createEditRecordForm($table, $fields);
00104         return $form;
00105 
00106 }
00107 
00108 function &df_create_new_related_record_form(&$record, $relationshipName, $fieldNames=null){
00109         import( 'Dataface/ShortRelatedRecordForm.php');
00110         $form = new Dataface_ShortRelatedRecordForm($record,$relationshipName,'', $fieldNames);
00111         return $form;
00112 
00113 }
00114 
00115 
00116 function &df_create_existing_related_record_form(&$record, $relationshpName){
00117         import( 'Dataface/ExistingRelatedRecordForm.php');
00118         $form = new Dataface_ExistingRelatedRecordForm($record, $relationshipName);
00119         return $form;
00120 }
00121 
00122 
00123 
00124 
00125 function &df_create_import_form(&$table, $relationshipName=null){
00126         import( 'Dataface/ImportForm.php');
00127         $form = new Dataface_ExistingRelatedRecordForm($record, $relationshipName);
00128         return $form;
00129 
00130 }
00131 
00132 function &df_create_search_form($tablename, $query=array(), $fields=null){
00133         import( 'Dataface/SearchForm.php');
00134         $app = Dataface_Application::getInstance();
00135         $form = new Dataface_SearchForm($tablename, $app->db(), $query, $fields);
00136         return $form;
00137 }
00138 
00139 
00140 
00141 function &df_get_records($table, $query=null, $start=null, $limit=null, $preview=true){
00142         import( 'Dataface/QueryTool.php');
00143         $app = Dataface_Application::getInstance();
00144         if ( $query === null and $start === null and $limit === null ){
00145                 $queryTool = Dataface_QueryTool::loadResult($table);
00146         } else {
00147                 if ( $query === null or !$query ) $query = array();
00148                 if ( $start !== null ) $query['-skip'] = $start;
00149                 if ( $limit !== null ) $query['-limit'] = $limit;
00150                 
00151                 $queryTool = new Dataface_QueryTool($table, null, $query);
00152         }
00153         
00154         $queryTool->loadSet('',false,false,$preview);
00155         $it = $queryTool->iterator();
00156         return $it;
00157 }
00158 
00159 function &df_get_records_array($table, $query=null, $start=null, $limit=null, $preview=true){
00160         $records = array();
00161         $it = df_get_records($table,$query,$start,$limit,$preview);
00162         if ( PEAR::isError($it) )return $it;
00163         while ($it->hasNext()){
00164                 $records[] = $it->next();
00165         }
00166         return $records;
00167 }
00168 
00169 function &df_get_related_records($query=array()){
00170         if ( !isset($query['-relationship']) ) return PEAR::raiseError("No relationship specified");
00171         
00172         $source = df_get_record($query['-table'],$query);
00173         if ( !$source ) return PEAR::raiseError("Source record not found");
00174         if ( PEAR::isError($source) ) return $source;
00175         
00176         $relationship = $source->_table->getRelationship($query['-relationship']);
00177         
00178         if ( isset( $query['-related:sort']) ){
00179                 $sortcols = explode(',', trim($query['-related:sort']));
00180                 $sort_columns = array();
00181                 foreach ($sortcols as $sortcol){
00182                         $sortcol = trim($sortcol);
00183                         if (strlen($sortcol) === 0 ) continue;
00184                         $sortcol = explode(' ', $sortcol);
00185                         if ( count($sortcol) > 1 ){
00186                                 $sort_columns[$sortcol[0]] = strtolower($sortcol[1]);
00187                         } else {
00188                                 $sort_columns[$sortcol[0]] = 'asc';
00189                         }
00190                 }
00191                 unset($sortcols);       // this was just a temp array so we get rid of it here
00192         } else {
00193                 $sort_columns = array();
00194         }
00195         $sort_columns_arr = array();
00196         foreach ( $sort_columns as $colkey=>$colorder) {
00197                 $sort_columns_arr[] =  '`'.$colkey.'`'. $colorder;
00198         }
00199         if ( count($sort_columns_arr) > 0 ){
00200                 $sort_columns_str = implode(', ',$sort_columns_arr);
00201         } else {
00202                 $sort_columns_str = 0;
00203         }
00204         
00205         if ( isset($query['-related:search']) ){
00206                 $rwhere = array();
00207                 foreach ($relationship->fields() as $rfield){
00208                         //list($garbage,$rfield) = explode('.', $rfield);
00209                         $rwhere[] = '`'.str_replace('.','`.`',$rfield).'` LIKE \'%'.addslashes($query['-related:search']).'%\'';
00210                 }
00211                 $rwhere = implode(' OR ', $rwhere);
00212         } else {
00213                 $rwhere = 0;
00214         }
00215         $start = isset($query['-related:start']) ? $query['-related:start'] : 0;
00216         $limit = isset($query['-related:limit']) ? $query['-related:limit'] : 30;
00217         
00218         $out =& $source->getRelatedRecordObjects($query['-relationship'], $start, $limit, $rwhere, $sort_columns_str);
00219         return $out;
00220 }
00221 
00222 
00223 function df_singularize($label){
00224 
00225         if ( preg_match('/s$/i', $label) ){
00226                 if ( preg_match('/ies$/i', $label) ){
00227                         return preg_replace('/ies$/i', 'y', $label);
00228                 } else if ( preg_match('/es$/i', $label) 
00229                                 and !preg_match('/ees$/i', $label)
00230                                 and !preg_match('/[aeoiuy][qwrtpsdfghjklzxcvbnm]es$/i', $label)
00231                                 
00232                 ){
00233                         return preg_replace('/es$/', '', $label);
00234                 } else {
00235                         return preg_replace('/s$/', '', $label);
00236                 }
00237         }
00238         return $label;
00239 }
00240 
00241 
00242 function df_append_query($url, $query){
00243         if ( strpos($url,'?') === false ){
00244                 $url .= '?';
00245         }
00246         foreach ($query as $k=>$v){
00247                 $url .= '&'.urlencode($k).'='.urlencode($v);
00248         }
00249         return $url;
00250 }
00251 
00252 
00253 function df_clear_views(){
00254 
00255 
00256         
00257         
00258         $res = mysql_query("show tables like 'dataface__view_%'", df_db());
00259         $views = array();
00260         while ( $row = mysql_fetch_row($res) ){
00261                 $views[] = $row[0];
00262         }
00263         if ( $views ) {
00264                 $sql = "drop view `".implode('`,`', $views)."`";
00265                 //echo $sql;
00266                 //echo "<br/>";
00267                 $res = mysql_query("drop view `".implode('`,`', $views)."`", df_db());
00268                 if ( !$res ) throw new Exception(mysql_error(df_db()));
00269         }
00270         
00271         
00272 }
00273 
00274 function df_clear_cache(){
00275         $res = mysql_query("truncate table __output_cache", df_db());
00276         if ( !$res ) throw new Exception(mysql_error(df_db()));
00277         return $res;
00278 }
00279 
00280 
00281 function &df_get_table_info($tablename){
00282         import( 'Dataface/Table.php');
00283         $table = Dataface_Table::loadTable($tablename);
00284         return $table;
00285 }
00286 
00287 function &df_get_record($table, $query, $io=null){
00288         import( 'Dataface/Record.php');
00289         import( 'Dataface/IO.php');
00290         
00291         $record = new Dataface_Record($table, array());
00292         if ( !isset($io) ){
00293                 $io = new Dataface_IO($table);
00294         }
00295         
00296         $query['-limit'] = 1;
00297         if ( @$query['-cursor'] > 0 ){
00298                 $query['-skip'] = $query['-cursor'];
00299         } 
00300         
00301         $res = $io->read($query, $record);
00302         if ( PEAR::isError($res) ) {
00303                 //print_r($query);
00304                 //echo $res->toString();
00305                 $null = null;
00306                 return $null;
00307         }
00308         return $record;
00309 }
00310 
00311 function &df_get_record_by_id($id){
00312         import('Dataface/IO.php');
00313         @list($id,$fieldname) = explode('#', $id);
00314         $record = Dataface_IO::getByID($id);
00315         return $record;
00316 }
00350 function df_parse_uri($uri){
00351         static $cache = 0;
00352         if ( $cache === 0 ) $cache = array();
00353         
00354         if ( !isset($cache[$uri])  ){
00355                 $out = array(
00356                         'table'=>null,
00357                         'relationship'=>null,
00358                         'query'=>null,
00359                         'related_where'=>null,
00360                         'field'=>null,
00361                         'action'=>null
00362                         );
00363                 if ( strpos($uri, '?') !== false ){
00364                         list($table,$query) = explode('?', $uri);
00365                 } else {
00366                         if ( strpos($uri, '#') !== false ){
00367                                 list($table, $fieldname) = explode('#', $uri);
00368                                 $query = null;
00369                         } else {
00370                                 $table = $uri;
00371                                 $query = null;
00372                                 $fieldname = null;
00373                         }
00374                 }
00375                 if ( strpos($query,'#') !== false ) 
00376                         list($query,$fieldname) = explode('#', $query);
00377                 
00378                 //if ( !isset($table) || !isset($query)) return PEAR::raiseError("Dataface_IO::getByID expects an id of a specific form, but received ".$uri, DATAFACE_E_ERROR);
00379                 
00380                 if ( strpos($table, '://') !== false ){
00381                         list($action, $table) = explode('://', $table);
00382                         $out['action'] = $action;
00383                 }
00384                 
00385                 @list($table, $relationship) = explode('/', $table);
00386                 
00387                 // Find the keys for this one
00388                 $params = explode('&',$query);
00389                 $params2 = array();
00390                 foreach ($params as $param){
00391                         if ( !$param) continue;
00392                         list($key,$val) = explode('=', $param);
00393                         $params2[trim(urldecode($key))] = trim(urldecode($val));
00394                 }
00395                 
00396                 $out['table'] = $table;
00397                 
00398                 if ( !isset($relationship) ){
00399                         $out['query'] = $params2;
00400                         
00401                         if ( isset($fieldname) ){
00402                                 $out['field'] = $fieldname;
00403                         }
00404                         
00405                 
00406                 } else {
00407                         $out['relationship'] = $relationship;
00408                         $primary_params = array();
00409                         $related_params = array();
00410                         foreach ($params2 as $key=>$val){
00411                                 @list($key1,$key2) = explode('::',$key);
00412                                 if ( !isset($key2) ){
00413                                         $primary_params[trim(urldecode($key1))] = trim(urldecode($val));
00414                                 } else {
00415                                         $related_params[trim(urldecode($key2))] = trim(urldecode($val));
00416                                 }       
00417                         }
00418                         
00419                         if ( count($related_params) > 0 ){
00420                                 $sql = array();
00421                                 foreach ($related_params as $k=>$v){
00422                                         $sql[] = "`{$k}`='{$v}'";
00423                                 }
00424                                 $sql = implode(' and ', $sql);
00425                                 $out['related_where'] = $sql;
00426                         }
00427                         
00428                         $out['query'] = $primary_params;
00429                         
00430                         if ( isset($fieldname) ) {
00431                                 $out['field'] = $fieldname;
00432                         }
00433                         
00434                 
00435                 }
00436                 // Let's make sure we don't overload it.  If the array is greater
00437                 // than 250 items, we'll remove the first one in order to add this
00438                 // next one.
00439                 if ( count($cache) > 200 ) array_shift($cache);
00440                 $cache[$uri] = $out;
00441         }
00442         return $cache[$uri];
00443                 
00444                 
00445 
00446 }
00447 
00448 function df_get_selected_records($query){
00449         if ( isset($query['--selected-ids']) ){
00450                 $selected = $query['--selected-ids'];
00451         } else if ( isset($query['-selected-ids']) ){
00452                 $selected = $query['-selected-ids'];
00453         } else {
00454                 return array();
00455         }
00456         
00457         $ids = explode("\n", $selected);
00458         $records = array();
00459         foreach ($ids as $id){
00460                 $records[] = df_get_record_by_id($id);
00461         }
00462         return $records;
00463 }
00464 
00465 function df_save_record(&$record, $keys=null, $lang=null, $secure=false){
00466         import( 'Dataface/Record.php');
00467         import( 'Dataface/IO.php');
00468         
00469         $io = new Dataface_IO($record->_table->tablename);
00470         if ( isset($lang) ) $io->lang = $lang;
00471         $res = $io->write($record, $keys, null, $secure);
00472         $io->__destruct();
00473         unset($io);
00474         return $res;
00475 
00476 }
00477 
00478 function &df_get_valuelist($tablename, $valuelistname){
00479         $table = Dataface_Table::loadTable($tablename);
00480         $vl =& $table->getValuelist($valuelistname);
00481         return $vl;
00482 }
00483 
00484 
00485 
00486 function &df_get_relationship_info($tablename, $relationshipname){
00487         $table = df_get_table_info($tablename);
00488         $relationship = $table->getRelationship($relationshipname);
00489         return $relationship;
00490 }
00491 
00492 
00493 function df_register_skin($name, $template_dir){
00494         import( 'Dataface/SkinTool.php');
00495         $st = Dataface_SkinTool::getInstance();
00496         $st->register_skin($name, $template_dir);
00497 
00498 }
00499 
00500 function df_display($context, $template_name){
00501         import( 'Dataface/SkinTool.php');
00502         $st = Dataface_SkinTool::getInstance();
00503         
00504         return $st->display($context, $template_name);
00505 }
00506 
00507 function df_config_get($varname){
00508         $app = Dataface_Application::getInstance();
00509         return $app->_conf[$varname];
00510 }
00511 
00512 function df_config_set($varname, $value){
00513         $app = Dataface_Application::getInstance();
00514         $app->_conf[$varname] = $value;
00515 }
00516 
00517 function df_db(){
00518         $app = Dataface_Application::getInstance();
00519         return $app->_db;
00520 }
00521 
00522 function df_query($sql, $lang=null, $as_array=false, $enumerated=false){
00523         import('Dataface/DB.php');
00524         $db = Dataface_DB::getInstance();
00525         return $db->query($sql,null,$lang,$as_array, $enumerated);
00526 }
00527 
00528 function df_insert_id(){
00529         import('Dataface/DB.php');
00530         $db = Dataface_DB::getInstance();
00531         return $db->insert_id();
00532 }
00533 
00534 function df_translate($id, $default=null, $params=array(), $lang=null){
00535         return Dataface_LanguageTool::getInstance($lang)->translate($id,$default,$params, $lang);
00536 }
00537 
00538 function df_load_realm($realm, $lang=null){
00539         Dataface_LanguageTool::getInstance($lang)->loadRealm($realm);
00540 }
00541 
00542 function df_check_permission($permission, &$object, $params=array() ){
00543         return Dataface_PermissionsTool::checkPermission($permission, $object, $params);
00544 }
00545 
00546 function df_permission_names_as_array(&$perms){
00547         $ptool = Dataface_PermissionsTool::getInstance();
00548         return $ptool->namesAsArray($perms);
00549 }
00550 
00551 function df_permission_names_as_string(&$perms){
00552         $ptool = Dataface_PermissionsTool::getInstance();
00553         return $ptool->namesAsString($perms);
00554 }
00555 
00556 function df_block($params){
00557         $app = Dataface_Application::getInstance();
00558         $query =& $app->getQuery();
00559         
00560         if ( isset($params['table']) ) $table = Dataface_Table::loadTable($params['table']);
00561         else if ( isset($params['record']) ) $table = $params['record']->_table;
00562         else $table = Dataface_Table::loadTable($query['-table']);
00563                 
00564         if ( isset($params['name']) ) $name = $params['name'];
00565         else throw new Exception('No name specified for block.', E_USER_ERROR);
00566         
00567         unset($params['name']); unset($params['table']);
00568         
00569         return $table->displayBlock($name, $params);
00570 }
00571 
00572 
00573 function df_translation_warning(&$record, $language=null){
00574         import('Dataface/TranslationTool.php');
00575         $tt = new Dataface_TranslationTool();
00576         $tt->printTranslationStatusAlert($record, $language);
00577 }
00578 
00579 function df_editable($content, $id){
00580         $skinTool = Dataface_SkinTool::getInstance();
00581         return $skinTool->editable(array('id'=>$id), $content, $skinTool);
00582 }
00583 
00584 function df_offset($date){
00585         if ( !$date ){
00586                 return df_translate('scripts.global.MESSAGE_UNKNOWN','Unknown');
00587         }
00588         $date = strtotime($date);
00589         $offset = (strftime("%j")+strftime("%Y")*365)-
00590         (strftime("%j",$date)+strftime("%Y",$date)*365);
00591         if ($offset>7){
00592         $offset = (strftime("%W")+strftime("%Y")*52)-
00593         (strftime("%W",$date)+strftime("%Y",$date)*52);
00594         $end=($offset!=0?($offset>1?$offset . " weeks ago":"a week ago"):"Today");
00595         } else
00596         $end=($offset!=0?($offset>1?"$offset days ago":"Yesterday"):"Today");
00597         return strftime("%A, %B %d, %Y",$date)." - ". $end;
00598 }
00602 function &df_get($uri, $filter=null){
00603         $res = Dataface_IO::getByID($uri, $filter);
00604         return $res;
00605 }
00606 
00610 function df_set($uri, $value){
00611         $res = Dataface_IO::setByID($uri, $value);
00612         return $res;
00613 }
00614 
00615 if ( !function_exists('array_merge_recursive_unique') ){
00616         // array_merge_recursive which override value with next value.
00617         // based on: http://www.php.net/manual/hu/function.array-merge-recursive.php 09-Dec-2006 03:38
00618         function array_merge_recursive_unique($array0, $array1){
00619                 $func = __FUNCTION__;
00620                 $result = array();
00621                 $arrays = func_get_args();
00622                 $keyarrs = array_map('array_keys', $arrays);
00623                 $keys = array_merge($keyarrs[0], $keyarrs[1]);
00624                 foreach ($keys as $key){
00625                         foreach ( $arrays as $array ){
00626                                 if ( array_key_exists($key, $array) ){
00627                                         if ( is_array($array[$key]) ){
00628                                                 if ( is_array(@$result[$key]) ) $result[$key] = $func($result[$key], $array[$key]);
00629                                                 else $result[$key] = $array[$key];
00630                                         } else {
00631                                                 $result[$key] = $array[$key];
00632                                         }
00633                                 }
00634                         }
00635                 }
00636                 
00637                 return $result;
00638         }
00639         
00640         
00641         function array_merge_recursive_unique2($array0, $array1)
00642         {
00643                 $arrays = func_get_args();
00644                 $remains = $arrays;
00645         
00646                 // We walk through each arrays and put value in the results (without
00647                 // considering previous value).
00648                 $result = array();
00649         
00650                 // loop available array
00651                 foreach($arrays as $array) {
00652         
00653                         // The first remaining array is $array. We are processing it. So
00654                         // we remove it from remaing arrays.
00655                         array_shift($remains);
00656         
00657                         // We don't care non array param, like array_merge since PHP 5.0.
00658                         if(is_array($array)) {
00659                                 // Loop values
00660                                 foreach($array as $key => $value) {
00661                                         if(is_array($value)) {
00662                                                 // we gather all remaining arrays that have such key available
00663                                                 $args = array();
00664                                                 foreach($remains as $remain) {
00665                                                         if(array_key_exists($key, $remain)) {
00666                                                                 array_push($args, $remain[$key]);
00667                                                         }
00668                                                 }
00669         
00670                                                 if(count($args) > 2) {
00671                                                         // put the recursion
00672                                                         $result[$key] = call_user_func_array(__FUNCTION__, $args);
00673                                                 } else {
00674                                                         foreach($value as $vkey => $vval) {
00675                                                                 $result[$key][$vkey] = $vval;
00676                                                         }
00677                                                 }
00678                                         } else {
00679                                                 // simply put the value
00680                                                 $result[$key] = $value;
00681                                         }
00682                                 }
00683                         }
00684                 }
00685                 return $result;
00686         }
00687 }
00688 
00689 function df_is_logged_in(){
00690         return ( class_exists('Dataface_AuthenticationTool') and ($auth = Dataface_AuthenticationTool::getInstance()) and $auth->isLoggedIn());
00691 }
00692 
00693 function df_absolute_url($url){
00694         if ( !$url ) return $_SERVER['HOST_URI'];
00695         else if ( $url{0} == '/' ){
00696                 return $_SERVER['HOST_URI'].$url;
00697         } else if ( preg_match('/http(s)?:\/\//', $url) ){
00698                 return $url;
00699         } else {
00700                 $host_uri = $_SERVER['HOST_URI'];
00701                 $site_url = DATAFACE_SITE_URL;
00702                 if ( $site_url ) {
00703                         if ($site_url{0} == '/' ) $host_uri = $host_uri.$site_url;
00704                         else $host_uri = $host_uri.'/'.$site_url;
00705                 }
00706                 
00707                 return $host_uri.'/'.$url;
00708         }
00709 }
00710 
00711 
00717 function df_utc_offset(){
00718         $diff = preg_replace('/^([+-])(\d{1,2})(\d{2,2})$/', '$1$2:$3',date('O'));
00719         return $diff;
00720         
00721 }
00722 
00723 
00724 
00725         
00737         function df_get_file_system_version(){
00738                 static $version = -1;
00739                 
00740                 if ( $version == -1 ){
00741                         if ( file_exists('version.txt') ){
00742                                 list($fs_version) = file('version.txt');
00743                         } else {
00744                                 $fs_version = '0';
00745                         }
00746                         
00747                         $fs_version = explode(' ', $fs_version);
00748                         $fs_version = intval($fs_version[count($fs_version)-1]);
00749                         $version = $fs_version;
00750                 } 
00751                 if ( !$version ) return df_get_database_version();
00752                 
00753                 return $version;
00754         
00755         }
00756         
00761         function df_get_database_version($db=null){
00762                 if (!$db ) $db = df_db();
00763                 static $version = -1;
00764                 if ( $version == -1 ){
00765                         $sql = "select `version` from dataface__version limit 1";
00766                         $res = @mysql_query($sql, $db);
00767                         if ( !$res ){
00768                                 $res = mysql_query("create table dataface__version ( `version` int(5) not null default 0)", $db);
00769                                 if ( !$res ) throw new Exception(mysql_error($db), E_USER_ERROR);
00770                                 //$fs_version = df_get_file_system_version();
00771                                 $res = mysql_query("insert into dataface__version values ('0')", $db);
00772                                 if ( !$res ) throw new Exception(mysql_error($db), E_USER_ERROR);
00773                                 
00774                                 $res = mysql_query($sql, $db);
00775                                 if ( !$res ){
00776                                         throw new Exception(mysql_error($db), E_USER_ERROR);
00777                                 }
00778                         
00779                         }
00780                         list($version) = mysql_fetch_row($res);
00781                 }
00782                 return $version;
00783         }
00784 
00785 
00786 
00787 
00788         function df_q($sql){
00789         
00790                 if ( is_array($sql) ){
00791                         foreach ($sql as $q){
00792                                 $res = df_q($q);
00793                         }
00794                         return $res;
00795                 } else {
00796                         $res = mysql_query($sql, df_db());
00797                         if ( !$res ) throw new Exception(mysql_error(df_db()));
00798                         return $res;
00799                 }
00800         }
00801 
00802 
00803 
00804                 
00805 } // end if ( !defined( DATAFACE_PUBLIC_API_LOADED ) ){
 All Data Structures Namespaces Files Functions Variables Enumerations