![]() |
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 import('Dataface/LanguageTool.php'); 00022 00026 class Dataface_ActionTool { 00027 00028 //var $_actionsConfig; 00029 var $actions=array(); 00030 var $tableActions=array(); 00031 00032 function Dataface_ActionTool($conf=null){ 00033 if ( $conf === null ){ 00034 $this->_loadActionsINIFile(/*DATAFACE_PATH."/actions.ini"*/); 00035 //$this->_loadActionsINIFile(DATAFACE_SITE_PATH."/actions.ini"); 00036 } else { 00037 $this->actions =& $conf; 00038 } 00039 00040 } 00041 00042 00043 00044 function _loadActionsINIFile(/*$path*/){ 00045 00046 import('Dataface/ConfigTool.php'); 00047 $configTool =& Dataface_ConfigTool::getInstance(); 00048 $actions =& $configTool->loadConfig('actions', null); 00049 foreach ( array_keys($actions) as $key){ 00050 $action =& $actions[$key]; 00051 $action['name'] = $key; 00052 if ( !isset($action['order']) ) $action['order'] = 0; 00053 if ( !isset($action['id']) ) $action['id'] = $action['name']; 00054 if ( !isset($action['label']) ) $action['label'] = str_replace('_',' ',ucfirst($action['name'])); 00055 if ( !isset($action['accessKey'])) $action['accessKey'] = substr($action['name'],0,1); 00056 //if ( !isset($action['label_i18n']) ) $action['label_i18n'] = 'action:'.$action['name'].' label'; 00057 //if ( !isset($action['description_i18n'])) $action['description_i18n'] = 'action:'.$action['name'].' description'; 00058 00059 if ( isset($action['description']) ){ 00060 $action['description'] = df_translate('actions.'.$action['name'].'.description', $action['description']); 00061 } 00062 if ( isset($action['label']) ){ 00063 $action['label'] = df_translate('actions.'.$action['name'].'.label',$action['label']); 00064 } 00065 00066 $this->actions[$key] =& $action; 00067 unset($action); 00068 } 00069 unset($temp); 00070 $this->actions =& $actions; 00071 00072 } 00073 00074 function _loadTableActions($tablename){ 00075 import('Dataface/Table.php'); 00076 // Some actions are loaded from the table's actions.ini file and must be loaded before we return the actions. 00077 00078 $table =& Dataface_Table::loadTable($tablename); 00079 if ( !$table->_actionsLoaded ){ 00080 $params = array(); 00081 $table->getActions($params); 00082 } 00083 } 00084 00092 function &getAction($params, $action=null){ 00093 $app =& Dataface_Application::getInstance(); 00094 $actions =& $this->actions; 00095 if ( !isset($action) ){ 00096 if ( @$params['table'] ){ 00097 $this->_loadTableActions($params['table']); 00098 unset($actions); 00099 if ( !isset($this->tableActions[$params['table']]) ){ 00100 $this->tableActions[$params['table']] = array(); 00101 } 00102 $actions =& $this->tableActions[$params['table']]; 00103 00104 } 00105 00106 if ( !isset($params['name']) or !$params['name'] ){ 00107 throw new Exception("ActionTool::getAction() requires 'name' parameter to be specified.", E_USER_ERROR); 00108 } 00109 if ( !isset( $actions[$params['name']] ) ) { 00110 $err = PEAR::raiseError( 00111 Dataface_LanguageTool::translate( 00112 "No action found", /* i18n id */ 00113 "No action found named '".$params['name']."'", /*default error message*/ 00114 array('name'=>$params['name']) /* i18n parameters */ 00115 ) 00116 ); 00117 return $err; 00118 } 00119 00120 00121 $action = $actions[$params['name']]; 00122 } 00123 00124 00125 if ( isset($action['selected_condition']) ) { 00126 $action['selected'] = $app->testCondition($action['selected_condition'], $params); 00127 } 00128 00129 00130 //if ( isset($action['visible']) and !$action['visible']) continue; 00131 // Filter based on a condition 00132 foreach (array_keys($action) as $attribute){ 00133 // Some entries may have variables that need to be evaluated. We use Dataface_Application::eval() 00134 // to evaluate these entries. The eval method will replace variables such as $site_url, $site_href 00135 // $dataface_url with the appropriate real values. Also if $params['record'] contains a 00136 // Record object or a related record object its values are treated as php variables that can be 00137 // replaced. For example if a Profile record has fields 'ProfileID' and 'ProfileName' with 00138 // ProfileID=10 and ProfileName = 'John Smith', then: 00139 // $app->parseString('ID is ${ProfileID} and Name is ${ProfileName}') === 'ID is 10 and Name is John Smith' 00140 if ( preg_match('/condition/i',$attribute) ) continue; 00141 if ( isset($action[$attribute.'_condition']) and !$app->testCondition($action[$attribute.'_condition'], $params) ){ 00142 $action[$attribute] = null; 00143 } else { 00144 $action[$attribute] = $app->parseString($action[$attribute], $params); 00145 } 00146 } 00147 return $action; 00148 00149 } 00150 00160 function getActions($params=array(), $actions=null){ 00161 if ( !is_array($params) ){ 00162 trigger_error("In Dataface_ActionTool::getActions(), expected parameter to be an array but received a scalar: ".$params.".".Dataface_Error::printStackTrace(), E_USER_ERROR); 00163 } 00164 $app =& Dataface_Application::getInstance(); 00165 00166 $out = array(); 00167 00168 $tablename = null; 00169 if ( isset($params['table']) ) $tablename = $params['table']; 00170 if ( isset($params['record']) and is_a($params['record'], 'Dataface_Record') ) $tablename = $params['record']->_table->tablename; 00171 else if ( isset($params['record']) and is_a($params['record'], 'Dataface_RelatedRecord')) $tablename = $params['record']->_record->_table->tablename; 00172 00173 if ( isset( $params['record'] ) && is_a($params['record'], 'Dataface_Record') ){ 00174 // we have received a record as a parameter... we can infer the table information 00175 $params['table'] = $params['record']->_table->tablename; 00176 } else if ( isset($params['record']) && is_a($params['record'], 'Dataface_RelatedRecord') ){ 00177 // we have recieved a related record object... we can infer both the table and relationship information. 00178 $temp =& $params['record']->getParent(); 00179 $params['table'] = $temp->_table->tablename; 00180 unset($temp); 00181 00182 $params['relationship'] = $params['record']->_relationshipName; 00183 } 00184 00185 if ( @$params['relationship']){ 00186 if ( strpos($params['relationship'], '.') !== false ){ 00187 // if the relationship is specified in the form 'Tablename.RElationshipname' parse it. 00188 list($params['table'],$params['relationship']) = explode('.', $params['relationship']); 00189 } 00190 } 00191 00192 if ( $tablename !== null ){ 00193 // Some actions are loaded from the table's actions.ini file and must be loaded before we return the actions. 00194 $table =& Dataface_Table::loadTable($tablename); 00195 if ( !$table->_actionsLoaded ){ 00196 $tparams = array(); 00197 $table->getActions($tparams, true); 00198 } 00199 unset($table); 00200 } 00201 00202 00203 if ( $actions === null ){ 00204 if ( @$params['table'] ){ 00205 if ( !isset($this->tableActions[$params['table']]) ){ 00206 $this->tableActions[$params['table']] = array(); 00207 } 00208 $actions = $this->tableActions[$params['table']]; 00209 } 00210 else $actions = $this->actions; 00211 } 00212 foreach ( array_keys($actions) as $key ){ 00213 if ( isset($action) ) unset($action); 00214 $action =& $actions[$key]; 00215 00216 if ( @$params['name'] and @$params['name'] !== @$action['name']) continue; 00217 if ( @$params['id'] and @$params['id'] !== @$action['id']) continue; 00218 00219 if ( isset($params['category']) and $params['category'] !== @$action['category']) continue; 00220 // make sure that the category matches 00221 00222 if ( @$params['table'] /*&& @$action['table']*/ && !(@$action['table'] == @$params['table'] or @in_array(@$params['table'], @$action['table']) )) continue; 00223 // Filter actions by table 00224 00225 if ( @$params['relationship'] && @$action['relationship'] && @$action['relationship'] != @$params['relationship']) continue; 00226 // Filter actions by relationship. 00227 00228 if ( @$action['condition'] and !$app->testCondition($action['condition'], $params) ) { 00229 continue; 00230 } 00231 if ( isset($params['record']) ){ 00232 if ( isset($action['permission']) and !$params['record']->checkPermission($action['permission']) ){ 00233 continue; 00234 } 00235 } else { 00236 if ( isset( $action['permission'] ) and !$app->checkPermission($action['permission'])){ 00237 continue; 00238 } 00239 } 00240 00241 if ( @$action['selected_condition'] ) $action['selected'] = $app->testCondition($action['selected_condition'], $params); 00242 else { 00243 $query = $app->getQuery(); 00244 if ( @$action['name'] == @$query['-action'] ) $action['selected'] = true; 00245 } 00246 00247 if ( isset($action['visible']) and !$action['visible']) continue; 00248 // Filter based on a condition 00249 foreach (array_keys($action) as $attribute){ 00250 // Some entries may have variables that need to be evaluated. We use Dataface_Application::eval() 00251 // to evaluate these entries. The eval method will replace variables such as $site_url, $site_href 00252 // $dataface_url with the appropriate real values. Also if $params['record'] contains a 00253 // Record object or a related record object its values are treated as php variables that can be 00254 // replaced. For example if a Profile record has fields 'ProfileID' and 'ProfileName' with 00255 // ProfileID=10 and ProfileName = 'John Smith', then: 00256 // $app->parseString('ID is ${ProfileID} and Name is ${ProfileName}') === 'ID is 10 and Name is John Smith' 00257 //if ( strpos($attribute, 'condition') !== false) continue; 00258 if ( preg_match('/condition/i',$attribute) ) continue; 00259 if ( is_array($action[$attribute]) ) continue; 00260 if ( isset($action[$attribute.'_condition']) and !$app->testCondition($action[$attribute.'_condition'], $params) ){ 00261 00262 $action[$attribute] = null; 00263 } else { 00264 $action[$attribute] = $app->parseString($action[$attribute], $params); 00265 } 00266 } 00267 $out[$key] =& $action; 00268 00269 unset($action); 00270 } 00271 00272 uasort($out, array(&$this, '_compareActions')); 00273 return $out; 00274 } 00275 00279 function _compareActions($a,$b){ 00280 if ( @$a['order'] < @$b['order'] ) return -1; 00281 else return 1; 00282 } 00283 00289 function addAction($name, $action){ 00290 if ( @$action['table'] ){ 00291 $this->tableActions[$action['table']][$name] = $action; 00292 $query = Dataface_Application::getInstance()->getQuery(); 00293 if ( $query['-table'] == $action['table'] ){ 00294 // Note: For some reason this needs to be passed by value 00295 $this->actions[$name] = $this->tableActions[$action['table']][$name]; 00296 } 00297 } 00298 else{ 00299 $this->actions[$name] = $action; 00300 } 00301 } 00302 00306 function removeAction($name){ 00307 $action = $this->getAction($name); 00308 if ( @$action['table'] ){ 00309 unset($this->tableActions[$action['table']][$name]); 00310 } 00311 unset( $this->actions[$name] ); 00312 } 00313 00318 public static function &getInstance($conf=null){ 00319 static $instance = 0; 00320 if ( !$instance ){ 00321 $instance = new Dataface_ActionTool($conf); 00322 } 00323 return $instance; 00324 } 00325 00326 00327 }