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