INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Core / Form / Date.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Core_Form_Date {
36
37 /**
38 * Various Date Formats
39 */
40 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;
41
42 /**
43 * build the date-format form
44 *
45 * @param CRM_Core_Form $form
46 * The form object that we are operating on.
47 *
48 * @static
49 */
50 public static function buildAllowedDateFormats(&$form) {
51
52 $dateOptions = array();
53
54 if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Import_Form_DataSource') {
55 $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)');
56 }
57 else {
58 $dateText = ts('yyyy-mm-dd OR yyyymmdd (1998-12-25 OR 19981225) OR (2008-9-1 OR 20080901)');
59 }
60
61 $dateOptions[] = $form->createElement('radio', NULL, NULL, $dateText, self::DATE_yyyy_mm_dd);
62
63 $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);
64 $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);
65 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('Month dd, yyyy (December 12, 1998)'), self::DATE_Month_dd_yyyy);
66 $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);
67 $dateOptions[] = $form->createElement('radio', NULL, NULL, ts('dd/mm/yyyy (25/12/1998) OR (1/9/2008)'), self::DATE_dd_mm_yyyy);
68 $form->addGroup($dateOptions, 'dateFormats', ts('Date Format'), '<br/>');
69 $form->setDefaults(array('dateFormats' => self::DATE_yyyy_mm_dd));
70 }
71
72
73 /**
74 * retrieve the date range - relative or absolute
75 * and assign it to the form
76 *
77 * @param CRM_Core_Form $form
78 * The form the dates should be added to.
79 * @param string $fieldName
80 * @param int $count
81 * @param string $from
82 * @param string $to
83 * @param string $fromLabel
84 * @param bool $required
85 * @param array $operators
86 * Additional value pairs to add.
87 * @param string $dateFormat
88 * @param bool|string $displayTime
89 *
90 *
91 * @static
92 */
93
94 static function buildDateRange(
95 &$form, $fieldName, $count = 1,
96 $from = '_from', $to = '_to', $fromLabel = 'From:',
97 $required = FALSE, $operators = array(),
98 $dateFormat = 'searchDate', $displayTime = FALSE
99 ) {
100 $selector =
101 CRM_Core_Form_Date::returnDateRangeSelector(
102 $form, $fieldName, $count,
103 $from, $to, $fromLabel,
104 $required, $operators,
105 $dateFormat, $displayTime
106 );
107 CRM_Core_Form_Date::addDateRangeToForm(
108 $form, $fieldName, $selector,
109 $from, $to, $fromLabel,
110 $required, $dateFormat, $displayTime
111 );
112 }
113
114 /**
115 * build the date range array that will provide the form option values
116 * It can be - relative or absolute
117 *
118 * @param CRM_Core_Form $form
119 * The form object that we are operating on.
120 * @param string $fieldName
121 * @param int $count
122 * @param string $from
123 * @param string $to
124 * @param string $fromLabel
125 * @param bool $required
126 * @param array $operators
127 * Additional Operator Selections to add.
128 * @param string $dateFormat
129 * @param bool $displayTime
130 * @return array
131 * Values for Selector
132 */
133 static function returnDateRangeSelector(
134 &$form, $fieldName, $count = 1,
135 $from = '_from', $to = '_to', $fromLabel = 'From:',
136 $required = FALSE, $operators = array(),
137 $dateFormat = 'searchDate', $displayTime = FALSE
138 ) {
139 $selector =
140 array(
141 '' => ts('- any -'),
142 0 => ts('Choose Date Range'),
143 'previous_2.year' => ts('Previous 2 Years'),
144 'previous_2.quarter' => ts('Previous 2 Quarters'),
145 'previous_2.month' => ts('Previous 2 Months'),
146 'previous_2.week' => ts('Previous 2 Weeks'),
147 'previous_2.day' => ts('Previous 2 Days'),
148
149 'previous_before.year' => ts('Prior to Previous Year'),
150 'previous_before.quarter' => ts('Prior to Previous Quarter'),
151 'previous_before.month' => ts('Prior to Previous Month'),
152 'previous_before.week' => ts('Prior to Previous Week'),
153 'previous_before.day' => ts('Prior to Previous Day'),
154
155 'previous.year' => ts('Previous Year'),
156 'previous.fiscal_year' => ts('Previous Fiscal Year'),
157 'previous.quarter' => ts('Previous Quarter'),
158 'previous.month' => ts('Previous Month'),
159 'previous.week' => ts('Previous Week'),
160
161 'earlier.year' => ts('To End of Previous Year'),
162 'earlier.quarter' => ts('To End of Previous Quarter'),
163 'earlier.month' => ts('To End of Previous Month'),
164 'earlier.week' => ts('To End of Previous Week'),
165 'earlier.day' => ts('To End of Previous Day'),
166
167 'greater_previous.year' => ts('From End of Previous Year'),
168 'greater_previous.quarter' => ts('From End of Previous Quarter'),
169 'greater_previous.month' => ts('From End of Previous Month'),
170 'greater_previous.week' => ts('From End of Previous Week'),
171
172 'greater.year' => ts('From Start of Current Year'),
173 'greater.quarter' => ts('From Start of Current Quarter'),
174 'greater.month' => ts('From Start of Current Month'),
175 'greater.week' => ts('From Start of Current Week'),
176 'greater.day' => ts('From Start of Current Day'),
177
178 'current.year' => ts('Current Year to-date'),
179 'current.quarter' => ts('Current Quarter to-date'),
180 'current.month' => ts('Current Month to-date'),
181 'current.week' => ts('Current Week to-date'),
182
183 'ending_3.year' => ts('Last 3 Years'),
184 'ending_2.year' => ts('Last 2 Years'),
185 'ending.year' => ts('Last 12 Months'),
186 'ending.quarter' => ts('Last 3 Months'),
187 'ending.month' => ts('Last Month'),
188 'ending.week' => ts('Last 7 days'),
189 'previous.day' => ts('Yesterday'),
190
191 'this.year' => ts('This Year'),
192 'this.fiscal_year' => ts('This Fiscal Year'),
193 'this.quarter' => ts('This Quarter'),
194 'this.month' => ts('This Month'),
195 'this.week' => ts('This Week'),
196 'this.day' => ts('Today'),
197
198 'starting.day' => ts('Tomorrow'),
199 'starting.week' => ts('Upcoming 7 days'),
200 'starting.month' => ts('Upcoming Month'),
201 'starting.year' => ts('Upcoming 12 Months'),
202
203 'less.year' => ts('To End of Current Year'),
204 'less.quarter' => ts('To End of Current Quarter'),
205 'less.month' => ts('To End of Current Month'),
206 'less.week' => ts('To End of Current Week'),
207
208 'next.week' => ts('Next Week'),
209 'next.month' => ts('Next Month'),
210 'next.quarter' => ts('Next Quarter'),
211 'next.fiscal_year' => ts('Next Fiscal Year'),
212 'next.year' => ts('Next Year'),
213
214 );
215
216 if (is_array($operators)) {
217 $selector = array_merge($selector, $operators);
218 }
219
220 $config = CRM_Core_Config::singleton();
221 //if fiscal year start on 1 jan then remove fiscal year task
222 //form list
223 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
224 unset($selector['this.fiscal_year']);
225 unset($selector['previous.fiscal_year']);
226 }
227 return $selector;
228 }
229
230 /**
231 * build the date range - relative or absolute
232 *
233 * @param CRM_Core_Form $form
234 * The form object that we are operating on.
235 * @param string $fieldName
236 * @param array $selector
237 * Array of option values to add.
238 * @param string $from
239 * Label.
240 * @param string|\stringe $to
241 * @param string $fromLabel
242 * @param bool $required
243 * @param string $dateFormat
244 * @param bool $displayTime
245 *
246 * @return null
247 */
248 public static function addDateRangeToForm(&$form, $fieldName, $selector, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $dateFormat = 'searchDate', $displayTime = FALSE) {
249 $form->add('select',
250 "{$fieldName}_relative",
251 ts('Relative Date Range'),
252 $selector,
253 $required,
254 array('class' => 'crm-select2')
255 );
256
257 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
258 }
259
260 }