Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_metadata_field_date.inc
1 <?php
15 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
16 
28 {
29 
50  public static function execute($settings, &$state)
51  {
52  $mm =& $GLOBALS['SQ_SYSTEM']->getMetadataManager();
53 
54  // check required settings
55  if (empty($settings['fieldid'])) return FALSE;
56 
57 
58  // If no context ID is set at ALL, then presume it's an old trigger, and
59  // that we use the current context instead.
60  if (array_key_exists('contextid', $settings) === FALSE) {
61  $settings['contextid'] = NULL;
62  }
63 
64  if ($settings['contextid'] === NULL) {
65  $settings['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
66  }
67 
68  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
69  if (array_key_exists($settings['contextid'], $all_contexts) === FALSE) {
70  return FALSE;
71  }
72 
73  $GLOBALS['SQ_SYSTEM']->changeContext($settings['contextid']);
74 
75  // Format: 2008-02-01 00:00:00
76  $value = date('Y-m-d H:i:s', strtotime('+'.$settings['offset_months'].' month '.$settings['offset_days'].' day '.$settings['offset_hours'].' hour '.$settings['offset_mins'].' min'));
77 
78  if (empty($state['asset'])) {
79  // grab the asset if assetid is given, but not the asset.
80  if (empty($state['assetid'])) {
81  $GLOBALS['SQ_SYSTEM']->restoreContext();
82  return FALSE;
83  } else {
84  $state['asset'] = &$GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
85  }
86  }
87 
88  // verify the fieldid
89  $success = $mm->isMetadataFieldAssetid($settings['fieldid']);
90  if (!$success) {
91  $GLOBALS['SQ_SYSTEM']->restoreContext();
92  return FALSE;
93  }
94 
95  // get the asset
96  $asset =& $state['asset'];
97 
98  // get the current metadata values
99  $metadata = $mm->getMetadata($asset->id);
100 
101  // get the field name
102  $field_name = '';
103  if (isset($metadata[$settings['fieldid']])) {
104  $field_name = $metadata[$settings['fieldid']][0]['name'];
105  }
106 
107  // Sanitise the value for equal signs (=)
108  // As per Bug #2951 HTML Submitted becomes malformed due to a simple equals sign
109  $value = str_replace('=', '\=', $value);
110 
111  // set the new value
112  $metadata[$settings['fieldid']] = Array(
113  0 => Array(
114  'name' => $field_name,
115  'value' => $value,
116  )
117  );
118 
119  $success = $mm->setMetadata($asset->id, $metadata);
120 
121  // Regenerate Metadata File
122  if ($success) {
123  $field = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['fieldid']);
124  // If this date field is contextable, regenerate the metadata file in the current context only;
125  // otherwise, regenerate all metadata files
126  if ($field->attr('is_contextable')) {
127  $mm->generateContentFile($asset->id);
128  } else {
129  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
130  foreach ($all_contexts as $contextid => $context_data) {
131  $mm->generateContentFile($asset->id, FALSE, $contextid);
132  }
133  }
134  }
135 
136  $GLOBALS['SQ_SYSTEM']->restoreContext();
137 
138  // all done very smoothly like an ice cream
139  if ($success) {
140  return Array(
141  'fieldid' => $settings['fieldid'],
142  'field_name' => $field_name,
143  'value' => $value,
144  );
145  } else {
146  return FALSE;
147  }
148 
149  }//end execute()
150 
151 
162  public static function getInterface($settings, $prefix, $write_access=FALSE)
163  {
164  $metadata_fieldid = array_get_index($settings, 'fieldid', 0);
165  $metadata_field_value = array_get_index($settings, 'value', NULL);
166 
167  // asset finder for Metadata Field
168  ob_start();
169  if ($write_access) {
170  echo translate('trigger_metadata_date_select_field');
171  asset_finder($prefix.'[fieldid]', $metadata_fieldid, Array('metadata_field' => 'D'));
172  } else {
173  echo translate('trigger_metadata_date_show_field');
174  echo $metadata_fieldid;
175  }
176  echo '<br/>';
177 
178  $disabled = ($write_access ? '' : 'disabled=disabled');
179  ob_start();
180  text_box($prefix.'[offset_months]', array_get_index($settings, 'offset_months', 0), 5, 2, FALSE, $disabled);
181  echo '|';
182  text_box($prefix.'[offset_days]', array_get_index($settings, 'offset_days', 0), 5, 2, FALSE, $disabled);
183  echo '|';
184  text_box($prefix.'[offset_hours]', array_get_index($settings, 'offset_hours', 0), 5, 2, FALSE, $disabled);
185  echo '|';
186  text_box($prefix.'[offset_mins]', array_get_index($settings, 'offset_mins', 0), 5, 2, FALSE, $disabled);
187  list($text_box_months, $text_box_days, $text_box_mins, $text_box_hours) = explode('|', ob_get_clean());
188  echo translate('trigger_metadata_date_offset', $text_box_months, $text_box_days, $text_box_mins, $text_box_hours);
189  $output = ob_get_clean();
190 
191  ob_start();
192  $current_context = array_get_index($settings, 'context_to_select', NULL);
193 
194  $contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
195  $context_list = Array(
196  '-default-' => translate('trigger_use_currently_active_context'),
197  '-sep-' => '----------------------',
198  );
199  foreach ($contexts as $context_item_id => $context_item) {
200  if ((int)$current_context === (int)$context_item_id) {
201  $context_name = $context_item['name'];
202  }
203 
204  $context_list[rawurlencode($context_item['name'])] = $context_item['name'];
205  }
206 
207  $default = Array('-default-');
208  if (array_key_exists($current_context, $contexts) === TRUE) {
209  $default = Array($current_context);
210  }
211 
212  if ($metadata_fieldid) {
213  label(translate('trigger_set_value_in_this_context'), $prefix.'[context_to_select]');
214  echo ' ';
215  if ($write_access) {
216  combo_box($prefix.'[context_to_select]', $context_list, FALSE, $default);
217  } else {
218  if ($current_context === NULL) {
219  echo translate('trigger_use_currently_active_context');
220  } else if (array_key_exists($current_context, $contexts) === TRUE) {
221  echo $contexts[$current_context]['name'];
222  } else {
223  echo '<span class="sq-backend-warning">'.translate('trigger_context_deleted_fail_until_changed').'</span>';
224  }
225  }
226  }
227  $output_context_select = ob_get_clean();
228  $output .= '<br />'.$output_context_select;
229 
230  return $output;
231 
232  }//end getInterface()
233 
234 
246  public static function processInterface(&$settings, $request_data)
247  {
248  // process fieldid
249  $fieldid_data = array_get_index($request_data, 'fieldid', 0);
250  $fieldid = $fieldid_data['assetid'];
251 
252  if ($fieldid) {
253  $settings['fieldid'] = $fieldid;
254  foreach (Array('offset_months', 'offset_days', 'offset_hours', 'offset_mins') as $field_name) {
255  $settings[$field_name] = array_get_index($request_data, $field_name, '0');
256  }
257 
258  $metadata_field =& $GLOBALS['SQ_SYSTEM']->am->getAsset($fieldid);
259 
260  // Can we edit the destination field? If we can't then what's the point;
261  // we can't even change it through a trigger
262  if (!$metadata_field->attr('editable')) {
263  trigger_localised_error('CORE0246', E_USER_WARNING, $metadata_field->name, $metadata_field->id);
264  return FALSE;
265  }
266 
267  $settings['field_name'] = $metadata_field->name;
268 
269  $new_context = array_get_index($request_data, 'context_to_select', 0);
270  if (is_numeric($new_context) === TRUE) {
271  $new_context = (int)$new_context;
272  $contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
273  if (array_key_exists($new_context, $contexts) === TRUE) {
274  $settings['contextid'] = $new_context;
275  } else {
276  trigger_localised_error('TRIG0005', E_USER_WARNING, $new_context);
277  }
278  } else if ($new_context === '-default-') {
279  $settings['contextid'] = NULL;
280  }
281  }
282 
283  return FALSE;
284 
285  }//end processInterface()
286 
287 
297  public static function getLocks($settings, &$state)
298  {
299  return Array($state['assetid'] => Array('all'));
300 
301  }//end getLocks()
302 
303 
304 }//end class
305 
306 ?>