Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
conditions_list.inc
1 <?php
17 define('SQ_KEYWORD_CONDITION_BEGIN', 'begin');
18 define('SQ_KEYWORD_CONDITION_END', 'end');
19 define('SQ_KEYWORD_CONDITION_ELSE', 'else');
20 
38 {
45  var $host_asset = NULL;
46 
64  var $conditions_attribute_name = '';
65 
66 
72  function Conditions_List(&$host_asset, $conditions_attribute_name='')
73  {
74  $this->setHost($host_asset, $conditions_attribute_name);
75 
76  }//end constructor
77 
78 
88  function setHost(&$host_asset, $conditions_attribute_name)
89  {
90  $this->host_asset = $host_asset;
91  $this->conditions_attribute_name = $conditions_attribute_name;
92 
93  return $this->isValid();
94 
95  }//end setHost()
96 
97 
111  function evaluate(&$asset_being_painted, $condition_names, &$logical_keywords, $boolean_values=Array(0, 1))
112  {
113  $ret = Array();
114 
115  if ($this->isValid()) {
116  $GLOBALS['SQ_SYSTEM']->am->includeAsset('condition');
117 
118  $conditions_info = $this->_getAllConditions();
119 
120  $condition_names = array_intersect($condition_names, array_keys($conditions_info));
121 
122  if (!empty($condition_names)) {
123  $current_user = $this->_getCurrentUser();
124 
125  foreach ($condition_names as $condition_name) {
126 
127  // TODO: should avoid this array_merge, potentialy heavy
128  $merge_result = array_merge($logical_keywords, $ret);
129  $this->_updateKeywordsInRestriction($merge_result, $conditions_info[$condition_name]);
130 
131  $ret[$condition_name] = $boolean_values[Condition::evaluateRestriction($asset_being_painted, $conditions_info[$condition_name]['restriction'], $conditions_info[$condition_name]['type_code']) ? 1 : 0];
132  }
133  }
134  }
135 
136  return $ret;
137 
138  }//end evaluate()
139 
140 
155  function _updateKeywordsInRestriction(&$logical_keywords, &$condition_parameters)
156  {
157  $type_code = $condition_parameters['type_code'];
158  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
159 
160  call_user_func_array(Array($type_code, 'updateKeywords'), Array($logical_keywords, &$condition_parameters['restriction']['condition_data']));
161 
162  }//end _updateKeywordsInRestriction()
163 
164 
172  {
173  $conditions_info = $this->_getAllConditions();
174 
175  return (array_keys($conditions_info));
176 
177  }//end getConditionsNames()
178 
179 
192  function getRequiredKeywords($condition_name)
193  {
194  $ret = Array();
195 
196  $conditions = $this->_getAllConditions();
197 
198  if (!empty($conditions[$condition_name])) {
199  $type_code = $conditions[$condition_name]['type_code'];
200  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type_code);
201  $ret = call_user_func(Array($type_code, 'getRequiredKeywords'), $conditions[$condition_name]['restriction']['condition_data']);
202  }
203 
204  return $ret;
205 
206  }//end getRequiredKeywords()
207 
208 
217  function _getConditionRestriction($condition_name)
218  {
219  return $this->_getConditionParameter($condition_name, 'restriction', Array());
220 
221  }//end _getConditionRestriction()
222 
223 
233  function &_getConditionAsset($condition_type, $edit_fns=FALSE)
234  {
235  // TODO: AM includeAsset() can't include edit_fns file. Must find a better solution for this
236  $included_file = SQ_SYSTEM_ROOT.'/'.$GLOBALS['SQ_SYSTEM']->am->_asset_types[$condition_type]['dir'].'/';
237  if ($edit_fns) $condition_type .= '_edit_fns';
238  $included_file .= $condition_type.'.inc';
239  require_once $included_file;
240 
241  $new_condition = new $condition_type();
242  return $new_condition;
243 
244  }//end _getConditionAsset()
245 
246 
261  function _getConditionParameter($condition_name, $key='', $default=Array())
262  {
263  $ret = Array();
264 
265  $conditions_parameters = $this->_getAllConditions();
266  if (!empty($conditions_parameters) && isset($conditions_parameters[$condition_name])) {
267  $ret = $conditions_parameters[$condition_name];
268  }
269 
270  if (!empty($ret) && !empty($key)) {
271  $ret = isset($ret[$key]) ? $ret[$key] : $default;
272  }
273 
274  return $ret;
275 
276  }//end _getConditionParameter()
277 
278 
285  function _getAllConditions()
286  {
287  $ret = Array();
288 
289  $host_asset = $this->_getHostAsset();
290  $ret = $host_asset->attr($this->conditions_attribute_name);
291 
292  return ($ret);
293 
294  }//end _getAllConditions()
295 
296 
303  function &_getHostAsset()
304  {
305  return $this->host_asset;
306 
307  }//end _getHostAsset()
308 
309 
316  function &_getCurrentUser()
317  {
318  $userid = $GLOBALS['SQ_SYSTEM']->currentUserId();
319  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($userid);
320  return $user;
321 
322  }//end _getCurrentUser()
323 
324 
332  function isValid()
333  {
334  return (!empty($this->host_asset) && !empty($this->conditions_attribute_name));
335 
336  }//end isValid()
337 
338 
339  // --- Screen Edit Functions ---
340 
341 
354  function paintConditionsList(&$asset, &$o, $prefix, $write_access, $allowed_keywords)
355  {
356  if ($this->isValid()) {
357  // 1. show all existing conditions
358  $conditions_parameters = $this->_getAllConditions();
359 
360  foreach ($conditions_parameters as $condition_name => $parameters) {
361  $condition_prefix = $prefix.$condition_name;
362 
363  $o->openSection(ucwords(str_replace('_', ' ', $parameters['type_code'])));
364 
365  // paint keyword name
366  $o->openField(translate('keyword_name'));
367  if ($write_access) {
368  text_box($condition_prefix.'_keyword_name', $condition_name, 30);
369  } else {
370  echo $condition_name;
371  }
372  $o->closeField();
373 
374  // let the condition paint itself
375  $condition_type = $parameters['type_code'];
376  $condition = $this->_getConditionAsset($condition_type);
377  $condition_edit_fns = $this->_getConditionAsset($condition_type, TRUE);
378 
379  // paint TRUE/FALSE bit
380  $o->openField('Keyword is true if: ');
381  $match_options = call_user_func(Array($condition_type, 'getMatchKeywords'), $condition_type);
382  if ($write_access) {
383  combo_box($condition_prefix.'_match', $match_options, FALSE, $parameters['restriction']['match']);
384  } else {
385  echo $match_options[$parameters['restriction']['match']];
386  }
387 
388  if (isset($parameters['restriction']) && empty($parameters['restriction']['condition_data'])) {
389  $parameters['restriction']['condition_data'] = Array();
390  }
391 
392  ob_start();
393  $condition_edit_fns->paintEditInterface($parameters['restriction']['condition_data'], $o, $condition_prefix, $write_access, $allowed_keywords);
394  $condition_interface = ob_get_contents();
395  ob_end_clean();
396 
397  if (!empty($condition_interface)) {
398  $o->openField(translate('options'));
399  echo $condition_interface;
400  $o->closeField();
401  }
402 
403  if ($write_access) {
404  $o->openField(translate('delete'));
405  check_box($condition_prefix.'_delete', $value='1', $checked=FALSE);
406  $o->closeField();
407  }
408 
409  unset($condition_edit_fns);
410  unset($condition);
411 
412  $o->closeSection();
413  }//end foreach
414 
415  // 2. show "add condition" box
416  if ($write_access) {
417  // get the list of all condition types
418  $options = Array('' => '- '.translate('select_condition_type').' -');
419 
420  $conditions_type_code = $GLOBALS['SQ_SYSTEM']->am->getTypeDescendants('condition', FALSE);
421  foreach ($conditions_type_code as $type_code) {
422  $options[$type_code] = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code, 'name');
423  }
424 
425  $o->openSection(translate('new_conditions'));
426 
427  $o->openField(translate('condition_type').':');
428  combo_box($prefix.'_new_condition_type', $options, FALSE, '');
429  $o->closeField();
430 
431  $o->closeSection();
432 
433  }
434  }//end if is valid
435 
436  return TRUE;
437 
438  }//end paintConditionsList()
439 
440 
453  function processConditionsList(&$asset, &$o, $prefix, $write_access, $allowed_keywords)
454  {
455  $ret = TRUE;
456 
457  if ($this->isValid()) {
458  $conditions_parameters = $this->_getAllConditions();
459  $conditions_parameters_new = Array();
460 
461  // 1. update the existing condition
462  foreach ($conditions_parameters as $condition_name => $parameters) {
463  $condition_prefix = $prefix.$condition_name;
464 
465  if (empty($_REQUEST[$condition_prefix.'_delete']) || $_REQUEST[$condition_prefix.'_delete'] != 1) {
466  // not deleted
467  $new_name = trim($_REQUEST[$condition_prefix.'_keyword_name']);
468  if (preg_match('/^('.SQ_KEYWORD_CONDITION_BEGIN.'|'.SQ_KEYWORD_CONDITION_END.'|'.SQ_KEYWORD_CONDITION_ELSE.')_(.*)$/', $new_name)) {
469  trigger_error('Incorrect keyword name: keyword names cannot begin with "begin_", "end_" or "else_" ('.$new_name.').', E_USER_WARNING);
470  $new_name = $condition_name;
471  }
472  $new_name = $this->_generateUniqueName($new_name, $conditions_parameters_new);
473 
474  // let the condition process itself
475  $condition_type = $parameters['type_code'];
476  $condition = $this->_getConditionAsset($condition_type);
477  $condition_edit_fns = $this->_getConditionAsset($condition_type, TRUE);
478  $conditions_parameters_new[$new_name]['restriction']['condition_data'] = $condition_edit_fns->processEditInterface($o, $condition_prefix, $allowed_keywords);
479  $conditions_parameters_new[$new_name]['type_code'] = $condition_type;
480  $conditions_parameters_new[$new_name]['restriction']['match'] = array_get_index($_POST, $condition_prefix.'_match', TRUE);
481  }
482  }
483 
484  // 2. add a new condition
485  if (!empty($_REQUEST[$prefix.'_new_condition_type'])) {
486  $new_condition_type = $_REQUEST[$prefix.'_new_condition_type'];
487 
488  // find a new name
489  $conditions_parameters_new[$this->_generateUniqueName('', $conditions_parameters_new)] = Array('type_code' => $new_condition_type, 'restriction' => Array('match' => 1, 'condition_data' => Array()));
490  }
491 
492  if ($ret) {
493  $asset->setAttrValue($this->conditions_attribute_name, $conditions_parameters_new);
494  $asset->saveAttributes();
495  }
496 
497  }//end if is valid
498 
499  return $ret;
500 
501  }//end processConditionsList()
502 
503 
514  function _generateUniqueName($desired_name, &$reserved_names)
515  {
516  $desired_name = trim($desired_name);
517  $desired_name = preg_replace('/ /' , '_',$desired_name);
518  if (empty($desired_name)) {
519  $desired_name = 'condition_1';
520  }
521 
522  $ret = $desired_name;
523 
524  if (!empty($reserved_names[$ret])) {
525  $desired_name = preg_replace('/_\d*^/' , '', $desired_name);
526  for ($i = 2; $i < 1000; $i++) {
527  $ret = $desired_name.'_'.$i;
528  if (empty($reserved_names[$ret])) break;
529  }
530  }
531 
532  return $ret;
533 
534  }//end _generateUniqueName()
535 
536 
537 }//end class
538 
539 ?>