| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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 | * This file is for civievent search |
| 42 | */ |
| 43 | class CRM_Event_Form_Search extends CRM_Core_Form_Search { |
| 44 | |
| 45 | /** |
| 46 | * the params that are sent to the query |
| 47 | * |
| 48 | * @var array |
| 49 | * @access protected |
| 50 | */ |
| 51 | protected $_queryParams; |
| 52 | |
| 53 | /** |
| 54 | * are we restricting ourselves to a single contact |
| 55 | * |
| 56 | * @access protected |
| 57 | * @var boolean |
| 58 | */ |
| 59 | protected $_single = FALSE; |
| 60 | |
| 61 | /** |
| 62 | * are we restricting ourselves to a single contact |
| 63 | * |
| 64 | * @access protected |
| 65 | * @var boolean |
| 66 | */ |
| 67 | protected $_limit = NULL; |
| 68 | |
| 69 | /** |
| 70 | * prefix for the controller |
| 71 | * |
| 72 | */ |
| 73 | protected $_prefix = "event_"; |
| 74 | |
| 75 | protected $_defaults; |
| 76 | |
| 77 | /** |
| 78 | * the saved search ID retrieved from the GET vars |
| 79 | * |
| 80 | * @var int |
| 81 | * @access protected |
| 82 | */ |
| 83 | protected $_ssID; |
| 84 | |
| 85 | /** |
| 86 | * processing needed for buildForm and later |
| 87 | * |
| 88 | * @return void |
| 89 | * @access public |
| 90 | */ |
| 91 | function preProcess() { |
| 92 | $this->set('searchFormName', 'Search'); |
| 93 | |
| 94 | /** |
| 95 | * set the button names |
| 96 | */ |
| 97 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 98 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 99 | |
| 100 | $this->_done = FALSE; |
| 101 | $this->defaults = array(); |
| 102 | |
| 103 | /* |
| 104 | * we allow the controller to set force/reset externally, useful when we are being |
| 105 | * driven by the wizard framework |
| 106 | */ |
| 107 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 108 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
| 109 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
| 110 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
| 111 | $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); |
| 112 | $this->assign("context", $this->_context); |
| 113 | |
| 114 | // get user submitted values |
| 115 | // get it from controller only if form has been submitted, else preProcess has set this |
| 116 | if (!empty($_POST) && !$this->controller->isModal()) { |
| 117 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 118 | } |
| 119 | else { |
| 120 | $this->_formValues = $this->get('formValues'); |
| 121 | } |
| 122 | |
| 123 | if (empty($this->_formValues)) { |
| 124 | if (isset($this->_ssID)) { |
| 125 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if ($this->_force) { |
| 130 | $this->postProcess(); |
| 131 | $this->set('force', 0); |
| 132 | } |
| 133 | |
| 134 | $sortID = NULL; |
| 135 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 136 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 137 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 142 | $selector = new CRM_Event_Selector_Search($this->_queryParams, |
| 143 | $this->_action, |
| 144 | NULL, |
| 145 | $this->_single, |
| 146 | $this->_limit, |
| 147 | $this->_context |
| 148 | ); |
| 149 | $prefix = NULL; |
| 150 | if ($this->_context == 'user') { |
| 151 | $prefix = $this->_prefix; |
| 152 | } |
| 153 | |
| 154 | $this->assign("{$prefix}limit", $this->_limit); |
| 155 | $this->assign("{$prefix}single", $this->_single); |
| 156 | |
| 157 | $controller = new CRM_Core_Selector_Controller($selector, |
| 158 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 159 | $sortID, |
| 160 | CRM_Core_Action::VIEW, |
| 161 | $this, |
| 162 | CRM_Core_Selector_Controller::TRANSFER, |
| 163 | $prefix |
| 164 | ); |
| 165 | $controller->setEmbedded(TRUE); |
| 166 | $controller->moveFromSessionToTemplate(); |
| 167 | |
| 168 | $this->assign('summary', $this->get('summary')); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Build the form |
| 173 | * |
| 174 | * @access public |
| 175 | * |
| 176 | * @return void |
| 177 | */ |
| 178 | function buildQuickForm() { |
| 179 | parent::buildQuickForm(); |
| 180 | $this->addElement('text', 'sort_name', ts('Participant Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
| 181 | |
| 182 | CRM_Event_BAO_Query::buildSearchForm($this); |
| 183 | |
| 184 | $rows = $this->get('rows'); |
| 185 | if (is_array($rows)) { |
| 186 | $lineItems = $eventIds = array(); |
| 187 | if (!$this->_single) { |
| 188 | $this->addRowSelectors($rows); |
| 189 | } |
| 190 | foreach ($rows as $row) { |
| 191 | $eventIds[$row['event_id']] = $row['event_id']; |
| 192 | if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) { |
| 193 | // add line item details if applicable |
| 194 | $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | //get actual count only when we are dealing w/ single event. |
| 199 | $participantCount = 0; |
| 200 | if (count($eventIds) == 1) { |
| 201 | //convert form values to clause. |
| 202 | $seatClause = array(); |
| 203 | // Filter on is_test if specified in search form |
| 204 | if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0' ) { |
| 205 | $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )"; |
| 206 | } |
| 207 | if (!empty($this->_formValues['participant_status_id'])) { |
| 208 | $statuses = array_keys($this->_formValues['participant_status_id']); |
| 209 | $seatClause[] = '( participant.status_id IN ( ' . implode(' , ', $statuses) . ' ) )'; |
| 210 | } |
| 211 | if (!empty($this->_formValues['participant_role_id'])) { |
| 212 | $roles = array_keys($this->_formValues['participant_role_id']); |
| 213 | $seatClause[] = '( participant.role_id IN ( ' . implode(' , ', $roles) . ' ) )'; |
| 214 | } |
| 215 | // CRM-15379 |
| 216 | if (!empty($this->_formValues['participant_fee_id'])) { |
| 217 | $participant_fee_id = $this->_formValues['participant_fee_id']; |
| 218 | $feeLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $participant_fee_id, 'label'); |
| 219 | $feeLabel = CRM_Core_DAO::escapeString(trim($feeLabel)); |
| 220 | $seatClause[] = "( participant.fee_level LIKE '%$feeLabel%' )"; |
| 221 | } |
| 222 | $clause = NULL; |
| 223 | if (!empty($seatClause)) { |
| 224 | $clause = implode(' AND ', $seatClause); |
| 225 | } |
| 226 | |
| 227 | $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $clause); |
| 228 | } |
| 229 | $this->assign('participantCount', $participantCount); |
| 230 | $this->assign('lineItems', $lineItems); |
| 231 | |
| 232 | $permission = CRM_Core_Permission::getPermission(); |
| 233 | |
| 234 | $tasks = CRM_Event_Task::permissionedTaskTitles($permission); |
| 235 | if (isset($this->_ssID)) { |
| 236 | if ($permission == CRM_Core_Permission::EDIT) { |
| 237 | $tasks = $tasks + CRM_Event_Task::optionalTaskTitle(); |
| 238 | } |
| 239 | |
| 240 | $savedSearchValues = array( |
| 241 | 'id' => $this->_ssID, |
| 242 | 'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'), |
| 243 | ); |
| 244 | $this->assign_by_ref('savedSearch', $savedSearchValues); |
| 245 | $this->assign('ssID', $this->_ssID); |
| 246 | } |
| 247 | |
| 248 | $this->addTaskMenu($tasks); |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * The post processing of the form gets done here. |
| 255 | * |
| 256 | * Key things done during post processing are |
| 257 | * - check for reset or next request. if present, skip post procesing. |
| 258 | * - now check if user requested running a saved search, if so, then |
| 259 | * the form values associated with the saved search are used for searching. |
| 260 | * - if user has done a submit with new values the regular post submissing is |
| 261 | * done. |
| 262 | * The processing consists of using a Selector / Controller framework for getting the |
| 263 | * search results. |
| 264 | * |
| 265 | * @param |
| 266 | * |
| 267 | * @return void |
| 268 | * @access public |
| 269 | */ |
| 270 | function postProcess() { |
| 271 | if ($this->_done) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | $this->_done = TRUE; |
| 276 | |
| 277 | if (!empty($_POST)) { |
| 278 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 279 | } |
| 280 | |
| 281 | if (empty($this->_formValues)) { |
| 282 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 283 | } |
| 284 | |
| 285 | $this->fixFormValues(); |
| 286 | |
| 287 | if (isset($this->_ssID) && empty($_POST)) { |
| 288 | // if we are editing / running a saved search and the form has not been posted |
| 289 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 290 | } |
| 291 | |
| 292 | // We don't show test records in summaries or dashboards |
| 293 | if (empty($this->_formValues['participant_test']) && $this->_force) { |
| 294 | $this->_formValues["participant_test"] = 0; |
| 295 | } |
| 296 | |
| 297 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); |
| 298 | |
| 299 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 300 | |
| 301 | $this->set('formValues', $this->_formValues); |
| 302 | $this->set('queryParams', $this->_queryParams); |
| 303 | |
| 304 | $buttonName = $this->controller->getButtonName(); |
| 305 | if ($buttonName == $this->_actionButtonName) { |
| 306 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 307 | |
| 308 | // hack, make sure we reset the task values |
| 309 | $stateMachine = $this->controller->getStateMachine(); |
| 310 | $formName = $stateMachine->getTaskFormName(); |
| 311 | $this->controller->resetPage($formName); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | $sortID = NULL; |
| 316 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 317 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 318 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 323 | |
| 324 | $selector = new CRM_Event_Selector_Search($this->_queryParams, |
| 325 | $this->_action, |
| 326 | NULL, |
| 327 | $this->_single, |
| 328 | $this->_limit, |
| 329 | $this->_context |
| 330 | ); |
| 331 | |
| 332 | $selector->setKey($this->controller->_key); |
| 333 | |
| 334 | $prefix = NULL; |
| 335 | if ($this->_context == 'user') { |
| 336 | $prefix = $this->_prefix; |
| 337 | } |
| 338 | |
| 339 | $this->assign("{$prefix}limit", $this->_limit); |
| 340 | $this->assign("{$prefix}single", $this->_single); |
| 341 | |
| 342 | $controller = new CRM_Core_Selector_Controller($selector, |
| 343 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 344 | $sortID, |
| 345 | CRM_Core_Action::VIEW, |
| 346 | $this, |
| 347 | CRM_Core_Selector_Controller::SESSION, |
| 348 | $prefix |
| 349 | ); |
| 350 | $controller->setEmbedded(TRUE); |
| 351 | |
| 352 | $query = $selector->getQuery(); |
| 353 | if ($this->_context == 'user') { |
| 354 | $query->setSkipPermission(TRUE); |
| 355 | } |
| 356 | $controller->run(); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * This function is used to add the rules (mainly global rules) for form. |
| 361 | * All local rules are added near the element |
| 362 | * |
| 363 | * @return void |
| 364 | * @access public |
| 365 | * @see valid_date |
| 366 | */ |
| 367 | function addRules() {} |
| 368 | |
| 369 | /** |
| 370 | * Set the default form values |
| 371 | * |
| 372 | * @access protected |
| 373 | * |
| 374 | * @return array the default array reference |
| 375 | */ |
| 376 | function setDefaultValues() { |
| 377 | $defaults = array(); |
| 378 | $defaults = $this->_formValues; |
| 379 | return $defaults; |
| 380 | } |
| 381 | |
| 382 | function fixFormValues() { |
| 383 | // if this search has been forced |
| 384 | // then see if there are any get values, and if so over-ride the post values |
| 385 | // note that this means that GET over-rides POST :) |
| 386 | $event = CRM_Utils_Request::retrieve('event', 'Positive', |
| 387 | CRM_Core_DAO::$_nullObject |
| 388 | ); |
| 389 | if ($event) { |
| 390 | $this->_formValues['event_id'] = $event; |
| 391 | $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE); |
| 392 | } |
| 393 | |
| 394 | $status = CRM_Utils_Request::retrieve('status', 'String', |
| 395 | CRM_Core_DAO::$_nullObject |
| 396 | ); |
| 397 | |
| 398 | if (isset($status)) { |
| 399 | if ($status === 'true') { |
| 400 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1"); |
| 401 | } |
| 402 | elseif ($status === 'false') { |
| 403 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0"); |
| 404 | } |
| 405 | elseif (is_numeric($status)) { |
| 406 | $status = (int) $status; |
| 407 | $statusTypes = array($status => CRM_Event_PseudoConstant::participantStatus($status)); |
| 408 | } |
| 409 | $status = array(); |
| 410 | foreach ($statusTypes as $key => $value) { |
| 411 | $status[$key] = 1; |
| 412 | } |
| 413 | $this->_formValues['participant_status_id'] = $status; |
| 414 | } |
| 415 | |
| 416 | $role = CRM_Utils_Request::retrieve('role', 'String', |
| 417 | CRM_Core_DAO::$_nullObject |
| 418 | ); |
| 419 | |
| 420 | if (isset($role)) { |
| 421 | if ($role === 'true') { |
| 422 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1"); |
| 423 | } |
| 424 | elseif ($role === 'false') { |
| 425 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0"); |
| 426 | } |
| 427 | elseif (is_numeric($role)) { |
| 428 | $role = (int) $role; |
| 429 | $roleTypes = array($role => CRM_Event_PseudoConstant::participantRole($role)); |
| 430 | } |
| 431 | $role = array(); |
| 432 | foreach ($roleTypes as $key => $value) { |
| 433 | $role[$key] = 1; |
| 434 | } |
| 435 | $this->_formValues['participant_role_id'] = $role; |
| 436 | } |
| 437 | |
| 438 | $type = CRM_Utils_Request::retrieve('type', 'Positive', |
| 439 | CRM_Core_DAO::$_nullObject |
| 440 | ); |
| 441 | if ($type) { |
| 442 | $this->_formValues['event_type'] = $type; |
| 443 | } |
| 444 | |
| 445 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 446 | |
| 447 | if ($cid) { |
| 448 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 449 | if ($cid > 0) { |
| 450 | $this->_formValues['contact_id'] = $cid; |
| 451 | |
| 452 | // also assign individual mode to the template |
| 453 | $this->_single = TRUE; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @return null |
| 460 | */ |
| 461 | function getFormValues() { |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Return a descriptive name for the page, used in wizard header |
| 467 | * |
| 468 | * @return string |
| 469 | * @access public |
| 470 | */ |
| 471 | public function getTitle() { |
| 472 | return ts('Find Participants'); |
| 473 | } |
| 474 | } |
| 475 | |