Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_standard_asset_date.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
19 
34 {
35 
36 
55  public static function execute($settings, &$state)
56  {
57  // check settings, status
58  if (empty($settings['date_field'])) {
59  // if no settings, fail
60  return FALSE;
61  }
62 
63  if (empty($state['asset'])) {
64  // grab the asset if assetid is given, but not the asset.
65  if (empty($state['assetid'])) {
66  return FALSE;
67  } else {
68  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
69  }
70  }
71 
72  $when = NULL;
73 
74  if ($settings['when_type'] == 'by_attr_value') {
75  // need to try to consult an attribute value
76  if (empty($settings['when_attr_name'])) return FALSE; // incomplete config
77  if (!($state['asset'] instanceof $settings['when_asset_type'])) {
78  return FALSE; // wrong asset type
79  }
80  $val = @$state['asset']->attr($settings['when_attr_name']);
81  if (empty($val)) {
82  return FALSE; // empty date time attr
83  }
84  if ($settings['offset_used']) {
85  $offset = (int)$settings['offset'] * 60;
86  $when = iso8601_ts($val) + $offset;
87  } else {
88  // just the straight value
89  $when = iso8601_ts($val);
90  }
91  } else if ($settings['when_type'] == 'by_meta_value') {
92  // need to try to consult a metadata value
93  if (empty($settings['when_meta_field_id'])) {
94  return FALSE; // incomplete config
95  }
96 
97  // Grab the Metadata for the asset which fired this trigger
98  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
99  $val = $mm->getMetadataValueByAssetid($state['assetid'], $settings['when_meta_field_id'], FALSE, TRUE);
100 
101  // Now attempt to find our relevant Metadata Field
102  // If everything is fine then we should have a date value of some sort - otherwise fail
103  if (!isset($val)) {
104  return FALSE; // Expected Metadata Field not assigned
105  }
106 
107  if (empty($val)) {
108  return FALSE; // empty date time attr
109  }
110 
111  if ($settings['offset_used']) {
112  $offset = (int)$settings['offset'] * 60;
113  $when = iso8601_ts($val) + $offset;
114  } else {
115  // just the straight value
116  $when = iso8601_ts($val);
117  }
118  } else if ($settings['when_type'] == 'on_trigger_fire') {
119  $when = time();
120  if ($settings['offset_used']) {
121  $offset = (int)$settings['offset'] * 60;
122  $when += $offset;
123  }
124 
125  } else {
126  // just the straight value
127  $when = iso8601_ts($settings['when']);
128  }
129 
130  if ($when === NULL) return FALSE;
131 
132  $userid = $GLOBALS['SQ_SYSTEM']->user->id;
133 
134  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
135  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
136 
137  try {
138  $sql = 'UPDATE sq_ast
139  SET '.$settings['date_field'].' = :when,
140  '.$settings['date_field'].'_userid = :userid
141  WHERE assetid = :assetid';
142  $bind_vars = Array(
143  'assetid' => $state['asset']->id,
144  'userid' => $userid,
145  'when' => ts_iso8601($when),
146  );
147  $query = MatrixDAL::preparePdoQuery($sql);
148  foreach ($bind_vars as $bind_var => $bind_value) {
149  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
150  }
151 
152  $affected = MatrixDAL::execPdoQuery($query);
153  // Didn't hit the mark - no asset updated!
154  if ($affected === 0) {
155  return FALSE;
156  }
157  } catch (PDOException $e) {
158  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
159  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
160  trigger_error('SQL ERROR: '.$e->getMessage().' ('.$sql.')', E_USER_WARNING);
161  return FALSE;
162  }
163 
164  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
165  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
166 
167  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
168  $date_field = $settings['date_field'];
169  $date_field_userid = $date_field.'_userid';
170  $changed_data = Array($date_field, $date_field_userid);
171 
172  $state['asset']->$date_field = $when;
173  $state['asset']->$date_field_userid = $userid;
174 
175  $em->broadcastEvent($state['asset'], 'AssetUpdate', $changed_data);
176 
177  return Array(
178  'assetid' => $state['asset']->id,
179  'userid' => $userid,
180  'date_field' => $settings['date_field'],
181  'when' => $when,
182  );
183 
184  }//end execute()
185 
186 
197  public static function getInterface($settings, $prefix, $write_access=FALSE)
198  {
199  // set defaults
200  $settings['date_field'] = array_get_index($settings, 'date_field', 'published');
201  $settings['offset'] = array_get_index($settings, 'offset', 0);
202  $settings['when_type'] = array_get_index($settings, 'when_type', 'on_trigger_fire');
203  $settings['when'] = array_get_index($settings, 'when');
204  $settings['when_asset_type'] = array_get_index($settings, 'when_asset_type', '');
205  $settings['when_attr_name'] = array_get_index($settings, 'when_attr_name', '');
206  $settings['when_meta_field_id'] = array_get_index($settings, 'when_meta_field_id', '');
207 
208  // Available date field types. Don't find any valid reason for offering
209  // "Status Changed" (at least "Updated" is validated by its usage in,
210  // eg. cacheable headers), so won't.
211  $date_field_types = self::_getValidDateTypes();
212 
213  if (empty($settings['when']) === TRUE) {
214  $settings['when'] = substr(ts_iso8601(time()), 0, -3);
215  }
216 
217  $munge_prefix = str_replace('[', '_', $prefix);
218  $munge_prefix = str_replace(']', '', $munge_prefix);
219  hidden_field($prefix.'[prefix]', $munge_prefix);
220  $prefix = $munge_prefix;
221 
222  ob_start();
223 
224  include_once SQ_ATTRIBUTES_PATH.'/duration/duration.inc';
225  $duration = new Asset_Attribute_Duration();
226  $duration->setEditParam('biggest_units', $duration->units['days']);
227  $duration->setEditParam('smallest_units', $duration->units['minutes']);
228  $duration->value = abs($settings['offset'])*60;
229 
230  include_once SQ_ATTRIBUTES_PATH.'/datetime/datetime.inc';
231  $datetime = new Asset_Attribute_Datetime();
232  $datetime->setEditParam('show', Array('y', 'm', 'd', 'h', 'i'));
233  if (NULL !== $settings['when']) {
234  $datetime->value = $settings['when'].':00';
235  }
236 
237  echo '<p>';
238  label(translate('set_standard_date_field_type').': ', $prefix.'_date_field');
239  if ($write_access) {
240  combo_box($prefix.'_date_field', $date_field_types, FALSE, $settings['date_field']);
241  } else {
242  echo translate($settings['date_field']);
243  }
244  echo '</p>';
245 
246  $formats = Array();
247 
248  ob_start();
249  label(translate('immediately_when_trigger_fired'), $prefix.'_when_type_on_trigger_fire');
250  $formats['on_trigger_fire'] = ob_get_clean();
251 
252  ob_start();
253  label(translate('to_this_exact_time').' ', $prefix.'_when_type_explicit_exact');
254  $datetime->paint($prefix.'_explicit_exact', !$write_access);
255  $formats['explicit_exact'] = ob_get_clean();
256 
257  ob_start();
258  label(translate('as_determined_from_asset_attribute').'&nbsp', $prefix.'_as_determined_from_attr');
259  echo self::getAttributeChooser($prefix.'_exact_attr', $write_access, $settings['when_asset_type'], $settings['when_attr_name']);
260  $formats['by_attr_value'] = ob_get_clean();
261 
262  ob_start();
263  label(translate('as_determined_from_metadata_field').'&nbsp;&nbsp;&nbsp;', $prefix.'_as_determined_from_meta');
264  $metadata_field_id = $settings['when_meta_field_id'];
265  if ($write_access) {
266  asset_finder($prefix.'_when_meta_field_id', $metadata_field_id, Array('metadata_field_date' => 'D'));
267  ?>
268  <p><em class="sq-backend-smallprint">(<?php echo translate('trigger_action_future_status_date_format'); ?>)</em></p>
269  <?php
270  } else {
271  if ($metadata_field_id) {
272  $metadata_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($metadata_field_id);
273 
274  // Found the associated Metadata Field asset
275  if ($metadata_field) {
276  echo get_asset_tag_line($metadata_field_id);
277  } else {
278  // Looks like we have an ex-asset...
279  echo '<span class="sq-backend-warning">Unknown asset (Id: #'.$metadata_field_id.')</span>';
280  }
281  } else {
282  echo '<b>No metadata field specified</b>';
283  }
284  }
285  $formats['by_meta_value'] = ob_get_clean();
286 
287  if ($write_access) {
288  $GLOBALS['SQ_SYSTEM']->backend->out->addOnLoad('initEnableFieldLists();');
289  ?>
290  <ul class="radio-list enable-field-list">
291  <?php
292  foreach ($formats as $type => $content) {
293  ?>
294  <li>
295  <?php radio_button($prefix.'_when_type', $type, ($settings['when_type'] == $type)); ?><div>
296  <?php echo $content; ?>
297  </div>
298  </li>
299  <?php
300  }
301 
302 
303  ?>
304  </ul>
305  <?php
306  } else {
307  echo preg_replace('/<label([^>]*)>/', '', $formats[$settings['when_type']]);
308  }
309 
310  echo '<p>';
311  label(translate('offset_for_above_fields').': ', $prefix.'_offset');
312  $duration->paint($prefix.'_offset_value', !$write_access);
313  echo ' ';
314  if ($write_access) {
315  combo_box($prefix.'_offset_operator', Array('-' => translate('before'), '+' => translate('after')), FALSE, ($settings['offset'] < 0) ? '-' : '+');
316  } else {
317  echo translate(($settings['offset'] < 0) ? 'before' : 'after');
318  }
319  echo ' '.translate('specified_value').'<br />';
320  echo translate('offset_ignored_for_exact_time').'<br/><br /></p>';
321 
322  return ob_get_clean();
323 
324  }//end getInterface()
325 
326 
338  public static function processInterface(&$settings, $request_data)
339  {
340  $prefix = $request_data['prefix'];
341 
342  if (empty($_POST[$prefix.'_when_type'])) return TRUE;
343 
344  $settings['when_type'] = $_POST[$prefix.'_when_type'];
345 
346  $date_field_types = self::_getValidDateTypes();
347  if (!isset($date_field_types[$_POST[$prefix.'_date_field']])) {
348  return 'Invalid date field name';
349  }
350  $settings['date_field'] = $_POST[$prefix.'_date_field'];
351 
352  include_once SQ_ATTRIBUTES_PATH.'/duration/duration.inc';
353  $duration = new Asset_Attribute_Duration();
354  $duration->setEditParam('biggest_units', $duration->units['days']);
355  $duration->setEditParam('smallest_units', $duration->units['minutes']);
356 
357  include_once SQ_ATTRIBUTES_PATH.'/datetime/datetime.inc';
358  $datetime = new Asset_Attribute_Datetime();
359  $datetime->setEditParam('show', Array('y', 'm', 'd', 'h', 'i'));
360  //$datetime->setEditParam('min', date('Y-m-d H:i:s')); // must be in the future
361 
362  switch ($settings['when_type']) {
363  case 'explicit_exact':
364  $settings['when_asset_type'] = '';
365  $settings['when_attr_name'] = '';
366  $datetime->process($prefix.'_explicit_exact');
367  $settings['when'] = substr($datetime->value, 0, -3);
368  break;
369 
370  case 'on_trigger_fire':
371  $settings['when_asset_type'] = '';
372  $settings['when_attr_name'] = '';
373  $settings['when'] = '';
374  break;
375 
376  case 'by_attr_value':
377  $settings['when_asset_type'] = $_POST[$prefix.'_exact_attr']['asset_type'];
378  $settings['when_attr_name'] = array_get_index($_POST[$prefix.'_exact_attr'], 'attribute');
379  $settings['when'] = '';
380  break;
381 
382  case 'by_meta_value':
383  $settings['when_meta_field_id'] = 0;
384  if (isset($_POST[$prefix.'_when_meta_field_id']['assetid'])) {
385  $settings['when_meta_field_id'] = $_POST[$prefix.'_when_meta_field_id']['assetid'];
386  }
387 
388  $settings['when'] = '';
389  }//end switch
390 
391  // Add or subtract any applicable offset when the "when_type" is not exact (ie; an absolute time)
392  $settings['offset_used'] = FALSE;
393  if ($settings['when_type'] != 'explicit_exact') {
394  $duration->process($prefix.'_offset_value');
395  $mins = ((int)$duration->value/60);
396 
397  if ($_POST[$prefix.'_offset_operator'] == '-') {
398  $mins *= -1;
399  }
400 
401  $settings['offset'] = $mins;
402  $settings['offset_used'] = TRUE;
403  }
404 
405  return FALSE;
406 
407  }//end processInterface()
408 
409 
418  protected static function _getValidDateTypes()
419  {
420  $date_types = Array(
421  'created' => translate('created_date'),
422  'published' => translate('published_date'),
423  'updated' => translate('last_updated_date'),
424  'status_changed' => translate('status_changed_date'),
425  );
426  return $date_types;
427 
428  }//end _getStatusList()
429 
430 
442  protected static function getAttributeChooser($prefix, $write_access, $type, $attr)
443  {
444  ob_start();
445  if ($write_access) {
446  asset_type_chooser($prefix.'[asset_type]', FALSE, Array($type), TRUE);
447  } else {
448  echo '<b>'.$type.'</b> ';
449  }
450  $basic_part_1 = ob_get_contents();
451  ob_end_clean();
452 
453  ob_start();
454  if ($type == '') {
455  echo '<em>['.translate('asset_type_not_selected').']</em>';
456  } else {
457  $attrs = $GLOBALS['SQ_SYSTEM']->am->getAssetTypeAttributes($type, Array('name', 'type'));
458  if (empty($attrs)) {
459  echo '<b>['.translate('asset_type_no_attributes_found').']</b>';
460  } else {
461  if ($write_access) {
462  foreach ($attrs as $name => $type) {
463  if ($type['type'] == 'datetime') {
464  $attr_options[$name] = $name;
465  }
466  }
467  combo_box($prefix.'[attribute]', $attr_options, FALSE, $attr);
468  } else {
469  echo '<b>'.$attr.'</b>';
470  }
471  }
472  }
473  $basic_part_2 = ob_get_contents();
474  ob_end_clean();
475 
476  return $basic_part_1.$basic_part_2;
477 
478  }//end getAttributeChooser()
479 
480 
481 }//end class
482 
483 ?>