7693b97eae1d965d598c1246062d6b1e787876fb
[civicrm-core.git] / CRM / Core / Form / Date.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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_UploadFile') {
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(&$form, $fieldName, $count = 1, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $operators = array(), $dateFormat = 'searchDate', $displayTime = FALSE) {
93 $selector = CRM_Core_Form_Date::returnDateRangeSelector($form, $fieldName, $count, $from, $to, $fromLabel, $required, $operators, $dateFormat, $displayTime);
94 CRM_Core_Form_Date::addDateRangeToForm($form, $fieldName, $selector, $from, $to, $fromLabel, $required , $dateFormat, $displayTime);
95 }
96
97 /**
98 * This function is to build the date range array that will provide the form option values
99 * It can be - relative or absolute
100 *
101 * @param Object $form the form object that we are operating on
102 * @param string $fieldName
103 * @param integer $count
104 * @param String $from
105 * @param String $to
106 * @param String $fromLabel
107 * @param Boolean $required
108 * @param Array $operators Additional Operator Selections to add
109 * @param String $dateFormat
110 * @param Boolean $displayTime
111 * @return array Values for Selector
112 */
113 static function returnDateRangeSelector(&$form, $fieldName, $count = 1, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $operators = array(), $dateFormat = 'searchDate', $displayTime = FALSE) {
114 $selector = array('' => ts('- any -'),
115 0 => ts('Choose Date Range'),
116 'this.year' => ts('This Year'),
117 'this.fiscal_year' => ts('This Fiscal Year'),
118 'this.quarter' => ts('This Quarter'),
119 'this.month' => ts('This Month'),
120 'this.week' => ts('This Week'),
121 'this.day' => ts('This Day'),
122 'previous.year' => ts('Previous Year'),
123 'previous.fiscal_year' => ts('Previous Fiscal Year'),
124 'previous.quarter' => ts('Previous Quarter'),
125 'previous.month' => ts('Previous Month'),
126 'previous.week' => ts('Previous Week'),
127 'previous.day' => ts('Previous Day'),
128 'previous_before.year' => ts('Prior to Previous Year'),
129 'previous_before.quarter' => ts('Prior to Previous Quarter'),
130 'previous_before.month' => ts('Prior to Previous Month'),
131 'previous_before.week' => ts('Prior to Previous Week'),
132 'previous_before.day' => ts('Prior to Previous Day'),
133 'previous_2.year' => ts('Previous 2 Years'),
134 'previous_2.quarter' => ts('Previous 2 Quarters'),
135 'previous_2.month' => ts('Previous 2 Months'),
136 'previous_2.week' => ts('Previous 2 Weeks'),
137 'previous_2.day' => ts('Previous 2 Days'),
138 'earlier.year' => ts('To End of Prior Year'),
139 'earlier.quarter' => ts('To End of Prior Quarter'),
140 'earlier.month' => ts('To End of Prior Month'),
141 'earlier.week' => ts('To End of Prior Week'),
142 'earlier.day' => ts('To End of Prior Day'),
143 'greater.year' => ts('Current Year to-date'),
144 'greater.quarter' => ts('Current Quarter to-date'),
145 'greater.month' => ts('Current Month to-date'),
146 'greater.week' => ts('Current Week to-date'),
147 'greater.day' => ts('Current Day'),
148 'ending.year' => ts('From 12 Months Ago'),
149 'ending.quarter' => ts('From 3 Months Ago'),
150 'ending.month' => ts('From 1 Month Ago'),
151 'ending.week' => ts('From 1 Week Ago'),
152 );
153 $selector += $operators;
154
155 $config = CRM_Core_Config::singleton();
156 //if fiscal year start on 1 jan then remove fiscal year task
157 //form list
158 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
159 unset($selector['this.fiscal_year']);
160 unset($selector['previous.fiscal_year']);
161 }
162 return $selector;
163 }
164
165 /**
166 * This function is to build the date range - relative or absolute
167 *
168 * @param Object $form the form object that we are operating on
169 * @param string $fieldName
170 * @param Array $selector array of option values to add
171 * @param integer $count
172 * @param string $from
173 * @param stringe $to
174 * @param string $from Label
175 * @param boolean $required
176 * @param string $dateFormat
177 * @param boolean $displayTime
178 * @return null
179 */
180 static function addDateRangeToForm(&$form, $fieldName, $selector, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $dateFormat = 'searchDate', $displayTime = FALSE) {
181 $form->add('select',
182 "{$fieldName}_relative",
183 ts('Relative Date Range'),
184 $selector,
185 $required
186 );
187
188 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
189 }
190
191 }
192