Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
34cd78e1 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
34cd78e1 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
34cd78e1 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'CRM/Contact/Form/Search/Custom/Base.php'; | |
a1a55b61 EM |
37 | |
38 | /** | |
39 | * Class org_civicrm_search_multivalue | |
40 | */ | |
6a488035 TO |
41 | class org_civicrm_search_multivalue extends CRM_Contact_Form_Search_Custom_BaseimplementsCRM_Contact_Form_Search_Interface { |
42 | ||
43 | protected $_groupTree; | |
44 | protected $_tables; | |
430ae6dd TO |
45 | protected $_options; |
46 | ||
a1a55b61 EM |
47 | /** |
48 | * @param $formValues | |
49 | */ | |
430ae6dd | 50 | function __construct(&$formValues) { |
6a488035 TO |
51 | parent::__construct($formValues); |
52 | ||
53 | require_once 'CRM/Core/BAO/CustomGroup.php'; | |
54 | $this->_groupTree = CRM_Core_BAO_CustomGroup::getTree("'Contact', 'Individual', 'Organization', 'Household'", | |
55 | CRM_Core_DAO::$_nullObject, | |
56 | NULL, -1 | |
57 | ); | |
58 | ||
7b99ead3 | 59 | $this->_columns = array(ts('Contact ID') => 'contact_id', |
6a488035 TO |
60 | ts('Contact Type') => 'contact_type', |
61 | ts('Name') => 'sort_name', | |
62 | ); | |
63 | ||
64 | $this->_customGroupIDs = CRM_Utils_Array::value('custom_group', $formValues); | |
65 | ||
66 | if (!empty($this->_customGroupIDs)) { | |
67 | $this->addColumns(); | |
68 | } | |
69 | } | |
70 | ||
71 | function addColumns() { | |
72 | // add all the fields for chosen groups | |
73 | $this->_tables = $this->_options = array(); | |
74 | foreach ($this->_groupTree as $groupID => $group) { | |
a7488080 | 75 | if (empty($this->_customGroupIDs[$groupID])) { |
6a488035 TO |
76 | continue; |
77 | } | |
78 | ||
79 | // now handle all the fields | |
80 | foreach ($group['fields'] as $fieldID => $field) { | |
81 | $this->_columns[$field['label']] = "custom_{$field['id']}"; | |
82 | if (!array_key_exists($group['table_name'], $this->_tables)) { | |
83 | $this->_tables[$group['table_name']] = array(); | |
84 | } | |
85 | $this->_tables[$group['table_name']][$field['id']] = $field['column_name']; | |
86 | ||
87 | // also build the option array | |
88 | $this->_options[$field['id']] = array(); | |
89 | CRM_Core_BAO_CustomField::buildOption($field, | |
90 | $this->_options[$field['id']] | |
91 | ); | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
a1a55b61 EM |
96 | /** |
97 | * @param $form | |
98 | */ | |
6a488035 TO |
99 | function buildForm(&$form) { |
100 | ||
101 | /** | |
102 | * You can define a custom title for the search form | |
103 | */ | |
104 | $this->setTitle('Multiple Value Custom Group Search and Export'); | |
105 | ||
106 | $form->add('text', | |
107 | 'sort_name', | |
108 | ts('Contact Name'), | |
109 | TRUE | |
110 | ); | |
111 | if (empty($this->_groupTree)) { | |
112 | CRM_Core_Error::statusBounce(ts("At least one Custom Group must be present, for Custom Group search."), | |
113 | CRM_Utils_System::url('civicrm/contact/search/custom/list', | |
114 | 'reset=1' | |
115 | ) | |
116 | ); | |
117 | } | |
118 | // add the checkbox for custom_groups | |
119 | foreach ($this->_groupTree as $groupID => $group) { | |
120 | if ($groupID == 'info') { | |
121 | continue; | |
122 | } | |
123 | $form->addElement('checkbox', "custom_group[$groupID]", NULL, $group['title']); | |
124 | } | |
125 | } | |
126 | ||
a1a55b61 EM |
127 | /** |
128 | * @return null | |
129 | */ | |
6a488035 TO |
130 | function summary() { |
131 | return NULL; | |
132 | } | |
133 | ||
a1a55b61 EM |
134 | /** |
135 | * @param int $offset | |
136 | * @param int $rowcount | |
137 | * @param null $sort | |
138 | * @param bool $includeContactIDs | |
139 | * | |
140 | * @return mixed | |
141 | */ | |
6a488035 TO |
142 | function all($offset = 0, $rowcount = 0, $sort = NULL, |
143 | $includeContactIDs = FALSE | |
144 | ) { | |
145 | //redirect if custom group not select in search criteria | |
a7488080 | 146 | if (empty($this->_formValues['custom_group'])) { |
6a488035 TO |
147 | CRM_Core_Error::statusBounce(ts("You must select at least one Custom Group as a search criteria."), |
148 | CRM_Utils_System::url('civicrm/contact/search/custom', | |
149 | "reset=1&csid={$this->_formValues['customSearchID']}", | |
150 | FALSE, NULL, FALSE, TRUE | |
151 | ) | |
152 | ); | |
153 | } | |
154 | $selectClause = " | |
155 | contact_a.id as contact_id , | |
156 | contact_a.contact_type as contact_type, | |
157 | contact_a.sort_name as sort_name, | |
158 | "; | |
159 | ||
160 | $customClauses = array(); | |
161 | foreach ($this->_tables as $tableName => $fields) { | |
162 | foreach ($fields as $fieldID => $fieldName) { | |
163 | $customClauses[] = "{$tableName}.{$fieldName} as custom_{$fieldID}"; | |
164 | } | |
165 | } | |
166 | $selectClause .= implode(',', $customClauses); | |
167 | ||
168 | return $this->sql($selectClause, | |
169 | $offset, $rowcount, $sort, | |
170 | $includeContactIDs, NULL | |
171 | ); | |
172 | } | |
173 | ||
a1a55b61 EM |
174 | /** |
175 | * @return string | |
176 | */ | |
6a488035 TO |
177 | function from() { |
178 | $from = "FROM civicrm_contact contact_a"; | |
179 | $customFrom = array(); | |
180 | if (!empty($this->_tables)) { | |
181 | foreach ($this->_tables as $tableName => $fields) { | |
182 | $customFrom[] = " LEFT JOIN $tableName ON {$tableName}.entity_id = contact_a.id "; | |
183 | } | |
184 | return $from . implode(' ', $customFrom); | |
185 | } | |
186 | return $from; | |
187 | } | |
188 | ||
a1a55b61 EM |
189 | /** |
190 | * @param bool $includeContactIDs | |
191 | * | |
192 | * @return mixed | |
193 | */ | |
6a488035 TO |
194 | function where($includeContactIDs = FALSE) { |
195 | $count = 1; | |
196 | $clause = array(); | |
197 | $params = array(); | |
198 | $name = CRM_Utils_Array::value('sort_name', | |
199 | $this->_formValues | |
200 | ); | |
201 | if ($name != NULL) { | |
202 | if (strpos($name, '%') === FALSE) { | |
203 | $name = "%{$name}%"; | |
204 | } | |
205 | $params[$count] = array($name, 'String'); | |
206 | $clause[] = "contact_a.sort_name LIKE %{$count}"; | |
207 | $count++; | |
208 | } | |
209 | ||
210 | $where = '( 1 )'; | |
211 | if (!empty($clause)) { | |
212 | $where .= ' AND ' . implode(' AND ', $clause); | |
213 | } | |
214 | ||
215 | return $this->whereClause($where, $params); | |
216 | } | |
217 | ||
a1a55b61 EM |
218 | /** |
219 | * @return string | |
220 | */ | |
6a488035 TO |
221 | function templateFile() { |
222 | return 'MultipleValues.tpl'; | |
223 | } | |
224 | ||
a1a55b61 EM |
225 | /** |
226 | * @return array | |
227 | */ | |
6a488035 TO |
228 | function setDefaultValues() { |
229 | return array(); | |
230 | } | |
231 | ||
a1a55b61 EM |
232 | /** |
233 | * @param $row | |
234 | */ | |
6a488035 TO |
235 | function alterRow(&$row) { |
236 | foreach ($this->_options as $fieldID => $values) { | |
237 | $customVal = $valueSeparatedArray = array(); | |
238 | if (in_array($values['attributes']['html_type'], | |
239 | array('Radio', 'Select', 'Autocomplete-Select') | |
240 | )) { | |
241 | if ($values['attributes']['data_type'] == 'ContactReference' && $row["custom_{$fieldID}"]) { | |
242 | $row["custom_{$fieldID}"] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', (int)$row["custom_{$fieldID}"], 'display_name'); | |
243 | } | |
244 | elseif ($row["custom_{$fieldID}"] && | |
245 | array_key_exists($row["custom_{$fieldID}"], | |
246 | $values | |
247 | ) | |
248 | ) { | |
249 | $row["custom_{$fieldID}"] = $values[$row["custom_{$fieldID}"]]; | |
250 | } | |
251 | } | |
252 | elseif (in_array($values['attributes']['html_type'], | |
253 | array('CheckBox', 'Multi-Select', 'AdvMulti-Select') | |
254 | )) { | |
255 | $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); | |
256 | foreach ($valueSeparatedArray as $val) { | |
257 | $customVal[] = $values[$val]; | |
258 | } | |
259 | $row["custom_{$fieldID}"] = implode(', ', $customVal); | |
260 | } | |
261 | elseif (in_array($values['attributes']['html_type'], | |
262 | array('Multi-Select State/Province', 'Select State/Province') | |
263 | )) { | |
264 | $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); | |
265 | $stateName = CRM_Core_PseudoConstant::stateProvince(); | |
266 | foreach ($valueSeparatedArray as $val) { | |
267 | $customVal[] = $stateName[$val]; | |
268 | } | |
269 | $row["custom_{$fieldID}"] = implode(', ', $customVal); | |
270 | } | |
271 | elseif (in_array($values['attributes']['html_type'], | |
272 | array('Multi-Select Country', 'Select Country') | |
273 | )) { | |
274 | $valueSeparatedArray = array_filter(explode(CRM_Core_DAO::VALUE_SEPARATOR, $row["custom_{$fieldID}"])); | |
275 | CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', | |
276 | TRUE, 'name', 'is_active' | |
277 | ); | |
278 | foreach ($valueSeparatedArray as $val) { | |
279 | $customVal[] = $countryNames[$val]; | |
280 | } | |
281 | $row["custom_{$fieldID}"] = implode(', ', $customVal); | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
a1a55b61 EM |
286 | /** |
287 | * @param $title | |
288 | */ | |
6a488035 TO |
289 | function setTitle($title) { |
290 | CRM_Utils_System::setTitle($title); | |
291 | } | |
292 | } | |
293 |