Example #1
0
 /**
  * Apps email title (assuming that app itself have already loaded the language file before calling this function)
  * APP_{ELEMENT}_{RULENAME}_EMAIL_TITLE
  *
  * Apps email template namespace
  * apps/{group}/{element}/alerts.{rulename}
  *
  * Apps email template path
  * apps/{group}/{element}/themes/{default/themeName}/emails/{html/text}/alerts.{rulename}
  *
  * Core email title
  * COM_EASYSOCIAL_{ELEMENT}_{RULENAME}_EMAIL_TITLE
  *
  * Core email template namespace
  * site/{element}/alerts.{rulename}
  *
  * Core email template path
  * site/themes/{wireframe/themeName}/emails/{html/text}/{element}/alert.{rulename}
  *
  * @since 1.0
  * @access	public
  * @param	array	$participants	The array of participants (user id) of the action
  * @param	array	$options		Custom options of the email notification
  *
  * @return	boolean		State of the email notification
  *
  * @author	Jason Rey <*****@*****.**>
  */
 public function sendEmail($participants, $options = array())
 {
     $users = $this->getUsers('email', $participants);
     if (empty($users)) {
         return true;
     }
     if (is_object($options)) {
         $options = FD::makeArray($options);
     }
     // If params is not set, just give it an empty array
     if (!isset($options['params'])) {
         $options['params'] = array();
     } else {
         // If params is already set, it is possible that it might be object or registry object
         $options['params'] = FD::makeArray($options['params']);
     }
     // Assign any non-table key into params automatically
     $columns = FD::db()->getTableColumns('#__social_mailer');
     foreach ($options as $key => $val) {
         if (!in_array($key, $columns)) {
             $options['params'][$key] = $val;
         }
     }
     // Set default title if no title is passed in
     if (!isset($options['title'])) {
         $options['title'] = $this->getNotificationTitle('email');
     }
     // Set default template if no template is passed in
     if (!isset($options['template'])) {
         $options['template'] = $this->getMailTemplateName();
     }
     if (!isset($options['html'])) {
         $options['html'] = 1;
     }
     $mailer = FD::mailer();
     $data = new SocialMailerData();
     $data->set('title', $options['title']);
     $data->set('template', $options['template']);
     $data->set('html', $options['html']);
     if (isset($options['params'])) {
         $data->setParams($options['params']);
     }
     // If priority is set, set the priority
     if (isset($options['priority'])) {
         $data->set('priority', $options['priority']);
     }
     if (isset($options['sender_name'])) {
         $data->set('sender_name', $options['sender_name']);
     }
     if (isset($options['sender_email'])) {
         $data->set('sender_email', $options['sender_email']);
     }
     if (isset($options['replyto_email'])) {
         $data->set('replyto_email', $options['replyto_email']);
     }
     // The caller might be passing in 'params' as a SocialRegistry object. Just need to standardize them here.
     // Ensure that the params if set is a valid array
     if (isset($options['params']) && is_object($options['params'])) {
         $options['params'] = FD::makeArray($options['params']);
     }
     // Init a few default widely used parameter
     $user = FD::user();
     if (!isset($options['params']['actor'])) {
         $options['params']['actor'] = $user->getName();
     }
     if (!isset($options['params']['posterName'])) {
         $options['params']['posterName'] = $user->getName();
     }
     if (!isset($options['params']['posterAvatar'])) {
         $options['params']['posterAvatar'] = $user->getAvatar();
     }
     if (!isset($options['params']['posterLink'])) {
         $options['params']['posterLink'] = $user->getPermalink(true, true);
     }
     foreach ($users as $uid) {
         $user = FD::user($uid);
         // If user has been blocked, skip this altogether.
         if ($user->block) {
             continue;
         }
         // If user do not have community access, skip this altogether.
         if (!$user->hasCommunityAccess()) {
             continue;
         }
         // Get the params
         $params = $options['params'];
         // Set the language of the email
         $data->setLanguage($user->getLanguage());
         // Detect the "name" in the params. If it doesn't exist, set the target's name.
         if (is_array($params)) {
             if (!isset($params['recipientName'])) {
                 $params['recipientName'] = $user->getName();
             }
             if (!isset($params['recipientAvatar'])) {
                 $params['recipientAvatar'] = $user->getAvatar();
             }
             $data->setParams($params);
         }
         $data->set('recipient_name', $user->getName());
         $data->set('recipient_email', $user->email);
         $mailer->create($data);
     }
     return true;
 }