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/email_format/email_format.inc';
20 require_once SQ_SYSTEM_ROOT.
'/core/attributes/parameter_map/parameter_map.inc';
46 public static function execute($settings, &$state)
48 if (empty($state[
'asset'])) {
50 if (empty($state[
'assetid'])) {
53 $state[
'asset'] = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($state[
'assetid']);
57 if (is_null($state[
'asset']))
return FALSE;
59 $assetid = $state[
'asset']->id;
62 $settings[
'groups'] = array_get_index($settings,
'groups', Array());
63 $settings[
'roles'] = array_get_index($settings,
'roles', Array());
64 $settings[
'email_recipient_map'] = array_get_index($settings,
'email_recipient_map', Array());
66 $email_format_value = $settings[
'email_format_value'];
67 if ($settings[
'to_owner'] && $GLOBALS[
'SQ_SYSTEM']->am->isTypeDecendant($state[
'asset']->type(),
'user')) {
69 $unserialised = unserialize($email_format_value);
70 $unserialised[
'to_assetids'][] = $assetid;
71 $email_format_value = serialize($unserialised);
74 if ($settings[
'groups']) {
76 $group_members = Array();
77 foreach ($settings[
'groups'] as $group) {
79 $group_result = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($group, Array(
'user',
'bulkmail_user'), FALSE, NULL, NULL, NULL, TRUE, NULL, NULL, FALSE, NULL);
80 while (!empty($group_result)) {
81 $member_id = reset(array_keys($group_result));
82 $member_type = reset($group_result);
85 if (stristr($member_id,
':') && empty($member_type[0][
'type_code'])) {
86 $group_result = array_merge($group_result, $GLOBALS[
'SQ_SYSTEM']->am->getChildren($member_id,
'user', FALSE));
87 $member_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($member_id, Array(
'user'), FALSE,
'type_code');
88 if (!empty($member_info) && $GLOBALS[
'SQ_SYSTEM']->am->isTypeDecendant($member_info[$member_id],
'user')) {
89 $member_type[0][
'type_code'] = $member_info[$member_id];
94 if (!empty($member_type[0][
'type_code'])) {
95 $group_members[$member_id] = $member_type;
97 unset($group_result[$member_id]);
102 if (!empty($group_members)) {
103 $asset_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(array_keys($group_members), Array(), TRUE,
'status');
105 foreach ($asset_info as $member_id => $member_status) {
106 if ((!stristr($member_id,
':')) && ($member_status < SQ_STATUS_LIVE)) {
107 unset($group_members[$member_id]);
110 $unserialised = unserialize($email_format_value);
111 $unserialised[
'to_assetids'] = array_merge($unserialised[
'to_assetids'],array_keys($group_members));
112 $email_format_value = serialize($unserialised);
116 if ($settings[
'roles']) {
118 $role_members = Array();
119 $fetch_global_roles = (SQ_CONF_ENABLE_GLOBAL_ROLES ==
'1');
120 foreach ($settings[
'roles'] as $role) {
121 $role_result = $GLOBALS[
'SQ_SYSTEM']->am->getRole($state[
'assetid'], $role, NULL, FALSE, $fetch_global_roles);
122 if (!empty($role_result)) {
123 foreach ($role_result[$role] as $role_member) {
126 $role_member_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($role_member);
129 if ($role_member_asset instanceof
User || $role_member_asset->
type() ==
'user_group') {
130 $role_members[] = $role_member;
133 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($role_memeber_asset);
137 $unserialised = unserialize($email_format_value);
138 $unserialised[
'to_assetids'] = array_merge($unserialised[
'to_assetids'],$role_members);
139 $email_format_value = serialize($unserialised);
143 $old_current_asset = $GLOBALS[
'SQ_SYSTEM']->getGlobalDefine(
'CURRENT_ASSET', NULL);
144 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $state[
'asset']);
146 if ($settings[
'email_recipient_map']) {
148 $parameter_map_value = array_get_index($settings,
'email_recipient_map', serialize(Array()));
150 $paramater_map_assetids = $atr_parameter_map->getParameterValue(
'user_assetid');
152 $paramater_map_emails = $atr_parameter_map->getParameterValue(
'user_email');
154 $unserialised = unserialize($email_format_value);
155 $unserialised[
'to'] = array_merge($unserialised[
'to'], self::_parseCommaSeparatedString($paramater_map_emails));
156 $unserialised[
'to_assetids'] = array_merge($unserialised[
'to_assetids'], self::_parseCommaSeparatedString($paramater_map_assetids));
157 $email_format_value = serialize($unserialised);
162 if (!empty($settings[
'asset_to_send_map'])){
163 $ast2send_param_map_value = array_get_index($settings,
'asset_to_send_map', serialize(Array()));
165 $ast2send_id = $ast2send_atr_param_map->getParameterValue(
'ast2send_assetid');
168 if (empty($ast2send_id) && array_get_index($settings,
'override_asset_to_send', FALSE)) {
169 $ast2send_id = $assetid;
172 if (!empty($ast2send_id)) {
173 $unserialised = unserialize($email_format_value);
174 $unserialised[
'asset_to_send'] = $ast2send_id;
175 $email_format_value = serialize($unserialised);
180 if (is_null($email_format_attr))
return FALSE;
182 $current_value = @unserialize($email_format_attr->value);
185 if (empty($current_value[
'to']) && empty($current_value[
'to_assetids'])) {
190 $keywords_list = array_merge(
191 retrieve_keywords_replacements($current_value[
'html_format']),
192 retrieve_keywords_replacements($current_value[
'text_format']),
193 retrieve_keywords_replacements(implode(
', ', $current_value[
'to'])),
194 retrieve_keywords_replacements($current_value[
'from']),
195 retrieve_keywords_replacements($current_value[
'subject'])
198 $keywords_list = array_unique($keywords_list);
201 $replacements = Array();
202 foreach ($keywords_list as $keyword) {
203 $replacements[$keyword] = $state[
'asset']->getKeywordReplacement($keyword);
205 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($state[
'asset']);
207 $email_format_attr->sendMail($replacements);
209 if (is_null($old_current_asset)) {
210 $GLOBALS[
'SQ_SYSTEM']->unsetGlobalDefine(
'CURRENT_ASSET');
212 $GLOBALS[
'SQ_SYSTEM']->setGlobalDefine(
'CURRENT_ASSET', $old_current_asset);
216 'assetid' => $assetid,
232 public static function getInterface($settings, $prefix, $write_access=FALSE)
235 $to_owner = array_get_index($settings,
'to_owner', 0);
237 $current_roles = array_get_index($settings,
'roles', Array());
238 $current_groups = array_get_index($settings,
'groups', Array());
240 $new_prefix = str_replace(array(
'[',
']'),
'_', $prefix);
241 $email_format_value = array_get_index($settings,
'email_format_value', Array());
243 $atr_email_format->setEditParam(
'with_asset_finder', TRUE);
245 $atr_email_format->setEditParam(
'show_asset_to_send', TRUE);
247 $parameter_map_value = array_get_index($settings,
'email_recipient_map', serialize(Array()));
250 $atr_parameter_map->setParameter(
'user_assetid',
'Recipient Asset IDs');
251 $atr_parameter_map->setParameter(
'user_email',
'Recipient E-mail Addresses');
254 $ast2send_param_map_value = array_get_index($settings,
'asset_to_send_map', serialize(Array()));
257 $ast2send_atr_param_map->setParameter(
'ast2send_assetid',
'Asset To Send Asset ID');
260 <table
class=
"sq-backend-table" border=
"1" style=
"width: auto">
262 <th><?php echo translate(
'trigger_active_user_recipient'); ?></th>
266 check_box($prefix.
'[to_owner]', 1, $to_owner);
267 label(translate(
'trigger_send_to_active_user'), $prefix.
'[to_owner]');
268 echo
'<div class="sq-backend-note" style="margin-left: 10ex">'.translate(
'trigger_send_to_active_user_note').
'</div>';
271 <img src=
"<?php echo sq_web_path('lib'); ?>/web/images/<?php echo ($to_owner ? 'tick' : 'cross'); ?>.gif" width=
"15" height=
"15" />
273 echo translate(
'trigger_send_to_active_user');
279 <th><?php echo translate(
'trigger_group_recipients'); ?></th>
283 multiple_asset_finder($prefix.
'[groups]', $current_groups, Array(
'user_group' =>
'I'));
284 echo
'<div class="sq-backend-note">'.translate(
'trigger_send_to_groups_note').
'</div>';
286 if (empty($current_groups)) {
287 echo translate(
'none');
289 foreach ($current_groups as $group) {
290 echo get_asset_tag_line($group).
'<br />';
298 <th><?php echo translate(
'trigger_role_recipients'); ?></th>
302 multiple_asset_finder($prefix.
'[roles]', $current_roles, Array(
'role' =>
'I'));
303 echo
'<div class="sq-backend-note">'.translate(
'trigger_send_to_roles_note').
'</div>';
305 if (empty($current_roles)) {
306 echo translate(
'none');
308 foreach ($current_roles as $role) {
309 echo get_asset_tag_line($role).
'<br />';
317 <th><?php echo
'Recipient Source'; ?></th>
319 <?php echo $atr_parameter_map->paint($new_prefix.
'_param', !$write_access); ?>
323 <th><?php echo
'Asset To Send'; ?></th>
327 check_box($prefix.
'[override_asset_to_send]',
'1', array_get_index($settings,
'override_asset_to_send',
'0'),
'', ($write_access ?
'' :
'disabled="disabled"'));
328 label(
'Override the Asset To Send with the asset that causes this trigger to fire. You do not need to check this option if you only want to override the asset with the parameter map below. The value in the parameter map will override this option.', $prefix.
'[override_asset_to_send]');
331 echo $ast2send_atr_param_map->paint($new_prefix.
'_ast2send_param', !$write_access);
336 <th><?php echo translate(
'trigger_general_email_settings'); ?></th>
338 <?php $atr_email_format->paint($new_prefix.
'_email', !$write_access); ?>
343 hidden_field($prefix.
'[new_prefix]', $new_prefix);
344 return ob_get_clean();
363 $settings[
'to_owner'] = array_get_index($request_data,
'to_owner', FALSE);
366 $groups = array_get_index($request_data,
'groups', Array());
367 $group_ids = Array();
368 foreach ($groups as $group) {
369 if (!empty($group[
'assetid'])) {
370 $group_ids[] = $group[
'assetid'];
373 $settings[
'groups'] = $group_ids;
376 $roles = array_get_index($request_data,
'roles', Array());
378 foreach ($roles as $role) {
379 if (!empty($role[
'assetid'])) {
380 $role_ids[] = $role[
'assetid'];
383 $settings[
'roles'] = $role_ids;
386 $new_prefix = str_replace(array(
'[',
']'),
'_', array_get_index($request_data,
'new_prefix',
''));
389 $email_format->process($new_prefix.
'_email');
390 $settings[
'email_format_value'] = $email_format->value;
393 $atr_parameter_map->process($new_prefix.
'_param');
395 $settings[
'email_recipient_map'] = $atr_parameter_map->value;
398 $settings[
'override_asset_to_send'] = array_get_index($request_data,
'override_asset_to_send', FALSE);
402 $ast2send_param_map->process($new_prefix.
'_ast2send_param');
404 $settings[
'asset_to_send_map'] = $ast2send_param_map->value;
420 public static function getLocks($settings, &$state)
422 return Array($state[
'assetid'] => Array(
'lookups'));
435 private static function _parseCommaSeparatedString($str)
438 $pieces = explode(
',', $str);
439 foreach ($pieces as $piece) {
440 $item = trim($piece);