17 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
55 public static function execute($settings, &$state)
58 if (empty($settings[
'date_field'])) {
63 if (empty($state[
'asset'])) {
65 if (empty($state[
'assetid'])) {
68 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
74 if ($settings[
'when_type'] ==
'by_attr_value') {
76 if (empty($settings[
'when_attr_name']))
return FALSE;
77 if (!($state[
'asset'] instanceof $settings[
'when_asset_type'])) {
80 $val = @$state[
'asset']->attr($settings[
'when_attr_name']);
84 if ($settings[
'offset_used']) {
85 $offset = (int)$settings[
'offset'] * 60;
86 $when = iso8601_ts($val) + $offset;
89 $when = iso8601_ts($val);
91 }
else if ($settings[
'when_type'] ==
'by_meta_value') {
93 if (empty($settings[
'when_meta_field_id'])) {
98 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
99 $val = $mm->getMetadataValueByAssetid($state[
'assetid'], $settings[
'when_meta_field_id'], FALSE, TRUE);
111 if ($settings[
'offset_used']) {
112 $offset = (int)$settings[
'offset'] * 60;
113 $when = iso8601_ts($val) + $offset;
116 $when = iso8601_ts($val);
118 }
else if ($settings[
'when_type'] ==
'on_trigger_fire') {
120 if ($settings[
'offset_used']) {
121 $offset = (int)$settings[
'offset'] * 60;
127 $when = iso8601_ts($settings[
'when']);
130 if ($when === NULL)
return FALSE;
132 $userid = $GLOBALS[
'SQ_SYSTEM']->user->id;
134 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
135 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
138 $sql =
'UPDATE sq_ast
139 SET '.$settings[
'date_field'].
' = :when,
140 '.$settings[
'date_field'].
'_userid = :userid
141 WHERE assetid = :assetid';
143 'assetid' => $state[
'asset']->
id,
145 'when' => ts_iso8601($when),
148 foreach ($bind_vars as $bind_var => $bind_value) {
154 if ($affected === 0) {
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);
164 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
165 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
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);
172 $state[
'asset']->$date_field = $when;
173 $state[
'asset']->$date_field_userid = $userid;
175 $em->broadcastEvent($state[
'asset'],
'AssetUpdate', $changed_data);
178 'assetid' => $state[
'asset']->
id,
180 'date_field' => $settings[
'date_field'],
197 public static function getInterface($settings, $prefix, $write_access=FALSE)
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',
'');
211 $date_field_types = self::_getValidDateTypes();
213 if (empty($settings[
'when']) === TRUE) {
214 $settings[
'when'] = substr(ts_iso8601(time()), 0, -3);
217 $munge_prefix = str_replace(
'[',
'_', $prefix);
218 $munge_prefix = str_replace(
']',
'', $munge_prefix);
219 hidden_field($prefix.
'[prefix]', $munge_prefix);
220 $prefix = $munge_prefix;
224 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
226 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
227 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
228 $duration->value = abs($settings[
'offset'])*60;
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';
238 label(translate(
'set_standard_date_field_type').
': ', $prefix.
'_date_field');
240 combo_box($prefix.
'_date_field', $date_field_types, FALSE, $settings[
'date_field']);
242 echo translate($settings[
'date_field']);
249 label(translate(
'immediately_when_trigger_fired'), $prefix.
'_when_type_on_trigger_fire');
250 $formats[
'on_trigger_fire'] = ob_get_clean();
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();
258 label(translate(
'as_determined_from_asset_attribute').
' ', $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();
263 label(translate(
'as_determined_from_metadata_field').
' ', $prefix.
'_as_determined_from_meta');
264 $metadata_field_id = $settings[
'when_meta_field_id'];
266 asset_finder($prefix.
'_when_meta_field_id', $metadata_field_id, Array(
'metadata_field_date' =>
'D'));
268 <p><em
class=
"sq-backend-smallprint">(<?php echo translate(
'trigger_action_future_status_date_format'); ?>)</em></p>
271 if ($metadata_field_id) {
272 $metadata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($metadata_field_id);
275 if ($metadata_field) {
276 echo get_asset_tag_line($metadata_field_id);
279 echo
'<span class="sq-backend-warning">Unknown asset (Id: #'.$metadata_field_id.
')</span>';
282 echo
'<b>No metadata field specified</b>';
285 $formats[
'by_meta_value'] = ob_get_clean();
288 $GLOBALS[
'SQ_SYSTEM']->backend->out->addOnLoad(
'initEnableFieldLists();');
290 <ul
class=
"radio-list enable-field-list">
292 foreach ($formats as $type => $content) {
295 <?php radio_button($prefix.
'_when_type', $type, ($settings[
'when_type'] == $type)); ?><div>
296 <?php echo $content; ?>
307 echo preg_replace(
'/<label([^>]*)>/',
'', $formats[$settings[
'when_type']]);
311 label(translate(
'offset_for_above_fields').
': ', $prefix.
'_offset');
312 $duration->paint($prefix.
'_offset_value', !$write_access);
315 combo_box($prefix.
'_offset_operator', Array(
'-' => translate(
'before'),
'+' => translate(
'after')), FALSE, ($settings[
'offset'] < 0) ?
'-' :
'+');
317 echo translate(($settings[
'offset'] < 0) ?
'before' :
'after');
319 echo
' '.translate(
'specified_value').
'<br />';
320 echo translate(
'offset_ignored_for_exact_time').
'<br/><br /></p>';
322 return ob_get_clean();
340 $prefix = $request_data[
'prefix'];
342 if (empty($_POST[$prefix.
'_when_type']))
return TRUE;
344 $settings[
'when_type'] = $_POST[$prefix.
'_when_type'];
346 $date_field_types = self::_getValidDateTypes();
347 if (!isset($date_field_types[$_POST[$prefix.
'_date_field']])) {
348 return 'Invalid date field name';
350 $settings[
'date_field'] = $_POST[$prefix.
'_date_field'];
352 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
354 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
355 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
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'));
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);
370 case 'on_trigger_fire':
371 $settings[
'when_asset_type'] =
'';
372 $settings[
'when_attr_name'] =
'';
373 $settings[
'when'] =
'';
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'] =
'';
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'];
388 $settings[
'when'] =
'';
392 $settings[
'offset_used'] = FALSE;
393 if ($settings[
'when_type'] !=
'explicit_exact') {
394 $duration->process($prefix.
'_offset_value');
395 $mins = ((int)$duration->value/60);
397 if ($_POST[$prefix.
'_offset_operator'] ==
'-') {
401 $settings[
'offset'] = $mins;
402 $settings[
'offset_used'] = TRUE;
421 'created' => translate(
'created_date'),
422 'published' => translate(
'published_date'),
423 'updated' => translate(
'last_updated_date'),
424 'status_changed' => translate(
'status_changed_date'),
446 asset_type_chooser($prefix.
'[asset_type]', FALSE, Array($type), TRUE);
448 echo
'<b>'.$type.
'</b> ';
450 $basic_part_1 = ob_get_contents();
455 echo
'<em>['.translate(
'asset_type_not_selected').
']</em>';
457 $attrs = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($type, Array(
'name',
'type'));
459 echo
'<b>['.translate(
'asset_type_no_attributes_found').
']</b>';
462 foreach ($attrs as $name => $type) {
463 if ($type[
'type'] ==
'datetime') {
464 $attr_options[$name] = $name;
467 combo_box($prefix.
'[attribute]', $attr_options, FALSE, $attr);
469 echo
'<b>'.$attr.
'</b>';
473 $basic_part_2 = ob_get_contents();
476 return $basic_part_1.$basic_part_2;