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