CRM-17646 - Refactor out CRM_Core_BAO_CustomOption::valuesByID
[civicrm-core.git] / CRM / Core / Form / Date.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Core_Form_Date {
34
35 /**
36 * Various Date Formats.
37 */
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;
39
40 /**
41 * Build the date-format form.
42 *
43 * @param CRM_Core_Form $form
44 * The form object that we are operating on.
45 */
46 public static function buildAllowedDateFormats(&$form) {
47
48 $dateOptions = array();
49
50 if (CRM_Utils_System::getClassName($form) == 'CRM_Activity_Import_Form_DataSource') {
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/>');
65 $form->setDefaults(array('dateFormats' => self::DATE_yyyy_mm_dd));
66 }
67
68
69 /**
70 * Retrieve the date range - relative or absolute and assign it to the form.
71 *
72 * @param CRM_Core_Form $form
73 * The form the dates should be added to.
74 * @param string $fieldName
75 * @param int $count
76 * @param string $from
77 * @param string $to
78 * @param string $fromLabel
79 * @param bool $required
80 * @param array $operators
81 * Additional value pairs to add.
82 * @param string $dateFormat
83 * @param bool|string $displayTime
84 */
85 public static function buildDateRange(
86 &$form, $fieldName, $count = 1,
87 $from = '_from', $to = '_to', $fromLabel = 'From:',
88 $required = FALSE, $operators = array(),
89 $dateFormat = 'searchDate', $displayTime = FALSE
90 ) {
91 $selector
92 = CRM_Core_Form_Date::returnDateRangeSelector(
93 $form, $fieldName, $count,
94 $from, $to, $fromLabel,
95 $required, $operators,
96 $dateFormat, $displayTime
97 );
98 CRM_Core_Form_Date::addDateRangeToForm(
99 $form, $fieldName, $selector,
100 $from, $to, $fromLabel,
101 $required, $dateFormat, $displayTime
102 );
103 }
104
105 /**
106 * Build the date range array that will provide the form option values.
107 *
108 * It can be - relative or absolute.
109 *
110 * @param CRM_Core_Form $form
111 * The form object that we are operating on.
112 * @param string $fieldName
113 * @param int $count
114 * @param string $from
115 * @param string $to
116 * @param string $fromLabel
117 * @param bool $required
118 * @param array $operators
119 * Additional Operator Selections to add.
120 * @param string $dateFormat
121 * @param bool $displayTime
122 *
123 * @return array
124 * Values for Selector
125 */
126 public static function returnDateRangeSelector(
127 &$form, $fieldName, $count = 1,
128 $from = '_from', $to = '_to', $fromLabel = 'From:',
129 $required = FALSE, $operators = array(),
130 $dateFormat = 'searchDate', $displayTime = FALSE
131 ) {
132 $selector = array(
133 '' => ts('- any -'),
134 0 => ts('Choose Date Range'),
135 );
136 // CRM-16195 Pull relative date filters from an option group
137 $selector = $selector + CRM_Core_OptionGroup::values('relative_date_filters');
138
139 if (is_array($operators)) {
140 $selector = array_merge($selector, $operators);
141 }
142
143 $config = CRM_Core_Config::singleton();
144 //if fiscal year start on 1 jan then remove fiscal year task
145 //form list
146 if ($config->fiscalYearStart['d'] == 1 & $config->fiscalYearStart['M'] == 1) {
147 unset($selector['this.fiscal_year']);
148 unset($selector['previous.fiscal_year']);
149 }
150 return $selector;
151 }
152
153 /**
154 * Build the date range - relative or absolute.
155 *
156 * @param CRM_Core_Form $form
157 * The form object that we are operating on.
158 * @param string $fieldName
159 * @param array $selector
160 * Array of option values to add.
161 * @param string $from
162 * Label.
163 * @param string $to
164 * @param string $fromLabel
165 * @param bool $required
166 * @param string $dateFormat
167 * @param bool $displayTime
168 */
169 public static function addDateRangeToForm(&$form, $fieldName, $selector, $from = '_from', $to = '_to', $fromLabel = 'From:', $required = FALSE, $dateFormat = 'searchDate', $displayTime = FALSE) {
170 $form->add('select',
171 "{$fieldName}_relative",
172 ts('Relative Date Range'),
173 $selector,
174 $required,
175 array('class' => 'crm-select2')
176 );
177
178 $form->addDateRange($fieldName, $from, $to, $fromLabel, $dateFormat, FALSE, $displayTime);
179 }
180
181 }