copyright and version fixes
[civicrm-core.git] / CRM / Core / Form / Date.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * This function is to build the date-format form
44 *
45 * @param Object $form the form object that we are operating on
46 *
47 * @static
48 * @access public
49 */
50 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 * This function is to retrieve the date range - relative or absolute
75 * and assign it to the form
76 * @param object $form - the form the dates should be added to
77 * @param string $fieldName
78 * @param integer $count
79 * @param string $from
80 * @param string $to
81 * @param string $fromLabel
82 * @param boolean $required
83 * @param array $operators Additional value pairs to add
84 * @param string $dateFormat
85 * @param string $displayTime
86 *
87 *
88 * @static
89 * @access public
90 */
91
92 static function buildDateRange(
93 &$form, $fieldName, $count = 1,
94 $from = '_from', $to = '_to', $fromLabel = 'From:',
95 $required = FALSE, $operators = array(),
96 $dateFormat = 'searchDate', $displayTime = FALSE
97 ) {
98 $selector =
99 CRM_Core_Form_Date::returnDateRangeSelector(
100 $form, $fieldName, $count,
101 $from, $to, $fromLabel,
102 $required, $operators,
103 $dateFormat, $displayTime
104 );
105 CRM_Core_Form_Date::addDateRangeToForm(
106 $form, $fieldName, $selector,
107 $from, $to, $fromLabel,
108 $required, $dateFormat, $displayTime
109 );
110 }
111
112 /**
113 * This function is to build the date range array that will provide the form option values
114 * It can be - relative or absolute
115 *
116 * @param Object $form the form object that we are operating on
117 * @param string $fieldName
118 * @param integer $count
119 * @param String $from
120 * @param String $to
121 * @param String $fromLabel
122 * @param Boolean $required
123 * @param Array $operators Additional Operator Selections to add
124 * @param String $dateFormat
125 * @param Boolean $displayTime
126 * @return array Values for Selector
127 */
128 static function returnDateRangeSelector(
129 &$form, $fieldName, $count = 1,
130 $from = '_from', $to = '_to', $fromLabel = 'From:',
131 $required = FALSE, $operators = array(),
132 $dateFormat = 'searchDate', $displayTime = FALSE
133 ) {
134 $selector =
135 array(
136 '' => ts('- any -'),
137 0 => ts('Choose Date Range'),
138 'this.year' => ts('This Year'),
139 'this.fiscal_year' => ts('This Fiscal Year'),
140 'this.quarter' => ts('This Quarter'),
141 'this.month' => ts('This Month'),
142 'this.week' => ts('This Week'),
143 'this.day' => ts('This Day'),
144 'previous.year' => ts('Previous Year'),
145 'previous.fiscal_year' => ts('Previous Fiscal Year'),
146 'previous.quarter' => ts('Previous Quarter'),
147 'previous.month' => ts('Previous Month'),
148 'previous.week' => ts('Previous Week'),
149 'previous.day' => ts('Previous Day'),
150 'previous_before.year' => ts('Prior to Previous Year'),
151 'previous_before.quarter' => ts('Prior to Previous Quarter'),
152 'previous_before.month' => ts('Prior to Previous Month'),
153 'previous_before.week' => ts('Prior to Previous Week'),
154 'previous_before.day' => ts('Prior to Previous Day'),
155 'previous_2.year' => ts('Previous 2 Years'),
156 'previous_2.quarter' => ts('Previous 2 Quarters'),
157 'previous_2.month' => ts('Previous 2 Months'),
158 'previous_2.week' => ts('Previous 2 Weeks'),
159 'previous_2.day' => ts('Previous 2 Days'),
160 'earlier.year' => ts('To End of Prior Year'),
161 'earlier.quarter' => ts('To End of Prior Quarter'),
162 'earlier.month' => ts('To End of Prior Month'),
163 'earlier.week' => ts('To End of Prior Week'),
164 'earlier.day' => ts('To End of Prior Day'),
165 'greater.year' => ts('From Start of Current Year'),
166 'greater.quarter' => ts('From Start of Current Quarter'),
167 'greater.month' => ts('From Start of Current Month'),
168 'greater.week' => ts('From Start of Current Week'),
169 'greater.day' => ts('From Start of Current Day'),
170 'current.year' => ts('Current Year to-date'),
171 'current.quarter' => ts('Current Quarter to-date'),
172 'current.month' => ts('Current Month to-date'),
173 'current.week' => ts('Current Week to-date'),
174 'ending_3.year' => ts('Last 3 Years'),
175 'ending_2.year' => ts('Last 2 Years'),
176 'ending.year' => ts('Last 12 Months'),
177 'ending.quarter' => ts('Last 3 Months'),
178 'ending.month' => ts('Last Month'),
179 'ending.week' => ts('Last 7 days'),
180 );
181
182 if (is_array($operators)) {
183 $selector = array_merge($selector, $operators);
184 }
185
186 $config = CRM_Core_Config::singleton();
187 //if fiscal year start on 1 jan then remove fiscal year task
188 //form list
189 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
190 unset($selector['this.fiscal_year']);
191 unset($selector['previous.fiscal_year']);
192 }
193 return $selector;
194 }
195
196 /**
197 * This function is to build the date range - relative or absolute
198 *
199 * @param CRM_Core_Form $form the form object that we are operating on
200 * @param string $fieldName
201 * @param Array $selector array of option values to add
202 * @param integer $count
203 * @param string $from
204 * @param stringe $to
205 * @param string $from Label
206 * @param boolean $required
207 * @param string $dateFormat
208 * @param boolean $displayTime
209 * @return null
210 */
211 static function addDateRangeToForm(&$form, $fieldName, $selector, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $dateFormat = 'searchDate', $displayTime = FALSE) {
212 $form->add('select',
213 "{$fieldName}_relative",
214 ts('Relative Date Range'),
215 $selector,
216 $required,
217 array('class' => 'crm-select2')
218 );
219
220 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
221 }
222
223 }
224