Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_metadata_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 
31 {
32 
33 
54  public static function execute($settings, &$state)
55  {
56  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
57 
58  // check required settings
59  if (empty($settings['fieldid'])) return FALSE;
60  // do not return false already from here incase we are using parameter map for gettign the value
61 
62  // If no context ID is set at ALL, then presume it's an old trigger, and
63  // that we use the current context instead.
64  if (array_key_exists('contextid', $settings) === FALSE) {
65  $settings['contextid'] = NULL;
66  }
67 
68  if ($settings['contextid'] === NULL) {
69  $settings['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
70  }
71 
72  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
73  if (array_key_exists($settings['contextid'], $all_contexts) === FALSE) {
74  return FALSE;
75  }
76 
77  $GLOBALS['SQ_SYSTEM']->changeContext($settings['contextid']);
78 
79  if (empty($state['asset'])) {
80  // grab the asset if assetid is given, but not the asset.
81  if (empty($state['assetid'])) {
82  $GLOBALS['SQ_SYSTEM']->restoreContext();
83  return FALSE;
84  } else {
85  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
86  }
87  }
88 
89  // get the asset
90  $asset = $state['asset'];
91 
92  // verify the fieldid
93  $success = $mm->isMetadataFieldAssetid($settings['fieldid']);
94  if (!$success) {
95  $GLOBALS['SQ_SYSTEM']->restoreContext();
96  return FALSE;
97  }
98 
99  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
100  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $asset);
101 
102  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
103  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
104  $param_map_value = $atr_parameter_map->getParameterValue('metadata_value');
105 
106  // settings contains the 'fallback' static value.
107  if (!empty($param_map_value)) {
108  $value = $param_map_value;
109  // Fix for #3803 set metadata trigger not escaping "=" correctly. Moved following
110  // code from outside the IF block into. We will not need to escaps the "=" if we
111  // fetching value from DB. "=" are already escapped in there.
112  // use this only for param map values.
113  //
114  // Sanitise the value for equal signs (=)
115  // As per Bug #2951 HTML Submitted becomes malformed due to a simple equals sign
116  require_once SQ_CORE_PACKAGE_PATH.'/metadata/metadata_field/metadata_field.inc';
117  $value = Metadata_Field::encodeValueString($value, Array());
118  } else {
119  if (!isset($settings['value'])) {
120  return FALSE;
121  }
122  $value = $settings['value'];
123  }
124 
125  // get the current metadata values
126  $metadata = $mm->getMetadata($asset->id);
127 
128  // get the field name
129  $field_name = '';
130  if (isset($metadata[$settings['fieldid']])) {
131  $field_name = $metadata[$settings['fieldid']][0]['name'];
132  }
133 
134  //if the replace keywords checkbox is checked, do keyword replacement here before setting the value to metadata field
135  if (array_get_index($settings, 'replace_keywords_in_set_value', 0)) {
136  // replace global keywords
137  replace_global_keywords($value);
138 
139  // replace broadcasting asset keywords
140  $keywords = retrieve_keywords_replacements($value);
141  $replacements = Array();
142  foreach ($keywords as $keyword) {
143  $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
144  }
145  replace_keywords($value, $replacements);
146  }
147 
148  // set the new value
149  $metadata[$settings['fieldid']] = Array(
150  0 => Array (
151  'name' => $field_name,
152  'value' => $value,
153  ),
154  );
155  $success = $mm->setMetadata($asset->id, $metadata);
156 
157  // Regenerate Metadata File
158  if ($success) {
159  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['fieldid']);
160  // If the set field is contextable and is not a select field, regenerate the metadata file in the current context only;
161  // otherwise, regenerate all metadata files
162  if ($field->attr('is_contextable') && !($field instanceof Metadata_Field_Select)) {
163  $mm->generateContentFile($asset->id);
164  } else {
165  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
166  foreach ($all_contexts as $contextid => $context_data) {
167  $mm->generateContentFile($asset->id, FALSE, $contextid);
168  }
169  }
170  }
171 
172  $GLOBALS['SQ_SYSTEM']->restoreContext();
173 
174  if (is_null($old_current_asset)) {
175  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
176  } else {
177  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
178  }
179 
180  // all done very smoothly like an ice cream
181  if ($success) {
182  return Array(
183  'fieldid' => $settings['fieldid'],
184  'field_name' => $field_name,
185  'value' => $value,
186  'contextid' => $settings['contextid'],
187  );
188  } else {
189  return FALSE;
190  }
191 
192  }//end execute()
193 
194 
205  public static function getInterface($settings, $prefix, $write_access=FALSE)
206  {
207  $output = '';
208  $metadata_fieldid = array_get_index($settings, 'fieldid', 0);
209  $metadata_field_value = array_get_index($settings, 'value', NULL);
210  $metadata_field = NULL;
211 
212  if ($metadata_fieldid) {
213  $metadata_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($metadata_fieldid);
214  }
215  // asset finder for Metadata Field
216  ob_start();
217  if ($write_access) {
218  echo 'Choose Metadata Field&nbsp;&nbsp;&nbsp;';
219  asset_finder($prefix.'[fieldid]', $metadata_fieldid, Array('metadata_field' => 'D'));
220  } else {
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>';
225  } else {
226  echo '<b>No metadata field specified</b>';
227  }
228  }
229  $output_part1 = ob_get_contents();
230  ob_end_clean();
231  $output .= $output_part1;
232 
233  // Input field for the value
234  ob_start();
235  if ($write_access && $metadata_field) {
236  echo '<br>Set the value for this field<br>';
237  // set the field name
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);
241 
242  // attribute friendly prefix
243  $new_prefix = str_replace(array('[',']'), '_', $prefix);
244  hidden_field($prefix.'[new_prefix]', $new_prefix);
245 
246  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
247  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
248 
249  $atr_parameter_map->setParameter('metadata_value', 'Metadata Field Value');
250  echo $atr_parameter_map->paint($new_prefix.'_parameter_map');
251 
252  }
253  $output_part2 = ob_get_contents();
254  ob_end_clean();
255  $output .= $output_part2;
256 
257  // Replace keywords checkbox
258  ob_start();
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();
262 
263  $output .= $output_part3;
264 
265  ob_start();
266  $current_context = array_get_index($settings, 'contextid', NULL);
267 
268  $contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
269  $context_list = Array(
270  '-default-' => translate('trigger_use_currently_active_context'),
271  '-sep-' => '----------------------',
272  );
273  foreach ($contexts as $context_item_id => $context_item) {
274  if ((int)$current_context === (int)$context_item_id) {
275  $context_name = $context_item['name'];
276  }
277 
278  $context_list[$context_item_id] = $context_item['name'];
279  }
280 
281  $default = Array('-default-');
282  if (array_key_exists($current_context, $contexts) === TRUE) {
283  $default = Array($current_context);
284  }
285 
286  if ($metadata_field) {
287  label(translate('trigger_set_value_in_this_context'), $prefix.'[context_to_select]');
288  echo ' ';
289  if ($write_access) {
290  combo_box($prefix.'[context_to_select]', $context_list, FALSE, $default);
291  } else {
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'];
296  } else {
297  echo '<span class="sq-backend-warning">'.translate('trigger_context_deleted_fail_until_changed').'</span>';
298  }
299  }
300  }
301  $output_context_select = ob_get_clean();
302  $output .= '<br />'.$output_context_select;
303 
304  return $output;
305 
306  }//end getInterface()
307 
308 
320  public static function processInterface(&$settings, $request_data)
321  {
322  // process fieldid
323  $fieldid_data = array_get_index($request_data, 'fieldid', 0);
324  $form_printed = array_get_index($request_data, 'form_printed', 0);
325 
326  $fieldid = $fieldid_data['assetid'];
327 
328  if (!$GLOBALS['SQ_SYSTEM']->am->assetExists($fieldid)) {
329  return translate('selected_asset_does_not_exist');
330  }
331 
332  if ($fieldid) {
333  $metadata_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($fieldid);
334 
335  // Bug fix #4014 Set Metadata Value trigger action saves asset ID that is not Metadata Field ID
336  // Once we have the field check if the asset is a metadata field
337  if (!($metadata_field instanceof Metadata_Field)) {
338  return 'The input ID is not a Metadata Field ID';
339  }
340 
341  // Can we edit the destination field? If we can't then what's the point;
342  // we can't even change it through a trigger
343  if (!$metadata_field->attr('editable')) {
344  trigger_localised_error('CORE0246', E_USER_WARNING, $metadata_field->name, $metadata_field->id);
345  return FALSE;
346  }
347 
348  // got this far?? the field seems to be good for us
349  $settings['fieldid'] = $fieldid;
350 
351  $edit_fns = $metadata_field->getEditFns();
352 
353  $result = '';
354  if ($form_printed) {
355  $field_cascade_value=FALSE;
356  $edit_fns->processInlineValueInterface($metadata_field, $result, $field_cascade_value);
357  }
358 
359  $settings['field_name'] = $metadata_field->name;
360  $settings['value'] = $result;
361 
362  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
363  if ($new_prefix) {
364  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
365  $atr_parameter_map->process($new_prefix.'_parameter_map');
366  $settings['value_map'] = $atr_parameter_map->value;
367  }
368 
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;
375  } else {
376  trigger_localised_error('TRIG0005', E_USER_WARNING, $new_context);
377  }
378  } else if ($new_context === '-default-') {
379  $settings['contextid'] = NULL;
380  }
381  }
382 
383  $settings['replace_keywords_in_set_value'] = array_get_index($request_data, 'replace_keywords_in_set_value', 0);
384 
385  return FALSE;
386 
387  }//end processInterface()
388 
389 
399  public static function getLocks($settings, &$state)
400  {
401  return Array($state['assetid'] => Array('all'));
402 
403  }//end getLocks()
404 
405 
406 }//end class
407 
408 ?>