Merge pull request #4851 from totten/master-createtest-bao
[civicrm-core.git] / CRM / Contact / Form / Search / Advanced.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 s | 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-2014
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 */
50 public function preProcess() {
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 /**
60 * Build the form object
61 *
62 *
63 * @return void
64 */
65 public function buildQuickForm() {
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
127 $hookPanes = array();
128 CRM_Contact_BAO_Query_Hook::singleton()->registerAdvancedSearchPane($hookPanes);
129 $paneNames = array_merge($paneNames, $hookPanes);
130
131 $this->_paneTemplatePath = array();
132 foreach ($paneNames as $name => $type) {
133 if (!array_key_exists($type, $this->_searchOptions) && !in_array($type, $hookPanes)) {
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
146 if ($this->_searchPane == $type || !empty($_POST["hidden_{$type}"]) ||
147 CRM_Utils_Array::value("hidden_{$type}", $this->_formValues)
148 ) {
149 $allPanes[$name]['open'] = 'true';
150
151 if (!empty($components[$type])) {
152 $c = $components[$type];
153 $this->add('hidden', "hidden_$type", 1);
154 $c->buildAdvancedSearchPaneForm($this);
155 $this->_paneTemplatePath[$type] = $c->getAdvancedSearchPaneTemplatePath();
156 }
157 elseif (in_array($type, $hookPanes)) {
158 CRM_Contact_BAO_Query_Hook::singleton()->buildAdvancedSearchPaneForm($this, $type);
159 CRM_Contact_BAO_Query_Hook::singleton()->setAdvancedSearchPaneTemplatePath($this->_paneTemplatePath, $type);
160 }
161 else {
162 CRM_Contact_Form_Search_Criteria::$type($this);
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
178 /**
179 * Use the form name to create the tpl file name
180 *
181 * @return string
182 */
183 /**
184 * @return string
185 */
186 public function getTemplateFileName() {
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 *
204 *
205 * @return array the default array reference
206 */
207 public function setDefaultValues() {
208 $defaults = $this->_formValues;
209 $this->normalizeDefaultValues($defaults);
210
211 if ($this->_context === 'amtg') {
212 $defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
213 }
214
215 $defaults['privacy_toggle'] = 1;
216
217 return $defaults;
218 }
219
220 /**
221 * The post processing of the form gets done here.
222 *
223 * Key things done during post processing are
224 * - check for reset or next request. if present, skip post procesing.
225 * - now check if user requested running a saved search, if so, then
226 * the form values associated with the saved search are used for searching.
227 * - if user has done a submit with new values the regular post submissing is
228 * done.
229 * The processing consists of using a Selector / Controller framework for getting the
230 * search results.
231 *
232 * @param
233 *
234 * @return void
235 */
236 public function postProcess() {
237 $this->set('isAdvanced', '1');
238
239 // get user submitted values
240 // get it from controller only if form has been submitted, else preProcess has set this
241 if (!empty($_POST)) {
242 $this->_formValues = $this->controller->exportValues($this->_name);
243 $this->normalizeFormValues();
244 // FIXME: couldn't figure out a good place to do this,
245 // FIXME: so leaving this as a dependency for now
246 if (array_key_exists('contribution_amount_low', $this->_formValues)) {
247 foreach (array(
248 'contribution_amount_low', 'contribution_amount_high') as $f) {
249 $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]);
250 }
251 }
252
253 // set the group if group is submitted
254 if (!empty($this->_formValues['uf_group_id'])) {
255 $this->set('id', $this->_formValues['uf_group_id']);
256 }
257 else {
258 $this->set('id', '');
259 }
260 }
261
262 // retrieve ssID values only if formValues is null, i.e. form has never been posted
263 if (empty($this->_formValues) && isset($this->_ssID)) {
264 $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
265 }
266
267 if (isset($this->_groupID) && empty($this->_formValues['group'])) {
268 $this->_formValues['group'] = array($this->_groupID => 1);
269 }
270
271
272 //search for civicase
273 if (is_array($this->_formValues)) {
274 $allCases = FALSE;
275 if (array_key_exists('case_owner', $this->_formValues) &&
276 !$this->_formValues['case_owner'] &&
277 !$this->_force
278 ) {
279 foreach (array(
280 'case_type_id', 'case_status_id', 'case_deleted', 'case_tags') as $caseCriteria) {
281 if (!empty($this->_formValues[$caseCriteria])) {
282 $allCases = TRUE;
283 $this->_formValues['case_owner'] = 1;
284 continue;
285 }
286 }
287 if ($allCases) {
288 if (CRM_Core_Permission::check('access all cases and activities')) {
289 $this->_formValues['case_owner'] = 1;
290 }
291 else {
292 $this->_formValues['case_owner'] = 2;
293 }
294 }
295 else {
296 $this->_formValues['case_owner'] = 0;
297 }
298 }
299 if (array_key_exists('case_owner', $this->_formValues) && empty($this->_formValues['case_deleted'])) {
300 $this->_formValues['case_deleted'] = 0;
301 }
302 }
303
304 // we dont want to store the sortByCharacter in the formValue, it is more like
305 // a filter on the result set
306 // this filter is reset if we click on the search button
307 if ($this->_sortByCharacter !== NULL && empty($_POST)) {
308 if (strtolower($this->_sortByCharacter) == 'all') {
309 $this->_formValues['sortByCharacter'] = NULL;
310 }
311 else {
312 $this->_formValues['sortByCharacter'] = $this->_sortByCharacter;
313 }
314 }
315 else {
316 $this->_sortByCharacter = NULL;
317 }
318
319 CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues);
320
321 $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
322 $this->_returnProperties = &$this->returnProperties();
323 parent::postProcess();
324 }
325
326 /**
327 * Normalize the form values to make it look similar to the advanced form values
328 * this prevents a ton of work downstream and allows us to use the same code for
329 * multiple purposes (queries, save/edit etc)
330 *
331 * @return void
332 */
333 public function normalizeFormValues() {
334 $contactType = CRM_Utils_Array::value('contact_type', $this->_formValues);
335
336 if ($contactType && is_array($contactType)) {
337 unset($this->_formValues['contact_type']);
338 foreach ($contactType as $key => $value) {
339 $this->_formValues['contact_type'][$value] = 1;
340 }
341 }
342
343 $config = CRM_Core_Config::singleton();
344 $group = CRM_Utils_Array::value('group', $this->_formValues);
345 if ($group && is_array($group)) {
346 unset($this->_formValues['group']);
347 foreach ($group as $key => $value) {
348 $this->_formValues['group'][$value] = 1;
349 }
350 }
351
352 $tag = CRM_Utils_Array::value('contact_tags', $this->_formValues);
353 if ($tag && is_array($tag)) {
354 unset($this->_formValues['contact_tags']);
355 foreach ($tag as $key => $value) {
356 $this->_formValues['contact_tags'][$value] = 1;
357 }
358 }
359
360 // CRM-13848
361 $financialType = CRM_Utils_Array::value('financial_type_id', $this->_formValues);
362 if ($financialType && is_array($financialType)) {
363 unset($this->_formValues['financial_type_id']);
364 foreach ($financialType as $notImportant => $typeID) {
365 $this->_formValues['financial_type_id'][$typeID] = 1;
366 }
367 }
368
369 $taglist = CRM_Utils_Array::value('contact_taglist', $this->_formValues);
370
371 if ($taglist && is_array($taglist)) {
372 unset($this->_formValues['contact_taglist']);
373 foreach ($taglist as $value) {
374 if ($value) {
375 $value = explode(',', $value);
376 foreach ($value as $tId) {
377 if (is_numeric($tId)) {
378 $this->_formValues['contact_tags'][$tId] = 1;
379 }
380 }
381 }
382 }
383 }
384
385 return;
386 }
387
388 /**
389 * Normalize default values for multiselect plugins
390 *
391 * @param $defaults
392 * Array.
393 * @return array
394 */
395 public function normalizeDefaultValues(&$defaults) {
396 if (!is_array($defaults)) {
397 $defaults = array();
398 }
399
400 if ($this->_ssID && empty($_POST)) {
401 $fields = array('contact_type', 'group', 'contact_tags');
402
403 foreach ($fields as $field) {
404 $fieldValues = CRM_Utils_Array::value($field, $defaults);
405 if ($fieldValues && is_array($fieldValues)) {
406 $defaults[$field] = array_keys($fieldValues);
407 }
408 }
409 }
410 return $defaults;
411 }
412 }