![]() |
Xataface 2.0
Xataface Application Framework
|
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 class Dataface_ValuelistTool { 00022 00023 00024 var $_valuelists = array(); 00025 00026 function Dataface_ValuelistTool(){ 00027 00028 $this->_loadValuelistsIniFile(); 00029 00030 00031 } 00032 00033 function _valuelistsIniFilePath(){ 00034 return DATAFACE_SITE_PATH.'/valuelists.ini'; 00035 } 00036 00037 function _hasValuelistsIniFile(){ 00038 return file_exists($this->_valuelistsIniFilePath()); 00039 } 00040 00041 function _loadValuelistsIniFile(){ 00042 if ( !isset( $this->_valuelists ) ){ 00043 $this->_valuelists = array(); 00044 } 00045 $valuelists =& $this->_valuelists; 00046 00047 if ( $this->_hasValuelistsIniFile() ){ 00048 00049 00050 $conf = parse_ini_file( $this->_valuelistsIniFilePath(), true); 00051 00052 foreach ( $conf as $vlname=>$vllist ){ 00053 $valuelists[$vlname] = array(); 00054 if ( is_array( $vllist ) ){ 00055 foreach ( $vllist as $key=>$value ){ 00056 00057 if ( $key == '__sql__' ) { 00058 // we perform the sql query specified to produce our valuelist. 00059 // the sql query should return two columns only. If more are 00060 // returned, only the first two will be used. If one is returned 00061 // it will be used as both the key and value. 00062 $res = df_query($value, null, true, true); 00063 if ( is_array($res) ){ 00064 //while ($row = mysql_fetch_row($res) ){ 00065 foreach ($res as $row){ 00066 $valuekey = $row[0]; 00067 $valuevalue = count($row)>1 ? $row[1] : $row[0]; 00068 $valuelists[$vlname][$valuekey] = $valuevalue; 00069 00070 if ( count($row)>2 ){ 00071 $valuelists[$vlname.'__meta'][$valuekey] = $row[2]; 00072 } 00073 } 00074 } else { 00075 throw new Exception('Valuelist sql query failed: '.$value.': '.mysql_error(), E_USER_NOTICE); 00076 } 00077 00078 } else { 00079 $valuelists[$vlname][$key] = $value; 00080 } 00081 } 00082 } 00083 00084 00085 } 00086 00087 } 00088 } 00089 00090 00091 public static function &getInstance(){ 00092 static $instance = 0; 00093 if ( $instance === 0 ){ 00094 $instance = new Dataface_ValuelistTool(); 00095 } 00096 return $instance; 00097 } 00098 00099 function &getValuelist($name){ 00100 if ( !is_a($this, 'Dataface_ValuelistTool') ){ 00101 $vlt =& Dataface_ValuelistTool::getInstance(); 00102 } else { 00103 $vlt =& $this; 00104 } 00105 00106 if ( isset($vlt->_valuelists[$name] ) ){ 00107 return $vlt->_valuelists[$name]; 00108 } 00109 00110 throw new Exception("Request for valuelist '$name' that does not exist in Dataface_ValuelistTool::getValuelist().", E_USER_ERROR); 00111 } 00112 00113 00114 function hasValuelist($name){ 00115 if ( !is_a($this, 'Dataface_ValuelistTool') ){ 00116 $vlt =& Dataface_ValuelistTool::getInstance(); 00117 } else { 00118 $vlt =& $this; 00119 } 00120 return isset( $vlt->_valuelists[$name]); 00121 } 00122 00126 function &valuelists(){ 00127 if ( !is_a($this, 'Dataface_ValuelistTool') ){ 00128 $vlt =& Dataface_ValuelistTool::getInstance(); 00129 } else { 00130 $vlt =& $this; 00131 } 00132 00133 return $vlt->_valuelists; 00134 } 00135 00148 function addValueToValuelist(&$table, $valuelistName, $value, $key=null, $checkPerms=false){ 00149 00150 import( 'Dataface/ConfigTool.php'); 00151 $configTool =& Dataface_ConfigTool::getInstance(); 00152 $conf = $configTool->loadConfig('valuelists', $table->tablename); 00153 00154 $relname = $valuelistName.'__valuelist'; 00155 //$conf = array($relname=>$conf); 00156 $table->addRelationship( $relname, $conf[$valuelistName]); 00157 $rel =& $table->getRelationship($relname); 00158 $fields =& $rel->fields(); 00159 if ( count($fields) > 1 ) { 00160 $valfield = $fields[1]; 00161 $keyfield = $fields[0]; 00162 } 00163 else { 00164 $valfield = $fields[0]; 00165 $keyfield = $fields[0]; 00166 } 00167 00168 $record = new Dataface_Record($table->tablename); 00169 $rrecord = new Dataface_RelatedRecord($record, $relname); 00170 if ( $checkPerms and !$rrecord->checkPermission('edit', array('field'=>$valfield)) ){ 00171 return Dataface_Error::permissionDenied(); 00172 } 00173 $rrecord->setValue($valfield, $value); 00174 if (isset($key) and isset($keyfield) ){ 00175 if ( $checkPerms and !$rrecord->checkPermission('edit', array('field'=>$keyfield)) ){ 00176 return Dataface_Error::permissionDenied(); 00177 } 00178 $rrecord->setValue($keyfield, $key); 00179 } 00180 import('Dataface/IO.php'); 00181 $io = new Dataface_IO($table->tablename); 00182 $res = $io->addRelatedRecord($rrecord); 00183 if ( PEAR::isError($res) ) return $res; 00184 return array('key'=>$rrecord->val($keyfield), 'value'=>$rrecord->val($valfield)); 00185 00186 } 00187 00197 function &asRelationship(&$table, $valuelistName){ 00198 import( 'Dataface/ConfigTool.php'); 00199 $configTool =& Dataface_ConfigTool::getInstance(); 00200 $conf = $configTool->loadConfig('valuelists', $table->tablename); 00201 if ( !@$conf[$valuelistName]['__sql__'] ){ 00202 $out = null; 00203 return $out; 00204 } 00205 00206 $relname = $valuelistName.'__valuelist'; 00207 //$conf = array($relname=>$conf); 00208 $table->addRelationship( $relname, $conf[$valuelistName]); 00209 $rel =& $table->getRelationship($relname); 00210 $rel->_schema['action']['visible']=0; 00211 return $rel; 00212 00213 } 00214 00215 00216 00217 00218 }