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