CRM-14716 - Change default Visibility for Mailings for Public Pages.
[civicrm-core.git] / CRM / Mailing / BAO / Mailing.php
index 026750bedb8214cf6b8efb43a00ff45a2347849d..b425e5f0774a5406d60406d64ea07beff06df460 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -845,6 +845,8 @@ ORDER BY   i.contact_id, i.{$tempColumn}
    *
    * @params array $params contains form values
    *
+   * @param $testParams
+   *
    * @return void
    * @access public
    */
@@ -951,12 +953,12 @@ ORDER BY   civicrm_email.is_bulkmail DESC
   /**
    * static wrapper for getting verp and urls
    *
-   * @param int $job_id           ID of the Job associated with this message
-   * @param int $event_queue_id   ID of the EventQueue
-   * @param string $hash          Hash of the EventQueue
-   * @param string $email         Destination address
+   * @param int $job_id ID of the Job associated with this message
+   * @param int $event_queue_id ID of the EventQueue
+   * @param string $hash Hash of the EventQueue
+   * @param string $email Destination address
    *
-   * @return (reference) array    array ref that hold array refs to the verp info and urls
+   * @return array (reference) array    array ref that hold array refs to the verp info and urls
    */
   static function getVerpAndUrls($job_id, $event_queue_id, $hash, $email) {
     // create a skeleton object and set its properties that are required by getVerpAndUrlsAndHeaders()
@@ -973,13 +975,14 @@ ORDER BY   civicrm_email.is_bulkmail DESC
   /**
    * get verp, urls and headers
    *
-   * @param int $job_id           ID of the Job associated with this message
-   * @param int $event_queue_id   ID of the EventQueue
-   * @param string $hash          Hash of the EventQueue
-   * @param string $email         Destination address
+   * @param int $job_id ID of the Job associated with this message
+   * @param int $event_queue_id ID of the EventQueue
+   * @param string $hash Hash of the EventQueue
+   * @param string $email Destination address
    *
-   * @return (reference) array    array ref that hold array refs to the verp info, urls, and headers
-   * @access private
+   * @param bool $isForward
+   *
+   * @return array (reference) array    array ref that hold array refs to the verp info, urls, and headers@access private
    */
   private function getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward = FALSE) {
     $config = CRM_Core_Config::singleton();
@@ -1064,15 +1067,19 @@ ORDER BY   civicrm_email.is_bulkmail DESC
   /**
    * Compose a message
    *
-   * @param int $job_id           ID of the Job associated with this message
-   * @param int $event_queue_id   ID of the EventQueue
-   * @param string $hash          Hash of the EventQueue
-   * @param string $contactId     ID of the Contact
-   * @param string $email         Destination address
-   * @param string $recipient     To: of the recipient
-   * @param boolean $test         Is this mailing a test?
-   * @param boolean $isForward    Is this mailing compose for forward?
-   * @param string  $fromEmail    email address of who is forwardinf it.
+   * @param int $job_id ID of the Job associated with this message
+   * @param int $event_queue_id ID of the EventQueue
+   * @param string $hash Hash of the EventQueue
+   * @param string $contactId ID of the Contact
+   * @param string $email Destination address
+   * @param string $recipient To: of the recipient
+   * @param boolean $test Is this mailing a test?
+   * @param $contactDetails
+   * @param $attachments
+   * @param boolean $isForward Is this mailing compose for forward?
+   * @param string $fromEmail email address of who is forwardinf it.
+   *
+   * @param null $replyToEmail
    *
    * @return object               The mail object
    * @access public
@@ -1478,6 +1485,9 @@ ORDER BY   civicrm_email.is_bulkmail DESC
    *
    * @params array $params        Form values
    *
+   * @param $params
+   * @param array $ids
+   *
    * @return object $mailing      The new mailing object
    * @access public
    * @static
@@ -1518,7 +1528,7 @@ ORDER BY   civicrm_email.is_bulkmail DESC
         'forward_replies' => FALSE,
         'open_tracking'   => TRUE,
         'url_tracking'    => TRUE,
-        'visibility'      => 'User and User Admin Only',
+        'visibility'      => 'Public Pages',
         'replyto_email'   => $domain_email,
         'header_id'       => CRM_Mailing_PseudoConstant::defaultComponent('header_id', ''),
         'footer_id'       => CRM_Mailing_PseudoConstant::defaultComponent('footer_id', ''),
@@ -1562,6 +1572,8 @@ ORDER BY   civicrm_email.is_bulkmail DESC
       $transaction->rollback();
       return $mailing;
     }
+    // update mailings with hash values
+    CRM_Contact_BAO_Contact_Utils::generateChecksum($mailing->id, NULL, NULL, NULL, 'mailing', 16);
 
     $groupTableName = CRM_Contact_BAO_Group::getTableName();
     $mailingTableName = CRM_Mailing_BAO_Mailing::getTableName();
@@ -1622,13 +1634,27 @@ ORDER BY   civicrm_email.is_bulkmail DESC
     return $mailing;
   }
 
+  /**
+   * get hash value of the mailing
+   *
+   */
+  public static function getMailingHash($id) {
+    $hash = NULL;
+    if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'hash_mailing_url')) {
+      $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $id, 'hash', 'id');
+    }
+    return $hash;
+  }
+
   /**
    * Generate a report.  Fetch event count information, mailing data, and job
    * status.
    *
-   * @param int     $id          The mailing id to report
+   * @param int $id The mailing id to report
    * @param boolean $skipDetails whether return all detailed report
    *
+   * @param bool $isSMS
+   *
    * @return array        Associative array of reporting data
    * @access public
    * @static
@@ -2200,9 +2226,12 @@ LEFT JOIN civicrm_mailing_group g ON g.mailing_id   = m.id
   /**
    * Get the rows for a browse operation
    *
-   * @param int $offset       The row number to start from
-   * @param int $rowCount     The nmber of rows to return
-   * @param string $sort      The sql string that describes the sort order
+   * @param int $offset The row number to start from
+   * @param int $rowCount The nmber of rows to return
+   * @param string $sort The sql string that describes the sort order
+   *
+   * @param null $additionalClause
+   * @param null $additionalParams
    *
    * @return array            The rows
    * @access public
@@ -2293,6 +2322,7 @@ LEFT JOIN civicrm_mailing_group g ON g.mailing_id   = m.id
    *
    * @param int $id
    *
+   * @return string
    * @static
    * @access public
    */
@@ -2539,8 +2569,9 @@ SELECT  $mailing.id as mailing_id
    *
    * @param $form reference of this
    *
-   * @return $report array content/component.
-   * @access public
+   * @param bool $isSMS
+   *
+   * @return array $report array content/component.@access public
    */
   static function getMailingContent(&$report, &$form, $isSMS = FALSE) {
     $htmlHeader = $textHeader = NULL;
@@ -2564,32 +2595,22 @@ SELECT  $mailing.id as mailing_id
       }
     }
 
-    $text = CRM_Utils_Request::retrieve('text', 'Boolean', $form);
-    if ($text) {
-      echo "<pre>{$textHeader}</br>{$report['mailing']['body_text']}</br>{$textFooter}</pre>";
-      CRM_Utils_System::civiExit();
-    }
-
+    $mailingKey = $form->_mailing_id;
     if (!$isSMS) {
-      $html = CRM_Utils_Request::retrieve('html', 'Boolean', $form);
-      if ($html) {
-        $output = $htmlHeader . $report['mailing']['body_html'] . $htmlFooter ;
-        echo str_replace( "\n", '<br />', $output );
-        CRM_Utils_System::civiExit();
+      if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
+        $mailingKey = $hash;
       }
     }
 
     if (!empty($report['mailing']['body_text'])) {
-      $url = CRM_Utils_System::url('civicrm/mailing/report', 'reset=1&text=1&mid=' . $form->_mailing_id);
-      $popup = "javascript:popUp(\"$url\");";
-      $form->assign('textViewURL', $popup);
+      $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&text=1&id=' . $mailingKey);
+      $form->assign('textViewURL', $url);
     }
 
     if (!$isSMS) {
       if (!empty($report['mailing']['body_html'])) {
-        $url = CRM_Utils_System::url('civicrm/mailing/report', 'reset=1&html=1&mid=' . $form->_mailing_id);
-        $popup = "javascript:popUp(\"$url\");";
-        $form->assign('htmlViewURL', $popup);
+        $url = CRM_Utils_System::url('civicrm/mailing/view', 'reset=1&id=' . $mailingKey);
+        $form->assign('htmlViewURL', $url);
       }
     }
 
@@ -2774,9 +2795,9 @@ AND        m.id = %1
         CRM_Core_Action::VIEW => array(
           'name' => ts('View'),
           'url' => 'civicrm/mailing/view',
-          'qs' => "reset=1&id=%%mid%%",
+          'qs' => "reset=1&id=%%mkey%%",
           'title' => ts('View Mailing'),
-          'class' => 'crm-mailing-view',
+          'class' => 'crm-popup',
         ),
         CRM_Core_Action::BROWSE => array(
           'name' => ts('Mailing Report'),
@@ -2786,12 +2807,18 @@ AND        m.id = %1
         )
       );
 
+      $mailingKey = $values['mailing_id'];
+      if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
+        $mailingKey = $hash;
+      }
+
       $contactMailings[$mailingId]['links'] = CRM_Core_Action::formLink(
         $actionLinks,
         null,
         array(
           'mid' => $values['mailing_id'],
           'cid' => $params['contact_id'],
+          'mkey' => $mailingKey,
         ),
         ts('more'),
         FALSE,
@@ -2839,4 +2866,3 @@ AND        m.id = %1
     return civicrm_api('MailingContact', 'getcount', $params);
   }
 }
-