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