Example #1
0
 public function postProcess()
 {
     $values = $this->controller->exportValues($this->_name);
     //build hidden smart group. when user want to send  mailing
     //through search contact-> more action -> send Mailing. CRM-3711
     $groups = array();
     if ($this->_searchBasedMailing && $this->_contactIds) {
         //get the hidden smart group id.
         $ssId = $this->get('ssID');
         $session = CRM_Core_Session::singleton();
         $hiddenSmartParams = array('group_type' => array('2' => 1), 'form_values' => $this->get('formValues'), 'saved_search_id' => $ssId, 'search_custom_id' => $this->get('customSearchID'), 'search_context' => $this->get('context'));
         require_once 'CRM/Contact/BAO/Group.php';
         list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams);
         //set the saved search id.
         if (!$ssId) {
             if ($savedSearchId) {
                 $this->set('ssID', $savedSearchId);
             } else {
                 CRM_Core_Error::fatal();
             }
         }
         //get the base group for this mailing, CRM-3711
         $groups['base'] = array($values['baseGroup']);
         $values['includeGroups'][] = $smartGroupId;
     }
     foreach (array('name', 'group_id', 'search_id', 'search_args') as $n) {
         if (CRM_Utils_Array::value($n, $values)) {
             $params[$n] = $values[$n];
         }
     }
     $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
     $this->set('name', $params['name']);
     $inGroups = $values['includeGroups'];
     $outGroups = $values['excludeGroups'];
     $inMailings = $values['includeMailings'];
     $outMailings = $values['excludeMailings'];
     if (is_array($inGroups)) {
         foreach ($inGroups as $key => $id) {
             if ($id) {
                 $groups['include'][] = $id;
             }
         }
     }
     if (is_array($outGroups)) {
         foreach ($outGroups as $key => $id) {
             if ($id) {
                 $groups['exclude'][] = $id;
             }
         }
     }
     $mailings = array();
     if (is_array($inMailings)) {
         foreach ($inMailings as $key => $id) {
             if ($id) {
                 $mailings['include'][] = $id;
             }
         }
     }
     if (is_array($outMailings)) {
         foreach ($outMailings as $key => $id) {
             if ($id) {
                 $mailings['exclude'][] = $id;
             }
         }
     }
     $session = CRM_Core_Session::singleton();
     $params['groups'] = $groups;
     $params['mailings'] = $mailings;
     if ($this->get('mailing_id')) {
         $ids = array();
         // don't create a new mailing if already exists
         $ids['mailing_id'] = $this->get('mailing_id');
         $groupTableName = CRM_Contact_BAO_Group::getTableName();
         $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
         // delete previous includes/excludes, if mailing already existed
         require_once 'CRM/Contact/DAO/Group.php';
         foreach (array('groups', 'mailings') as $entity) {
             $mg = new CRM_Mailing_DAO_Group();
             $mg->mailing_id = $ids['mailing_id'];
             $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName;
             $mg->find();
             while ($mg->fetch()) {
                 $mg->delete();
             }
         }
     } else {
         // new mailing, so lets set the created_id
         $session = CRM_Core_Session::singleton();
         $params['created_id'] = $session->get('userID');
         $params['created_date'] = date('YmdHis');
     }
     require_once 'CRM/Mailing/BAO/Mailing.php';
     $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
     $this->set('mailing_id', $mailing->id);
     $count = CRM_Mailing_BAO_Mailing::getRecipientsCount(true, false, $mailing->id);
     $this->set('count', $count);
     $this->assign('count', $count);
     $this->set('groups', $groups);
     $this->set('mailings', $mailings);
     if ($qf_Group_submit) {
         //when user perform mailing from search context
         //redirect it to search result CRM-3711.
         $ssID = $this->get('ssID');
         $context = $this->get('context');
         if ($ssID && $this->_searchBasedMailing) {
             if ($this->_action == CRM_Core_Action::BASIC) {
                 $fragment = 'search';
             } else {
                 if ($this->_action == CRM_Core_Action::PROFILE) {
                     $fragment = 'search/builder';
                 } else {
                     if ($this->_action == CRM_Core_Action::ADVANCED) {
                         $fragment = 'search/advanced';
                     } else {
                         $fragment = 'search/custom';
                     }
                 }
             }
             $context = $this->get('context');
             if (!CRM_Contact_Form_Search::isSearchContext($context)) {
                 $context = 'search';
             }
             $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
             $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
             if (CRM_Utils_Rule::qfKey($qfKey)) {
                 $urlParams .= "&qfKey={$qfKey}";
             }
             $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             $status = ts("Your mailing has been saved. You can continue later by clicking the 'Continue' action to resume working on it.<br /> From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
             CRM_Core_Session::setStatus($status);
             //replace user context to search.
             $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
             CRM_Utils_System::redirect($url);
         } else {
             $status = ts("Your mailing has been saved. Click the 'Continue' action to resume working on it.");
             CRM_Core_Session::setStatus($status);
             $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             CRM_Utils_System::redirect($url);
         }
     }
 }
Example #2
0
 /**
  * Unsubscribe a contact from all groups that received this mailing
  *
  * @param int $job_id       The job ID
  * @param int $queue_id     The Queue Event ID of the recipient
  * @param string $hash      The hash
  * @param boolean $return   If true return the list of groups.
  * @return array|null $groups    Array of all groups from which the contact was removed, or null if the queue event could not be found.
  * @access public
  * @static
  */
 public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = false)
 {
     /* First make sure there's a matching queue event */
     $q =& CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     if (!$q) {
         return null;
     }
     $contact_id = $q->contact_id;
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $do =& new CRM_Core_DAO();
     $mg = CRM_Mailing_DAO_Group::getTableName();
     $job = CRM_Mailing_BAO_Job::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $group = CRM_Contact_BAO_Group::getTableName();
     $gc = CRM_Contact_BAO_GroupContact::getTableName();
     $do->query("\n            SELECT      {$mg}.entity_table as entity_table,\n                        {$mg}.entity_id as entity_id\n            FROM        {$mg}\n            INNER JOIN  {$job}\n                ON      {$job}.mailing_id = {$mg}.mailing_id\n            WHERE       {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n                AND     {$mg}.group_type = 'Include'");
     /* Make a list of groups and a list of prior mailings that received 
      * this mailing */
     $groups = array();
     $mailings = array();
     while ($do->fetch()) {
         if ($do->entity_table == $group) {
             //$groups[$do->entity_id] = true;
             $groups[$do->entity_id] = null;
         } else {
             if ($do->entity_table == $mailing) {
                 $mailings[] = $do->entity_id;
             }
         }
     }
     /* As long as we have prior mailings, find their groups and add to the
      * list */
     while (!empty($mailings)) {
         $do->query("\n                SELECT      {$mg}.entity_table as entity_table,\n                            {$mg}.entity_id as entity_id\n                FROM        {$mg}\n                WHERE       {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n                    AND     {$mg}.group_type = 'Include'");
         $mailings = array();
         while ($do->fetch()) {
             if ($do->entity_table == $group) {
                 $groups[$do->entity_id] = true;
             } else {
                 if ($do->entity_table == $mailing) {
                     $mailings[] = $do->entity_id;
                 }
             }
         }
     }
     /* Now we have a complete list of recipient groups.  Filter out all
      * those except smart groups and those that the contact belongs to */
     $do->query("\n            SELECT      {$group}.id as group_id,\n                        {$group}.title as title,\n                        {$group}.description as description\n            FROM        {$group}\n            LEFT JOIN   {$gc}\n                ON      {$gc}.group_id = {$group}.id\n            WHERE       {$group}.id IN (" . implode(', ', array_keys($groups)) . ")\n                AND     ({$group}.saved_search_id is not null\n                            OR  ({$gc}.contact_id = {$contact_id}\n                                AND {$gc}.status = 'Added')\n                        )");
     if ($return) {
         while ($do->fetch()) {
             $groups[$do->group_id] = array('title' => $do->title, 'description' => $do->description);
         }
         return $groups;
     } else {
         while ($do->fetch()) {
             $groups[$do->group_id] = $do->title;
         }
     }
     $contacts = array($contact_id);
     foreach ($groups as $group_id => $group_name) {
         $notremoved = false;
         if ($group_name) {
             list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email');
         }
         if ($notremoved) {
             unset($groups[$group_id]);
         }
     }
     $ue =& new CRM_Mailing_Event_BAO_Unsubscribe();
     $ue->event_queue_id = $queue_id;
     $ue->org_unsubscribe = 0;
     $ue->time_stamp = date('YmdHis');
     $ue->save();
     $transaction->commit();
     return $groups;
 }
 public function postProcess()
 {
     $values = $this->controller->exportValues($this->_name);
     //build hidden smart group. when user want to send  mailing
     //through search contact-> more action -> send Mailing. CRM-3711
     $groups = array();
     if ($this->_searchBasedMailing && $this->_contactIds) {
         $session = CRM_Core_Session::singleton();
         if ($this->_resultSelectOption == 'ts_sel') {
             // create a static grp if only a subset of result set was selected:
             $randID = md5(time());
             $grpTitle = "Hidden Group {$randID}";
             $grpID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $grpTitle, 'id', 'title');
             if (!$grpID) {
                 $groupParams = array('title' => $grpTitle, 'is_active' => 1, 'is_hidden' => 1, 'group_type' => array('2' => 1));
                 $group = CRM_Contact_BAO_Group::create($groupParams);
                 $grpID = $group->id;
                 CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $group->id);
                 $newGroupTitle = "Hidden Group {$grpID}";
                 $groupParams = array('id' => $grpID, 'name' => CRM_Utils_String::titleToVar($newGroupTitle), 'title' => $newGroupTitle);
                 $group = CRM_Contact_BAO_Group::create($groupParams);
             }
             // note at this point its a static group
             $smartGroupId = $grpID;
         } else {
             //get the hidden smart group id.
             $ssId = $this->get('ssID');
             $hiddenSmartParams = array('group_type' => array('2' => 1), 'form_values' => $this->get('formValues'), 'saved_search_id' => $ssId, 'search_custom_id' => $this->get('customSearchID'), 'search_context' => $this->get('context'));
             list($smartGroupId, $savedSearchId) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams);
             //set the saved search id.
             if (!$ssId) {
                 if ($savedSearchId) {
                     $this->set('ssID', $savedSearchId);
                 } else {
                     CRM_Core_Error::fatal();
                 }
             }
         }
         //get the base group for this mailing, CRM-3711
         $groups['base'] = array($values['baseGroup']);
         $values['includeGroups'][] = $smartGroupId;
     }
     foreach (array('name', 'group_id', 'search_id', 'search_args', 'campaign_id', 'dedupe_email') as $n) {
         if (CRM_Utils_Array::value($n, $values)) {
             $params[$n] = $values[$n];
         }
     }
     $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
     $this->set('name', $params['name']);
     $inGroups = $values['includeGroups'];
     $outGroups = $values['excludeGroups'];
     $inMailings = $values['includeMailings'];
     $outMailings = $values['excludeMailings'];
     if (is_array($inGroups)) {
         foreach ($inGroups as $key => $id) {
             if ($id) {
                 $groups['include'][] = $id;
             }
         }
     }
     if (is_array($outGroups)) {
         foreach ($outGroups as $key => $id) {
             if ($id) {
                 $groups['exclude'][] = $id;
             }
         }
     }
     $mailings = array();
     if (is_array($inMailings)) {
         foreach ($inMailings as $key => $id) {
             if ($id) {
                 $mailings['include'][] = $id;
             }
         }
     }
     if (is_array($outMailings)) {
         foreach ($outMailings as $key => $id) {
             if ($id) {
                 $mailings['exclude'][] = $id;
             }
         }
     }
     $session = CRM_Core_Session::singleton();
     $params['groups'] = $groups;
     $params['mailings'] = $mailings;
     if ($this->get('mailing_id')) {
         $ids = array();
         // don't create a new mailing if already exists
         $ids['mailing_id'] = $this->get('mailing_id');
         $groupTableName = CRM_Contact_BAO_Group::getTableName();
         $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
         // delete previous includes/excludes, if mailing already existed
         foreach (array('groups', 'mailings') as $entity) {
             $mg = new CRM_Mailing_DAO_Group();
             $mg->mailing_id = $ids['mailing_id'];
             $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName;
             $mg->find();
             while ($mg->fetch()) {
                 $mg->delete();
             }
         }
     } else {
         // new mailing, so lets set the created_id
         $session = CRM_Core_Session::singleton();
         $params['created_id'] = $session->get('userID');
         $params['created_date'] = date('YmdHis');
     }
     $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
     $this->set('mailing_id', $mailing->id);
     $dedupeEmail = FALSE;
     if (isset($params['dedupe_email'])) {
         $dedupeEmail = $params['dedupe_email'];
     }
     // also compute the recipients and store them in the mailing recipients table
     CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, $mailing->id, NULL, NULL, TRUE, $dedupeEmail);
     $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
     $this->set('count', $count);
     $this->assign('count', $count);
     $this->set('groups', $groups);
     $this->set('mailings', $mailings);
     if ($qf_Group_submit) {
         //when user perform mailing from search context
         //redirect it to search result CRM-3711.
         $ssID = $this->get('ssID');
         $context = $this->get('context');
         if ($ssID && $this->_searchBasedMailing) {
             if ($this->_action == CRM_Core_Action::BASIC) {
                 $fragment = 'search';
             } elseif ($this->_action == CRM_Core_Action::PROFILE) {
                 $fragment = 'search/builder';
             } elseif ($this->_action == CRM_Core_Action::ADVANCED) {
                 $fragment = 'search/advanced';
             } else {
                 $fragment = 'search/custom';
             }
             $context = $this->get('context');
             if (!CRM_Contact_Form_Search::isSearchContext($context)) {
                 $context = 'search';
             }
             $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
             $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
             if (CRM_Utils_Rule::qfKey($qfKey)) {
                 $urlParams .= "&qfKey={$qfKey}";
             }
             $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             $status = ts("Your mailing has been saved. You can continue later by clicking the 'Continue' action to resume working on it.<br /> From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
             CRM_Core_Session::setStatus($status);
             //replace user context to search.
             $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
             return $this->controller->setDestination($url);
         } else {
             $status = ts("Your mailing has been saved. Click the 'Continue' action to resume working on it.");
             CRM_Core_Session::setStatus($status);
             $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             return $this->controller->setDestination($url);
         }
     }
 }
 public function postProcess()
 {
     $values = $this->controller->exportValues($this->_name);
     $groups = array();
     foreach (array('name', 'group_id', 'is_sms') as $n) {
         if (CRM_Utils_Array::value($n, $values)) {
             $params[$n] = $values[$n];
         }
     }
     $qf_Group_submit = $this->controller->exportValue($this->_name, '_qf_Group_submit');
     $this->set('name', $params['name']);
     $inGroups = $values['includeGroups'];
     $outGroups = $values['excludeGroups'];
     $inMailings = $values['includeMailings'];
     $outMailings = $values['excludeMailings'];
     if (is_array($inGroups)) {
         foreach ($inGroups as $key => $id) {
             if ($id) {
                 $groups['include'][] = $id;
             }
         }
     }
     if (is_array($outGroups)) {
         foreach ($outGroups as $key => $id) {
             if ($id) {
                 $groups['exclude'][] = $id;
             }
         }
     }
     $mailings = array();
     if (is_array($inMailings)) {
         foreach ($inMailings as $key => $id) {
             if ($id) {
                 $mailings['include'][] = $id;
             }
         }
     }
     if (is_array($outMailings)) {
         foreach ($outMailings as $key => $id) {
             if ($id) {
                 $mailings['exclude'][] = $id;
             }
         }
     }
     $session = CRM_Core_Session::singleton();
     $params['groups'] = $groups;
     $params['mailings'] = $mailings;
     $ids = array();
     if ($this->get('mailing_id')) {
         // don't create a new mass sms if already exists
         $ids['mailing_id'] = $this->get('mailing_id');
         $groupTableName = CRM_Contact_BAO_Group::getTableName();
         $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
         // delete previous includes/excludes, if mailing already existed
         foreach (array('groups', 'mailings') as $entity) {
             $mg = new CRM_Mailing_DAO_Group();
             $mg->mailing_id = $ids['mailing_id'];
             $mg->entity_table = $entity == 'groups' ? $groupTableName : $mailingTableName;
             $mg->find();
             while ($mg->fetch()) {
                 $mg->delete();
             }
         }
     } else {
         // new mailing, so lets set the created_id
         $session = CRM_Core_Session::singleton();
         $params['created_id'] = $session->get('userID');
         $params['created_date'] = date('YmdHis');
     }
     $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
     $this->set('mailing_id', $mailing->id);
     // also compute the recipients and store them in the mailing recipients table
     CRM_Mailing_BAO_Mailing::getRecipients($mailing->id, $mailing->id, NULL, NULL, TRUE, FALSE, 'sms');
     $count = CRM_Mailing_BAO_Recipients::mailingSize($mailing->id);
     $this->set('count', $count);
     $this->assign('count', $count);
     $this->set('groups', $groups);
     $this->set('mailings', $mailings);
     if ($qf_Group_submit) {
         $status = ts("Your Mass SMS has been saved.");
         CRM_Core_Session::setStatus($status);
         $url = CRM_Utils_System::url('civicrm/mailing', 'reset=1&sms=1');
         return $this->controller->setDestination($url);
     }
 }
 /**
  * Get the search based mailing Ids
  *
  * @return array $mailingIDs, searched base mailing ids.
  * @access public
  */
 public function searchMailingIDs()
 {
     $group = CRM_Mailing_DAO_Group::getTableName();
     $mailing = self::getTableName();
     $query = "\nSELECT  {$mailing}.id as mailing_id\n  FROM  {$mailing}, {$group}\n WHERE  {$group}.mailing_id = {$mailing}.id\n   AND  {$group}.group_type = 'Base'";
     $searchDAO = CRM_Core_DAO::executeQuery($query);
     $mailingIDs = array();
     while ($searchDAO->fetch()) {
         $mailingIDs[] = $searchDAO->mailing_id;
     }
     return $mailingIDs;
 }
 /**
  * Unsubscribe a contact from all groups that received this mailing
  *
  * @param int $job_id       The job ID
  * @param int $queue_id     The Queue Event ID of the recipient
  * @param string $hash      The hash
  * @param boolean $return   If true return the list of groups.
  *
  * @return array|null $groups    Array of all groups from which the contact was removed, or null if the queue event could not be found.
  * @access public
  * @static
  */
 public static function &unsub_from_mailing($job_id, $queue_id, $hash, $return = FALSE)
 {
     /* First make sure there's a matching queue event */
     $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     $success = NULL;
     if (!$q) {
         return $success;
     }
     $contact_id = $q->contact_id;
     $transaction = new CRM_Core_Transaction();
     $do = new CRM_Core_DAO();
     $mgObject = new CRM_Mailing_DAO_Group();
     $mg = $mgObject->getTableName();
     $jobObject = new CRM_Mailing_BAO_Job();
     $job = $jobObject->getTableName();
     $mailingObject = new CRM_Mailing_BAO_Mailing();
     $mailing = $mailingObject->getTableName();
     $groupObject = new CRM_Contact_BAO_Group();
     $group = $groupObject->getTableName();
     $gcObject = new CRM_Contact_BAO_GroupContact();
     $gc = $gcObject->getTableName();
     //We Need the mailing Id for the hook...
     $do->query("SELECT {$job}.mailing_id as mailing_id \n                     FROM   {$job} \n                     WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $do->fetch();
     $mailing_id = $do->mailing_id;
     $do->query("\n            SELECT      {$mg}.entity_table as entity_table,\n                        {$mg}.entity_id as entity_id,\n                        {$mg}.group_type as group_type\n            FROM        {$mg}\n            INNER JOIN  {$job}\n                ON      {$job}.mailing_id = {$mg}.mailing_id\n            INNER JOIN  {$group}\n                ON      {$mg}.entity_id = {$group}.id\n            WHERE       {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n                AND     {$mg}.group_type IN ('Include', 'Base') \n                AND     {$group}.is_hidden = 0");
     /* Make a list of groups and a list of prior mailings that received 
      * this mailing */
     $groups = array();
     $base_groups = array();
     $mailings = array();
     while ($do->fetch()) {
         if ($do->entity_table == $group) {
             if ($do->group_type == 'Base') {
                 $base_groups[$do->entity_id] = NULL;
             } else {
                 $groups[$do->entity_id] = NULL;
             }
         } elseif ($do->entity_table == $mailing) {
             $mailings[] = $do->entity_id;
         }
     }
     /* As long as we have prior mailings, find their groups and add to the
      * list */
     while (!empty($mailings)) {
         $do->query("\n                SELECT      {$mg}.entity_table as entity_table,\n                            {$mg}.entity_id as entity_id\n                FROM        {$mg}\n                WHERE       {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n                    AND     {$mg}.group_type = 'Include'");
         $mailings = array();
         while ($do->fetch()) {
             if ($do->entity_table == $group) {
                 $groups[$do->entity_id] = TRUE;
             } elseif ($do->entity_table == $mailing) {
                 $mailings[] = $do->entity_id;
             }
         }
     }
     //Pass the groups to be unsubscribed from through a hook.
     $group_ids = array_keys($groups);
     $base_group_ids = array_keys($base_groups);
     CRM_Utils_Hook::unsubscribeGroups('unsubscribe', $mailing_id, $contact_id, $group_ids, $base_group_ids);
     /* Now we have a complete list of recipient groups.  Filter out all
      * those except smart groups, those that the contact belongs to and
      * base groups from search based mailings */
     $baseGroupClause = '';
     if (!empty($base_group_ids)) {
         $baseGroupClause = "OR  {$group}.id IN(" . implode(', ', $base_group_ids) . ")";
     }
     $do->query("\n            SELECT      {$group}.id as group_id,\n                        {$group}.title as title,\n                        {$group}.description as description\n            FROM        {$group}\n            LEFT JOIN   {$gc}\n                ON      {$gc}.group_id = {$group}.id\n            WHERE       {$group}.id IN (" . implode(', ', array_merge($group_ids, $base_group_ids)) . ")\n                AND     {$group}.is_hidden = 0\n                AND     ({$group}.saved_search_id is not null\n                            OR  ({$gc}.contact_id = {$contact_id}\n                                AND {$gc}.status = 'Added')\n                            {$baseGroupClause}\n                        )");
     if ($return) {
         $returnGroups = array();
         while ($do->fetch()) {
             $returnGroups[$do->group_id] = array('title' => $do->title, 'description' => $do->description);
         }
         return $returnGroups;
     } else {
         while ($do->fetch()) {
             $groups[$do->group_id] = $do->title;
         }
     }
     $contacts = array($contact_id);
     foreach ($groups as $group_id => $group_name) {
         $notremoved = FALSE;
         if ($group_name) {
             if (in_array($group_id, $base_group_ids)) {
                 list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email', 'Removed');
             } else {
                 list($total, $removed, $notremoved) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contacts, $group_id, 'Email');
             }
         }
         if ($notremoved) {
             unset($groups[$group_id]);
         }
     }
     $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
     $ue->event_queue_id = $queue_id;
     $ue->org_unsubscribe = 0;
     $ue->time_stamp = date('YmdHis');
     $ue->save();
     $transaction->commit();
     return $groups;
 }
Example #7
0
 /**
  * adds $value['foo_display'] for each $value['foo'] enum from civicrm_mailing_group
  *
  * @param array $values (reference)  the array up for enhancing
  * @return void
  */
 static function addDisplayEnums(&$values)
 {
     $enumFields =& CRM_Mailing_DAO_Group::getEnums();
     foreach ($enumFields as $enum) {
         if (isset($values[$enum])) {
             $values[$enum . '_display'] = CRM_Mailing_DAO_Group::tsEnum($enum, $values[$enum]);
         }
     }
 }
Example #8
0
 /**
  * Generate a report.  Fetch event count information, mailing data, and job
  * status.
  *
  * @param int $id       The mailing id to report
  * @return array        Associative array of reporting data
  * @access public
  * @static
  */
 function &report($id)
 {
     $mailing_id = CRM_Utils_Type::escape($id, 'Integer');
     $mailing =& new CRM_Mailing_BAO_Mailing();
     require_once 'CRM/Mailing/Event/BAO/Opened.php';
     require_once 'CRM/Mailing/Event/BAO/Reply.php';
     require_once 'CRM/Mailing/Event/BAO/Unsubscribe.php';
     require_once 'CRM/Mailing/Event/BAO/Forward.php';
     require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php';
     $t = array('mailing' => CRM_Mailing_BAO_Mailing::getTableName(), 'mailing_group' => CRM_Mailing_DAO_Group::getTableName(), 'group' => CRM_Contact_BAO_Group::getTableName(), 'job' => CRM_Mailing_BAO_Job::getTableName(), 'queue' => CRM_Mailing_Event_BAO_Queue::getTableName(), 'delivered' => CRM_Mailing_Event_BAO_Delivered::getTableName(), 'opened' => CRM_Mailing_Event_BAO_Opened::getTableName(), 'reply' => CRM_Mailing_Event_BAO_Reply::getTableName(), 'unsubscribe' => CRM_Mailing_Event_BAO_Unsubscribe::getTableName(), 'bounce' => CRM_Mailing_Event_BAO_Bounce::getTableName(), 'forward' => CRM_Mailing_Event_BAO_Forward::getTableName(), 'url' => CRM_Mailing_BAO_TrackableURL::getTableName(), 'urlopen' => CRM_Mailing_Event_BAO_TrackableURLOpen::getTableName(), 'component' => CRM_Mailing_BAO_Component::getTableName());
     $report = array();
     /* FIXME: put some permissioning in here */
     /* Get the mailing info */
     $mailing->query("\n            SELECT          {$t['mailing']}.*\n            FROM            {$t['mailing']}\n            WHERE           {$t['mailing']}.id = {$mailing_id}");
     $mailing->fetch();
     $report['mailing'] = array();
     foreach (array_keys(CRM_Mailing_BAO_Mailing::fields()) as $field) {
         $report['mailing'][$field] = $mailing->{$field};
     }
     /* Get the component info */
     $query = array();
     $components = array('header' => ts('Header'), 'footer' => ts('Footer'), 'reply' => ts('Reply'), 'unsubscribe' => ts('Unsubscribe'), 'optout' => ts('Opt-Out'));
     foreach (array_keys($components) as $type) {
         $query[] = "SELECT          {$t['component']}.name as name,\n                                        '{$type}' as type,\n                                        {$t['component']}.id as id\n                        FROM            {$t['component']}\n                        INNER JOIN      {$t['mailing']}\n                                ON      {$t['mailing']}.{$type}_id =\n                                                {$t['component']}.id\n                        WHERE           {$t['mailing']}.id = {$mailing_id}";
     }
     $q = '(' . implode(') UNION (', $query) . ')';
     $mailing->query($q);
     $report['component'] = array();
     while ($mailing->fetch()) {
         $report['component'][] = array('type' => $components[$mailing->type], 'name' => $mailing->name, 'link' => CRM_Utils_System::url('civicrm/mailing/component', "reset=1&action=update&id={$mailing->id}"));
     }
     /* Get the recipient group info */
     $mailing->query("\n            SELECT          {$t['mailing_group']}.group_type as group_type,\n                            {$t['group']}.id as group_id,\n                            {$t['group']}.name as group_name,\n                            {$t['mailing']}.id as mailing_id,\n                            {$t['mailing']}.name as mailing_name\n            FROM            {$t['mailing_group']}\n            LEFT JOIN       {$t['group']}\n                    ON      {$t['mailing_group']}.entity_id = {$t['group']}.id\n                    AND     {$t['mailing_group']}.entity_table =\n                                                                '{$t['group']}'\n            LEFT JOIN       {$t['mailing']}\n                    ON      {$t['mailing_group']}.entity_id =\n                                                            {$t['mailing']}.id\n                    AND     {$t['mailing_group']}.entity_table =\n                                                            '{$t['mailing']}'\n\n            WHERE           {$t['mailing_group']}.mailing_id = {$mailing_id}\n            ");
     $report['group'] = array('include' => array(), 'exclude' => array());
     while ($mailing->fetch()) {
         $row = array();
         if (isset($mailing->group_id)) {
             $row['id'] = $mailing->group_id;
             $row['name'] = $mailing->group_name;
             $row['link'] = CRM_Utils_System::url('civicrm/group/search', "reset=1&force=1&context=smog&gid={$row['id']}");
         } else {
             $row['id'] = $mailing->mailing_id;
             $row['name'] = $mailing->mailing_name;
             $row['mailing'] = true;
             $row['link'] = CRM_Utils_System::url('civicrm/mailing/report', "mid={$row['id']}");
         }
         if ($mailing->group_type == 'Include') {
             $report['group']['include'][] = $row;
         } else {
             $report['group']['exclude'][] = $row;
         }
     }
     /* Get the event totals, grouped by job (retries) */
     $mailing->query("\n            SELECT          {$t['job']}.*,\n                            COUNT(DISTINCT {$t['queue']}.id) as queue,\n                            COUNT(DISTINCT {$t['delivered']}.id) as delivered,\n                            COUNT(DISTINCT {$t['opened']}.id) as opened,\n                            COUNT(DISTINCT {$t['reply']}.id) as reply,\n                            COUNT(DISTINCT {$t['unsubscribe']}.id) as unsubscribe,\n                            COUNT(DISTINCT {$t['forward']}.id) as forward,\n                            COUNT(DISTINCT {$t['bounce']}.id) as bounce,\n                            COUNT(DISTINCT {$t['urlopen']}.id) as url\n            FROM            {$t['job']}\n            LEFT JOIN       {$t['queue']}\n                    ON      {$t['queue']}.job_id = {$t['job']}.id\n            LEFT JOIN       {$t['opened']}\n                    ON      {$t['opened']}.event_queue_id = {$t['queue']}.id\n            LEFT JOIN       {$t['reply']}\n                    ON      {$t['reply']}.event_queue_id = {$t['queue']}.id\n            LEFT JOIN       {$t['forward']}\n                    ON      {$t['forward']}.event_queue_id = {$t['queue']}.id\n            LEFT JOIN       {$t['unsubscribe']}\n                    ON      {$t['unsubscribe']}.event_queue_id = \n                                {$t['queue']}.id\n            LEFT JOIN       {$t['bounce']}\n                    ON      {$t['bounce']}.event_queue_id = {$t['queue']}.id\n            LEFT JOIN       {$t['delivered']}\n                    ON      {$t['delivered']}.event_queue_id = {$t['queue']}.id\n                    AND     {$t['bounce']}.id IS null\n            LEFT JOIN       {$t['urlopen']}\n                    ON      {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n                    \n            WHERE           {$t['job']}.mailing_id = {$mailing_id}\n            GROUP BY        {$t['job']}.id");
     $report['jobs'] = array();
     $report['event_totals'] = array();
     while ($mailing->fetch()) {
         $row = array();
         foreach (array('queue', 'delivered', 'opened', 'url', 'forward', 'reply', 'unsubscribe', 'bounce') as $field) {
             $row[$field] = $mailing->{$field};
             $report['event_totals'][$field] += $mailing->{$field};
         }
         foreach (array_keys(CRM_Mailing_BAO_Job::fields()) as $field) {
             $row[$field] = $mailing->{$field};
         }
         if ($mailing->queue) {
             $row['delivered_rate'] = 100.0 * $mailing->delivered / $mailing->queue;
             $row['bounce_rate'] = 100.0 * $mailing->bounce / $mailing->queue;
             $row['unsubscribe_rate'] = 100.0 * $mailing->unsubscribe / $mailing->queue;
         } else {
             $row['delivered_rate'] = 0;
             $row['bounce_rate'] = 0;
             $row['unsubscribe_rate'] = 0;
         }
         $row['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=click&mid={$mailing_id}&jid={$mailing->id}"), 'queue' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=queue&mid={$mailing_id}&jid={$mailing->id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=delivered&mid={$mailing_id}&jid={$mailing->id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=bounce&mid={$mailing_id}&jid={$mailing->id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=unsubscribe&mid={$mailing_id}&jid={$mailing->id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=forward&mid={$mailing_id}&jid={$mailing->id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=reply&mid={$mailing_id}&jid={$mailing->id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=opened&mid={$mailing_id}&jid={$mailing->id}"));
         foreach (array('scheduled_date', 'start_date', 'end_date') as $key) {
             $row[$key] = CRM_Utils_Date::customFormat($row[$key]);
         }
         $report['jobs'][] = $row;
     }
     if ($report['event_totals']['queue']) {
         $report['event_totals']['delivered_rate'] = 100.0 * $report['event_totals']['delivered'] / $report['event_totals']['queue'];
         $report['event_totals']['bounce_rate'] = 100.0 * $report['event_totals']['bounce'] / $report['event_totals']['queue'];
         $report['event_totals']['unsubscribe_rate'] = 100.0 * $report['event_totals']['unsubscribe'] / $report['event_totals']['queue'];
     } else {
         $report['event_totals']['delivered_rate'] = 0;
         $report['event_totals']['bounce_rate'] = 0;
         $report['event_totals']['unsubscribe_rate'] = 0;
     }
     /* Get the click-through totals, grouped by URL */
     $mailing->query("\n            SELECT      {$t['url']}.url,\n                        {$t['url']}.id,\n                        COUNT({$t['urlopen']}.id) as clicks,\n                        COUNT(DISTINCT {$t['queue']}.id) as unique_clicks\n            FROM        {$t['url']}\n            LEFT JOIN   {$t['urlopen']}\n                    ON  {$t['urlopen']}.trackable_url_id = {$t['url']}.id\n            LEFT JOIN  {$t['queue']}\n                    ON  {$t['urlopen']}.event_queue_id = {$t['queue']}.id\n            LEFT JOIN  {$t['job']}\n                    ON  {$t['queue']}.job_id = {$t['job']}.id\n            WHERE       {$t['url']}.mailing_id = {$mailing_id}\n            GROUP BY    {$t['url']}.id");
     $report['click_through'] = array();
     while ($mailing->fetch()) {
         $report['click_through'][] = array('url' => $mailing->url, 'link' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}"), 'link_unique' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=click&mid={$mailing_id}&uid={$mailing->id}&distinct=1"), 'clicks' => $mailing->clicks, 'unique' => $mailing->unique_clicks, 'rate' => $report['event_totals']['delivered'] ? 100.0 * $mailing->unique_clicks / $report['event_totals']['delivered'] : 0);
     }
     $report['event_totals']['links'] = array('clicks' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=click&mid={$mailing_id}"), 'clicks_unique' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=click&mid={$mailing_id}&distinct=1"), 'queue' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=queue&mid={$mailing_id}"), 'delivered' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=delivered&mid={$mailing_id}"), 'bounce' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=bounce&mid={$mailing_id}"), 'unsubscribe' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=unsubscribe&mid={$mailing_id}"), 'forward' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=forward&mid={$mailing_id}"), 'reply' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=reply&mid={$mailing_id}"), 'opened' => CRM_Utils_System::url('civicrm/mailing/event', "reset=1&event=opened&mid={$mailing_id}"));
     $report['retry'] = CRM_Utils_System::url('civicrm/mailing/retry', "reset=1&mid={$mailing_id}");
     return $report;
 }
Example #9
0
$status = array('Scheduled', 'Running', 'Complete', 'Paused', 'Canceled', 'Testing');
for ($i = 1; $i <= $numGroups; $i++) {
    $mailing = new CRM_Mailing_BAO_Mailing();
    $alphabet = mt_rand(97, 122);
    $cnt = sprintf('%05d', $i);
    $mailing->name = chr($alphabet) . ": {$prefix} {$cnt}";
    $mailing->header_id = $mailing->footer_id = $mailing->reply_id = $mailing->unsubscribe_id = $mailing->optout_id = 1;
    $mailing->is_completed = 1;
    $mailing->save();
    $job = new CRM_Mailing_BAO_Job();
    $job->mailing_id = $mailing->id;
    $job->scheduled_date = generateRandomDate();
    $job->start_date = generateRandomDate();
    $job->end_date = generateRandomDate();
    $job->status = 'Complete';
    $job->save();
    $group = new CRM_Mailing_DAO_Group();
    $group->mailing_id = $mailing->id;
    $group->group_type = 'Include';
    $group->entity_table = 'civicrm_group';
    $group->entity_id = 1;
    $group->save();
}
function generateRandomDate()
{
    $year = 2006 + mt_rand(0, 2);
    $month = 1 + mt_rand(0, 11);
    $day = 1 + mt_rand(0, 27);
    $date = sprintf("%4d%02d%02d", $year, $month, $day) . '000000';
    return $date;
}
 /**
  * Resubscribe a contact to the groups, he/she was unsubscribed from.
  *
  * @param int $job_id       The job ID
  * @param int $queue_id     The Queue Event ID of the recipient
  * @param string $hash      The hash
  *
  * @return array|null $groups    Array of all groups to which the contact was added, or null if the queue event could not be found.
  * @access public
  * @static
  */
 public static function &resub_to_mailing($job_id, $queue_id, $hash)
 {
     /* First make sure there's a matching queue event */
     $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
     $success = NULL;
     if (!$q) {
         return $success;
     }
     // check if this queue_id was actually unsubscribed
     $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
     $ue->event_queue_id = $queue_id;
     $ue->org_unsubscribe = 0;
     if (!$ue->find(TRUE)) {
         return $success;
     }
     $contact_id = $q->contact_id;
     $transaction = new CRM_Core_Transaction();
     $do = new CRM_Core_DAO();
     $mg = CRM_Mailing_DAO_Group::getTableName();
     $job = CRM_Mailing_BAO_Job::getTableName();
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $group = CRM_Contact_BAO_Group::getTableName();
     $gc = CRM_Contact_BAO_GroupContact::getTableName();
     //We Need the mailing Id for the hook...
     $do->query("SELECT {$job}.mailing_id as mailing_id \n                     FROM   {$job} \n                     WHERE {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer'));
     $do->fetch();
     $mailing_id = $do->mailing_id;
     $do->query("\n            SELECT      {$mg}.entity_table as entity_table,\n                        {$mg}.entity_id as entity_id\n            FROM        {$mg}\n            INNER JOIN  {$job}\n                ON      {$job}.mailing_id = {$mg}.mailing_id\n            INNER JOIN  {$group}\n                ON      {$mg}.entity_id = {$group}.id\n            WHERE       {$job}.id = " . CRM_Utils_Type::escape($job_id, 'Integer') . "\n                AND     {$mg}.group_type IN ( 'Include', 'Base' )\n                AND     {$group}.is_hidden = 0");
     /* Make a list of groups and a list of prior mailings that received 
      * this mailing */
     $groups = array();
     $mailings = array();
     while ($do->fetch()) {
         if ($do->entity_table == $group) {
             $groups[$do->entity_id] = NULL;
         } elseif ($do->entity_table == $mailing) {
             $mailings[] = $do->entity_id;
         }
     }
     /* As long as we have prior mailings, find their groups and add to the
      * list */
     while (!empty($mailings)) {
         $do->query("\n                SELECT      {$mg}.entity_table as entity_table,\n                            {$mg}.entity_id as entity_id\n                FROM        {$mg}\n                WHERE       {$mg}.mailing_id IN (" . implode(', ', $mailings) . ")\n                    AND     {$mg}.group_type = 'Include'");
         $mailings = array();
         while ($do->fetch()) {
             if ($do->entity_table == $group) {
                 $groups[$do->entity_id] = TRUE;
             } elseif ($do->entity_table == $mailing) {
                 $mailings[] = $do->entity_id;
             }
         }
     }
     $group_ids = array_keys($groups);
     $base_groups = NULL;
     CRM_Utils_Hook::unsubscribeGroups('resubscribe', $mailing_id, $contact_id, $group_ids, $base_groups);
     /* Now we have a complete list of recipient groups.  Filter out all
      * those except smart groups and those that the contact belongs to */
     $do->query("\n            SELECT      {$group}.id as group_id,\n                        {$group}.title as title\n            FROM        {$group}\n            LEFT JOIN   {$gc}\n                ON      {$gc}.group_id = {$group}.id\n            WHERE       {$group}.id IN (" . implode(', ', $group_ids) . ")\n                AND     ({$group}.saved_search_id is not null\n                            OR  ({$gc}.contact_id = {$contact_id}\n                                AND {$gc}.status = 'Removed')\n                        )");
     while ($do->fetch()) {
         $groups[$do->group_id] = $do->title;
     }
     $contacts = array($contact_id);
     foreach ($groups as $group_id => $group_name) {
         $notadded = 0;
         if ($group_name) {
             list($total, $added, $notadded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $group_id, 'Email');
         }
         if ($notadded) {
             unset($groups[$group_id]);
         }
     }
     // remove entry from Unsubscribe table.
     $ue = new CRM_Mailing_Event_BAO_Unsubscribe();
     $ue->event_queue_id = $queue_id;
     $ue->org_resubscribe = 0;
     if ($ue->find(TRUE)) {
         $ue->delete();
     }
     $transaction->commit();
     return $groups;
 }