(NFC) Update CRM/Contact to match new coder style
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / TagContributions.php
index fdeeb499227fd0301905798ec4ccc942db0b5614..df0e60488fe1774121e44e2a0a83c844f30a1491 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 class CRM_Contact_Form_Search_Custom_TagContributions extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
 
@@ -43,20 +43,20 @@ class CRM_Contact_Form_Search_Custom_TagContributions extends CRM_Contact_Form_S
    * @param array $formValues
    */
   public function __construct(&$formValues) {
-    $this->_formValues = $formValues;
+    $this->_formValues = self::formatSavedSearchFields($formValues);
     $this->_permissionedComponent = 'CiviContribute';
 
     /**
      * Define the columns for search result rows
      */
-    $this->_columns = array(
+    $this->_columns = [
       ts('Contact ID') => 'contact_id',
       ts('Full Name') => 'sort_name',
       ts('First Name') => 'first_name',
       ts('Last Name') => 'last_name',
       ts('Tag') => 'tag_name',
       ts('Totals') => 'amount',
-    );
+    ];
   }
 
   /**
@@ -73,16 +73,16 @@ class CRM_Contact_Form_Search_Custom_TagContributions extends CRM_Contact_Form_S
      * Define the search form fields here
      */
 
-    $form->addDate('start_date', ts('Contribution Date From'), FALSE, array('formatType' => 'custom'));
-    $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
-    $tag = array('' => ts('- any tag -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
+    $form->add('datepicker', 'start_date', ts('Contribution Date From'), [], FALSE, ['time' => FALSE]);
+    $form->add('datepicker', 'end_date', ts('...through'), [], FALSE, ['time' => FALSE]);
+    $tag = ['' => ts('- any tag -')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', ['onlyActive' => FALSE]);
     $form->addElement('select', 'tag', ts('Tagged'), $tag);
 
     /**
      * If you are using the sample template, this array tells the template fields to render
      * for the search form.
      */
-    $form->assign('elements', array('start_date', 'end_date', 'tag'));
+    $form->assign('elements', ['start_date', 'end_date', 'tag']);
   }
 
   /**
@@ -169,25 +169,24 @@ WHERE  $where
    * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
    *
    */
+
   /**
    * @param bool $includeContactIDs
    *
    * @return string
    */
   public function where($includeContactIDs = FALSE) {
-    $clauses = array();
+    $clauses = [];
 
     $clauses[] = "contact_a.contact_type = 'Individual'";
     $clauses[] = "civicrm_contribution.contact_id = contact_a.id";
 
-    $startDate = CRM_Utils_Date::processDate($this->_formValues['start_date']);
-    if ($startDate) {
-      $clauses[] = "civicrm_contribution.receive_date >= $startDate";
+    if ($this->_formValues['start_date']) {
+      $clauses[] = "civicrm_contribution.receive_date >= '{$this->_formValues['start_date']} 00:00:00'";
     }
 
-    $endDate = CRM_Utils_Date::processDate($this->_formValues['end_date']);
-    if ($endDate) {
-      $clauses[] = "civicrm_contribution.receive_date <= $endDate";
+    if ($this->_formValues['end_date']) {
+      $clauses[] = "civicrm_contribution.receive_date <= '{$this->_formValues['end_date']} 23:59:59'";
     }
 
     $tag = CRM_Utils_Array::value('tag', $this->_formValues);
@@ -200,7 +199,7 @@ WHERE  $where
     }
 
     if ($includeContactIDs) {
-      $contactIDs = array();
+      $contactIDs = [];
       foreach ($this->_formValues as $id => $value) {
         if ($value &&
           substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
@@ -220,7 +219,6 @@ WHERE  $where
     return implode(' AND ', $clauses);
   }
 
-
   /*
    * Functions below generally don't need to be modified
    */
@@ -282,4 +280,27 @@ WHERE  $where
     list($this->_aclFrom, $this->_aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause($tableAlias);
   }
 
+  /**
+   * Format saved search fields for this custom group.
+   *
+   * Note this is a function to facilitate the transition to jcalendar for
+   * saved search groups. In time it can be stripped out again.
+   *
+   * @param array $formValues
+   *
+   * @return array
+   */
+  public static function formatSavedSearchFields($formValues) {
+    $dateFields = [
+      'start_date',
+      'end_date',
+    ];
+    foreach ($formValues as $element => $value) {
+      if (in_array($element, $dateFields) && !empty($value)) {
+        $formValues[$element] = date('Y-m-d', strtotime($value));
+      }
+    }
+    return $formValues;
+  }
+
 }