Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_attribute_value.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
19 
32 {
33 
34 
55  public static function execute($settings, &$state)
56  {
57 
58  // check required settings
59  if (empty($settings['attribute'])) return FALSE;
60 
61  if (empty($state['asset'])) {
62  // grab the asset if assetid is given, but not the asset.
63  if (empty($state['assetid'])) {
64  return FALSE;
65  } else {
66  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
67  }
68  }
69 
70  if (is_null($state['asset'])) return FALSE;
71 
72 
73  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
74  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
75  $param_map_value = $atr_parameter_map->getParameterValue('attribute_value');
76 
77 
78  // settings contains the 'fallback' static value.
79  if (!empty($param_map_value)) {
80  $value = $param_map_value;
81  } else {
82  $value = $settings['value'];
83  }
84 
85 
86  // handle serialised attributes
87  if (is_scalar($value)) {
88  $attr = $state['asset']->getAttribute($settings['attribute']);
89  if (is_null($attr)) return FALSE;
90 
91  switch (get_class($attr)) {
92  case 'Asset_Attribute_Serialise' :
93  case 'Asset_Attribute_Parameter_Map' :
94  case 'Asset_Attribute_Email_Format' :
95  if (Trigger_Action_Set_Attribute_Value::_safeToEval($value)) {
96  // set $value to be the contents of the array
97 
98  eval('$value = '.$value.';');
99  } else {
100  trigger_localised_error('TRIG0003', E_USER_WARNING);
101  return FALSE;
102  }
103  break;
104  }
105 
106  //if the replace keywords checkbox is checked, do keyword replacement here before setting the value to attribute
107  if (array_get_index($settings, 'replace_keywords_in_set_value', 0)) {
108  // replace global keywords
109  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
110  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
111 
112  replace_global_keywords($value);
113 
114  if (is_null($old_current_asset)) {
115  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
116  } else {
117  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
118  }
119 
120  // replace broadcasting asset keywords
121  $keywords = retrieve_keywords_replacements($value);
122  $replacements = Array();
123  foreach ($keywords as $keyword) {
124  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
125  }
126  replace_keywords($value, $replacements);
127  }
128 
129  }
130 
131  // dont change anything if the attribute already has the required value
132  $current_value = $state['asset']->attr($settings['attribute']);
133  if ($current_value != $value) {
134  if (!$state['asset']->setAttrValue($settings['attribute'], $value)) {
135 
136  return FALSE;
137  }
138  if (!$state['asset']->saveAttributes()) return FALSE;
139  }
140 
141  return Array(
142  'assetid' => $state['asset']->id,
143  'attribute' => $settings['attribute'],
144  'old_value' => $current_value,
145  'new_value' => $state['asset']->attr($settings['attribute']),
146  );
147 
148  }//end execute()
149 
150 
161  private static function _safeToEval($str)
162  {
163  // first check the value contains only allowable components
164  $allowed_expressions = Array('array', '(', ')', '=>', ',', ';');
165  $reduced_value = str_replace($allowed_expressions, '', strtolower($str));
166 
167  // digits are ok
168  $reduced_value = preg_replace('/\d/', '', $reduced_value);
169  // check for TRUE/FALSE/NULL (dependent on no white space, so trim() the string first)
170  $reduced_value = trim($reduced_value);
171  $reduced_value = preg_replace('/(^|[^[:alnum:]])(TRUE|FALSE|NULL)([^[:alnum:]]|$)/i', '$1$3', $reduced_value);
172  // escaped double-quoted strings are ok
173  $reduced_value = preg_replace('/\\\\"/', '', $reduced_value);
174  // escaped single-quoted strings are ok
175  $reduced_value = preg_replace("/\\\\'/", '', $reduced_value);
176  // double-quoted strings are ok
177  $reduced_value = preg_replace('/"([^"]*)"/', '', $reduced_value);
178  // single-quoted strings are ok
179  $reduced_value = preg_replace("/'([^']*)'/", '', $reduced_value);
180  $reduced_value = trim($reduced_value);
181 
182  return empty($reduced_value);
183 
184  }//end _safeToEval()
185 
186 
197  public static function getInterface($settings, $prefix, $write_access=FALSE)
198  {
199  // begin buffering basic options
200 
201  $selected_type = array_get_index($settings, 'asset_type', '');
202  ob_start();
203  if ($write_access) {
204  asset_type_chooser($prefix.'[asset_type]', FALSE, Array($selected_type), TRUE);
205  } else {
206  echo '<b>'.$selected_type.'</b>';
207  }
208 
209  $basic_part_1 = ob_get_contents();
210  ob_end_clean();
211 
212  ob_start();
213  if ($selected_type == '') {
214  echo '<b>['.translate('asset_type_not_selected').']</b';
215  } else {
216  $attribute = array_get_index($settings, 'attribute', '');
217  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($selected_type, Array('name'));
218  if (empty($attrs)) {
219  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
220  } else {
221  if ($write_access) {
222  $attr_options = Array('' => '');
223  foreach ($attrs as $attr_name) {
224  $attr_options[$attr_name] = $attr_name;
225  }
226  combo_box($prefix.'[attribute]', $attr_options, FALSE, $attribute);
227 
228  } else {
229  echo '<b>'.$attribute.'</b>';
230  }
231  }
232  }
233 
234  $basic_part_2 = ob_get_contents();
235  ob_end_clean();
236 
237  ob_start();
238  $value = array_get_index($settings, 'value', '');
239  if ($write_access) {
240  text_area($prefix.'[value]', $value, 80, 5);
241  } else {
242  echo '<b>'.$value.'</b>';
243  }
244 
245  echo '<div>';
246  $new_prefix = str_replace(array('[',']'), '_', $prefix);
247  hidden_field($prefix.'[new_prefix]', $new_prefix);
248 
249  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
250  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
251 
252  $atr_parameter_map->setParameter('attribute_value', 'Attribute Value');
253  echo $atr_parameter_map->paint($new_prefix.'_parameter_map');
254  echo '</div>';
255 
256  $basic_part_3 = ob_get_contents();
257  ob_end_clean();
258 
259  $output = translate('trigger_set_attribute_value_basic', $basic_part_1, $basic_part_2, $basic_part_3);
260 
261  if (!empty($selected_type) && isset($attrs[$attribute])) {
262  if (in_array($attrs[$attribute], Array('serialise', 'parameter_map'))) {
263  $output .= '<br /><i>'.translate('trigger_set_attribute_value_serialise_note').'</i>';
264  }
265  }
266 
267  // Replace keywords checkbox
268  ob_start();
269  check_box($prefix.'[replace_keywords_in_set_value]', '1', array_get_index($settings, 'replace_keywords_in_set_value', 0), NULL, ($write_access? '' : 'disabled="disabled"'));
270  label('Replace keywords in the set value', $prefix.'[replace_keywords_in_set_value]');
271  $basic_part_4 = '<br />'.ob_get_clean();
272 
273  $output .= $basic_part_4;
274 
275  return $output;
276 
277  }//end getInterface()
278 
279 
291  public static function processInterface(&$settings, $request_data)
292  {
293  $type_code = array_get_index($request_data, 'asset_type', FALSE);
294  if (!$type_code) {
295  return translate('asset_type_not_specified');
296  }
297  $settings['asset_type'] = $type_code;
298 
299  $settings['attribute'] = array_get_index($request_data, 'attribute', '');
300  $settings['value'] = array_get_index($request_data, 'value', '');
301 
302  if (!empty($settings['attribute']) && !empty($settings['asset_type'])) {
303  $GLOBALS['SQ_SYSTEM']->am->includeAsset($settings['asset_type']);
304  $dummy_asset = new $settings['asset_type'];
305  $attr = $dummy_asset->getAttribute($settings['attribute']);
306  if (is_null($attr)) {
307  // selected asset type does not have an attribute with the selected name, so reset
308  $settings['attribute'] = '';
309  }
310 
311  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
312  if ($new_prefix) {
313  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
314  $atr_parameter_map->process($new_prefix.'_parameter_map');
315  $settings['value_map'] = $atr_parameter_map->value;
316  }
317 
318 
319  switch (get_class($attr)) {
320  case 'Asset_Attribute_Serialise' :
321  case 'Asset_Attribute_Parameter_Map' :
322  case 'Asset_Attribute_Email_Format' :
323  if (!Trigger_Action_Set_Attribute_Value::_safeToEval($settings['value'])) {
324  trigger_localised_error('TRIG0002', E_USER_NOTICE);
325  $settings['value'] = '';
326  }
327  break;
328  }
329  }
330 
331  $settings['replace_keywords_in_set_value'] = array_get_index($request_data, 'replace_keywords_in_set_value', 0);
332 
333  return FALSE;
334 
335  }//end processInterface()
336 
337 
347  public static function getLocks($settings, &$state)
348  {
349  return Array($state['assetid'] => Array('attributes'));
350 
351  }//end getLocks()
352 
353 
354 }//end class
355 
356 ?>