Merge pull request #19044 from seamuslee001/dev_drupal_149
[civicrm-core.git] / CRM / Core / Form / Date.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Core_Form_Date {
18
19 /**
20 * Various Date Formats.
21 */
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;
23
24 /**
25 * Build the date-format form.
26 *
27 * @param CRM_Core_Form $form
28 * The form object that we are operating on.
29 */
30 public static function buildAllowedDateFormats(&$form) {
31
32 $dateOptions = [];
33
34 if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Import_Form_DataSource') {
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 $form->addRadio('dateFormats', ts('Date Format'), [
42 self::DATE_yyyy_mm_dd => $dateText,
43 self::DATE_mm_dd_yy => ts('mm/dd/yy OR mm-dd-yy (12/25/98 OR 12-25-98) OR (9/1/08 OR 9-1-08)'),
44 self::DATE_mm_dd_yyyy => ts('mm/dd/yyyy OR mm-dd-yyyy (12/25/1998 OR 12-25-1998) OR (9/1/2008 OR 9-1-2008)'),
45 self::DATE_Month_dd_yyyy => ts('Month dd, yyyy (December 12, 1998)'),
46 self::DATE_dd_mon_yy => ts('dd-mon-yy OR dd/mm/yy (25-Dec-98 OR 25/12/98)'),
47 self::DATE_dd_mm_yyyy => ts('dd/mm/yyyy (25/12/1998) OR (1/9/2008)'),
48 ], [], '<br/>');
49 $form->setDefaults(['dateFormats' => self::DATE_yyyy_mm_dd]);
50 }
51
52 /**
53 * Retrieve the date range - relative or absolute and assign it to the form.
54 *
55 * @deprecated
56 *
57 * @param CRM_Core_Form $form
58 * The form the dates should be added to.
59 * @param string $fieldName
60 * @param int $count
61 * @param string $from
62 * @param string $to
63 * @param string $fromLabel
64 * @param bool $required
65 * @param array $operators
66 * Additional value pairs to add.
67 * @param string $dateFormat
68 * @param bool|string $displayTime
69 * @param array $attributes
70 */
71 public static function buildDateRange(
72 &$form, $fieldName, $count = 1,
73 $from = '_from', $to = '_to', $fromLabel = 'From:',
74 $required = FALSE, $operators = [],
75 $dateFormat = 'searchDate', $displayTime = FALSE,
76 $attributes = ['class' => 'crm-select2']
77 ) {
78 CRM_Core_Error::deprecatedFunctionWarning('function will be removed');
79 $selector
80 = CRM_Core_Form_Date::returnDateRangeSelector(
81 $form, $fieldName, $count,
82 $from, $to, $fromLabel,
83 $required, $operators,
84 $dateFormat, $displayTime
85 );
86 CRM_Core_Form_Date::addDateRangeToForm(
87 $form, $fieldName, $selector,
88 $from, $to, $fromLabel,
89 $required, $dateFormat, $displayTime,
90 $attributes
91 );
92 }
93
94 /**
95 * Build the date range array that will provide the form option values.
96 *
97 * @deprecated
98 *
99 * It can be - relative or absolute.
100 *
101 * @param CRM_Core_Form $form
102 * The form object that we are operating on.
103 * @param string $fieldName
104 * @param int $count
105 * @param string $from
106 * @param string $to
107 * @param string $fromLabel
108 * @param bool $required
109 * @param array $operators
110 * Additional Operator Selections to add.
111 * @param string $dateFormat
112 * @param bool $displayTime
113 *
114 * @return array
115 * Values for Selector
116 */
117 public static function returnDateRangeSelector(
118 &$form, $fieldName, $count = 1,
119 $from = '_from', $to = '_to', $fromLabel = 'From:',
120 $required = FALSE, $operators = [],
121 $dateFormat = 'searchDate', $displayTime = FALSE
122 ) {
123 CRM_Core_Error::deprecatedFunctionWarning('function will be removed');
124 $selector = [
125 '' => ts('- any -'),
126 0 => ts('Choose Date Range'),
127 ];
128 // CRM-16195 Pull relative date filters from an option group
129 $selector = $selector + CRM_Core_OptionGroup::values('relative_date_filters');
130
131 if (is_array($operators)) {
132 $selector = array_merge($selector, $operators);
133 }
134
135 $config = CRM_Core_Config::singleton();
136 //if fiscal year start on 1 jan then remove fiscal year task
137 //form list
138 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
139 unset($selector['this.fiscal_year']);
140 unset($selector['previous.fiscal_year']);
141 }
142 return $selector;
143 }
144
145 /**
146 * Build the date range - relative or absolute.
147 *
148 * @deprecated
149 *
150 * @param CRM_Core_Form $form
151 * The form object that we are operating on.
152 * @param string $fieldName
153 * @param array $selector
154 * Array of option values to add.
155 * @param string $from
156 * Label.
157 * @param string $to
158 * @param string $fromLabel
159 * @param bool $required
160 * @param string $dateFormat
161 * @param bool $displayTime
162 * @param array $attributes
163 */
164 public static function addDateRangeToForm(
165 &$form,
166 $fieldName,
167 $selector,
168 $from = '_from',
169 $to = '_to',
170 $fromLabel = 'From:',
171 $required = FALSE,
172 $dateFormat = 'searchDate',
173 $displayTime = FALSE,
174 $attributes
175 ) {
176 CRM_Core_Error::deprecatedFunctionWarning('function will be removed');
177 $form->add('select',
178 "{$fieldName}_relative",
179 ts('Relative Date Range'),
180 $selector,
181 $required,
182 $attributes
183 );
184
185 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
186 }
187
188 }