Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Core / Form / Date.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
6a488035 32 */
2aa397bc 33class CRM_Core_Form_Date {
6a488035
TO
34
35 /**
fe482240 36 * Various Date Formats.
6a488035 37 */
7da04cde 38 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
39
40 /**
8eedd10a 41 * Build the date-format form.
6a488035 42 *
6a0b768e
TO
43 * @param CRM_Core_Form $form
44 * The form object that we are operating on.
6a488035 45 */
00be9182 46 public static function buildAllowedDateFormats(&$form) {
6a488035 47
be2fb01f 48 $dateOptions = [];
6a488035 49
ac4410cd 50 if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Import_Form_DataSource') {
6a488035
TO
51 $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)');
52 }
53 else {
54 $dateText = ts('yyyy-mm-dd OR yyyymmdd (1998-12-25 OR 19981225) OR (2008-9-1 OR 20080901)');
55 }
56
57 $dateOptions[] = $form->createElement('radio', NULL, NULL, $dateText, self::DATE_yyyy_mm_dd);
58
59 $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);
60 $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);
61 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('Month dd, yyyy (December 12, 1998)'), self::DATE_Month_dd_yyyy);
62 $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);
63 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('dd/mm/yyyy (25/12/1998) OR (1/9/2008)'), self::DATE_dd_mm_yyyy);
64 $form->addGroup($dateOptions, 'dateFormats', ts('Date Format'), '<br/>');
be2fb01f 65 $form->setDefaults(['dateFormats' => self::DATE_yyyy_mm_dd]);
6a488035
TO
66 }
67
68 /**
8eedd10a 69 * Retrieve the date range - relative or absolute and assign it to the form.
77b97be7 70 *
6a0b768e
TO
71 * @param CRM_Core_Form $form
72 * The form the dates should be added to.
bc3f7f04 73 * @param string $fieldName
6a0b768e 74 * @param int $count
bc3f7f04 75 * @param string $from
76 * @param string $to
77 * @param string $fromLabel
6a0b768e
TO
78 * @param bool $required
79 * @param array $operators
80 * Additional value pairs to add.
bc3f7f04 81 * @param string $dateFormat
77b97be7 82 * @param bool|string $displayTime
49d4d222 83 * @param array $attributes
6a488035 84 */
8d7a9d07 85 public static function buildDateRange(
634e1a1a
DL
86 &$form, $fieldName, $count = 1,
87 $from = '_from', $to = '_to', $fromLabel = 'From:',
be2fb01f 88 $required = FALSE, $operators = [],
49d4d222 89 $dateFormat = 'searchDate', $displayTime = FALSE,
be2fb01f 90 $attributes = ['class' => 'crm-select2']
634e1a1a 91 ) {
8d7a9d07
CB
92 $selector
93 = CRM_Core_Form_Date::returnDateRangeSelector(
634e1a1a
DL
94 $form, $fieldName, $count,
95 $from, $to, $fromLabel,
96 $required, $operators,
97 $dateFormat, $displayTime
98 );
99 CRM_Core_Form_Date::addDateRangeToForm(
100 $form, $fieldName, $selector,
101 $from, $to, $fromLabel,
49d4d222 102 $required, $dateFormat, $displayTime,
103 $attributes
634e1a1a 104 );
f119bedd 105 }
106
107 /**
8eedd10a 108 * Build the date range array that will provide the form option values.
109 *
110 * It can be - relative or absolute.
f119bedd 111 *
6a0b768e
TO
112 * @param CRM_Core_Form $form
113 * The form object that we are operating on.
f119bedd 114 * @param string $fieldName
6a0b768e
TO
115 * @param int $count
116 * @param string $from
117 * @param string $to
118 * @param string $fromLabel
119 * @param bool $required
120 * @param array $operators
121 * Additional Operator Selections to add.
122 * @param string $dateFormat
123 * @param bool $displayTime
8eedd10a 124 *
a6c01b45
CW
125 * @return array
126 * Values for Selector
f119bedd 127 */
8d7a9d07 128 public static function returnDateRangeSelector(
634e1a1a
DL
129 &$form, $fieldName, $count = 1,
130 $from = '_from', $to = '_to', $fromLabel = 'From:',
be2fb01f 131 $required = FALSE, $operators = [],
634e1a1a
DL
132 $dateFormat = 'searchDate', $displayTime = FALSE
133 ) {
be2fb01f 134 $selector = [
8d7a9d07
CB
135 '' => ts('- any -'),
136 0 => ts('Choose Date Range'),
be2fb01f 137 ];
7537a84b
J
138 // CRM-16195 Pull relative date filters from an option group
139 $selector = $selector + CRM_Core_OptionGroup::values('relative_date_filters');
180214e9
DL
140
141 if (is_array($operators)) {
142 $selector = array_merge($selector, $operators);
143 }
bc3f7f04 144
6a488035
TO
145 $config = CRM_Core_Config::singleton();
146 //if fiscal year start on 1 jan then remove fiscal year task
147 //form list
148 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
149 unset($selector['this.fiscal_year']);
150 unset($selector['previous.fiscal_year']);
151 }
f119bedd 152 return $selector;
153 }
6a488035 154
f119bedd 155 /**
8eedd10a 156 * Build the date range - relative or absolute.
f119bedd 157 *
6a0b768e
TO
158 * @param CRM_Core_Form $form
159 * The form object that we are operating on.
f119bedd 160 * @param string $fieldName
6a0b768e
TO
161 * @param array $selector
162 * Array of option values to add.
163 * @param string $from
164 * Label.
8eedd10a 165 * @param string $to
da6b46f4 166 * @param string $fromLabel
6a0b768e 167 * @param bool $required
f119bedd 168 * @param string $dateFormat
6a0b768e 169 * @param bool $displayTime
49d4d222 170 * @param array $attributes
f119bedd 171 */
49d4d222 172 public static function addDateRangeToForm(
173 &$form,
174 $fieldName,
175 $selector,
176 $from = '_from',
177 $to = '_to',
178 $fromLabel = 'From:',
179 $required = FALSE,
180 $dateFormat = 'searchDate',
181 $displayTime = FALSE,
182 $attributes
183 ) {
6a488035
TO
184 $form->add('select',
185 "{$fieldName}_relative",
186 ts('Relative Date Range'),
1eef83c9
CW
187 $selector,
188 $required,
49d4d222 189 $attributes
353ffa53 190 );
6a488035 191
2aa397bc 192 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
6a488035 193 }
f119bedd 194
6a488035 195}