Merge pull request #4809 from totten/master-cs2
[civicrm-core.git] / CRM / Mailing / Selector / Browse.php
index 458d3f316e46b3f0a4ab2e0a5559384a262b3ebe..8c0fca37fbb41ee1f7f144feeb41f30512867962 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | 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$
  *
  */
@@ -39,7 +39,7 @@
 class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
 
   /**
-   * array of supported links, currenly null
+   * Array of supported links, currenly null
    *
    * @var array
    * @static
@@ -47,7 +47,7 @@ class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_
   static $_links = NULL;
 
   /**
-   * we use desc to remind us what that column is, name is used in the tpl
+   * We use desc to remind us what that column is, name is used in the tpl
    *
    * @var array
    * @static
@@ -59,53 +59,48 @@ class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_
   /**
    * Class constructor
    *
-   * @param
    *
-   * @return CRM_Contact_Selector_Profile
-   * @access public
+   * @return \CRM_Mailing_Selector_Browse
+  @access public
    */
-  function __construct() {
+  public function __construct() {
   }
-  //end of constructor
 
   /**
    * This method returns the links that are given for each search row.
    *
    * @return array
-   * @access public
    *
    */
-  static function &links() {
+  public static function &links() {
     return self::$_links;
   }
-  //end of function
 
   /**
-   * getter for array of the parameters required for creating pager.
+   * Getter for array of the parameters required for creating pager.
+   *
+   * @param $action
+   * @param array $params
    *
-   * @param
-   * @access public
    */
-  function getPagerParams($action, &$params) {
+  public function getPagerParams($action, &$params) {
     $params['csvString'] = NULL;
     $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
     $params['status'] = ts('Mailings %%StatusMessage%%');
     $params['buttonTop'] = 'PagerTopButton';
     $params['buttonBottom'] = 'PagerBottomButton';
   }
-  //end of function
 
   /**
-   * returns the column headers as an array of tuples:
+   * Returns the column headers as an array of tuples:
    * (name, sortName (key to the sort array))
    *
    * @param string $action the action being performed
    * @param enum   $output what should the result set include (web/email/csv)
    *
    * @return array the column headers that need to be displayed
-   * @access public
    */
-  function &getColumnHeaders($action = NULL, $output = NULL) {
+  public function &getColumnHeaders($action = NULL, $output = NULL) {
     $mailing = CRM_Mailing_BAO_Mailing::getTableName();
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     if (!isset(self::$_columnHeaders)) {
@@ -188,9 +183,8 @@ class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_
    * @param
    *
    * @return int Total number of rows
-   * @access public
    */
-  function getTotalCount($action) {
+  public function getTotalCount($action) {
     $job        = CRM_Mailing_BAO_MailingJob::getTableName();
     $mailing    = CRM_Mailing_BAO_Mailing::getTableName();
     $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL();
@@ -212,7 +206,7 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
   }
 
   /**
-   * returns all the rows in the given offset and rowCount
+   * Returns all the rows in the given offset and rowCount
    *
    * @param enum   $action   the action being performed
    * @param int    $offset   the row number to start from
@@ -222,7 +216,7 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
    *
    * @return int   the total number of rows for this action
    */
-  function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
+  public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
     static $actionLinks = NULL;
     if (empty($actionLinks)) {
       $cancelExtra  = ts('Are you sure you want to cancel this mailing?');
@@ -374,7 +368,8 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
           }
         }
 
-        if ($row['status'] == 'Complete' && !$row['archived']) {
+        if (in_array($row['status'], array('Complete', 'Canceled')) &&
+          !$row['archived']) {
           if ($allAccess || $showCreateLinks) {
             $actionMask |= CRM_Core_Action::RENEW;
           }
@@ -416,25 +411,34 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
   }
 
   /**
-   * name of export file.
+   * Name of export file.
    *
    * @param string $output type of output
    *
    * @return string name of the file
    */
-  function getExportFileName($output = 'csv') {
+  public function getExportFileName($output = 'csv') {
     return ts('CiviMail Mailings');
   }
 
-  function setParent($parent) {
+  /**
+   * @param $parent
+   */
+  public function setParent($parent) {
     $this->_parent = $parent;
   }
 
-  function whereClause(&$params, $sortBy = TRUE) {
+  /**
+   * @param array $params
+   * @param bool $sortBy
+   *
+   * @return int|string
+   */
+  public function whereClause(&$params, $sortBy = TRUE) {
     $values = $clauses = array();
+    $isFormSubmitted   = $this->_parent->get('hidden_find_mailings');
 
     $title = $this->_parent->get('mailing_name');
-
     if ($title) {
       $clauses[] = 'name LIKE %1';
       if (strpos($title, '%') !== FALSE) {
@@ -445,6 +449,7 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
       }
     }
 
+    $dateClause1 = $dateClause2 = array();
     $from = $this->_parent->get('mailing_from');
     if (!CRM_Utils_System::isNull($from)) {
       if ($this->_parent->get('unscheduled')) {
@@ -469,78 +474,62 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
       $params[3]     = array($to, 'String');
     }
 
-    $addtlStatuses = $this->_parent->get('archived') ? ", 'Canceled'" : '';
+    $dateClauses = array();
     if (!empty($dateClause1)) {
-      if (!$this->_parent->get('unscheduled')) {
-        $dateClause1[] = "civicrm_mailing_job.status IN ('Complete', 'Running' $addtlStatuses)";
-        $dateClause2[] = "civicrm_mailing_job.status IN ('Scheduled' $addtlStatuses)";
-      }
-
-      if (!empty($dateClause1)) {
-        $dateClause1   = implode(' AND ', $dateClause1);
-      }
-      if (!empty($dateClause2)) {
-        $dateClause2   = implode(' AND ', $dateClause2);
-      }
-
-      $dateC = NULL;
-      if (!empty($dateClause1)) {
-        $dateC = " ({$dateClause1}) ";
-      }
-      if (!empty($dateClause2)) {
-        if ($dateC) {
-          $dateC .= " OR ({$dateClause2}) ";
-        }
-        else {
-          $dateC = " ({$dateClause2}) ";
-        }
-      }
-      $clauses[] = " ({$dateC}) ";
+      $dateClauses[] = implode(' AND ', $dateClause1);
     }
-
-    if ($this->_parent->get('unscheduled')) {
-      $clauses[] = "civicrm_mailing_job.status is null";
-      $clauses[] = "civicrm_mailing.scheduled_id IS NULL";
+    if (!empty($dateClause2)) {
+      $dateClauses[] = implode(' AND ', $dateClause2);
+    }
+    $dateClauses = implode(' OR ', $dateClauses);
+    if (!empty($dateClauses)) {
+      $clauses[] = "({$dateClauses})";
     }
 
     if ($this->_parent->get('sms')) {
-      $clauses[] = "(civicrm_mailing.sms_provider_id IS NOT NULL)";
+      $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
     }
     else {
-      $clauses[] = "(civicrm_mailing.sms_provider_id IS NULL)";
+      $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
     }
 
-    // CRM-4290, do not show archived or unscheduled mails
-    // on 'Scheduled and Sent Mailing' page selector
-    if ($this->_parent->get('scheduled')) {
-      $clauses[] = "civicrm_mailing.scheduled_id IS NOT NULL";
-      $clauses[] = "( civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0 )";
+    // get values submitted by form
+    $isDraft       = $this->_parent->get('status_unscheduled');
+    $isArchived    = $this->_parent->get('is_archived');
+    $mailingStatus = $this->_parent->get('mailing_status');
+
+    if (!$isFormSubmitted && $this->_parent->get('scheduled')) {
+      // mimic default behavior for scheduled screen
+      $isArchived = 0;
+      $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
+    }
+    if (!$isFormSubmitted && $this->_parent->get('archived')) {
+      // mimic default behavior for archived screen
+      $isArchived = 1;
+    }
+    if (!$isFormSubmitted && $this->_parent->get('unscheduled')) {
+      // mimic default behavior for draft screen
+      $isDraft = 1;
     }
-    if (!$this->_parent->get('unscheduled')) {
 
-      // all status filter implementation
-      if ($status = $this->_parent->get('mailing_status')) {
-        $status = array_keys($status);
-      }
-      if ($this->_parent->get('all_status')) {
-        $status = $this->_parent->get('allStatuses');
-      }
-      if ($this->_parent->get('all_status') && $this->_parent->get('scheduled')) {
-        $status = array('Scheduled', 'Complete', 'Running');
-      }
+    $statusClauses = array();
+    if ($isDraft) {
+      $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
+    }
+    if (!empty($mailingStatus)) {
+      $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
+    }
+    if (!empty($statusClauses)) {
+      $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
+    }
 
-      if (!empty($status)) {
-        $status    = implode("','", $status);
-        $clauses[] = "civicrm_mailing_job.status IN ('$status')";
-      }
-      elseif ($this->_parent->get('scheduled')) {
-        $clauses[] = "civicrm_mailing_job.status IN ('Scheduled', 'Complete', 'Running')";
+    if (isset($isArchived)) {
+      if ($isArchived) {
+        $clauses[] = "civicrm_mailing.is_archived = 1";
+      } else {
+        $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
       }
     }
-    if ($this->_parent->get('archived')) {
-      // CRM-6446: archived view should also show cancelled mailings
-      $clauses[] = "(civicrm_mailing.is_archived = 1 OR civicrm_mailing_job.status = 'Canceled')";
-    }
 
     if ($sortBy &&
       $this->_parent->_sortByCharacter !== NULL
@@ -586,7 +575,7 @@ LEFT JOIN  civicrm_contact scheduledContact ON ( $mailing.scheduled_id = schedul
     return implode(' AND ', $clauses);
   }
 
-  function pagerAtoZ() {
+  public function pagerAtoZ() {
 
     $params = array();
     $whereClause = $this->whereClause($params, FALSE);
@@ -607,5 +596,3 @@ ORDER BY LEFT(name, 1)
     $this->_parent->assign('aToZ', $aToZBar);
   }
 }
-//end of class
-