Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
ActionTool.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 import('Dataface/LanguageTool.php');
22 
27 
28  //var $_actionsConfig;
29  var $actions=array();
30  var $tableActions=array();
31 
32  function Dataface_ActionTool($conf=null){
33  if ( $conf === null ){
34  $this->_loadActionsINIFile(/*DATAFACE_PATH."/actions.ini"*/);
35  //$this->_loadActionsINIFile(DATAFACE_SITE_PATH."/actions.ini");
36  } else {
37  $this->actions =& $conf;
38  }
39 
40  }
41 
42 
43 
44  function _loadActionsINIFile(/*$path*/){
45 
46  import('Dataface/ConfigTool.php');
47  $configTool =& Dataface_ConfigTool::getInstance();
48  $actions =& $configTool->loadConfig('actions', null);
49  foreach ( array_keys($actions) as $key){
50  $action =& $actions[$key];
51  $action['name'] = $key;
52  if ( !isset($action['order']) ) $action['order'] = 0;
53  if ( !isset($action['id']) ) $action['id'] = $action['name'];
54  if ( !isset($action['label']) ) $action['label'] = str_replace('_',' ',ucfirst($action['name']));
55  if ( !isset($action['accessKey'])) $action['accessKey'] = substr($action['name'],0,1);
56  //if ( !isset($action['label_i18n']) ) $action['label_i18n'] = 'action:'.$action['name'].' label';
57  //if ( !isset($action['description_i18n'])) $action['description_i18n'] = 'action:'.$action['name'].' description';
58 
59  if ( isset($action['description']) ){
60  $action['description'] = df_translate('actions.'.$action['name'].'.description', $action['description']);
61  }
62  if ( isset($action['label']) ){
63  $action['label'] = df_translate('actions.'.$action['name'].'.label',$action['label']);
64  }
65 
66  $this->actions[$key] =& $action;
67  unset($action);
68  }
69  unset($temp);
70  $this->actions =& $actions;
71 
72  }
73 
75  import('Dataface/Table.php');
76  // Some actions are loaded from the table's actions.ini file and must be loaded before we return the actions.
77 
79  if ( !$table->_actionsLoaded ){
80  $params = array();
81  $table->getActions($params);
82  }
83  }
84 
92  function &getAction($params, $action=null){
95  if ( !isset($action) ){
96  if ( @$params['table'] ){
97  $this->_loadTableActions($params['table']);
98  unset($actions);
99  if ( !isset($this->tableActions[$params['table']]) ){
100  $this->tableActions[$params['table']] = array();
101  }
102  $actions =& $this->tableActions[$params['table']];
103 
104  }
105 
106  if ( !isset($params['name']) or !$params['name'] ){
107  throw new Exception("ActionTool::getAction() requires 'name' parameter to be specified.", E_USER_ERROR);
108  }
109  if ( !isset( $actions[$params['name']] ) ) {
110  $err = PEAR::raiseError(
112  "No action found", /* i18n id */
113  "No action found named '".$params['name']."'", /*default error message*/
114  array('name'=>$params['name']) /* i18n parameters */
115  )
116  );
117  return $err;
118  }
119 
120 
121  $action = $actions[$params['name']];
122  }
123 
124 
125  if ( isset($action['selected_condition']) ) {
126  $action['selected'] = $app->testCondition($action['selected_condition'], $params);
127  }
128 
129 
130  //if ( isset($action['visible']) and !$action['visible']) continue;
131  // Filter based on a condition
132  foreach (array_keys($action) as $attribute){
133  // Some entries may have variables that need to be evaluated. We use Dataface_Application::eval()
134  // to evaluate these entries. The eval method will replace variables such as $site_url, $site_href
135  // $dataface_url with the appropriate real values. Also if $params['record'] contains a
136  // Record object or a related record object its values are treated as php variables that can be
137  // replaced. For example if a Profile record has fields 'ProfileID' and 'ProfileName' with
138  // ProfileID=10 and ProfileName = 'John Smith', then:
139  // $app->parseString('ID is ${ProfileID} and Name is ${ProfileName}') === 'ID is 10 and Name is John Smith'
140  if ( preg_match('/condition/i',$attribute) ) continue;
141  if ( isset($action[$attribute.'_condition']) and !$app->testCondition($action[$attribute.'_condition'], $params) ){
142  $action[$attribute] = null;
143  } else {
144  $action[$attribute] = $app->parseString($action[$attribute], $params);
145  }
146  }
147  return $action;
148 
149  }
150 
160  function getActions($params=array(), $actions=null){
161  if ( !is_array($params) ){
162  trigger_error("In Dataface_ActionTool::getActions(), expected parameter to be an array but received a scalar: ".$params.".".Dataface_Error::printStackTrace(), E_USER_ERROR);
163  }
165 
166  $out = array();
167 
168  $tablename = null;
169  if ( isset($params['table']) ) $tablename = $params['table'];
170  if ( isset($params['record']) and is_a($params['record'], 'Dataface_Record') ) $tablename = $params['record']->_table->tablename;
171  else if ( isset($params['record']) and is_a($params['record'], 'Dataface_RelatedRecord')) $tablename = $params['record']->_record->_table->tablename;
172 
173  if ( isset( $params['record'] ) && is_a($params['record'], 'Dataface_Record') ){
174  // we have received a record as a parameter... we can infer the table information
175  $params['table'] = $params['record']->_table->tablename;
176  } else if ( isset($params['record']) && is_a($params['record'], 'Dataface_RelatedRecord') ){
177  // we have recieved a related record object... we can infer both the table and relationship information.
178  $temp =& $params['record']->getParent();
179  $params['table'] = $temp->_table->tablename;
180  unset($temp);
181 
182  $params['relationship'] = $params['record']->_relationshipName;
183  }
184 
185  if ( @$params['relationship']){
186  if ( strpos($params['relationship'], '.') !== false ){
187  // if the relationship is specified in the form 'Tablename.RElationshipname' parse it.
188  list($params['table'],$params['relationship']) = explode('.', $params['relationship']);
189  }
190  }
191 
192  if ( $tablename !== null ){
193  // Some actions are loaded from the table's actions.ini file and must be loaded before we return the actions.
195  if ( !$table->_actionsLoaded ){
196  $tparams = array();
197  $table->getActions($tparams, true);
198  }
199  unset($table);
200  }
201 
202 
203  if ( $actions === null ){
204  if ( @$params['table'] ){
205  if ( !isset($this->tableActions[$params['table']]) ){
206  $this->tableActions[$params['table']] = array();
207  }
208  $actions = $this->tableActions[$params['table']];
209  }
210  else $actions = $this->actions;
211  }
212  foreach ( array_keys($actions) as $key ){
213  if ( isset($action) ) unset($action);
214  $action = $actions[$key];
215  $action['atts'] = array();
216 
217  if ( @$params['name'] and @$params['name'] !== @$action['name']) continue;
218  if ( @$params['id'] and @$params['id'] !== @$action['id']) continue;
219 
220  if ( isset($params['category']) and $params['category'] !== @$action['category']) continue;
221  // make sure that the category matches
222 
223  if ( @$params['table'] /*&& @$action['table']*/ && !(@$action['table'] == @$params['table'] or @in_array(@$params['table'], @$action['table']) )) continue;
224  // Filter actions by table
225 
226  if ( @$params['relationship'] && @$action['relationship'] && @$action['relationship'] != @$params['relationship']) continue;
227  // Filter actions by relationship.
228 
229  if ( @$action['condition'] and !$app->testCondition($action['condition'], $params) ) {
230  continue;
231  }
232  if ( isset($params['record']) ){
233  if ( isset($action['permission']) and !$params['record']->checkPermission($action['permission']) ){
234  continue;
235  }
236  } else {
237  if ( isset( $action['permission'] ) and !$app->checkPermission($action['permission'])){
238  continue;
239  }
240  }
241 
242  if ( @$action['selected_condition'] ) $action['selected'] = $app->testCondition($action['selected_condition'], $params);
243  else {
244  $query = $app->getQuery();
245  if ( @$action['name'] == @$query['-action'] ) $action['selected'] = true;
246  }
247 
248  if ( isset($action['visible']) and !$action['visible']) continue;
249  // Filter based on a condition
250  foreach (array_keys($action) as $attribute){
251  // Some entries may have variables that need to be evaluated. We use Dataface_Application::eval()
252  // to evaluate these entries. The eval method will replace variables such as $site_url, $site_href
253  // $dataface_url with the appropriate real values. Also if $params['record'] contains a
254  // Record object or a related record object its values are treated as php variables that can be
255  // replaced. For example if a Profile record has fields 'ProfileID' and 'ProfileName' with
256  // ProfileID=10 and ProfileName = 'John Smith', then:
257  // $app->parseString('ID is ${ProfileID} and Name is ${ProfileName}') === 'ID is 10 and Name is John Smith'
258  //if ( strpos($attribute, 'condition') !== false) continue;
259  if ( preg_match('/condition/i',$attribute) ) continue;
260  if ( is_array($action[$attribute]) ) continue;
261  if ( isset($action[$attribute.'_condition']) and !$app->testCondition($action[$attribute.'_condition'], $params) ){
262 
263  $action[$attribute] = null;
264  } else {
265  $action[$attribute] = $app->parseString($action[$attribute], $params);
266  }
267  if ( strpos($attribute, 'atts:') === 0 ){
268  $attAtt = substr($attribute, 5);
269  if ( !preg_match('/_condition$/', $attAtt) ){
270  $action['atts'][$attAtt] = $action[$attribute];
271  }
272  }
273  }
274  $out[$key] =& $action;
275 
276  unset($action);
277  }
278 
279  uasort($out, array(&$this, '_compareActions'));
280  return $out;
281  }
282 
286  function _compareActions($a,$b){
287  if ( @$a['order'] < @$b['order'] ) return -1;
288  else return 1;
289  }
290 
296  function addAction($name, $action){
297  if ( @$action['table'] ){
298  $this->tableActions[$action['table']][$name] = $action;
299  $query = Dataface_Application::getInstance()->getQuery();
300  if ( $query['-table'] == $action['table'] ){
301  // Note: For some reason this needs to be passed by value
302  $this->actions[$name] = $this->tableActions[$action['table']][$name];
303  }
304  }
305  else{
306  $this->actions[$name] = $action;
307  }
308  }
309 
313  function removeAction($name){
314  $action = $this->getAction($name);
315  if ( @$action['table'] ){
316  unset($this->tableActions[$action['table']][$name]);
317  }
318  unset( $this->actions[$name] );
319  }
320 
325  public static function &getInstance($conf=null){
326  static $instance = 0;
327  if ( !$instance ){
328  $instance = new Dataface_ActionTool($conf);
329  }
330  return $instance;
331  }
332 
333 
334 }