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