Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_send_email.inc
1 <?php
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';
21 
34 {
35 
36 
46  public static function execute($settings, &$state)
47  {
48  if (empty($state['asset'])) {
49  // grab the asset if assetid is given, but not the asset.
50  if (empty($state['assetid'])) {
51  return FALSE;
52  } else {
53  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
54  }
55  }
56 
57  if (is_null($state['asset'])) return FALSE;
58 
59  $assetid = $state['asset']->id;
60 
61  // Fill in possibly missing settings (from upgrades)
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());
65 
66  $email_format_value = $settings['email_format_value'];
67  if ($settings['to_owner'] && $GLOBALS['SQ_SYSTEM']->am->isTypeDecendant($state['asset']->type(), 'user')) {
68  // add owners id
69  $unserialised = unserialize($email_format_value);
70  $unserialised['to_assetids'][] = $assetid;
71  $email_format_value = serialize($unserialised);
72  }
73 
74  if ($settings['groups']) {
75  // add group members
76  $group_members = Array();
77  foreach ($settings['groups'] as $group) {
78  // Grab all users and all directly and indirectly-linked shadow assets
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);
83 
84  // check shadow assets
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];
90  }//end if
91  }
92 
93  // this is a user we can send to
94  if (!empty($member_type[0]['type_code'])) {
95  $group_members[$member_id] = $member_type;
96  }
97  unset($group_result[$member_id]);
98  }
99  }
100 
101  // strip out non-live users unless they are shadow assets (eg; LDAP Users) and add to the email attribute value
102  if (!empty($group_members)) {
103  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(array_keys($group_members), Array(), TRUE, 'status');
104 
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]);
108  }
109  }
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);
113  }
114  }//end if
115 
116  if ($settings['roles']) {
117  // add role members
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) {
124 
125  // Get the role_member asset to check if its of type User
126  $role_member_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($role_member);
127 
128  // If asset does not belongs to User class then there is no point in sending email to it
129  if ($role_member_asset instanceof User || $role_member_asset->type() == 'user_group') {
130  $role_members[] = $role_member;
131  }
132 
133  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($role_memeber_asset);
134  }
135  }
136  }
137  $unserialised = unserialize($email_format_value);
138  $unserialised['to_assetids'] = array_merge($unserialised['to_assetids'],$role_members);
139  $email_format_value = serialize($unserialised);
140  }
141 
142  // Set the current asset temporaily to one that fired this trigger action
143  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
144  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
145 
146  if ($settings['email_recipient_map']) {
147  // paramater map parameters
148  $parameter_map_value = array_get_index($settings, 'email_recipient_map', serialize(Array()));
149  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
150  $paramater_map_assetids = $atr_parameter_map->getParameterValue('user_assetid');
151 
152  $paramater_map_emails = $atr_parameter_map->getParameterValue('user_email');
153 
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);
158  }
159 
160  //override the Asset To Send if specified
161  $ast2send_id = NULL;
162  if (!empty($settings['asset_to_send_map'])){
163  $ast2send_param_map_value = array_get_index($settings, 'asset_to_send_map', serialize(Array()));
164  $ast2send_atr_param_map = new Asset_Attribute_Parameter_Map(0, $ast2send_param_map_value);
165  $ast2send_id = $ast2send_atr_param_map->getParameterValue('ast2send_assetid');
166  }
167 
168  if (empty($ast2send_id) && array_get_index($settings, 'override_asset_to_send', FALSE)) {
169  $ast2send_id = $assetid;
170  }
171 
172  if (!empty($ast2send_id)) {
173  $unserialised = unserialize($email_format_value);
174  $unserialised['asset_to_send'] = $ast2send_id;
175  $email_format_value = serialize($unserialised);
176  }
177 
178  // create a new email format object and send emails
179  $email_format_attr = new Asset_Attribute_Email_Format(0, $email_format_value);
180  if (is_null($email_format_attr)) return FALSE;
181 
182  $current_value = @unserialize($email_format_attr->value);
183 
184  // return false if there is no-one to send to
185  if (empty($current_value['to']) && empty($current_value['to_assetids'])) {
186  return FALSE;
187  }
188 
189  // Get list of replacements
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'])
196  );
197 
198  $keywords_list = array_unique($keywords_list);
199 
200  // ...and find their replacements
201  $replacements = Array();
202  foreach ($keywords_list as $keyword) {
203  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
204  }
205  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($state['asset']);
206 
207  $email_format_attr->sendMail($replacements);
208 
209  if (is_null($old_current_asset)) {
210  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
211  } else {
212  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
213  }
214 
215  return Array(
216  'assetid' => $assetid,
217  );
218 
219  }//end execute()
220 
221 
232  public static function getInterface($settings, $prefix, $write_access=FALSE)
233  {
234  ob_start();
235  $to_owner = array_get_index($settings, 'to_owner', 0);
236 
237  $current_roles = array_get_index($settings, 'roles', Array());
238  $current_groups = array_get_index($settings, 'groups', Array());
239 
240  $new_prefix = str_replace(array('[',']'), '_', $prefix);
241  $email_format_value = array_get_index($settings, 'email_format_value', Array());
242  $atr_email_format = new Asset_Attribute_Email_Format(0, $email_format_value);
243  $atr_email_format->setEditParam('with_asset_finder', TRUE);
244  // set attr value for show_asset_to_send to true so that we have an option to send as asset in email instead
245  $atr_email_format->setEditParam('show_asset_to_send', TRUE);
246 
247  $parameter_map_value = array_get_index($settings, 'email_recipient_map', serialize(Array()));
248  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
249 
250  $atr_parameter_map->setParameter('user_assetid', 'Recipient Asset IDs');
251  $atr_parameter_map->setParameter('user_email', 'Recipient E-mail Addresses');
252 
253  //parameter map for Asset To Send
254  $ast2send_param_map_value = array_get_index($settings, 'asset_to_send_map', serialize(Array()));
255  $ast2send_atr_param_map = new Asset_Attribute_Parameter_Map(0, $ast2send_param_map_value);
256 
257  $ast2send_atr_param_map->setParameter('ast2send_assetid', 'Asset To Send Asset ID');
258 
259  ?>
260  <table class="sq-backend-table" border="1" style="width: auto">
261  <tr>
262  <th><?php echo translate('trigger_active_user_recipient'); ?></th>
263  <td>
264  <?php
265  if ($write_access) {
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>';
269  } else {
270  ?>
271  <img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo ($to_owner ? 'tick' : 'cross'); ?>.gif" width="15" height="15" />
272  <?php
273  echo translate('trigger_send_to_active_user');
274  }
275  ?>
276  </td>
277  </tr>
278  <tr>
279  <th><?php echo translate('trigger_group_recipients'); ?></th>
280  <td>
281  <?php
282  if ($write_access) {
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>';
285  } else {
286  if (empty($current_groups)) {
287  echo translate('none');
288  } else {
289  foreach ($current_groups as $group) {
290  echo get_asset_tag_line($group).'<br />';
291  }
292  }
293  }
294  ?>
295  </td>
296  </tr>
297  <tr>
298  <th><?php echo translate('trigger_role_recipients'); ?></th>
299  <td>
300  <?php
301  if ($write_access) {
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>';
304  } else {
305  if (empty($current_roles)) {
306  echo translate('none');
307  } else {
308  foreach ($current_roles as $role) {
309  echo get_asset_tag_line($role).'<br />';
310  }
311  }
312  }
313  ?>
314  </td>
315  </tr>
316  <tr>
317  <th><?php echo 'Recipient Source'; ?></th>
318  <td>
319  <?php echo $atr_parameter_map->paint($new_prefix.'_param', !$write_access); ?>
320  </td>
321  </tr>
322  <tr>
323  <th><?php echo 'Asset To Send'; ?></th>
324  <td>
325  <?php
326  //checkbox for Asset To Send
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]');
329 
330  //parameter map for Asset To Send
331  echo $ast2send_atr_param_map->paint($new_prefix.'_ast2send_param', !$write_access);
332  ?>
333  </td>
334  </tr>
335  <tr>
336  <th><?php echo translate('trigger_general_email_settings'); ?></th>
337  <td>
338  <?php $atr_email_format->paint($new_prefix.'_email', !$write_access); ?>
339  </td>
340  </tr>
341  </table>
342  <?php
343  hidden_field($prefix.'[new_prefix]', $new_prefix);
344  return ob_get_clean();
345 
346  }//end getInterface()
347 
348 
360  public static function processInterface(&$settings, $request_data)
361  {
362  // save other settings
363  $settings['to_owner'] = array_get_index($request_data, 'to_owner', FALSE);
364 
365  // group recipients
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'];
371  }
372  }
373  $settings['groups'] = $group_ids;
374 
375  // role recipients
376  $roles = array_get_index($request_data, 'roles', Array());
377  $role_ids = Array();
378  foreach ($roles as $role) {
379  if (!empty($role['assetid'])) {
380  $role_ids[] = $role['assetid'];
381  }
382  }
383  $settings['roles'] = $role_ids;
384 
385  // create a wysiwyg compatable prefix
386  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
387 
388  $email_format = new Asset_Attribute_Email_Format();
389  $email_format->process($new_prefix.'_email');
390  $settings['email_format_value'] = $email_format->value;
391 
392  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
393  $atr_parameter_map->process($new_prefix.'_param');
394 
395  $settings['email_recipient_map'] = $atr_parameter_map->value;
396 
397  //checkbox for Asset To Send
398  $settings['override_asset_to_send'] = array_get_index($request_data, 'override_asset_to_send', FALSE);
399 
400  //parameter map for Asset To Send
401  $ast2send_param_map = new Asset_Attribute_Parameter_Map();
402  $ast2send_param_map->process($new_prefix.'_ast2send_param');
403 
404  $settings['asset_to_send_map'] = $ast2send_param_map->value;
405 
406  return FALSE;
407 
408  }//end processInterface()
409 
410 
420  public static function getLocks($settings, &$state)
421  {
422  return Array($state['assetid'] => Array('lookups'));
423 
424  }//end getLocks()
425 
426 
435  private static function _parseCommaSeparatedString($str)
436  {
437  $result = Array();
438  $pieces = explode(',', $str);
439  foreach ($pieces as $piece) {
440  $item = trim($piece);
441  if ($item != '') {
442  $result[] = $item;
443  }
444  }
445 
446  return $result;
447 
448  }//end _parseCommaSeparatedString()
449 
450 
451 }//end class
452 
453 ?>