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';
54 public static function execute($settings, &$state)
56 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
59 if (empty($settings[
'fieldid']))
return FALSE;
64 if (array_key_exists(
'contextid', $settings) === FALSE) {
65 $settings[
'contextid'] = NULL;
68 if ($settings[
'contextid'] === NULL) {
69 $settings[
'contextid'] = $GLOBALS[
'SQ_SYSTEM']->getContextId();
72 $all_contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
73 if (array_key_exists($settings[
'contextid'], $all_contexts) === FALSE) {
77 $GLOBALS[
'SQ_SYSTEM']->changeContext($settings[
'contextid']);
79 if (empty($state[
'asset'])) {
81 if (empty($state[
'assetid'])) {
82 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
85 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
90 $asset = $state[
'asset'];
93 $success = $mm->isMetadataFieldAssetid($settings[
'fieldid']);
95 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
99 $old_current_asset = $GLOBALS[
'SQ_SYSTEM']->getGlobalDefine(
'CURRENT_ASSET', NULL);
100 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $asset);
102 $parameter_map_value = array_get_index($settings,
'value_map', serialize(Array()));
104 $param_map_value = $atr_parameter_map->getParameterValue(
'metadata_value');
107 if (!empty($param_map_value)) {
108 $value = $param_map_value;
116 require_once SQ_CORE_PACKAGE_PATH.
'/metadata/metadata_field/metadata_field.inc';
119 if (!isset($settings[
'value'])) {
122 $value = $settings[
'value'];
126 $metadata = $mm->getMetadata($asset->id);
130 if (isset($metadata[$settings[
'fieldid']])) {
131 $field_name = $metadata[$settings[
'fieldid']][0][
'name'];
135 if (array_get_index($settings,
'replace_keywords_in_set_value', 0)) {
137 replace_global_keywords($value);
140 $keywords = retrieve_keywords_replacements($value);
141 $replacements = Array();
142 foreach ($keywords as $keyword) {
143 $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
145 replace_keywords($value, $replacements);
149 $metadata[$settings[
'fieldid']] = Array(
151 'name' => $field_name,
155 $success = $mm->setMetadata($asset->id, $metadata);
159 $field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($settings[
'fieldid']);
163 $mm->generateContentFile($asset->id);
165 $all_contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
166 foreach ($all_contexts as $contextid => $context_data) {
167 $mm->generateContentFile($asset->id, FALSE, $contextid);
172 $GLOBALS[
'SQ_SYSTEM']->restoreContext();
174 if (is_null($old_current_asset)) {
175 $GLOBALS[
'SQ_SYSTEM']->unsetGlobalDefine(
'CURRENT_ASSET');
177 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $old_current_asset);
183 'fieldid' => $settings[
'fieldid'],
184 'field_name' => $field_name,
186 'contextid' => $settings[
'contextid'],
205 public static function getInterface($settings, $prefix, $write_access=FALSE)
208 $metadata_fieldid = array_get_index($settings,
'fieldid', 0);
209 $metadata_field_value = array_get_index($settings,
'value', NULL);
210 $metadata_field = NULL;
212 if ($metadata_fieldid) {
213 $metadata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($metadata_fieldid);
218 echo
'Choose Metadata Field ';
219 asset_finder($prefix.
'[fieldid]', $metadata_fieldid, Array(
'metadata_field' =>
'D'));
221 if ($metadata_fieldid && $metadata_field) {
222 echo get_asset_tag_line($metadata_fieldid);
223 }
else if ($metadata_fieldid && !$metadata_field) {
224 echo
'<span class="sq-backend-warning">Unknown asset (Id: #'.$metadata_fieldid.
')</span>';
226 echo
'<b>No metadata field specified</b>';
229 $output_part1 = ob_get_contents();
231 $output .= $output_part1;
235 if ($write_access && $metadata_field) {
236 echo
'<br>Set the value for this field<br>';
238 $edit_fns = $metadata_field->getEditFns();
239 $edit_fns->paintValueInterface($metadata_field, $GLOBALS[
'SQ_SYSTEM']->backend->out, $metadata_field_value, TRUE, TRUE, FALSE);
240 hidden_field($prefix.
'[form_printed]', 1);
243 $new_prefix = str_replace(array(
'[',
']'),
'_', $prefix);
244 hidden_field($prefix.
'[new_prefix]', $new_prefix);
246 $parameter_map_value = array_get_index($settings,
'value_map', serialize(Array()));
249 $atr_parameter_map->setParameter(
'metadata_value',
'Metadata Field Value');
250 echo $atr_parameter_map->paint($new_prefix.
'_parameter_map');
253 $output_part2 = ob_get_contents();
255 $output .= $output_part2;
259 check_box($prefix.
'[replace_keywords_in_set_value]',
'1', array_get_index($settings,
'replace_keywords_in_set_value', 0), NULL, ($write_access?
'' :
'disabled="disabled"'));
260 label(
'Replace keywords in the set value', $prefix.
'[replace_keywords_in_set_value]');
261 $output_part3 =
'<br />'.ob_get_clean();
263 $output .= $output_part3;
266 $current_context = array_get_index($settings,
'contextid', NULL);
268 $contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
269 $context_list = Array(
270 '-default-' => translate(
'trigger_use_currently_active_context'),
271 '-sep-' =>
'----------------------',
273 foreach ($contexts as $context_item_id => $context_item) {
274 if ((
int)$current_context === (
int)$context_item_id) {
275 $context_name = $context_item[
'name'];
278 $context_list[$context_item_id] = $context_item[
'name'];
281 $default = Array(
'-default-');
282 if (array_key_exists($current_context, $contexts) === TRUE) {
283 $default = Array($current_context);
286 if ($metadata_field) {
287 label(translate(
'trigger_set_value_in_this_context'), $prefix.
'[context_to_select]');
290 combo_box($prefix.
'[context_to_select]', $context_list, FALSE, $default);
292 if ($current_context === NULL) {
293 echo translate(
'trigger_use_currently_active_context');
294 }
else if (array_key_exists($current_context, $contexts) === TRUE) {
295 echo $contexts[$current_context][
'name'];
297 echo
'<span class="sq-backend-warning">'.translate(
'trigger_context_deleted_fail_until_changed').
'</span>';
301 $output_context_select = ob_get_clean();
302 $output .=
'<br />'.$output_context_select;
323 $fieldid_data = array_get_index($request_data,
'fieldid', 0);
324 $form_printed = array_get_index($request_data,
'form_printed', 0);
326 $fieldid = $fieldid_data[
'assetid'];
328 if (!$GLOBALS[
'SQ_SYSTEM']->am->assetExists($fieldid)) {
329 return translate(
'selected_asset_does_not_exist');
333 $metadata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($fieldid);
338 return 'The input ID is not a Metadata Field ID';
343 if (!$metadata_field->attr(
'editable')) {
344 trigger_localised_error(
'CORE0246', E_USER_WARNING, $metadata_field->name, $metadata_field->id);
349 $settings[
'fieldid'] = $fieldid;
351 $edit_fns = $metadata_field->getEditFns();
355 $field_cascade_value=FALSE;
356 $edit_fns->processInlineValueInterface($metadata_field, $result, $field_cascade_value);
359 $settings[
'field_name'] = $metadata_field->name;
360 $settings[
'value'] = $result;
362 $new_prefix = str_replace(array(
'[',
']'),
'_', array_get_index($request_data,
'new_prefix',
''));
365 $atr_parameter_map->process($new_prefix.
'_parameter_map');
366 $settings[
'value_map'] = $atr_parameter_map->value;
369 $new_context = array_get_index($request_data,
'context_to_select', 0);
370 if (is_numeric($new_context) === TRUE) {
371 $new_context = (int)$new_context;
372 $contexts = $GLOBALS[
'SQ_SYSTEM']->getAllContexts();
373 if (array_key_exists($new_context, $contexts) === TRUE) {
374 $settings[
'contextid'] = $new_context;
376 trigger_localised_error(
'TRIG0005', E_USER_WARNING, $new_context);
378 }
else if ($new_context ===
'-default-') {
379 $settings[
'contextid'] = NULL;
383 $settings[
'replace_keywords_in_set_value'] = array_get_index($request_data,
'replace_keywords_in_set_value', 0);
399 public static function getLocks($settings, &$state)
401 return Array($state[
'assetid'] => Array(
'all'));