Merge pull request #17390 from mattwire/api3activitydatetimedefault
[civicrm-core.git] / CRM / Core / Form / Date.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 16 */
2aa397bc 17class CRM_Core_Form_Date {
6a488035
TO
18
19 /**
fe482240 20 * Various Date Formats.
6a488035 21 */
7da04cde 22 const DATE_yyyy_mm_dd = 1, DATE_mm_dd_yy = 2, DATE_mm_dd_yyyy = 4, DATE_Month_dd_yyyy = 8, DATE_dd_mon_yy = 16, DATE_dd_mm_yyyy = 32;
6a488035
TO
23
24 /**
8eedd10a 25 * Build the date-format form.
6a488035 26 *
6a0b768e
TO
27 * @param CRM_Core_Form $form
28 * The form object that we are operating on.
6a488035 29 */
00be9182 30 public static function buildAllowedDateFormats(&$form) {
6a488035 31
be2fb01f 32 $dateOptions = [];
6a488035 33
ac4410cd 34 if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Import_Form_DataSource') {
6a488035
TO
35 $dateText = ts('yyyy-mm-dd OR yyyy-mm-dd HH:mm OR yyyymmdd OR yyyymmdd HH:mm (1998-12-25 OR 1998-12-25 15:33 OR 19981225 OR 19981225 10:30 OR ( 2008-9-1 OR 2008-9-1 15:33 OR 20080901 15:33)');
36 }
37 else {
38 $dateText = ts('yyyy-mm-dd OR yyyymmdd (1998-12-25 OR 19981225) OR (2008-9-1 OR 20080901)');
39 }
40
41 $dateOptions[] = $form->createElement('radio', NULL, NULL, $dateText, self::DATE_yyyy_mm_dd);
42
43 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('mm/dd/yy OR mm-dd-yy (12/25/98 OR 12-25-98) OR (9/1/08 OR 9-1-08)'), self::DATE_mm_dd_yy);
44 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('mm/dd/yyyy OR mm-dd-yyyy (12/25/1998 OR 12-25-1998) OR (9/1/2008 OR 9-1-2008)'), self::DATE_mm_dd_yyyy);
45 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('Month dd, yyyy (December 12, 1998)'), self::DATE_Month_dd_yyyy);
46 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('dd-mon-yy OR dd/mm/yy (25-Dec-98 OR 25/12/98)'), self::DATE_dd_mon_yy);
47 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('dd/mm/yyyy (25/12/1998) OR (1/9/2008)'), self::DATE_dd_mm_yyyy);
48 $form->addGroup($dateOptions, 'dateFormats', ts('Date Format'), '<br/>');
be2fb01f 49 $form->setDefaults(['dateFormats' => self::DATE_yyyy_mm_dd]);
6a488035
TO
50 }
51
52 /**
8eedd10a 53 * Retrieve the date range - relative or absolute and assign it to the form.
77b97be7 54 *
6a0b768e
TO
55 * @param CRM_Core_Form $form
56 * The form the dates should be added to.
bc3f7f04 57 * @param string $fieldName
6a0b768e 58 * @param int $count
bc3f7f04 59 * @param string $from
60 * @param string $to
61 * @param string $fromLabel
6a0b768e
TO
62 * @param bool $required
63 * @param array $operators
64 * Additional value pairs to add.
bc3f7f04 65 * @param string $dateFormat
77b97be7 66 * @param bool|string $displayTime
49d4d222 67 * @param array $attributes
6a488035 68 */
8d7a9d07 69 public static function buildDateRange(
634e1a1a
DL
70 &$form, $fieldName, $count = 1,
71 $from = '_from', $to = '_to', $fromLabel = 'From:',
be2fb01f 72 $required = FALSE, $operators = [],
49d4d222 73 $dateFormat = 'searchDate', $displayTime = FALSE,
be2fb01f 74 $attributes = ['class' => 'crm-select2']
634e1a1a 75 ) {
8d7a9d07
CB
76 $selector
77 = CRM_Core_Form_Date::returnDateRangeSelector(
634e1a1a
DL
78 $form, $fieldName, $count,
79 $from, $to, $fromLabel,
80 $required, $operators,
81 $dateFormat, $displayTime
82 );
83 CRM_Core_Form_Date::addDateRangeToForm(
84 $form, $fieldName, $selector,
85 $from, $to, $fromLabel,
49d4d222 86 $required, $dateFormat, $displayTime,
87 $attributes
634e1a1a 88 );
f119bedd 89 }
90
91 /**
8eedd10a 92 * Build the date range array that will provide the form option values.
93 *
94 * It can be - relative or absolute.
f119bedd 95 *
6a0b768e
TO
96 * @param CRM_Core_Form $form
97 * The form object that we are operating on.
f119bedd 98 * @param string $fieldName
6a0b768e
TO
99 * @param int $count
100 * @param string $from
101 * @param string $to
102 * @param string $fromLabel
103 * @param bool $required
104 * @param array $operators
105 * Additional Operator Selections to add.
106 * @param string $dateFormat
107 * @param bool $displayTime
8eedd10a 108 *
a6c01b45
CW
109 * @return array
110 * Values for Selector
f119bedd 111 */
8d7a9d07 112 public static function returnDateRangeSelector(
634e1a1a
DL
113 &$form, $fieldName, $count = 1,
114 $from = '_from', $to = '_to', $fromLabel = 'From:',
be2fb01f 115 $required = FALSE, $operators = [],
634e1a1a
DL
116 $dateFormat = 'searchDate', $displayTime = FALSE
117 ) {
be2fb01f 118 $selector = [
8d7a9d07
CB
119 '' => ts('- any -'),
120 0 => ts('Choose Date Range'),
be2fb01f 121 ];
7537a84b
J
122 // CRM-16195 Pull relative date filters from an option group
123 $selector = $selector + CRM_Core_OptionGroup::values('relative_date_filters');
180214e9
DL
124
125 if (is_array($operators)) {
126 $selector = array_merge($selector, $operators);
127 }
bc3f7f04 128
6a488035
TO
129 $config = CRM_Core_Config::singleton();
130 //if fiscal year start on 1 jan then remove fiscal year task
131 //form list
132 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
133 unset($selector['this.fiscal_year']);
134 unset($selector['previous.fiscal_year']);
135 }
f119bedd 136 return $selector;
137 }
6a488035 138
f119bedd 139 /**
8eedd10a 140 * Build the date range - relative or absolute.
f119bedd 141 *
6a0b768e
TO
142 * @param CRM_Core_Form $form
143 * The form object that we are operating on.
f119bedd 144 * @param string $fieldName
6a0b768e
TO
145 * @param array $selector
146 * Array of option values to add.
147 * @param string $from
148 * Label.
8eedd10a 149 * @param string $to
da6b46f4 150 * @param string $fromLabel
6a0b768e 151 * @param bool $required
f119bedd 152 * @param string $dateFormat
6a0b768e 153 * @param bool $displayTime
49d4d222 154 * @param array $attributes
f119bedd 155 */
49d4d222 156 public static function addDateRangeToForm(
157 &$form,
158 $fieldName,
159 $selector,
160 $from = '_from',
161 $to = '_to',
162 $fromLabel = 'From:',
163 $required = FALSE,
164 $dateFormat = 'searchDate',
165 $displayTime = FALSE,
166 $attributes
167 ) {
6a488035
TO
168 $form->add('select',
169 "{$fieldName}_relative",
170 ts('Relative Date Range'),
1eef83c9
CW
171 $selector,
172 $required,
49d4d222 173 $attributes
353ffa53 174 );
6a488035 175
2aa397bc 176 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
6a488035 177 }
f119bedd 178
6a488035 179}