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';
55 public static function execute($settings, &$state)
59 if (empty($settings[
'attribute']))
return FALSE;
61 if (empty($state[
'asset'])) {
63 if (empty($state[
'assetid'])) {
66 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
70 if (is_null($state[
'asset']))
return FALSE;
73 $parameter_map_value = array_get_index($settings,
'value_map', serialize(Array()));
75 $param_map_value = $atr_parameter_map->getParameterValue(
'attribute_value');
79 if (!empty($param_map_value)) {
80 $value = $param_map_value;
82 $value = $settings[
'value'];
87 if (is_scalar($value)) {
88 $attr = $state[
'asset']->getAttribute($settings[
'attribute']);
89 if (is_null($attr))
return FALSE;
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)) {
98 eval(
'$value = '.$value.
';');
100 trigger_localised_error(
'TRIG0003', E_USER_WARNING);
107 if (array_get_index($settings,
'replace_keywords_in_set_value', 0)) {
109 $old_current_asset = $GLOBALS[
'SQ_SYSTEM']->getGlobalDefine(
'CURRENT_ASSET', NULL);
110 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $state[
'asset']);
112 replace_global_keywords($value);
114 if (is_null($old_current_asset)) {
115 $GLOBALS[
'SQ_SYSTEM']->unsetGlobalDefine(
'CURRENT_ASSET');
117 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $old_current_asset);
121 $keywords = retrieve_keywords_replacements($value);
122 $replacements = Array();
123 foreach ($keywords as $keyword) {
124 $replacements[$keyword] = $state[
'asset']->getKeywordReplacement($keyword);
126 replace_keywords($value, $replacements);
132 $current_value = $state[
'asset']->attr($settings[
'attribute']);
133 if ($current_value != $value) {
134 if (!$state[
'asset']->
setAttrValue($settings[
'attribute'], $value)) {
142 'assetid' => $state[
'asset']->
id,
143 'attribute' => $settings[
'attribute'],
144 'old_value' => $current_value,
145 'new_value' => $state[
'asset']->
attr($settings[
'attribute']),
161 private static function _safeToEval($str)
164 $allowed_expressions = Array(
'array',
'(',
')',
'=>',
',',
';');
165 $reduced_value = str_replace($allowed_expressions,
'', strtolower($str));
168 $reduced_value = preg_replace(
'/\d/',
'', $reduced_value);
170 $reduced_value = trim($reduced_value);
171 $reduced_value = preg_replace(
'/(^|[^[:alnum:]])(TRUE|FALSE|NULL)([^[:alnum:]]|$)/i',
'$1$3', $reduced_value);
173 $reduced_value = preg_replace(
'/\\\\"/',
'', $reduced_value);
175 $reduced_value = preg_replace(
"/\\\\'/",
'', $reduced_value);
177 $reduced_value = preg_replace(
'/"([^"]*)"/',
'', $reduced_value);
179 $reduced_value = preg_replace(
"/'([^']*)'/",
'', $reduced_value);
180 $reduced_value = trim($reduced_value);
182 return empty($reduced_value);
197 public static function getInterface($settings, $prefix, $write_access=FALSE)
201 $selected_type = array_get_index($settings,
'asset_type',
'');
204 asset_type_chooser($prefix.
'[asset_type]', FALSE, Array($selected_type), TRUE);
206 echo
'<b>'.$selected_type.
'</b>';
209 $basic_part_1 = ob_get_contents();
213 if ($selected_type ==
'') {
214 echo
'<b>['.translate(
'asset_type_not_selected').
']</b';
216 $attribute = array_get_index($settings,
'attribute',
'');
217 $attrs = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($selected_type, Array(
'name'));
219 echo
'<b>['.translate(
'asset_type_no_attributes_found').
']</b>';
222 $attr_options = Array(
'' =>
'');
223 foreach ($attrs as $attr_name) {
224 $attr_options[$attr_name] = $attr_name;
226 combo_box($prefix.
'[attribute]', $attr_options, FALSE, $attribute);
229 echo
'<b>'.$attribute.
'</b>';
234 $basic_part_2 = ob_get_contents();
238 $value = array_get_index($settings,
'value',
'');
240 text_area($prefix.
'[value]', $value, 80, 5);
242 echo
'<b>'.$value.
'</b>';
246 $new_prefix = str_replace(array(
'[',
']'),
'_', $prefix);
247 hidden_field($prefix.
'[new_prefix]', $new_prefix);
249 $parameter_map_value = array_get_index($settings,
'value_map', serialize(Array()));
252 $atr_parameter_map->setParameter(
'attribute_value',
'Attribute Value');
253 echo $atr_parameter_map->paint($new_prefix.
'_parameter_map');
256 $basic_part_3 = ob_get_contents();
259 $output = translate(
'trigger_set_attribute_value_basic', $basic_part_1, $basic_part_2, $basic_part_3);
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>';
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();
273 $output .= $basic_part_4;
293 $type_code = array_get_index($request_data,
'asset_type', FALSE);
295 return translate(
'asset_type_not_specified');
297 $settings[
'asset_type'] = $type_code;
299 $settings[
'attribute'] = array_get_index($request_data,
'attribute',
'');
300 $settings[
'value'] = array_get_index($request_data,
'value',
'');
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)) {
308 $settings[
'attribute'] =
'';
311 $new_prefix = str_replace(array(
'[',
']'),
'_', array_get_index($request_data,
'new_prefix',
''));
314 $atr_parameter_map->process($new_prefix.
'_parameter_map');
315 $settings[
'value_map'] = $atr_parameter_map->value;
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'] =
'';
331 $settings[
'replace_keywords_in_set_value'] = array_get_index($request_data,
'replace_keywords_in_set_value', 0);
347 public static function getLocks($settings, &$state)
349 return Array($state[
'assetid'] => Array(
'attributes'));