Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_attribute_value.inc
1 <?php
17 require_once dirname(dirname(__FILE__)).'/trigger_condition_asset_type/trigger_condition_asset_type.inc';
18 
32 {
33 
40  protected static function getOperatorOptions()
41  {
42  return Array(
43  '=' => strtolower(translate('equals')),
44  '!=' => strtolower(translate('does_not_equal')),
45  );
46 
47  }//end getOperatorOptions()
48 
49 
67  public static function evaluate($settings, &$state)
68  {
69  if (empty($settings['asset_type'])) return FALSE;
70  if (empty($settings['attribute'])) return FALSE;
71 
72  if ((!$settings['inherit']) && (array_get_index($state, 'asset_type') != $settings['asset_type'])) {
73  // definitely not the right type
74  return FALSE;
75  }
76 
77  if (empty($state['asset'])) {
78  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
79  }
80 
81  if (!isset($state['asset_type'])) {
82  $state['asset_type'] = $state['asset']->type();
83  }
84 
85  $value = $settings['value'];
86 
87  if ($settings['inherit']) {
88  if ($state['asset'] instanceof $settings['asset_type']) {
89  return ($state['asset']->attr($settings['attribute']) == $value) == (array_get_index($settings, 'operator', '=') == '=');
90  }
91  } else {
92  if ($state['asset_type'] == $settings['asset_type']) {
93  return ($state['asset']->attr($settings['attribute']) == $value) == (array_get_index($settings, 'operator', '=') == '=');
94  }
95  }
96  return FALSE;
97 
98  }//end evaluate()
99 
100 
111  public static function getInterface($settings, $prefix, $write_access=FALSE)
112  {
113  $selected_type = array_get_index($settings, 'asset_type', 'page');
114  $am = $GLOBALS['SQ_SYSTEM']->am;
115 
116  // asset type selector
117  ob_start();
118  if ($write_access) {
119  asset_type_chooser($prefix.'[type_code]', FALSE, Array('type_code' => $selected_type), NULL, FALSE, FALSE);
120  echo ' ( ';
121  check_box($prefix.'[inherit]', 1, array_get_index($settings, 'inherit'), '', 'id="'.$prefix.'_inherit" style="margin: 0px"');
122  echo ' ';
123  label(translate('or_inherited_types'), $prefix.'_inherit');
124  echo ') ';
125  } else {
126  echo '<b>'.$selected_type.'</b>';
127  if (array_get_index($settings, 'inherit')) {
128  echo ' '.translate('or_inherited_types').' ';
129  }
130  }
131  $asset_type_component = ob_get_contents();
132  ob_end_clean();
133 
134  // attribute selector
135  ob_start();
136  $attribute = array_get_index($settings, 'attribute', '');
137  if ($selected_type == '') {
138  echo '<b>['.translate('asset_type_not_selected').']</b>';
139  } else {
140  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($selected_type, Array('name'));
141  if (empty($attrs)) {
142  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
143  } else {
144  if ($write_access) {
145  $attr_options = Array('' => '');
146  foreach ($attrs as $attr_name) {
147  $attr_options[$attr_name] = $attr_name;
148  }
149  combo_box($prefix.'[attribute]', $attr_options, FALSE, $attribute);
150  } else {
151  echo '<b>'.$attribute.'</b>';
152  }
153  }
154  }
155  $attribute_component = ob_get_contents();
156  ob_end_clean();
157 
158  // operator selector
159  ob_start();
161  $operator = array_get_index($settings, 'operator', '=');
162  if ($write_access) {
163  combo_box($prefix.'[operator]', $operator_options, FALSE, $operator);
164  } else {
165  echo '<b>'.$operator_options[$operator].'</b>';
166  }
167  $operator_component = ob_get_contents();
168  ob_end_clean();
169 
170 
171  // value selector
172  ob_start();
173  $value = array_get_index($settings, 'text_value', '');
174  if ($write_access) {
175  echo '<br />';
176  text_area($prefix.'[value]', $value, 80, 5);
177  } else {
178  echo '<b>'.$value.'</b>';
179  }
180  $value_component = ob_get_contents();
181  ob_end_clean();
182 
183  return translate('trigger_attribute_value', $asset_type_component, $attribute_component, $operator_component, $value_component);
184 
185  }//end getInterface()
186 
187 
198  public static function processInterface(&$settings, $request_data)
199  {
200  if (empty($request_data)) {
201  return translate('trigger_input_data_error');
202  }
203 
204  $asset_type = array_get_index($request_data, 'type_code', FALSE);
205  if (!$asset_type) {
206  return translate('trigger_type_code_not_supplied');
207  }
208 
209  $settings['asset_type'] = $asset_type;
210  $settings['inherit'] = !empty($request_data['inherit']);
211  $settings['attribute'] = array_get_index($request_data, 'attribute', FALSE);
212  $settings['operator'] = array_get_index($request_data, 'operator', '=');
213 
214  $value = array_get_index($request_data, 'value', FALSE);
215  $settings['text_value'] = $value;
216 
217  // handle serialised attributes
218  if (!empty($settings['asset_type']) && !empty($settings['attribute'])) {
219  $GLOBALS['SQ_SYSTEM']->am->includeAsset($settings['asset_type']);
220  $fake_asset = new $settings['asset_type'];
221  $attr = $fake_asset->getAttribute($settings['attribute'], TRUE);
222  if (!is_null($attr)) {
223  if ($attr instanceof Asset_Attribute_Serialise) {
224  $value = str_replace(';', '', $value); // avoid code injection
225  eval('$value = '.$value.';');
226  }
227  }
228  }
229  $settings['value'] = $value;
230 
231  return FALSE;
232 
233  }//end processInterface()
234 
235 
236 }//end class
237 
238 ?>