17 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.
'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_SYSTEM_ROOT.
'/core/attributes/parameter_map/parameter_map.inc';
60 public static function execute($settings, &$state)
63 if (!isset($settings[
'add']))
return FALSE;
64 if (!isset($settings[
'granted']))
return FALSE;
65 if (!isset($settings[
'permission']))
return FALSE;
66 if (!isset($settings[
'dependants_only']))
return FALSE;
67 if (!isset($settings[
'cascade_to_new']))
return FALSE;
69 $parameter_map_value = array_get_index($settings,
'user_map', serialize(Array()));
71 $userid = $atr_parameter_map->getParameterValue(
'user');
73 if (!empty($userid)) {
75 $user_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($userid, Array(
'user',
'user_group'), FALSE);
76 if (empty($user_info))
return FALSE;
79 if (empty($settings[
'userid']))
return FALSE;
80 $userid = $settings[
'userid'];
84 if (empty($state[
'asset'])) {
86 if (empty($state[
'assetid'])) {
89 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
93 $cron_mgr = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager');
94 if (is_null($cron_mgr))
return FALSE;
96 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset(
'cron_job_future_permission');
99 $fp->setAttrValue(
'add', $settings[
'add']);
100 $fp->setAttrValue(
'permission', $settings[
'permission']);
101 $fp->setAttrValue(
'userids', Array($userid => $settings[
'granted']));
102 $fp->setAttrValue(
'cascade_to_new', $settings[
'cascade_to_new']);
103 $fp->setAttrValue(
'dependants_only', $settings[
'dependants_only']);
105 if ($settings[
'when_type'] ==
'by_attr_value') {
107 if (empty($settings[
'when_attr_name']))
return FALSE;
108 if (!($state[
'asset'] instanceof $settings[
'when_asset_type'])) {
111 $val = @$state[
'asset']->attr($settings[
'when_attr_name']);
112 if (empty($val))
return FALSE;
113 if ($settings[
'offset_used']) {
114 $offset = (int)substr($settings[
'when'], 4) * 60;
115 $when = iso8601_ts($val) + $offset;
116 if ($when < time())
return FALSE;
117 $fp->setAttrValue(
'when',
'OO='.substr(ts_iso8601($when), 0, 16));
120 $ts = iso8601_ts($val);
121 if ($ts < time())
return FALSE;
122 $fp->setAttrValue(
'when',
'OO='.substr(ts_iso8601($ts), 0, 16));
124 }
else if ($settings[
'when_type'] ==
'by_meta_value') {
126 if (empty($settings[
'when_meta_field_id'])) {
131 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
132 $val = $mm->getMetadataValueByAssetid($state[
'assetid'], $settings[
'when_meta_field_id'], FALSE, TRUE);
136 if (!isset($val))
return FALSE;
138 if (empty($val))
return FALSE;
140 if ($settings[
'offset_used']) {
141 $offset = (int)substr($settings[
'when'], 4) * 60;
142 $when = iso8601_ts($val) + $offset;
143 if ($when < time())
return FALSE;
144 $fp->setAttrValue(
'when',
'OO='.substr(ts_iso8601($when), 0, 16));
147 $ts = iso8601_ts($val);
148 if ($ts < time())
return FALSE;
149 $fp->setAttrValue(
'when',
'OO='.substr(ts_iso8601($ts), 0, 16));
153 $fp->setAttrValue(
'when', $settings[
'when']);
157 if ($GLOBALS[
'SQ_SYSTEM']->runLevelEnables(SQ_SECURITY_PERMISSIONS)) {
158 $user_for_permission_change = $GLOBALS[
'SQ_SYSTEM']->user;
161 $user_for_permission_change = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
164 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
165 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
168 if (!$fp->setAssetToUpdate($state[
'asset'])) {
169 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
170 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
174 if ($cron_mgr->addJob($fp, $user_for_permission_change)) {
175 if ($GLOBALS[
'SQ_SYSTEM']->am->acquireLock($fp->id,
'links')) {
176 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
177 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
178 $GLOBALS[
'SQ_SYSTEM']->am->releaseLock($fp->id,
'links');
180 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
181 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
185 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
186 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
192 'userid' => $user_for_permission_change->id,
193 'add' => $settings[
'add'],
194 'granted' => $settings[
'granted'],
195 'permission' => $fp->attr(
'permission'),
196 'granted_userid' => $userid,
197 'when' => $fp->attr(
'when'),
213 public static function getInterface($settings, $prefix, $write_access=FALSE)
216 $settings[
'add'] = array_get_index($settings,
'add', TRUE);
217 $settings[
'granted'] = array_get_index($settings,
'granted', 1);
218 $settings[
'permission'] = array_get_index($settings,
'permission', SQ_PERMISSION_READ);
219 $settings[
'userid'] = array_get_index($settings,
'userid', 0);
220 $settings[
'dependants_only'] = array_get_index($settings,
'dependants_only', TRUE);
221 $settings[
'cascade_to_new'] = array_get_index($settings,
'cascade_to_new', FALSE);
222 $settings[
'when'] = array_get_index($settings,
'when');
223 $settings[
'when_asset_type'] = array_get_index($settings,
'when_asset_type',
'');
224 $settings[
'when_attr_name'] = array_get_index($settings,
'when_attr_name',
'');
225 $settings[
'when_meta_field_id'] = array_get_index($settings,
'when_meta_field_id',
'');
227 if (!isset($settings[
'when_type'])) {
228 if (empty($settings[
'when'])) {
229 $settings[
'when_type'] =
'explicit_exact';
232 if (FALSE === strpos($settings[
'when'],
'OO!')) {
233 $settings[
'when_type'] =
'explicit_exact';
235 $settings[
'when_type'] =
'on_trigger_fire';
236 $weights = Array(
'i' => 1,
'h' => 60,
'd' => 1440,
'w' => 10080,
'm' => 43200,
'y' => 535600);
237 $settings[
'when'] =
'OO!i'.($weights[$settings[
'when'][3]] * substr($settings[
'when'], 4));
241 $munge_prefix = str_replace(
'[',
'_', $prefix);
242 $munge_prefix = str_replace(
']',
'', $munge_prefix);
243 hidden_field($prefix.
'[prefix]', $munge_prefix);
244 $prefix = $munge_prefix;
248 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
250 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
251 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
253 if (0 === strpos($settings[
'when'],
'OO!i')) {
254 $mins = ((int)(substr($settings[
'when'], 4)) * 60);
255 $duration->value = abs($mins);
258 include_once SQ_ATTRIBUTES_PATH.
'/datetime/datetime.inc';
259 $datetime =
new Asset_Attribute_Datetime();
260 $datetime->setEditParam(
'show', Array(
'y',
'm',
'd',
'h',
'i'));
261 $datetime->setEditParam(
'min', date(
'Y-m-d H:i:s'));
262 if (FALSE === strpos($settings[
'when'],
'OO!')) {
263 $datetime->value = substr($settings[
'when'], 3).
':00';
266 echo translate(
'trigger_action_future_permission_add_cron_job_to').
' ';
270 $action_options = Array(
271 '1' => strtolower(translate(
'add')),
272 '0' => strtolower(translate(
'remove')),
274 combo_box($prefix.
'_add', $action_options, FALSE, ($settings[
'add'] ?
'1' :
'0'));
277 echo ($settings[
'add']) ? strtolower(translate(
'add')) : strtolower(translate(
'remove'));
282 $permissions = self::_getPermissionList();
284 combo_box($prefix.
'_permission', $permissions, FALSE, $settings[
'permission']);
286 echo
'<b>'.$permissions[$settings[
'permission']].
'</b>';
288 echo
' permission to ';
292 $permission_options = Array(
293 '1' => strtolower(translate(
'grant')),
294 '0' => strtolower(translate(
'deny')),
296 combo_box($prefix.
'_granted', $permission_options, FALSE, $settings[
'granted']);
299 echo ($settings[
'granted']) ? strtolower(translate(
'grant')) : strtolower(translate(
'deny'));
303 echo
' '.translate(
'access_for').
' ';
317 if ($settings[
'userid'] > 0) {
318 $user = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($settings[
'userid']);
320 if (is_null($user)) {
321 echo
'<b>['.translate(
'trigger_no_user_selected').
']</b>';
323 echo
'<b>'.$user->name.
'</b> ';
331 label(translate(
'immediately_when_trigger_fired'), $prefix.
'_when_type_on_trigger_fire');
332 $formats[
'on_trigger_fire'] = ob_get_clean();
335 label(translate(
'trigger_action_future_permission_explicit_exact').
' ', $prefix.
'_when_type_explicit_exact');
336 $datetime->paint($prefix.
'_explicit_exact', !$write_access);
337 $formats[
'explicit_exact'] = ob_get_clean();
340 label(translate(
'as_determined_from_asset_attribute').
' ', $prefix.
'_as_determined_from_attr');
341 echo self::getAttributeChooser($prefix.
'_exact_attr', $write_access, $settings[
'when_asset_type'], $settings[
'when_attr_name']);
342 $formats[
'by_attr_value'] = ob_get_clean();
345 label(translate(
'as_determined_from_metadata_field').
' ', $prefix.
'_as_determined_from_meta');
346 $metadata_field_id = $settings[
'when_meta_field_id'];
348 asset_finder($prefix.
'_when_meta_field_id', $metadata_field_id, Array(
'metadata_field_date' =>
'D'));
350 if ($metadata_field_id) {
351 $metadata_field = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($metadata_field_id);
354 if ($metadata_field) {
355 echo get_asset_tag_line($metadata_field_id);
358 echo
'<span class="sq-backend-warning">Unknown asset (Id: #'.$metadata_field_id.
')</span>';
361 echo
'<b>No metadata field specified</b>';
364 $formats[
'by_meta_value'] = ob_get_clean();
367 $GLOBALS[
'SQ_SYSTEM']->backend->out->addOnLoad(
'initEnableFieldLists();');
369 <ul
class=
"radio-list enable-field-list">
371 foreach ($formats as $type => $content) {
374 <?php radio_button($prefix.
'_when_type', $type, ($settings[
'when_type'] == $type)); ?><div>
375 <?php echo $content; ?>
384 echo preg_replace(
'/<label([^>]*)>/',
'', $formats[$settings[
'when_type']]);
388 label(translate(
'offset_for_above_fields').
': ', $prefix.
'_offset');
389 $duration->paint($prefix.
'_offset_value', !$write_access);
392 combo_box($prefix.
'_offset_operator', Array(
'-' => translate(
'before'),
'+' => translate(
'after')), FALSE, ($mins < 0) ?
'-' :
'+');
394 echo translate(($mins < 0) ?
'before' :
'after');
396 echo
' '.translate(
'specified_value').
'<br /><br /></p>';
401 check_box($prefix.
'_dependants_only',
'1', $settings[
'dependants_only']);
402 echo translate(
'trigger_set_permission_dependants_only');
405 echo
'<img src="'.sq_web_path(
'lib').
'/web/images/'.($settings[
'dependants_only'] ?
'tick' :
'cross').
'.gif" alt="'.($settings[
'dependants_only'] ? translate(
'yes') : translate(
'no')).
'" /> ';
406 echo translate(
'trigger_set_permission_dependants_only');
412 check_box($prefix.
'_cascade_to_new',
'1', $settings[
'cascade_to_new']);
413 echo translate(
'cascade_permission_to_new_children');
416 echo
'<img src="'.sq_web_path(
'lib').
'/web/images/'.($settings[
'cascade_to_new'] ?
'tick' :
'cross').
'.gif" alt="'.($settings[
'cascade_to_new'] ? translate(
'yes') : translate(
'no')).
'" /> ';
417 echo translate(
'cascade_permission_to_new_children');
421 $parameter_map_value = array_get_index($settings,
'user_map', serialize(Array()));
425 $atr_parameter_map->setParameter(
'user',
'Userid or Groupid');
426 echo $atr_parameter_map->paint($prefix.
'_parameter_map', !$write_access);
429 return ob_get_clean();
447 $prefix = $request_data[
'prefix'];
449 if (empty($_POST[$prefix.
'_when_type']))
return TRUE;
451 $settings[
'when_type'] = $_POST[$prefix.
'_when_type'];
454 $add = array_get_index($_POST, $prefix.
'_add',
'1');
455 if ($add !=
'1' && $add !=
'0') {
456 return 'Add/Remove parameter is invalid';
458 $settings[
'add'] = ($add ==
'1') ? TRUE : FALSE;
461 $permission = array_get_index($_POST, $prefix.
'_permission',
'');
462 if ($permission ===
'') {
463 return 'Permission not specified';
466 $valid_permissions = self::_getPermissionList();
467 if (isset($valid_permissions[$permission])) {
468 $settings[
'permission'] = $permission;
470 return 'Specified permission is invalid';
474 $granted = array_get_index($_POST, $prefix.
'_granted',
'');
475 if ($granted !=
'1' && $granted !=
'0') {
476 return 'Grant parameter is invalid';
478 $settings[
'granted'] = $granted;
481 $user_data = array_get_index($_POST, $prefix.
'_userid', Array(
'assetid' =>
''));
482 $userid = $user_data[
'assetid'];
484 if (!empty($userid)) {
485 $user = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($userid);
486 if (is_null($user)) {
487 return 'Specified userid is not a valid userid';
489 $settings[
'userid'] = $userid;
494 $dependants_only = array_get_index($_POST, $prefix.
'_dependants_only',
'0');
495 $settings[
'dependants_only'] = ($dependants_only ==
'1') ? TRUE : FALSE;
498 $cascade_to_new = array_get_index($_POST, $prefix.
'_cascade_to_new',
'0');
499 $settings[
'cascade_to_new'] = ($cascade_to_new ==
'1') ? TRUE : FALSE;
503 $atr_parameter_map->process($prefix.
'_parameter_map');
505 $settings[
'user_map'] = $atr_parameter_map->value;
508 include_once SQ_ATTRIBUTES_PATH.
'/duration/duration.inc';
510 $duration->setEditParam(
'biggest_units', $duration->units[
'days']);
511 $duration->setEditParam(
'smallest_units', $duration->units[
'minutes']);
513 include_once SQ_ATTRIBUTES_PATH.
'/datetime/datetime.inc';
514 $datetime =
new Asset_Attribute_Datetime();
515 $datetime->setEditParam(
'show', Array(
'y',
'm',
'd',
'h',
'i'));
516 $datetime->setEditParam(
'min', date(
'Y-m-d H:i:s'));
518 switch ($settings[
'when_type']) {
519 case 'explicit_exact':
520 $settings[
'when_asset_type'] =
'';
521 $settings[
'when_attr_name'] =
'';
522 $datetime->process($prefix.
'_explicit_exact');
523 $settings[
'when'] =
'OO='.substr($datetime->value, 0, 16);
526 case 'on_trigger_fire':
527 $settings[
'when_asset_type'] =
'';
528 $settings[
'when_attr_name'] =
'';
529 $settings[
'when'] =
'';
532 case 'by_attr_value':
533 $settings[
'when_asset_type'] = $_POST[$prefix.
'_exact_attr'][
'asset_type'];
534 $settings[
'when_attr_name'] = array_get_index($_POST[$prefix.
'_exact_attr'],
'attribute');
535 $settings[
'when'] =
'';
539 case 'by_meta_value':
540 $settings[
'when_meta_field_id'] = 0;
541 if (isset($_POST[$prefix.
'_when_meta_field_id'][
'assetid'])) {
542 $settings[
'when_meta_field_id'] = $_POST[$prefix.
'_when_meta_field_id'][
'assetid'];
544 $settings[
'when'] =
'';
548 $settings[
'offset_used'] = FALSE;
549 if ($settings[
'when_type'] !=
'explicit_exact') {
550 $duration->process($prefix.
'_offset_value');
551 $mins = ((int)$duration->value/60);
554 if (($settings[
'when_type'] !=
'on_trigger_fire') && ($_POST[$prefix.
'_offset_operator'] ==
'-')) {
558 $settings[
'when'] =
'OO!i'.$mins;
559 $settings[
'offset_used'] = TRUE;
582 asset_type_chooser($prefix.
'[asset_type]', FALSE, Array($type), TRUE);
584 echo
'<b>'.$type.
'</b> ';
586 $basic_part_1 = ob_get_contents();
591 echo
'<em>['.translate(
'asset_type_not_selected').
']</em>';
593 $attrs = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($type, Array(
'name',
'type'));
595 echo
'<b>['.translate(
'asset_type_no_attributes_found').
']</b>';
598 $attr_options = Array(
'' =>
'');
599 foreach ($attrs as $name => $type) {
600 if ($type[
'type'] ==
'datetime') {
601 $attr_options[$name] = $name;
604 combo_box($prefix.
'[attribute]', $attr_options, FALSE, $attr);
606 echo
'<b>'.$attr.
'</b>';
610 $basic_part_2 = ob_get_contents();
613 return $basic_part_1.$basic_part_2;
627 SQ_PERMISSION_READ =>
'read',
628 SQ_PERMISSION_WRITE =>
'write',
629 SQ_PERMISSION_ADMIN =>
'admin',