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