Convert sort_name to use search form methodology to pass by url
[civicrm-core.git] / CRM / Utils / Date.php
index c2178eea9f869c6db310db2931183ac8e5a553f4..24b2f7388072276ff11abec6307c7d3a34ba6b84 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
  */
 
 /**
@@ -176,7 +176,7 @@ class CRM_Utils_Date {
    *
    */
   public static function getAbbrWeekdayNames() {
-    static $days = array();
+    static $days = [];
     if (!$days) {
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
@@ -205,7 +205,7 @@ class CRM_Utils_Date {
    *
    */
   public static function getFullWeekdayNames() {
-    static $days = array();
+    static $days = [];
     if (!$days) {
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
@@ -322,13 +322,13 @@ class CRM_Utils_Date {
       $config = CRM_Core_Config::singleton();
 
       if ($dateParts) {
-        if (array_intersect(array('h', 'H'), $dateParts)) {
+        if (array_intersect(['h', 'H'], $dateParts)) {
           $format = $config->dateformatDatetime;
         }
-        elseif (array_intersect(array('d', 'j'), $dateParts)) {
+        elseif (array_intersect(['d', 'j'], $dateParts)) {
           $format = $config->dateformatFull;
         }
-        elseif (array_intersect(array('m', 'M'), $dateParts)) {
+        elseif (array_intersect(['m', 'M'], $dateParts)) {
           $format = $config->dateformatPartial;
         }
         else {
@@ -410,7 +410,7 @@ class CRM_Utils_Date {
         $type = 'PM';
       }
 
-      $date = array(
+      $date = [
         '%b' => CRM_Utils_Array::value($month, $abbrMonths),
         '%B' => CRM_Utils_Array::value($month, $fullMonths),
         '%d' => $day > 9 ? $day : '0' . $day,
@@ -429,7 +429,7 @@ class CRM_Utils_Date {
         '%P' => $type,
         '%A' => $type,
         '%Y' => $year,
-      );
+      ];
 
       return strtr($format, $date);
     }
@@ -508,7 +508,7 @@ class CRM_Utils_Date {
    *   date/datetime in MySQL format
    */
   public static function isoToMysql($iso) {
-    $dropArray = array('-' => '', ':' => '', ' ' => '');
+    $dropArray = ['-' => '', ':' => '', ' ' => ''];
     return strtr($iso, $dropArray);
   }
 
@@ -763,7 +763,6 @@ class CRM_Utils_Date {
     }
   }
 
-
   /**
    * @param null $timeStamp
    *
@@ -889,7 +888,7 @@ class CRM_Utils_Date {
     $from = self::processDate($from, $fromTime);
     $to = self::processDate($to, $toTime);
 
-    return array($from, $to);
+    return [$from, $to];
   }
 
   /**
@@ -901,8 +900,8 @@ class CRM_Utils_Date {
    * @return int
    *   array $results contains years or months
    */
-  static public function calculateAge($birthDate) {
-    $results = array();
+  public static function calculateAge($birthDate) {
+    $results = [];
     $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d');
 
     $bDate = explode('-', $formatedBirthDate);
@@ -1003,7 +1002,7 @@ class CRM_Utils_Date {
 
     $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
 
-    $date = array();
+    $date = [];
     $date['M'] = $scheduleDate[0];
     $date['d'] = $scheduleDate[1];
     $date['Y'] = $scheduleDate[2];
@@ -1031,14 +1030,14 @@ class CRM_Utils_Date {
    *   according to the available parts at the smarty layer.
    */
   public static function getDateFieldViewFormat($format) {
-    $supportableFormats = array(
+    $supportableFormats = [
       'mm/dd' => '%B %E%f',
       'dd-mm' => '%E%f %B',
       'yy-mm' => '%Y %B',
       'M yy' => '%b %Y',
       'yy' => '%Y',
       'dd/mm/yy' => '%E%f %B %Y',
-    );
+    ];
 
     return array_key_exists($format, $supportableFormats) ? $supportableFormats[$format] : self::pickBestSmartyFormat($format);
   }
@@ -1071,7 +1070,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function datePluginToPHPFormats() {
-    $dateInputFormats = array(
+    $dateInputFormats = [
       "mm/dd/yy" => 'm/d/Y',
       "dd/mm/yy" => 'd/m/Y',
       "yy-mm-dd" => 'Y-m-d',
@@ -1089,7 +1088,7 @@ class CRM_Utils_Date {
       "M yy" => 'M Y',
       "M Y" => 'M Y',
       "yy" => 'Y',
-    );
+    ];
     return $dateInputFormats;
   }
 
@@ -1106,7 +1105,7 @@ class CRM_Utils_Date {
    */
   public static function relativeToAbsolute($relativeTerm, $unit) {
     $now = getdate();
-    $from = $to = $dateRange = array();
+    $from = $to = $dateRange = [];
     $from['H'] = $from['i'] = $from['s'] = 0;
     $relativeTermParts = explode('_', $relativeTerm);
     $relativeTermPrefix = $relativeTermParts[0];
@@ -1857,10 +1856,10 @@ class CRM_Utils_Date {
         break;
     }
 
-    foreach (array(
-               'from',
-               'to',
-             ) as $item) {
+    foreach ([
+      'from',
+      'to',
+    ] as $item) {
       if (!empty($$item)) {
         $dateRange[$item] = self::format($$item);
       }
@@ -1961,7 +1960,6 @@ class CRM_Utils_Date {
     return $field;
   }
 
-
   /**
    * Get the fields required for the 'extra' parameter when adding a datepicker.
    *
@@ -1970,7 +1968,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function getDatePickerExtra($field) {
-    $extra = array();
+    $extra = [];
     if (isset($field['date_format'])) {
       $extra['date'] = $field['date_format'];
       $extra['time'] = $field['time_format'];
@@ -1995,12 +1993,12 @@ class CRM_Utils_Date {
    *   Array ready to pass to $this->addForm('datepicker' as attributes.
    */
   public static function getDatePickerAttributes(&$field) {
-    $attributes = array();
-    $dateAttributes = array(
+    $attributes = [];
+    $dateAttributes = [
       'start_date_years' => 'minYear',
       'end_date_years' => 'maxYear',
       'date_format' => 'format',
-    );
+    ];
     foreach ($dateAttributes as $dateAttribute => $mapTo) {
       if (isset($field[$dateAttribute])) {
         $attributes[$mapTo] = $field[$dateAttribute];
@@ -2031,8 +2029,8 @@ class CRM_Utils_Date {
     $config = CRM_Core_Config::singleton();
     if ($formatType) {
       // get actual format
-      $params = array('name' => $formatType);
-      $values = array();
+      $params = ['name' => $formatType];
+      $values = [];
       CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
 
       if ($values['date_format']) {
@@ -2078,7 +2076,7 @@ class CRM_Utils_Date {
       $time = '0' . $time;
     }
 
-    return array($date, $time);
+    return [$date, $time];
   }
 
   /**
@@ -2122,13 +2120,13 @@ class CRM_Utils_Date {
     // 4. If date and time then convert to default date time format.
 
     $dateKey = 'date';
-    $dateParams = array($dateKey => $date);
+    $dateParams = [$dateKey => $date];
 
     if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
       $dateVal = $dateParams[$dateKey];
       $ruleName = 'date';
       if ($dateType == 1) {
-        $matches = array();
+        $matches = [];
         if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
           $ruleName = 'dateTime';
           if (strpos($date, '-') !== FALSE) {
@@ -2160,7 +2158,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function getCalendarDayOfMonth() {
-    $month = array();
+    $month = [];
     for ($i = 1; $i <= 31; $i++) {
       $month[$i] = $i;
       if ($i == 31) {
@@ -2170,4 +2168,40 @@ class CRM_Utils_Date {
     return $month;
   }
 
+  /**
+   * Convert a relative date format to an api field.
+   *
+   * @param array $params
+   * @param string $dateField
+   * @param bool $isDatePicker
+   *   Non datepicker fields are deprecated. Exterminate Exterminate.
+   *   (but for now handle them).
+   */
+  public static function convertFormDateToApiFormat(&$params, $dateField, $isDatePicker = TRUE) {
+    if (!empty($params[$dateField . '_relative'])) {
+      $dates = CRM_Utils_Date::getFromTo($params[$dateField . '_relative'], NULL, NULL);
+      unset($params[$dateField . '_relative']);
+    }
+    if (!empty($params[$dateField . '_low'])) {
+      $dates[0] = $isDatePicker ? $params[$dateField . '_low'] : date('Y-m-d H:i:s', strtotime($params[$dateField . '_low']));
+      unset($params[$dateField . '_low']);
+    }
+    if (!empty($params[$dateField . '_high'])) {
+      $dates[1] = $isDatePicker ? $params[$dateField . '_high'] : date('Y-m-d 23:59:59', strtotime($params[$dateField . '_high']));
+      unset($params[$dateField . '_high']);
+    }
+    if (empty($dates)) {
+      return;
+    }
+    if (empty($dates[0])) {
+      $params[$dateField] = ['<=' => $dates[1]];
+    }
+    elseif (empty($dates[1])) {
+      $params[$dateField] = ['>=' => $dates[0]];
+    }
+    else {
+      $params[$dateField] = ['BETWEEN' => $dates];
+    }
+  }
+
 }