/**
  * Process the posted form values.  Create and schedule a mailing.
  *
  * @param
  *
  * @return void
  * @access public
  */
 public function postProcess()
 {
     $params = array();
     $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
     if (empty($params['mailing_id'])) {
         CRM_Core_Error::fatal(ts('Could not find a mailing id'));
     }
     foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
         $params[$parameter] = $this->controller->exportValue($this->_name, $parameter);
     }
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $ids['mailing_id'];
     if ($mailing->find(TRUE)) {
         $job = new CRM_Mailing_BAO_Job();
         $job->mailing_id = $mailing->id;
         $job->is_test = 0;
         if ($job->find(TRUE)) {
             CRM_Core_Error::fatal(ts('A job for this mailing already exists'));
         }
         if (empty($mailing->is_template)) {
             $job->status = 'Scheduled';
             if ($params['now']) {
                 $job->scheduled_date = date('YmdHis');
             } else {
                 $job->scheduled_date = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
             }
             $job->save();
         }
         // set approval details if workflow is not enabled
         if (!CRM_Mailing_Info::workflowEnabled()) {
             $session = CRM_Core_Session::singleton();
             $mailing->approver_id = $session->get('userID');
             $mailing->approval_date = date('YmdHis');
             $mailing->approval_status_id = 1;
         } else {
             // reset them in case this mailing was rejected
             $mailing->approver_id = 'null';
             $mailing->approval_date = 'null';
             $mailing->approval_status_id = 'null';
         }
         if ($mailing->approval_date) {
             $mailing->approval_date = CRM_Utils_Date::isoToMysql($mailing->approval_date);
         }
         // also set the scheduled_id
         $session = CRM_Core_Session::singleton();
         $mailing->scheduled_id = $session->get('userID');
         $mailing->scheduled_date = date('YmdHis');
         $mailing->created_date = CRM_Utils_Date::isoToMysql($mailing->created_date);
         $mailing->save();
     }
     //when user perform mailing from search context
     //redirect it to search result CRM-3711.
     $ssID = $this->get('ssID');
     if ($ssID && $this->_searchBasedMailing && !CRM_Mailing_Info::workflowEnabled()) {
         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}";
         }
         $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
         return $this->controller->setDestination($url);
     }
     $session = CRM_Core_Session::singleton();
     $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true'));
 }
 /**
  * Process the posted form values.  Create and schedule a Mass SMS.
  *
  * @param
  *
  * @return void
  * @access public
  */
 public function postProcess()
 {
     $params = array();
     $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
     if (empty($params['mailing_id'])) {
         CRM_Core_Error::fatal(ts('Could not find a mailing id'));
     }
     foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
         $params[$parameter] = $this->controller->exportValue($this->_name, $parameter);
     }
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->id = $ids['mailing_id'];
     if ($mailing->find(TRUE)) {
         $job = new CRM_Mailing_BAO_Job();
         $job->mailing_id = $mailing->id;
         $job->is_test = 0;
         if ($job->find(TRUE)) {
             CRM_Core_Error::fatal(ts('A job for this mailing already exists'));
         }
         if (empty($mailing->is_template)) {
             $job->status = 'Scheduled';
             if ($params['now']) {
                 $job->scheduled_date = date('YmdHis');
             } else {
                 $job->scheduled_date = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
             }
             $job->save();
         }
         // also set the scheduled_id
         $session = CRM_Core_Session::singleton();
         $mailing->scheduled_id = $session->get('userID');
         $mailing->scheduled_date = date('YmdHis');
         $mailing->created_date = CRM_Utils_Date::isoToMysql($mailing->created_date);
         $mailing->save();
     }
     $session = CRM_Core_Session::singleton();
     $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true&sms=1'));
 }
 /**
  * Form rule to send out a test mailing.
  *
  * @param array $params     Array of the form values
  * @param array $files      Any files posted to the form
  * @param array $self       an current this object
  *
  * @return boolean          true on succesful SMTP handoff
  * @access public
  */
 static function &testMail($testParams, $files, $self)
 {
     $error = NULL;
     $urlString = 'civicrm/mailing/send';
     $urlParams = "_qf_Test_display=true&qfKey={$testParams['qfKey']}";
     $ssID = $self->get('ssID');
     if ($ssID && $self->_searchBasedMailing) {
         if ($self->_action == CRM_Core_Action::BASIC) {
             $fragment = 'search';
         } elseif ($self->_action == CRM_Core_Action::PROFILE) {
             $fragment = 'search/builder';
         } elseif ($self->_action == CRM_Core_Action::ADVANCED) {
             $fragment = 'search/advanced';
         } else {
             $fragment = 'search/custom';
         }
         $urlString = 'civicrm/contact/' . $fragment;
     }
     $emails = NULL;
     if (CRM_Utils_Array::value('sendtest', $testParams)) {
         if (!($testParams['test_group'] || $testParams['test_email'])) {
             CRM_Core_Session::setStatus(ts('Your did not provided any email address or selected any group. No test mail is sent.'));
             $error = TRUE;
         }
         if ($testParams['test_email']) {
             $emailAdd = explode(',', $testParams['test_email']);
             foreach ($emailAdd as $key => $value) {
                 $email = trim($value);
                 $testParams['emails'][] = $email;
                 $emails .= $emails ? ",'{$email}'" : "'{$email}'";
                 if (!CRM_Utils_Rule::email($email)) {
                     CRM_Core_Session::setStatus(ts('Please enter valid email addresses only.'));
                     $error = TRUE;
                 }
             }
         }
         if ($error) {
             $url = CRM_Utils_System::url($urlString, $urlParams);
             CRM_Utils_System::redirect($url);
             return $error;
         }
     }
     if (CRM_Utils_Array::value('_qf_Test_submit', $testParams)) {
         //when user perform mailing from search context
         //redirect it to search result CRM-3711.
         if ($ssID && $self->_searchBasedMailing) {
             $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.
             $context = $self->get('context');
             if (!CRM_Contact_Form_Search::isSearchContext($context)) {
                 $context = 'search';
             }
             $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}&qfKey={$testParams['qfKey']}";
             $url = CRM_Utils_System::url($urlString, $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);
         }
     }
     if (CRM_Mailing_Info::workflowEnabled()) {
         if (!CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings')) {
             $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             CRM_Utils_System::redirect($url);
         }
     }
     if (CRM_Utils_Array::value('_qf_Import_refresh', $_POST) || CRM_Utils_Array::value('_qf_Test_next', $testParams) || !CRM_Utils_Array::value('sendtest', $testParams)) {
         $error = TRUE;
         return $error;
     }
     $job = new CRM_Mailing_BAO_Job();
     $job->mailing_id = $self->get('mailing_id');
     $job->is_test = TRUE;
     $job->save();
     $newEmails = NULL;
     $session = CRM_Core_Session::singleton();
     if (!empty($testParams['emails'])) {
         $query = "\n                      SELECT id, contact_id, email  \n                      FROM civicrm_email  \n                      WHERE civicrm_email.email IN ({$emails})";
         $dao = CRM_Core_DAO::executeQuery($query);
         $emailDetail = array();
         // fetch contact_id and email id for all existing emails
         while ($dao->fetch()) {
             $emailDetail[$dao->email] = array('contact_id' => $dao->contact_id, 'email_id' => $dao->id);
         }
         $dao->free();
         foreach ($testParams['emails'] as $key => $email) {
             $email = trim($email);
             $contactId = $emailId = NULL;
             if (array_key_exists($email, $emailDetail)) {
                 $emailId = $emailDetail[$email]['email_id'];
                 $contactId = $emailDetail[$email]['contact_id'];
             }
             if (!$contactId) {
                 //create new contact.
                 $params = array('contact_type' => 'Individual', 'email' => array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => 1)));
                 $contact = CRM_Contact_BAO_Contact::create($params);
                 $emailId = $contact->email[0]->id;
                 $contactId = $contact->id;
                 $contact->free();
             }
             $params = array('job_id' => $job->id, 'email_id' => $emailId, 'contact_id' => $contactId);
             CRM_Mailing_Event_BAO_Queue::create($params);
         }
     }
     $testParams['job_id'] = $job->id;
     $isComplete = FALSE;
     while (!$isComplete) {
         $isComplete = CRM_Mailing_BAO_Job::runJobs($testParams);
     }
     if (CRM_Utils_Array::value('sendtest', $testParams)) {
         $status = ts('Your test message has been sent.');
         if (CRM_Mailing_Info::workflowEnabled()) {
             if (CRM_Core_Permission::check('schedule mailings') && CRM_Core_Permission::check('create mailings') || CRM_Core_Permission::check('access CiviMail')) {
                 $status .= ts(" Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page).");
             }
         } else {
             $status .= ts(" Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page).");
         }
         CRM_Core_Session::setStatus($status);
         $url = CRM_Utils_System::url($urlString, $urlParams);
         CRM_Utils_System::redirect($url);
     }
     $error = TRUE;
     return $error;
 }
 /**
  * cancel a mailing
  *
  * @param int $mailingId  the id of the mailing to be canceled
  * @static
  */
 public static function cancel($mailingId)
 {
     $sql = "\nSELECT *\nFROM   civicrm_mailing_job\nWHERE  mailing_id = %1\nAND    is_test = 0\nAND    ( ( job_type IS NULL ) OR\n           job_type <> 'child' )\n";
     $params = array(1 => array($mailingId, 'Integer'));
     $job = CRM_Core_DAO::executeQuery($sql, $params);
     if ($job->fetch() && in_array($job->status, array('Scheduled', 'Running', 'Paused'))) {
         $newJob = new CRM_Mailing_BAO_Job();
         $newJob->id = $job->id;
         $newJob->end_date = date('YmdHis');
         $newJob->status = 'Canceled';
         $newJob->save();
         // also cancel all child jobs
         $sql = "\nUPDATE civicrm_mailing_job\nSET    status = 'Canceled',\n       end_date = %2\nWHERE  parent_id = %1\nAND    is_test = 0\nAND    job_type = 'child'\nAND    status IN ( 'Scheduled', 'Running', 'Paused' )\n";
         $params = array(1 => array($job->id, 'Integer'), 2 => array(date('YmdHis'), 'Timestamp'));
         CRM_Core_DAO::executeQuery($sql, $params);
         CRM_Core_Session::setStatus(ts('The mailing has been canceled.'));
     }
 }
Beispiel #5
0
 /**
  * cancel a mailing
  *
  * @param int $mailingId  the id of the mailing to be canceled
  * @static
  */
 public static function cancel($mailingId)
 {
     $job = new CRM_Mailing_BAO_Job();
     $job->mailing_id = $mailingId;
     // test mailing should not be included during Cancellation
     $job->is_test = 0;
     if ($job->find(true) and in_array($job->status, array('Scheduled', 'Running', 'Paused'))) {
         // fix MySQL dates...
         $job->scheduled_date = CRM_Utils_Date::isoToMysql($job->scheduled_date);
         $job->start_date = CRM_Utils_Date::isoToMysql($job->start_date);
         $job->end_date = CRM_Utils_Date::isoToMysql($job->end_date);
         $job->status = 'Canceled';
         $job->save();
         CRM_Core_Session::setStatus(ts('The mailing has been canceled.'));
     }
 }
Beispiel #6
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;
}