Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Files required | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Base Search / View form for *all* listing of multiple | |
42 | * contacts | |
43 | */ | |
3efb5b86 | 44 | class CRM_Contact_Form_Search extends CRM_Core_Form_Search { |
6a488035 TO |
45 | |
46 | /* | |
47 | * list of valid contexts | |
48 | * | |
49 | * @var array | |
50 | * @static | |
51 | */ | |
52 | static $_validContext = NULL; | |
53 | ||
54 | /** | |
55 | * list of values used when we want to display other objects | |
56 | * | |
57 | * @var array | |
58 | * @static | |
59 | */ | |
60 | static $_modeValues = NULL; | |
61 | ||
6a488035 TO |
62 | /** |
63 | * The contextMenu | |
64 | * | |
65 | * @var array | |
66 | * @access protected | |
67 | */ | |
68 | protected $_contextMenu; | |
69 | ||
70 | /** | |
71 | * the groupId retrieved from the GET vars | |
72 | * | |
73 | * @var int | |
74 | * @access public | |
75 | */ | |
76 | public $_groupID; | |
77 | ||
78 | /** | |
79 | * the Group ID belonging to Add Member to group ID | |
80 | * retrieved from the GET vars | |
81 | * | |
82 | * @var int | |
83 | * @access protected | |
84 | */ | |
85 | protected $_amtgID; | |
86 | ||
87 | /** | |
88 | * the saved search ID retrieved from the GET vars | |
89 | * | |
90 | * @var int | |
91 | * @access protected | |
92 | */ | |
93 | protected $_ssID; | |
94 | ||
6a488035 TO |
95 | /** |
96 | * the group elements | |
97 | * | |
98 | * @var array | |
99 | * @access public | |
100 | */ | |
101 | public $_group; | |
102 | public $_groupElement; | |
103 | public $_groupIterator; | |
104 | ||
105 | /** | |
106 | * the tag elements | |
107 | * | |
108 | * @var array | |
109 | * @access protected | |
110 | */ | |
111 | public $_tag; | |
112 | public $_tagElement; | |
113 | ||
6a488035 TO |
114 | /** |
115 | * The params used for search | |
116 | * | |
117 | * @var array | |
118 | * @access protected | |
119 | */ | |
120 | protected $_params; | |
121 | ||
122 | /** | |
123 | * The return properties used for search | |
124 | * | |
125 | * @var array | |
126 | * @access protected | |
127 | */ | |
128 | protected $_returnProperties; | |
129 | ||
130 | /** | |
131 | * The sort by character | |
132 | * | |
133 | * @var string | |
134 | * @access protected | |
135 | */ | |
136 | protected $_sortByCharacter; | |
137 | ||
138 | /** | |
139 | * The profile group id used for display | |
140 | * | |
141 | * @var integer | |
142 | * @access protected | |
143 | */ | |
144 | protected $_ufGroupID; | |
145 | ||
146 | /* | |
147 | * csv - common search values | |
148 | * | |
149 | * @var array | |
150 | * @access protected | |
151 | * @static | |
152 | */ | |
153 | ||
154 | static $csv = array('contact_type', 'group', 'tag'); | |
155 | ||
156 | /** | |
157 | * @var string how to display the results. Should we display as | |
158 | * contributons, members, cases etc | |
159 | */ | |
160 | protected $_componentMode; | |
161 | ||
162 | /** | |
163 | * @var string what operator should we use, AND or OR | |
164 | */ | |
165 | protected $_operator; | |
166 | ||
167 | protected $_modeValue; | |
168 | ||
6a488035 TO |
169 | /** |
170 | * name of the selector to use | |
171 | */ | |
172 | static $_selectorName = 'CRM_Contact_Selector'; | |
173 | protected $_customSearchID = NULL; | |
174 | protected $_customSearchClass = NULL; | |
175 | ||
176 | protected $_openedPanes = array(); | |
177 | ||
178 | /** | |
179 | * define the set of valid contexts that the search form operates on | |
180 | * | |
181 | * @return array the valid context set and the titles | |
182 | * @access protected | |
183 | * @static | |
184 | */ | |
185 | static function &validContext() { | |
186 | if (!(self::$_validContext)) { | |
187 | self::$_validContext = array( | |
188 | 'smog' => 'Show members of group', | |
189 | 'amtg' => 'Add members to group', | |
190 | 'basic' => 'Basic Search', | |
191 | 'search' => 'Search', | |
192 | 'builder' => 'Search Builder', | |
193 | 'advanced' => 'Advanced Search', | |
194 | 'custom' => 'Custom Search', | |
195 | ); | |
196 | } | |
197 | return self::$_validContext; | |
198 | } | |
199 | ||
86538308 EM |
200 | /** |
201 | * @param $context | |
202 | * | |
203 | * @return bool | |
204 | */ | |
6a488035 TO |
205 | static function isSearchContext($context) { |
206 | $searchContext = CRM_Utils_Array::value($context, self::validContext()); | |
207 | return $searchContext ? TRUE : FALSE; | |
208 | } | |
209 | ||
210 | static function setModeValues() { | |
211 | if (!self::$_modeValues) { | |
212 | self::$_modeValues = array( | |
213 | 1 => array( | |
214 | 'selectorName' => self::$_selectorName, | |
215 | 'selectorLabel' => ts('Contacts'), | |
216 | 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl', | |
217 | 'taskContext' => NULL, | |
218 | 'resultFile' => 'CRM/Contact/Form/Selector.tpl', | |
219 | 'resultContext' => NULL, | |
220 | 'taskClassName' => 'CRM_Contact_Task', | |
221 | ), | |
222 | 2 => array( | |
223 | 'selectorName' => 'CRM_Contribute_Selector_Search', | |
224 | 'selectorLabel' => ts('Contributions'), | |
225 | 'taskFile' => 'CRM/common/searchResultTasks.tpl', | |
226 | 'taskContext' => 'Contribution', | |
227 | 'resultFile' => 'CRM/Contribute/Form/Selector.tpl', | |
228 | 'resultContext' => 'Search', | |
229 | 'taskClassName' => 'CRM_Contribute_Task', | |
230 | ), | |
231 | 3 => array( | |
232 | 'selectorName' => 'CRM_Event_Selector_Search', | |
233 | 'selectorLabel' => ts('Event Participants'), | |
234 | 'taskFile' => 'CRM/common/searchResultTasks.tpl', | |
235 | 'taskContext' => NULL, | |
236 | 'resultFile' => 'CRM/Event/Form/Selector.tpl', | |
237 | 'resultContext' => 'Search', | |
238 | 'taskClassName' => 'CRM_Event_Task', | |
239 | ), | |
240 | 4 => array( | |
241 | 'selectorName' => 'CRM_Activity_Selector_Search', | |
242 | 'selectorLabel' => ts('Activities'), | |
243 | 'taskFile' => 'CRM/common/searchResultTasks.tpl', | |
244 | 'taskContext' => NULL, | |
245 | 'resultFile' => 'CRM/Activity/Form/Selector.tpl', | |
246 | 'resultContext' => 'Search', | |
247 | 'taskClassName' => 'CRM_Activity_Task', | |
248 | ), | |
249 | 5 => array( | |
250 | 'selectorName' => 'CRM_Member_Selector_Search', | |
251 | 'selectorLabel' => ts('Memberships'), | |
252 | 'taskFile' => "CRM/common/searchResultTasks.tpl", | |
253 | 'taskContext' => NULL, | |
254 | 'resultFile' => 'CRM/Member/Form/Selector.tpl', | |
255 | 'resultContext' => 'Search', | |
256 | 'taskClassName' => 'CRM_Member_Task', | |
257 | ), | |
258 | 6 => array( | |
259 | 'selectorName' => 'CRM_Case_Selector_Search', | |
260 | 'selectorLabel' => ts('Cases'), | |
261 | 'taskFile' => "CRM/common/searchResultTasks.tpl", | |
262 | 'taskContext' => NULL, | |
263 | 'resultFile' => 'CRM/Case/Form/Selector.tpl', | |
264 | 'resultContext' => 'Search', | |
265 | 'taskClassName' => 'CRM_Case_Task', | |
266 | ), | |
267 | 7 => array( | |
268 | 'selectorName' => self::$_selectorName, | |
269 | 'selectorLabel' => ts('Related Contacts'), | |
270 | 'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl', | |
271 | 'taskContext' => NULL, | |
272 | 'resultFile' => 'CRM/Contact/Form/Selector.tpl', | |
273 | 'resultContext' => NULL, | |
274 | 'taskClassName' => 'CRM_Contact_Task', | |
275 | ), | |
2cc569f2 PJ |
276 | 8 => array( |
277 | 'selectorName' => 'CRM_Mailing_Selector_Search', | |
278 | 'selectorLabel' => ts('Mailings'), | |
279 | 'taskFile' => "CRM/common/searchResultTasks.tpl", | |
280 | 'taskContext' => NULL, | |
281 | 'resultFile' => 'CRM/Mailing/Form/Selector.tpl', | |
282 | 'resultContext' => 'Search', | |
283 | 'taskClassName' => 'CRM_Mailing_Task', | |
284 | ), | |
6a488035 TO |
285 | ); |
286 | } | |
287 | } | |
288 | ||
86538308 EM |
289 | /** |
290 | * @param int $mode | |
291 | * | |
292 | * @return mixed | |
293 | */ | |
6a488035 TO |
294 | static function getModeValue($mode = 1) { |
295 | self::setModeValues(); | |
296 | ||
297 | if (!array_key_exists($mode, self::$_modeValues)) { | |
298 | $mode = 1; | |
299 | } | |
300 | ||
301 | return self::$_modeValues[$mode]; | |
302 | } | |
303 | ||
86538308 EM |
304 | /** |
305 | * @return array | |
306 | */ | |
6a488035 TO |
307 | static function getModeSelect() { |
308 | self::setModeValues(); | |
309 | ||
310 | $select = array(); | |
311 | foreach (self::$_modeValues as $id => & $value) { | |
312 | $select[$id] = $value['selectorLabel']; | |
313 | } | |
314 | ||
315 | // unset contributions or participants if user does not have | |
316 | // permission on them | |
317 | if (!CRM_Core_Permission::access('CiviContribute')) { | |
318 | unset($select['2']); | |
319 | } | |
320 | ||
321 | if (!CRM_Core_Permission::access('CiviEvent')) { | |
322 | unset($select['3']); | |
323 | } | |
324 | ||
325 | if (!CRM_Core_Permission::check('view all activities')) { | |
326 | unset($select['4']); | |
327 | } | |
328 | return $select; | |
329 | } | |
330 | ||
331 | /** | |
332 | * Build the common elements between the search/advanced form | |
333 | * | |
334 | * @access public | |
335 | * | |
336 | * @return void | |
337 | */ | |
338 | function buildQuickForm() { | |
3efb5b86 | 339 | parent::buildQuickForm(); |
6e8d1c11 | 340 | $this->setAttribute('class', 'crm-search-form crm-ajax-selection-form'); |
8e4ec1f5 | 341 | CRM_Core_Resources::singleton() |
8e4ec1f5 CW |
342 | // jsTree is needed for tags popup |
343 | ->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE) | |
344 | ->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header'); | |
6a488035 TO |
345 | $permission = CRM_Core_Permission::getPermission(); |
346 | // some tasks.. what do we want to do with the selected contacts ? | |
34197a55 | 347 | $tasks = array(); |
6a488035 TO |
348 | if ($this->_componentMode == 1 || $this->_componentMode == 7) { |
349 | $tasks += CRM_Contact_Task::permissionedTaskTitles($permission, | |
350 | CRM_Utils_Array::value('deleted_contacts', $this->_formValues) | |
351 | ); | |
352 | } | |
353 | else { | |
354 | $className = $this->_modeValue['taskClassName']; | |
355 | $tasks += $className::permissionedTaskTitles($permission, false); | |
356 | } | |
357 | ||
358 | if (isset($this->_ssID)) { | |
359 | if ($permission == CRM_Core_Permission::EDIT) { | |
360 | $tasks = $tasks + CRM_Contact_Task::optionalTaskTitle(); | |
361 | } | |
362 | ||
363 | $search_custom_id = | |
364 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'search_custom_id'); | |
365 | ||
366 | $savedSearchValues = array( | |
367 | 'id' => $this->_ssID, | |
368 | 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'), | |
369 | 'search_custom_id' => $search_custom_id, | |
370 | ); | |
371 | $this->assign_by_ref('savedSearch', $savedSearchValues); | |
372 | $this->assign('ssID', $this->_ssID); | |
373 | } | |
374 | ||
375 | if ($this->_context === 'smog') { | |
376 | // CRM-11788, we might want to do this for all of search where force=1 | |
377 | $formQFKey = CRM_Utils_Array::value('qfKey', $this->_formValues); | |
378 | $getQFKey = CRM_Utils_Array::value('qfKey', $_GET); | |
379 | $postQFKey = CRM_Utils_Array::value('qfKey', $_POST); | |
380 | if ($formQFKey && empty($getQFKey) && empty($postQFKey)) { | |
381 | $url = CRM_Utils_System::makeURL('qfKey') . $formQFKey; | |
382 | CRM_Utils_System::redirect($url); | |
383 | } | |
384 | ||
385 | if (!empty($this->_groupID)) { | |
386 | $permissionForGroup = FALSE; | |
387 | ||
388 | // check if user has permission to edit members of this group | |
389 | $permission = CRM_Contact_BAO_Group::checkPermission($this->_groupID); | |
390 | if ($permission && in_array(CRM_Core_Permission::EDIT, $permission)) { | |
391 | $permissionForGroup = TRUE; | |
392 | } | |
393 | ||
394 | // check if _groupID exists, it might not if | |
395 | // we are displaying a hidden group | |
396 | if (!isset($this->_group[$this->_groupID])) { | |
397 | $permissionForGroup = FALSE; | |
398 | $this->_group[$this->_groupID] = | |
399 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'title'); | |
400 | } | |
401 | ||
402 | $this->assign('permissionedForGroup', $permissionForGroup); | |
403 | ||
404 | // set the group title | |
405 | $groupValues = array('id' => $this->_groupID, 'title' => $this->_group[$this->_groupID]); | |
406 | $this->assign_by_ref('group', $groupValues); | |
407 | ||
408 | // also set ssID if this is a saved search | |
409 | $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id'); | |
410 | $this->assign('ssID', $ssID); | |
411 | ||
412 | //get the saved search mapping id | |
413 | if ($ssID) { | |
414 | $this->_ssID = $ssID; | |
415 | $ssMappingId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $ssID, 'mapping_id'); | |
416 | $this->assign('ssMappingID', $ssMappingId); | |
417 | } | |
418 | ||
419 | // Set dynamic page title for 'Show Members of Group' | |
420 | CRM_Utils_System::setTitle(ts('Contacts in Group: %1', array(1 => $this->_group[$this->_groupID]))); | |
421 | } | |
422 | ||
423 | $group_contact_status = array(); | |
424 | foreach (CRM_Core_SelectValues::groupContactStatus() as $k => $v) { | |
425 | if (!empty($k)) { | |
426 | $group_contact_status[] = $this->createElement('checkbox', $k, NULL, $v); | |
427 | } | |
428 | } | |
429 | $this->addGroup($group_contact_status, | |
430 | 'group_contact_status', ts('Group Status') | |
431 | ); | |
432 | ||
433 | $this->assign('permissionedForGroup', FALSE); | |
434 | } | |
435 | ||
436 | // add the go button for the action form, note it is of type 'next' rather than of type 'submit' | |
437 | if ($this->_context === 'amtg') { | |
438 | // check if _groupID exists, it might not if | |
439 | // we are displaying a hidden group | |
440 | if (!isset($this->_group[$this->_amtgID])) { | |
441 | $this->assign('permissionedForGroup', FALSE); | |
442 | $this->_group[$this->_amtgID] = | |
443 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_amtgID, 'title'); | |
444 | } | |
445 | ||
446 | // Set dynamic page title for 'Add Members Group' | |
447 | CRM_Utils_System::setTitle(ts('Add to Group: %1', array(1 => $this->_group[$this->_amtgID]))); | |
448 | // also set the group title and freeze the action task with Add Members to Group | |
449 | $groupValues = array('id' => $this->_amtgID, 'title' => $this->_group[$this->_amtgID]); | |
450 | $this->assign_by_ref('group', $groupValues); | |
451 | $this->add('submit', $this->_actionButtonName, ts('Add Contacts to %1', array(1 => $this->_group[$this->_amtgID])), | |
452 | array( | |
453 | 'class' => 'form-submit', | |
6a488035 TO |
454 | ) |
455 | ); | |
456 | $this->add('hidden', 'task', CRM_Contact_Task::GROUP_CONTACTS); | |
457 | } | |
458 | else { | |
34197a55 | 459 | $this->addTaskMenu($tasks); |
6a488035 TO |
460 | } |
461 | ||
6a488035 TO |
462 | if ($qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues)) { |
463 | $qfKeyParam = "civicrm search {$qfKeyParam}"; | |
464 | $selectedContactIdsArr = CRM_Core_BAO_PrevNextCache::getSelection($qfKeyParam); | |
465 | $selectedContactIds = array_keys($selectedContactIdsArr[$qfKeyParam]); | |
466 | } | |
467 | ||
468 | $this->assign_by_ref('selectedContactIds', $selectedContactIds); | |
469 | ||
6a488035 TO |
470 | $rows = $this->get('rows'); |
471 | ||
472 | if (is_array($rows)) { | |
6e8d1c11 | 473 | $this->addRowSelectors($rows); |
6a488035 TO |
474 | } |
475 | ||
6a488035 TO |
476 | } |
477 | ||
478 | /** | |
479 | * processing needed for buildForm and later | |
480 | * | |
481 | * @return void | |
482 | * @access public | |
483 | */ | |
484 | function preProcess() { | |
485 | // set the various class variables | |
486 | ||
487 | $this->_group = CRM_Core_PseudoConstant::group(); | |
488 | ||
489 | $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator(); | |
490 | $this->_tag = CRM_Core_BAO_Tag::getTags(); | |
491 | $this->_done = FALSE; | |
492 | ||
493 | /* | |
deb50ba3 CW |
494 | * we allow the controller to set force/reset externally, useful when we are being |
495 | * driven by the wizard framework | |
496 | */ | |
6a488035 TO |
497 | |
498 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', | |
499 | CRM_Core_DAO::$_nullObject | |
500 | ); | |
501 | ||
502 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject); | |
503 | $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this); | |
504 | $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this); | |
505 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); | |
506 | $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this); | |
507 | $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
508 | $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST); | |
509 | $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND'); | |
510 | ||
511 | /** | |
512 | * set the button names | |
513 | */ | |
514 | $this->_searchButtonName = $this->getButtonName('refresh'); | |
6a488035 TO |
515 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
516 | ||
6a488035 TO |
517 | $this->assign('actionButtonName', $this->_actionButtonName); |
518 | ||
519 | // reset from session, CRM-3526 | |
520 | $session = CRM_Core_Session::singleton(); | |
521 | if ($this->_force && $session->get('selectedSearchContactIds')) { | |
522 | $session->resetScope('selectedSearchContactIds'); | |
523 | } | |
524 | ||
525 | // if we dont get this from the url, use default if one exsts | |
526 | $config = CRM_Core_Config::singleton(); | |
527 | if ($this->_ufGroupID == NULL && | |
528 | $config->defaultSearchProfileID != NULL | |
529 | ) { | |
530 | $this->_ufGroupID = $config->defaultSearchProfileID; | |
531 | } | |
532 | ||
533 | // assign context to drive the template display, make sure context is valid | |
534 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); | |
535 | if (!CRM_Utils_Array::value($this->_context, self::validContext())) { | |
536 | $this->_context = 'search'; | |
537 | } | |
538 | $this->set('context', $this->_context); | |
539 | $this->assign('context', $this->_context); | |
540 | ||
541 | $this->_modeValue = self::getModeValue($this->_componentMode); | |
542 | $this->assign($this->_modeValue); | |
543 | ||
544 | $this->set('selectorName', self::$_selectorName); | |
545 | ||
546 | // get user submitted values | |
547 | // get it from controller only if form has been submitted, else preProcess has set this | |
548 | // $this->controller->isModal( ) returns true if page is | |
549 | // valid, i.e all the validations are true | |
550 | ||
551 | if (!empty($_POST) && !$this->controller->isModal()) { | |
552 | $this->_formValues = $this->controller->exportValues($this->_name); | |
553 | ||
554 | $this->normalizeFormValues(); | |
555 | $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
556 | $this->_returnProperties = &$this->returnProperties(); | |
557 | ||
558 | // also get the uf group id directly from the post value | |
559 | $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID); | |
560 | $this->_formValues['uf_group_id'] = $this->_ufGroupID; | |
561 | $this->set('id', $this->_ufGroupID); | |
562 | ||
563 | // also get the object mode directly from the post value | |
564 | $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode); | |
565 | ||
566 | // also get the operator from the post value if set | |
567 | $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator); | |
568 | $this->_formValues['operator'] = $this->_operator; | |
569 | $this->set('operator', $this->_operator); | |
570 | } | |
571 | else { | |
572 | $this->_formValues = $this->get('formValues'); | |
573 | $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
574 | $this->_returnProperties = &$this->returnProperties(); | |
575 | if (!empty($this->_ufGroupID)) { | |
576 | $this->set('id', $this->_ufGroupID); | |
577 | } | |
578 | } | |
579 | ||
580 | if (empty($this->_formValues)) { | |
581 | //check if group is a smart group (fix for CRM-1255) | |
582 | if ($this->_groupID) { | |
583 | if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) { | |
584 | $this->_ssID = $ssId; | |
585 | } | |
586 | } | |
587 | ||
588 | // fix for CRM-1907 | |
589 | if (isset($this->_ssID) && $this->_context != 'smog') { | |
590 | // we only retrieve the saved search values if out current values are null | |
591 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); | |
592 | ||
593 | //fix for CRM-1505 | |
594 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) { | |
595 | $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID); | |
596 | } | |
597 | else { | |
598 | $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
599 | } | |
600 | $this->_returnProperties = &$this->returnProperties(); | |
601 | } | |
602 | else { | |
603 | if (isset($this->_ufGroupID)) { | |
604 | // also set the uf group id if not already present | |
605 | $this->_formValues['uf_group_id'] = $this->_ufGroupID; | |
606 | } | |
607 | if (isset($this->_componentMode)) { | |
608 | $this->_formValues['component_mode'] = $this->_componentMode; | |
609 | } | |
610 | if (isset($this->_operator)) { | |
611 | $this->_formValues['operator'] = $this->_operator; | |
612 | } | |
613 | ||
614 | // FIXME: we should generalise in a way that components could inject url-filters | |
615 | // just like they build their own form elements | |
616 | foreach (array( | |
617 | 'mailing_id', 'mailing_delivery_status', 'mailing_open_status', | |
618 | 'mailing_click_status', 'mailing_reply_status', 'mailing_optout', | |
619 | 'mailing_forward', 'mailing_unsubscribe', 'mailing_date_low', | |
620 | 'mailing_date_high', | |
621 | ) as $mailingFilter) { | |
622 | $type = 'String'; | |
623 | if ($mailingFilter == 'mailing_id' && | |
624 | $filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this) | |
625 | ) { | |
626 | $this->_formValues[$mailingFilter] = array($filterVal); | |
627 | } | |
628 | elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) { | |
629 | $this->_formValues[$mailingFilter] = $filterVal; | |
630 | } | |
631 | if ($filterVal) { | |
632 | $this->_openedPanes['Mailings'] = 1; | |
633 | $this->_formValues['hidden_CiviMail'] = 1; | |
634 | } | |
635 | } | |
636 | } | |
637 | } | |
638 | $this->assign('id', | |
639 | CRM_Utils_Array::value('uf_group_id', $this->_formValues) | |
640 | ); | |
641 | $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND'); | |
642 | $this->set('queryOperator', $operator); | |
643 | if ($operator == 'OR') { | |
644 | $this->assign('operator', ts('OR')); | |
645 | } | |
646 | else { | |
647 | $this->assign('operator', ts('AND')); | |
648 | } | |
649 | ||
650 | // show the context menu only when we’re not searching for deleted contacts; CRM-5673 | |
a7488080 | 651 | if (empty($this->_formValues['deleted_contacts'])) { |
6a488035 TO |
652 | $menuItems = CRM_Contact_BAO_Contact::contextMenu(); |
653 | $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array()); | |
654 | $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array()); | |
655 | $this->assign('contextMenu', $primaryActions + $this->_contextMenu); | |
656 | } | |
657 | ||
658 | if (!isset($this->_componentMode)) { | |
659 | $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS; | |
660 | } | |
661 | $modeValues = self::getModeValue($this->_componentMode); | |
662 | ||
663 | self::$_selectorName = $this->_modeValue['selectorName']; | |
664 | ||
665 | $setDynamic = FALSE; | |
666 | if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) { | |
667 | $selector = new self::$_selectorName( | |
668 | $this->_customSearchClass, | |
669 | $this->_formValues, | |
670 | $this->_params, | |
671 | $this->_returnProperties, | |
672 | $this->_action, | |
673 | false, true, | |
674 | $this->_context, | |
675 | $this->_contextMenu | |
676 | ); | |
677 | $setDynamic = TRUE; | |
678 | } | |
679 | else { | |
680 | $selector = new self::$_selectorName( | |
681 | $this->_params, | |
682 | $this->_action, | |
683 | null, false, null, | |
684 | "search", "advanced" | |
685 | ); | |
686 | } | |
687 | ||
688 | $selector->setKey($this->controller->_key); | |
689 | ||
690 | $controller = new CRM_Contact_Selector_Controller($selector, | |
691 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
692 | $this->get(CRM_Utils_Sort::SORT_ID), | |
693 | CRM_Core_Action::VIEW, | |
694 | $this, | |
695 | CRM_Core_Selector_Controller::TRANSFER | |
696 | ); | |
697 | $controller->setEmbedded(TRUE); | |
698 | $controller->setDynamicAction($setDynamic); | |
699 | ||
700 | if ($this->_force) { | |
701 | ||
702 | $this->postProcess(); | |
703 | ||
704 | /* | |
deb50ba3 CW |
705 | * Note that we repeat this, since the search creates and stores |
706 | * values that potentially change the controller behavior. i.e. things | |
707 | * like totalCount etc | |
708 | */ | |
6a488035 TO |
709 | $sortID = NULL; |
710 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
711 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
712 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
713 | ); | |
714 | } | |
715 | $controller = new CRM_Contact_Selector_Controller($selector, | |
716 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
717 | $sortID, | |
718 | CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER | |
719 | ); | |
720 | $controller->setEmbedded(TRUE); | |
721 | $controller->setDynamicAction($setDynamic); | |
722 | } | |
723 | ||
724 | $controller->moveFromSessionToTemplate(); | |
725 | } | |
726 | ||
86538308 EM |
727 | /** |
728 | * @return array | |
729 | */ | |
6a488035 TO |
730 | function &getFormValues() { |
731 | return $this->_formValues; | |
732 | } | |
733 | ||
734 | /** | |
735 | * Common post processing | |
736 | * | |
737 | * @return void | |
738 | * @access public | |
739 | */ | |
740 | function postProcess() { | |
741 | /* | |
742 | * sometime we do a postProcess early on, so we dont need to repeat it | |
743 | * this will most likely introduce some more bugs :( | |
744 | */ | |
745 | ||
746 | if ($this->_done) { | |
747 | return; | |
748 | } | |
749 | $this->_done = TRUE; | |
750 | ||
751 | //for prev/next pagination | |
752 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); | |
753 | ||
754 | if (array_key_exists($this->_searchButtonName, $_POST) || | |
755 | ($this->_force && !$crmPID)) { | |
756 | //reset the cache table for new search | |
757 | $cacheKey = "civicrm search {$this->controller->_key}"; | |
758 | CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey); | |
759 | } | |
760 | ||
761 | //get the button name | |
762 | $buttonName = $this->controller->getButtonName(); | |
763 | ||
8cc574cf | 764 | if (isset($this->_ufGroupID) && empty($this->_formValues['uf_group_id'])) { |
6a488035 TO |
765 | $this->_formValues['uf_group_id'] = $this->_ufGroupID; |
766 | } | |
767 | ||
8cc574cf | 768 | if (isset($this->_componentMode) && empty($this->_formValues['component_mode'])) { |
6a488035 TO |
769 | $this->_formValues['component_mode'] = $this->_componentMode; |
770 | } | |
771 | ||
8cc574cf | 772 | if (isset($this->_operator) && empty($this->_formValues['operator'])) { |
6a488035 TO |
773 | $this->_formValues['operator'] = $this->_operator; |
774 | } | |
775 | ||
a7488080 | 776 | if (empty($this->_formValues['qfKey'])) { |
6a488035 TO |
777 | $this->_formValues['qfKey'] = $this->controller->_key; |
778 | } | |
779 | ||
780 | if (!CRM_Core_Permission::check('access deleted contacts')) { | |
781 | unset($this->_formValues['deleted_contacts']); | |
782 | } | |
783 | ||
784 | $this->set('type', $this->_action); | |
785 | $this->set('formValues', $this->_formValues); | |
786 | $this->set('queryParams', $this->_params); | |
787 | $this->set('returnProperties', $this->_returnProperties); | |
788 | ||
e341bbee | 789 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
790 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
791 | // hack, make sure we reset the task values | |
792 | $stateMachine = $this->controller->getStateMachine(); | |
793 | $formName = $stateMachine->getTaskFormName(); | |
794 | $this->controller->resetPage($formName); | |
795 | return; | |
796 | } | |
797 | else { | |
798 | $output = CRM_Core_Selector_Controller::SESSION; | |
799 | ||
800 | // create the selector, controller and run - store results in session | |
801 | $searchChildGroups = TRUE; | |
802 | if ($this->get('isAdvanced')) { | |
803 | $searchChildGroups = FALSE; | |
804 | } | |
805 | ||
806 | $setDynamic = FALSE; | |
807 | ||
808 | if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) { | |
809 | $selector = new self::$_selectorName ( | |
810 | $this->_customSearchClass, | |
811 | $this->_formValues, | |
812 | $this->_params, | |
813 | $this->_returnProperties, | |
814 | $this->_action, | |
815 | false, | |
816 | $searchChildGroups, | |
817 | $this->_context, | |
818 | $this->_contextMenu | |
819 | ); | |
820 | $setDynamic = TRUE; | |
821 | } | |
822 | else { | |
823 | $selector = new self::$_selectorName ( | |
824 | $this->_params, | |
825 | $this->_action, | |
826 | null, | |
827 | false, | |
828 | null, | |
829 | "search", | |
830 | "advanced" | |
831 | ); | |
832 | } | |
833 | ||
834 | $selector->setKey($this->controller->_key); | |
835 | ||
836 | // added the sorting character to the form array | |
6a488035 TO |
837 | $config = CRM_Core_Config::singleton(); |
838 | // do this only for contact search | |
839 | if ($setDynamic && $config->includeAlphabeticalPager) { | |
e166ff79 CW |
840 | // Don't recompute if we are just paging/sorting |
841 | if ($this->_reset || (empty($_GET['crmPID']) && empty($_GET['crmSID']) && !$this->_sortByCharacter)) { | |
6a488035 TO |
842 | $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($selector, $this->_sortByCharacter); |
843 | $this->set('AToZBar', $aToZBar); | |
844 | } | |
845 | } | |
846 | ||
847 | $sortID = NULL; | |
848 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
849 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
850 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
851 | ); | |
852 | } | |
853 | $controller = new CRM_Contact_Selector_Controller($selector, | |
854 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
855 | $sortID, | |
856 | CRM_Core_Action::VIEW, | |
857 | $this, | |
858 | $output | |
859 | ); | |
860 | $controller->setEmbedded(TRUE); | |
861 | $controller->setDynamicAction($setDynamic); | |
862 | $controller->run(); | |
863 | } | |
864 | } | |
865 | ||
86538308 EM |
866 | /** |
867 | * @return null | |
868 | */ | |
6a488035 TO |
869 | function &returnProperties() { |
870 | return CRM_Core_DAO::$_nullObject; | |
871 | } | |
872 | ||
873 | /** | |
874 | * Return a descriptive name for the page, used in wizard header | |
875 | * | |
876 | * @return string | |
877 | * @access public | |
878 | */ | |
879 | function getTitle() { | |
880 | return ts('Search'); | |
881 | } | |
882 | } | |
883 |