worked on CRM-12357, show bulk mailing activities if they are created.
[civicrm-core.git] / CRM / Mailing / BAO / Mailing.php
index aff46def7ca36f93b54178509056e020b285a850..a940ed231d9b7eb9d9829ec85e999f23a0959b2d 100644 (file)
@@ -769,6 +769,11 @@ ORDER BY   i.contact_id, i.{$tempColumn}
 
       if ($this->body_html) {
         $this->_getTokens('html');
+        if (!$this->body_text) {
+          // Since the text template was created from html, use the html tokens.
+          // @see CRM_Mailing_BAO_Mailing::getTemplates()
+          $this->tokens['text'] = $this->tokens['html'];
+        }
       }
 
       if ($this->body_text) {
@@ -1081,12 +1086,14 @@ ORDER BY   civicrm_email.is_bulkmail DESC
       $this->_domain = CRM_Core_BAO_Domain::getDomain();
     }
 
-    list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders($job_id,
+    list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders(
+      $job_id,
       $event_queue_id,
       $hash,
       $email,
       $isForward
     );
+
     //set from email who is forwarding it and not original one.
     if ($fromEmail) {
       unset($headers['From']);
@@ -1299,7 +1306,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
    * domain and mailing tokens
    *
    */
-  function tokenReplace(&$mailing) {
+  public static function tokenReplace(&$mailing) {
     $domain = CRM_Core_BAO_Domain::getDomain();
 
     foreach (array(
@@ -1583,7 +1590,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
 
     /**
      * 'approval_status_id' set in
-     * CRM_Mailing_Form_Mailing_Schedule::postProcess() or via API.
+     * CRM_Mailing_Form_Schedule::postProcess() or via API.
      */
     if (isset($params['approval_status_id']) && $params['approval_status_id']) {
       $job = new CRM_Mailing_BAO_Job();
@@ -1593,7 +1600,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
       $job->scheduled_date = $params['scheduled_date'];
       $job->save();
       // Populate the recipients.
-      $mailing->getRecipients($job->id, $mailing->id, NULL, NULL, true, false);
+      $mailing->getRecipients($job->id, $mailing->id, NULL, NULL, TRUE, FALSE);
     }
 
     return $mailing;
@@ -2142,7 +2149,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
 
     // get all the groups that this user can access
     // if they dont have universal access
-    $groups = CRM_Core_PseudoConstant::group(null, false);
+    $groups = CRM_Core_PseudoConstant::group(NULL, FALSE);
     if (!empty($groups)) {
       $groupIDs = implode(',', array_keys($groups));
       $selectClause = ($count) ? 'COUNT( DISTINCT m.id) as count' : 'DISTINCT( m.id ) as id';
@@ -2542,7 +2549,7 @@ SELECT  $mailing.id as mailing_id
    * @return $report array content/component.
    * @access public
    */
-  public function getMailingContent(&$report, &$form, $isSMS = FALSE) {
+  public static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
     $htmlHeader = $textHeader = NULL;
     $htmlFooter = $textFooter = NULL;
 
@@ -2632,7 +2639,8 @@ WHERE  civicrm_mailing_job.id = %1
       shuffle($lockArray);
 
       // check if we are using global locks
-      $serverWideLock = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
+      $serverWideLock = CRM_Core_BAO_Setting::getItem(
+        CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
         'civimail_server_wide_lock'
       );
       foreach ($lockArray as $lockID) {
@@ -2721,5 +2729,80 @@ AND        m.id = %1
     $params = array( 1 => array( $mid, 'Integer' ) );
     return CRM_Core_DAO::singleValueQuery($sql, $params);
   }
+
+  /**
+   * This function is a wrapper for ajax activity selector
+   *
+   * @param  array   $params associated array for params record id.
+   *
+   * @return array   $contactActivities associated array of contact activities
+   * @access public
+   */
+  public static function getContactMailingSelector(&$params) {
+    // format the params
+    $params['offset']   = ($params['page'] - 1) * $params['rp'];
+    $params['rowCount'] = $params['rp'];
+    $params['sort']     = CRM_Utils_Array::value('sortBy', $params);
+    $params['caseId']   = NULL;
+    $context            = CRM_Utils_Array::value('context', $params);
+
+    // get contact mailings
+    $mailings = CRM_Mailing_BAO_Mailing::getContactMailings($params);
+
+    // add total
+    $params['total'] = CRM_Mailing_BAO_Mailing::getContactMailingsCount($params);
+
+    // format params and add links
+    $contactMailings = array();
+    foreach ($mailings as $mailingId => $values) {
+      $contactMailings[$mailingId]['subject'] = $values['subject'];
+      $contactMailings[$mailingId]['start_date'] = CRM_Utils_Date::customFormat($values['start_date']);
+
+      $contactMailings[$mailingId]['mailing_creator'] = CRM_Utils_System::href(
+          $values['creator_name'],
+          'civicrm/contact/view',
+          "reset=1&cid={$values['creator_id']}");
+
+      $contactMailings[$mailingId]['links'] = CRM_Utils_System::href(
+        ts('View Mailing'),
+        'civicrm/mailing/view',
+        "reset=1&id={$values['mailing_id']}");
+    }
+
+    return $contactMailings;
+  }
+
+  /**
+   * Function to retrieve contact mailing
+   *
+   * @param array $params associated array
+   *
+   * @return array of mailings for a contact
+   *
+   * @static
+   * @access public
+   */
+  static public function getContactMailings(&$params) {
+    $params['version'] = 3;
+    $result = civicrm_api('MailingContact', 'get', $params);
+    return $result['values'];
+  }
+
+  /**
+   * Function to retrieve contact mailing count
+   *
+   * @param array $params associated array
+   *
+   * @return int count of mailings for a contact
+   *
+   * @static
+   * @access public
+   */
+  static public function getContactMailingsCount(&$params) {
+    //FIX ME: need to implement getcount api for MailingContact
+    $params['version'] = 3;
+    $result = civicrm_api('MailingContact', 'get', $params);
+    return count($result['values']);
+  }
 }