17 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
57 public static function execute($settings, &$state)
60 if (empty($settings[
'status'])) {
65 if (empty($state[
'asset'])) {
67 if (empty($state[
'assetid'])) {
70 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
74 $custom_message = array_get_index($settings,
'custom_error_msg',
'');
75 $custom_error_level = (isset($settings[
'treat_as_fatal']) && $settings[
'treat_as_fatal']) ? E_USER_ERROR : E_USER_WARNING;
77 $cron_mgr = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager');
78 if (is_null($cron_mgr))
return FALSE;
80 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'cron_job_future_status');
83 $fs->setAttrValue(
'status', $settings[
'status']);
84 $fs->setAttrValue(
'dependants_only', !$settings[
'cascade']);
86 if ($settings[
'when_type'] ==
'by_attr_value') {
88 if (empty($settings[
'when_attr_name']))
return FALSE;
89 if (!($state[
'asset'] instanceof $settings[
'when_asset_type'])) {
92 $val = @$state[
'asset']->attr($settings[
'when_attr_name']);
93 if (empty($val))
return FALSE;
94 if ($settings[
'offset_used']) {
95 $offset = (int)substr($settings[
'when'], 4) * 60;
96 $when = iso8601_ts($val) + $offset;
99 if ($custom_message !=
'') trigger_error($custom_message, $custom_error_level);
102 $fs->setAttrValue(
'when',
'OO='.substr(ts_iso8601($when), 0, 16));
105 $ts = iso8601_ts($val);
108 if ($custom_message !=
'') trigger_error($custom_message, $custom_error_level);
111 $fs->setAttrValue(
'when',
'OO='.substr(ts_iso8601($ts), 0, 16));
113 }
else if ($settings[
'when_type'] ==
'by_meta_value') {
115 if (empty($settings[
'when_meta_field_id'])) {
120 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
121 $val = $mm->getMetadataValueByAssetid($state[
'assetid'], $settings[
'when_meta_field_id'], FALSE, TRUE);
125 if (!isset($val))
return FALSE;
127 if (empty($val))
return FALSE;
129 if ($settings[
'offset_used']) {
130 $offset = (int)substr($settings[
'when'], 4) * 60;
131 $when = iso8601_ts($val) + $offset;
132 if ($when < time()) {
134 if ($custom_message !=
'') trigger_error($custom_message, $custom_error_level);
137 $fs->setAttrValue(
'when',
'OO='.substr(ts_iso8601($when), 0, 16));
140 $ts = iso8601_ts($val);
143 if ($custom_message !=
'') trigger_error($custom_message, $custom_error_level);
146 $fs->setAttrValue(
'when',
'OO='.substr(ts_iso8601($ts), 0, 16));
151 $fs->setAttrValue(
'when', $settings[
'when']);
155 if ($GLOBALS[
'SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
156 $user_for_status_change = $GLOBALS[
'SQ_SYSTEM']->user;
159 $user_for_status_change = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
162 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
163 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
166 if (!$fs->setAssetToUpdate($state[
'asset'])) {
167 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
168 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
172 if ($cron_mgr->addJob($fs, $user_for_status_change)) {
173 if ($GLOBALS[
'SQ_SYSTEM']->am->acquireLock($fs->id,
'links')) {
174 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
175 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
177 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($fs->id,
'links');
179 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
180 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
184 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
185 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
191 'userid' => $user_for_status_change->id,
192 'status' => $fs->attr(
'status'),
193 'when' => $fs->attr(
'when'),
209 public static function getInterface($settings, $prefix, $write_access=FALSE)
212 $settings[
'status'] = array_get_index($settings,
'status', SQ_STATUS_LIVE_APPROVAL);
213 $settings[
'cascade'] = array_get_index($settings,
'cascade', FALSE);
214 $settings[
'when'] = array_get_index($settings,
'when');
215 $settings[
'when_asset_type'] = array_get_index($settings,
'when_asset_type',
'');
216 $settings[
'when_attr_name'] = array_get_index($settings,
'when_attr_name',
'');
217 $settings[
'when_meta_field_id'] = array_get_index($settings,
'when_meta_field_id',
'');
219 if (!isset($settings[
'when_type'])) {
220 if (empty($settings[
'when'])) {
221 $settings[
'when_type'] =
'explicit_exact';
224 if (FALSE === strpos($settings[
'when'],
'OO!')) {
225 $settings[
'when_type'] =
'explicit_exact';
227 $settings[
'when_type'] =
'on_trigger_fire';
228 $weights = Array(
'i' => 1,
'h' => 60,
'd' => 1440,
'w' => 10080,
'm' => 43200,
'y' => 535600);
229 $settings[
'when'] =
'OO!i'.($weights[$settings[
'when'][3]] * substr($settings[
'when'], 4));
233 $munge_prefix = str_replace(
'[',
'_', $prefix);
234 $munge_prefix = str_replace(
']',
'', $munge_prefix);
235 hidden_field($prefix.
'[prefix]', $munge_prefix);
236 $prefix = $munge_prefix;
240 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
242 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
243 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
245 if (0 === strpos($settings[
'when'],
'OO!i')) {
246 $mins = ((int)(substr($settings[
'when'], 4)) * 60);
247 $duration->value = abs($mins);
250 include_once SQ_ATTRIBUTES_PATH.
'/datetime/datetime.inc';
251 $datetime =
new Asset_Attribute_Datetime();
252 $datetime->setEditParam(
'show', Array(
'y',
'm',
'd',
'h',
'i'));
253 $datetime->setEditParam(
'min', date(
'Y-m-d H:i:s'));
254 if (FALSE === strpos($settings[
'when'],
'OO!')) {
255 $datetime->value = substr($settings[
'when'], 3).
':00';
258 echo translate(
'trigger_action_future_status_make_status_change_to').
' ';
260 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'cron_job_future_status');
263 combo_box($prefix.
'_status', $descs, FALSE, $settings[
'status'], NULL);
265 echo
'<b>'.$descs[$settings[
'status']].
'</b> ';
271 label(translate(
'immediately_when_trigger_fired'), $prefix.
'_when_type_on_trigger_fire');
272 $formats[
'on_trigger_fire'] = ob_get_clean();
275 label(translate(
'trigger_action_future_status_explicit_exact').
' ', $prefix.
'_when_type_explicit_exact');
276 $datetime->paint($prefix.
'_explicit_exact', !$write_access);
277 $formats[
'explicit_exact'] = ob_get_clean();
280 label(translate(
'as_determined_from_asset_attribute').
' ', $prefix.
'_as_determined_from_attr');
282 $formats[
'by_attr_value'] = ob_get_clean();
285 label(translate(
'as_determined_from_metadata_field').
' ', $prefix.
'_as_determined_from_meta');
286 $metadata_field_id = $settings[
'when_meta_field_id'];
288 asset_finder($prefix.
'_when_meta_field_id', $metadata_field_id, Array(
'metadata_field_date' =>
'D'));
290 <p><em
class=
"sq-backend-smallprint">(<?php echo translate(
'trigger_action_future_status_date_format'); ?>)</em></p>
293 if ($metadata_field_id) {
294 $metadata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($metadata_field_id);
297 if ($metadata_field) {
298 echo get_asset_tag_line($metadata_field_id);
301 echo
'<span class="sq-backend-warning">Unknown asset (Id: #'.$metadata_field_id.
')</span>';
304 echo
'<b>No metadata field specified</b>';
307 $formats[
'by_meta_value'] = ob_get_clean();
310 $GLOBALS[
'SQ_SYSTEM']->backend->out->addOnLoad(
'initEnableFieldLists();');
312 <ul
class=
"radio-list enable-field-list">
314 foreach ($formats as $type => $content) {
317 <?php radio_button($prefix.
'_when_type', $type, ($settings[
'when_type'] == $type)); ?><div>
318 <?php echo $content; ?>
329 echo preg_replace(
'/<label([^>]*)>/',
'', $formats[$settings[
'when_type']]);
333 label(translate(
'offset_for_above_fields').
': ', $prefix.
'_offset');
334 $duration->paint($prefix.
'_offset_value', !$write_access);
337 combo_box($prefix.
'_offset_operator', Array(
'-' => translate(
'before'),
'+' => translate(
'after')), FALSE, ($mins < 0) ?
'-' :
'+');
339 echo translate(($mins < 0) ?
'before' :
'after');
341 echo
' '.translate(
'specified_value').
'<br /><i>'.translate(
'offset_field_note').
'</i><br /><br /></p>';
344 check_box($prefix.
'_cascade_all',
'1', $settings[
'cascade']);
345 label(translate(
'trigger_action_future_status_cascade_all'), $prefix.
'_cascade_all');
347 <p><em
class=
"sq-backend-smallprint">(<?php echo translate(
'trigger_action_future_status_failure_conditions'); ?>)</em></p>
350 if ($settings[
'cascade']) {
351 echo
' '.translate(
'trigger_action_future_status_cascade_all');
356 echo translate(
'trigger_action_future_status_customise_msg');
357 $custom_message = array_get_index($settings,
'custom_error_msg',
'');
359 text_box($prefix.
'_custom_error_msg', $custom_message,
'40');
361 echo
' : '.$custom_message;
363 echo translate(
'trigger_action_future_status_customise_msg_note');
364 '<br />()<b>NOTE :</b> If customised, this error message will be displayed if the caluclated time is in past.</p>';
367 echo translate(
'trigger_action_future_status_treat_as_fatal');
368 $treat_fatal = array_get_index($settings,
'treat_as_fatal', 0);
370 check_box($prefix.
'_treat_as_fatal',
'1', $treat_fatal);
372 echo
'<img src="'.sq_web_path(
'lib').
'/web/images/'.($treat_fatal ?
'tick' :
'cross').
'.gif" alt="'.($treat_fatal ? translate(
'yes') : translate(
'no')).
'" /> ';
374 echo translate(
'trigger_action_future_status_treat_as_fatal_note');
376 return ob_get_clean();
394 $prefix = $request_data[
'prefix'];
396 if (empty($_POST[$prefix.
'_when_type']))
return TRUE;
398 $settings[
'when_type'] = $_POST[$prefix.
'_when_type'];
399 $settings[
'cascade'] = !empty($_POST[$prefix.
'_cascade_all']);
400 $settings[
'custom_error_msg'] = array_get_index($_POST, $prefix.
'_custom_error_msg',
'');
401 $settings[
'treat_as_fatal'] = array_get_index($_POST, $prefix.
'_treat_as_fatal', 0);
404 if (!isset($status_list[$_POST[$prefix.
'_status']])) {
405 return 'Invalid status';
407 $settings[
'status'] = $_POST[$prefix.
'_status'];
409 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
411 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
412 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
414 include_once SQ_ATTRIBUTES_PATH.
'/datetime/datetime.inc';
415 $datetime =
new Asset_Attribute_Datetime();
416 $datetime->setEditParam(
'show', Array(
'y',
'm',
'd',
'h',
'i'));
417 $datetime->setEditParam(
'min', date(
'Y-m-d H:i:s'));
419 switch ($settings[
'when_type']) {
420 case 'explicit_exact':
421 $settings[
'when_asset_type'] =
'';
422 $settings[
'when_attr_name'] =
'';
423 $datetime->process($prefix.
'_explicit_exact');
424 $settings[
'when'] =
'OO='.substr($datetime->value, 0, 16);
427 case 'on_trigger_fire':
428 $settings[
'when_asset_type'] =
'';
429 $settings[
'when_attr_name'] =
'';
430 $settings[
'when'] =
'';
433 case 'by_attr_value':
434 $settings[
'when_asset_type'] = $_POST[$prefix.
'_exact_attr'][
'asset_type'];
435 $settings[
'when_attr_name'] = array_get_index($_POST[$prefix.
'_exact_attr'],
'attribute');
436 $settings[
'when'] =
'';
439 case 'by_meta_value':
440 $settings[
'when_meta_field_id'] = 0;
441 if (isset($_POST[$prefix.
'_when_meta_field_id'][
'assetid'])) {
442 $settings[
'when_meta_field_id'] = $_POST[$prefix.
'_when_meta_field_id'][
'assetid'];
445 $settings[
'when'] =
'';
449 $settings[
'offset_used'] = FALSE;
450 if ($settings[
'when_type'] !=
'explicit_exact') {
451 $duration->process($prefix.
'_offset_value');
452 $mins = ((int)$duration->value/60);
455 if (($settings[
'when_type'] !=
'on_trigger_fire') && ($_POST[$prefix.
'_offset_operator'] ==
'-')) {
459 $settings[
'when'] =
'OO!i'.$mins;
460 $settings[
'offset_used'] = TRUE;
479 foreach (get_constant_values(
'SQ_STATUS_') as $status_value) {
480 $status[$status_value] = get_status_description($status_value);
502 asset_type_chooser($prefix.
'[asset_type]', FALSE, Array($type), TRUE);
504 echo
'<b>'.$type.
'</b> ';
506 $basic_part_1 = ob_get_contents();
511 echo
'<em>['.translate(
'asset_type_not_selected').
']</em>';
513 $attrs = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($type, Array(
'name',
'type'));
515 echo
'<b>['.translate(
'asset_type_no_attributes_found').
']</b>';
518 $attr_options = Array(
'' =>
'');
519 foreach ($attrs as $name => $type) {
520 if ($type[
'type'] ==
'datetime') {
521 $attr_options[$name] = $name;
524 combo_box($prefix.
'[attribute]', $attr_options, FALSE, $attr);
526 echo
'<b>'.$attr.
'</b>';
530 $basic_part_2 = ob_get_contents();
533 return $basic_part_1.$basic_part_2;