Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 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 | | |
9df8a11d | 11 | s | under the terms of the GNU Affero General Public License | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Files required | |
38 | */ | |
39 | ||
40 | /** | |
41 | * advanced search, extends basic search | |
42 | */ | |
43 | class CRM_Contact_Form_Search_Advanced extends CRM_Contact_Form_Search { | |
44 | ||
45 | /** | |
100fef9d | 46 | * Processing needed for buildForm and later |
6a488035 TO |
47 | * |
48 | * @return void | |
6a488035 | 49 | */ |
00be9182 | 50 | public function preProcess() { |
6a488035 TO |
51 | $this->set('searchFormName', 'Advanced'); |
52 | ||
53 | parent::preProcess(); | |
54 | $openedPanes = CRM_Contact_BAO_Query::$_openedPanes; | |
55 | $openedPanes = array_merge($openedPanes, $this->_openedPanes); | |
56 | $this->assign('openedPanes', $openedPanes); | |
57 | } | |
58 | ||
59 | /** | |
c490a46a | 60 | * Build the form object |
6a488035 | 61 | * |
6a488035 TO |
62 | * |
63 | * @return void | |
64 | */ | |
00be9182 | 65 | public function buildQuickForm() { |
6a488035 TO |
66 | $this->set('context', 'advanced'); |
67 | ||
68 | $this->_searchPane = CRM_Utils_Array::value('searchPane', $_GET); | |
69 | ||
70 | $this->_searchOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
71 | 'advanced_search_options' | |
72 | ); | |
73 | ||
74 | if (!$this->_searchPane || $this->_searchPane == 'basic') { | |
75 | CRM_Contact_Form_Search_Criteria::basic($this); | |
76 | } | |
77 | ||
78 | $allPanes = array(); | |
79 | $paneNames = array( | |
80 | ts('Address Fields') => 'location', | |
81 | ts('Custom Fields') => 'custom', | |
82 | ts('Activities') => 'activity', | |
83 | ts('Relationships') => 'relationship', | |
84 | ts('Demographics') => 'demographics', | |
85 | ts('Notes') => 'notes', | |
86 | ts('Change Log') => 'changeLog', | |
87 | ); | |
88 | ||
89 | //check if there are any custom data searchable fields | |
90 | $groupDetails = array(); | |
91 | $extends = array_merge(array('Contact', 'Individual', 'Household', 'Organization'), | |
92 | CRM_Contact_BAO_ContactType::subTypes() | |
93 | ); | |
94 | $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, | |
95 | $extends | |
96 | ); | |
97 | // if no searchable fields unset panel | |
98 | if (empty($groupDetails)) { | |
99 | unset($paneNames[ts('Custom Fields')]); | |
100 | } | |
101 | ||
102 | foreach ($paneNames as $name => $type) { | |
103 | if (!$this->_searchOptions[$type]) { | |
104 | unset($paneNames[$name]); | |
105 | } | |
106 | } | |
107 | ||
108 | $components = CRM_Core_Component::getEnabledComponents(); | |
109 | ||
110 | $componentPanes = array(); | |
111 | foreach ($components as $name => $component) { | |
112 | if (in_array($name, array_keys($this->_searchOptions)) && | |
113 | $this->_searchOptions[$name] && | |
114 | CRM_Core_Permission::access($component->name) | |
115 | ) { | |
116 | $componentPanes[$name] = $component->registerAdvancedSearchPane(); | |
117 | $componentPanes[$name]['name'] = $name; | |
118 | } | |
119 | } | |
120 | ||
121 | usort($componentPanes, array('CRM_Utils_Sort', 'cmpFunc')); | |
122 | foreach ($componentPanes as $name => $pane) { | |
123 | // FIXME: we should change the use of $name here to keyword | |
124 | $paneNames[$pane['title']] = $pane['name']; | |
125 | } | |
126 | ||
efa3a566 DS |
127 | $hookPanes = array(); |
128 | CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes); | |
129 | $paneNames = array_merge($paneNames, $hookPanes); | |
6a488035 | 130 | |
efa3a566 | 131 | $this->_paneTemplatePath = array(); |
6a488035 | 132 | foreach ($paneNames as $name => $type) { |
efa3a566 | 133 | if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) { |
6a488035 TO |
134 | continue; |
135 | } | |
136 | ||
137 | $allPanes[$name] = array( | |
138 | 'url' => CRM_Utils_System::url('civicrm/contact/search/advanced', | |
139 | "snippet=1&searchPane=$type&qfKey={$this->controller->_key}" | |
140 | ), | |
141 | 'open' => 'false', | |
142 | 'id' => $type, | |
143 | ); | |
144 | ||
145 | // see if we need to include this paneName in the current form | |
8cc574cf | 146 | if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) || |
6a488035 TO |
147 | CRM_Utils_Array::value("hidden_{$type}", $this->_formValues) |
148 | ) { | |
149 | $allPanes[$name]['open'] = 'true'; | |
150 | ||
a7488080 | 151 | if (!empty($components[$type])) { |
6a488035 TO |
152 | $c = $components[$type]; |
153 | $this->add('hidden', "hidden_$type", 1); | |
154 | $c->buildAdvancedSearchPaneForm($this); | |
155 | $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath(); | |
156 | } | |
4c9b6178 | 157 | elseif (in_array($type, $hookPanes)) { |
efa3a566 DS |
158 | CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type); |
159 | CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type); | |
160 | } | |
6a488035 | 161 | else { |
0e6e8724 | 162 | CRM_Contact_Form_Search_Criteria::$type($this); |
6a488035 TO |
163 | $template = ucfirst($type); |
164 | $this->_paneTemplatePath[$type] = "CRM/Contact/Form/Search/Criteria/{$template}.tpl"; | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | $this->assign('allPanes', $allPanes); | |
170 | if (!$this->_searchPane) { | |
171 | parent::buildQuickForm(); | |
172 | } | |
173 | else { | |
174 | $this->assign('suppressForm', TRUE); | |
175 | } | |
176 | } | |
177 | ||
86538308 EM |
178 | /** |
179 | * Use the form name to create the tpl file name | |
180 | * | |
181 | * @return string | |
86538308 EM |
182 | */ |
183 | /** | |
184 | * @return string | |
185 | */ | |
00be9182 | 186 | public function getTemplateFileName() { |
6a488035 TO |
187 | if (!$this->_searchPane) { |
188 | return parent::getTemplateFileName(); | |
189 | } | |
190 | else { | |
191 | if (isset($this->_paneTemplatePath[$this->_searchPane])) { | |
192 | return $this->_paneTemplatePath[$this->_searchPane]; | |
193 | } | |
194 | else { | |
195 | $name = ucfirst($this->_searchPane); | |
196 | return "CRM/Contact/Form/Search/Criteria/{$name}.tpl"; | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
201 | /** | |
202 | * Set the default form values | |
203 | * | |
6a488035 | 204 | * |
a6c01b45 CW |
205 | * @return array |
206 | * the default array reference | |
6a488035 | 207 | */ |
00be9182 | 208 | public function setDefaultValues() { |
6a488035 TO |
209 | $defaults = $this->_formValues; |
210 | $this->normalizeDefaultValues($defaults); | |
211 | ||
212 | if ($this->_context === 'amtg') { | |
213 | $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS; | |
214 | } | |
6a488035 TO |
215 | |
216 | $defaults['privacy_toggle'] = 1; | |
217 | ||
218 | return $defaults; | |
219 | } | |
220 | ||
221 | /** | |
222 | * The post processing of the form gets done here. | |
223 | * | |
224 | * Key things done during post processing are | |
225 | * - check for reset or next request. if present, skip post procesing. | |
226 | * - now check if user requested running a saved search, if so, then | |
227 | * the form values associated with the saved search are used for searching. | |
228 | * - if user has done a submit with new values the regular post submissing is | |
229 | * done. | |
230 | * The processing consists of using a Selector / Controller framework for getting the | |
231 | * search results. | |
232 | * | |
233 | * @param | |
234 | * | |
235 | * @return void | |
6a488035 | 236 | */ |
00be9182 | 237 | public function postProcess() { |
6a488035 TO |
238 | $this->set('isAdvanced', '1'); |
239 | ||
240 | // get user submitted values | |
241 | // get it from controller only if form has been submitted, else preProcess has set this | |
242 | if (!empty($_POST)) { | |
243 | $this->_formValues = $this->controller->exportValues($this->_name); | |
244 | $this->normalizeFormValues(); | |
245 | // FIXME: couldn't figure out a good place to do this, | |
246 | // FIXME: so leaving this as a dependency for now | |
247 | if (array_key_exists('contribution_amount_low', $this->_formValues)) { | |
248 | foreach (array( | |
353ffa53 | 249 | 'contribution_amount_low', |
af9b09df | 250 | 'contribution_amount_high', |
353ffa53 | 251 | ) as $f) { |
6a488035 TO |
252 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); |
253 | } | |
254 | } | |
255 | ||
256 | // set the group if group is submitted | |
81aa6678 | 257 | if (!empty($this->_formValues['uf_group_id'])) { |
6a488035 TO |
258 | $this->set('id', $this->_formValues['uf_group_id']); |
259 | } | |
260 | else { | |
261 | $this->set('id', ''); | |
262 | } | |
263 | } | |
264 | ||
265 | // retrieve ssID values only if formValues is null, i.e. form has never been posted | |
266 | if (empty($this->_formValues) && isset($this->_ssID)) { | |
267 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
268 | } | |
269 | ||
8cc574cf | 270 | if (isset($this->_groupID) && empty($this->_formValues['group'])) { |
6a488035 TO |
271 | $this->_formValues['group'] = array($this->_groupID => 1); |
272 | } | |
273 | ||
6a488035 TO |
274 | //search for civicase |
275 | if (is_array($this->_formValues)) { | |
276 | $allCases = FALSE; | |
277 | if (array_key_exists('case_owner', $this->_formValues) && | |
278 | !$this->_formValues['case_owner'] && | |
279 | !$this->_force | |
280 | ) { | |
281 | foreach (array( | |
353ffa53 TO |
282 | 'case_type_id', |
283 | 'case_status_id', | |
284 | 'case_deleted', | |
af9b09df | 285 | 'case_tags', |
353ffa53 | 286 | ) as $caseCriteria) { |
a7488080 | 287 | if (!empty($this->_formValues[$caseCriteria])) { |
6a488035 TO |
288 | $allCases = TRUE; |
289 | $this->_formValues['case_owner'] = 1; | |
290 | continue; | |
291 | } | |
292 | } | |
293 | if ($allCases) { | |
294 | if (CRM_Core_Permission::check('access all cases and activities')) { | |
295 | $this->_formValues['case_owner'] = 1; | |
296 | } | |
297 | else { | |
298 | $this->_formValues['case_owner'] = 2; | |
299 | } | |
300 | } | |
301 | else { | |
302 | $this->_formValues['case_owner'] = 0; | |
303 | } | |
304 | } | |
d8f7f92c RK |
305 | if (array_key_exists('case_owner', $this->_formValues) && empty($this->_formValues['case_deleted'])) { |
306 | $this->_formValues['case_deleted'] = 0; | |
307 | } | |
6a488035 TO |
308 | } |
309 | ||
310 | // we dont want to store the sortByCharacter in the formValue, it is more like | |
311 | // a filter on the result set | |
312 | // this filter is reset if we click on the search button | |
e166ff79 | 313 | if ($this->_sortByCharacter !== NULL && empty($_POST)) { |
6a488035 TO |
314 | if (strtolower($this->_sortByCharacter) == 'all') { |
315 | $this->_formValues['sortByCharacter'] = NULL; | |
316 | } | |
317 | else { | |
318 | $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; | |
319 | } | |
320 | } | |
e166ff79 CW |
321 | else { |
322 | $this->_sortByCharacter = NULL; | |
323 | } | |
6a488035 TO |
324 | |
325 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); | |
326 | ||
327 | $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
328 | $this->_returnProperties = &$this->returnProperties(); | |
329 | parent::postProcess(); | |
330 | } | |
331 | ||
332 | /** | |
100fef9d | 333 | * Normalize the form values to make it look similar to the advanced form values |
6a488035 TO |
334 | * this prevents a ton of work downstream and allows us to use the same code for |
335 | * multiple purposes (queries, save/edit etc) | |
336 | * | |
337 | * @return void | |
6a488035 | 338 | */ |
00be9182 | 339 | public function normalizeFormValues() { |
6a488035 TO |
340 | $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues); |
341 | ||
342 | if ($contactType && is_array($contactType)) { | |
343 | unset($this->_formValues['contact_type']); | |
344 | foreach ($contactType as $key => $value) { | |
345 | $this->_formValues['contact_type'][$value] = 1; | |
346 | } | |
347 | } | |
348 | ||
349 | $config = CRM_Core_Config::singleton(); | |
350 | $group = CRM_Utils_Array::value('group', $this->_formValues); | |
351 | if ($group && is_array($group)) { | |
352 | unset($this->_formValues['group']); | |
353 | foreach ($group as $key => $value) { | |
354 | $this->_formValues['group'][$value] = 1; | |
355 | } | |
356 | } | |
357 | ||
358 | $tag = CRM_Utils_Array::value('contact_tags', $this->_formValues); | |
359 | if ($tag && is_array($tag)) { | |
360 | unset($this->_formValues['contact_tags']); | |
361 | foreach ($tag as $key => $value) { | |
362 | $this->_formValues['contact_tags'][$value] = 1; | |
363 | } | |
364 | } | |
86538308 | 365 | |
afa0b07c | 366 | $specialParams = array( |
367 | 'financial_type_id', | |
368 | 'contribution_soft_credit_type_id', | |
369 | 'contribution_status', | |
370 | 'contribution_source', | |
371 | 'contribution_trxn_id', | |
372 | 'activity_type_id', | |
373 | 'status_id', | |
374 | 'activity_subject', | |
375 | 'participant_status_id', | |
af9b09df | 376 | 'participant_role_id', |
afa0b07c | 377 | ); |
378 | foreach ($specialParams as $element) { | |
3c151c70 | 379 | $value = CRM_Utils_Array::value($element, $this->_formValues); |
9ab34172 | 380 | if ($value) { |
381 | if (is_array($value)) { | |
382 | if ($element == 'status_id') { | |
383 | unset($this->_formValues[$element]); | |
384 | $element = 'activity_' . $element; | |
385 | } | |
386 | $this->_formValues[$element] = array('IN' => $value); | |
387 | } | |
388 | else { | |
389 | $this->_formValues[$element] = array('LIKE' => "%$value%"); | |
390 | } | |
1f0d8c92 PN |
391 | } |
392 | } | |
86538308 | 393 | |
6a488035 TO |
394 | $taglist = CRM_Utils_Array::value('contact_taglist', $this->_formValues); |
395 | ||
396 | if ($taglist && is_array($taglist)) { | |
397 | unset($this->_formValues['contact_taglist']); | |
398 | foreach ($taglist as $value) { | |
399 | if ($value) { | |
400 | $value = explode(',', $value); | |
401 | foreach ($value as $tId) { | |
402 | if (is_numeric($tId)) { | |
403 | $this->_formValues['contact_tags'][$tId] = 1; | |
404 | } | |
405 | } | |
406 | } | |
407 | } | |
408 | } | |
6a488035 TO |
409 | } |
410 | ||
411 | /** | |
100fef9d | 412 | * Normalize default values for multiselect plugins |
6a488035 | 413 | * |
5a4f6742 | 414 | * @param array $defaults |
f4bff68a | 415 | * @return array |
6a488035 | 416 | */ |
00be9182 | 417 | public function normalizeDefaultValues(&$defaults) { |
6a488035 TO |
418 | if (!is_array($defaults)) { |
419 | $defaults = array(); | |
420 | } | |
421 | ||
422 | if ($this->_ssID && empty($_POST)) { | |
423 | $fields = array('contact_type', 'group', 'contact_tags'); | |
424 | ||
425 | foreach ($fields as $field) { | |
426 | $fieldValues = CRM_Utils_Array::value($field, $defaults); | |
427 | if ($fieldValues && is_array($fieldValues)) { | |
428 | $defaults[$field] = array_keys($fieldValues); | |
429 | } | |
430 | } | |
431 | } | |
432 | return $defaults; | |
433 | } | |
96025800 | 434 | |
6a488035 | 435 | } |