Search.php 15 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
lobo's avatar
lobo committed
24
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
totten's avatar
totten committed
25
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34 35 36
 */

/**
 * This file is for civievent search
 */
37
class CRM_Event_Form_Search extends CRM_Core_Form_Search {
totten's avatar
totten committed
38 39

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
40
   * The params that are sent to the query.
totten's avatar
totten committed
41 42 43 44 45 46
   *
   * @var array
   */
  protected $_queryParams;

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
47
   * Are we restricting ourselves to a single contact.
totten's avatar
totten committed
48
   *
49
   * @var bool
totten's avatar
totten committed
50 51 52 53
   */
  protected $_single = FALSE;

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
54
   * Are we restricting ourselves to a single contact.
totten's avatar
totten committed
55
   *
56
   * @var bool
totten's avatar
totten committed
57 58 59 60
   */
  protected $_limit = NULL;

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
61
   * Prefix for the controller.
62
   * @var string
totten's avatar
totten committed
63 64 65 66
   */
  protected $_prefix = "event_";

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
67
   * The saved search ID retrieved from the GET vars.
totten's avatar
totten committed
68 69 70 71 72
   *
   * @var int
   */
  protected $_ssID;

eileen's avatar
eileen committed
73 74 75 76 77 78 79 80
  /**
   * Metadata of all fields to include on the form.
   *
   * @var array
   */
  protected $searchFieldMetadata = [];

  /**
81 82
   * Get the default entity for the form.
   *
eileen's avatar
eileen committed
83 84 85 86 87 88
   * @return string
   */
  public function getDefaultEntity() {
    return 'Participant';
  }

totten's avatar
totten committed
89
  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
90
   * Processing needed for buildForm and later.
totten's avatar
totten committed
91 92 93
   *
   * @return void
   */
94
  public function preProcess() {
totten's avatar
totten committed
95 96 97 98 99 100 101 102 103 104
    $this->set('searchFormName', 'Search');

    /**
     * set the button names
     */
    $this->_searchButtonName = $this->getButtonName('refresh');
    $this->_actionButtonName = $this->getButtonName('next', 'action');

    $this->_done = FALSE;

105 106
    $this->loadStandardSearchOptionsFromUrl();
    $this->loadFormValues();
totten's avatar
totten committed
107 108 109 110 111 112 113 114 115 116 117 118 119

    if ($this->_force) {
      $this->postProcess();
      $this->set('force', 0);
    }

    $sortID = NULL;
    if ($this->get(CRM_Utils_Sort::SORT_ID)) {
      $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
        $this->get(CRM_Utils_Sort::SORT_DIRECTION)
      );
    }

120
    $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
totten's avatar
totten committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    $selector = new CRM_Event_Selector_Search($this->_queryParams,
      $this->_action,
      NULL,
      $this->_single,
      $this->_limit,
      $this->_context
    );
    $prefix = NULL;
    if ($this->_context == 'user') {
      $prefix = $this->_prefix;
    }

    $this->assign("{$prefix}limit", $this->_limit);
    $this->assign("{$prefix}single", $this->_single);

    $controller = new CRM_Core_Selector_Controller($selector,
      $this->get(CRM_Utils_Pager::PAGE_ID),
      $sortID,
      CRM_Core_Action::VIEW,
      $this,
      CRM_Core_Selector_Controller::TRANSFER,
      $prefix
    );
    $controller->setEmbedded(TRUE);
    $controller->moveFromSessionToTemplate();

    $this->assign('summary', $this->get('summary'));
  }

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
151
   * Build the form object.
totten's avatar
totten committed
152 153
   *
   * @return void
eileen's avatar
eileen committed
154 155
   *
   * @throws \CRM_Core_Exception
totten's avatar
totten committed
156
   */
157
  public function buildQuickForm() {
158
    parent::buildQuickForm();
159
    $this->addContactSearchFields();
160

totten's avatar
totten committed
161 162 163 164
    CRM_Event_BAO_Query::buildSearchForm($this);

    $rows = $this->get('rows');
    if (is_array($rows)) {
165
      $lineItems = $eventIds = [];
totten's avatar
totten committed
166
      if (!$this->_single) {
167
        $this->addRowSelectors($rows);
totten's avatar
totten committed
168 169 170 171 172 173 174 175 176 177 178 179 180
      }
      foreach ($rows as $row) {
        $eventIds[$row['event_id']] = $row['event_id'];
        if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) {
          // add line item details if applicable
          $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']);
        }
      }

      //get actual count only when we are dealing w/ single event.
      $participantCount = 0;
      if (count($eventIds) == 1) {
        //convert form values to clause.
181
        $seatClause = [];
monishdeb's avatar
monishdeb committed
182 183 184 185
        if (CRM_Utils_Array::value('participant_test', $this->_formValues) == '1' || CRM_Utils_Array::value('participant_test', $this->_formValues) == '0') {
          $seatClause[] = "( participant.is_test = {$this->_formValues['participant_test']} )";
        }
        if (!empty($this->_formValues['participant_status_id'])) {
186
          $seatClause[] = CRM_Contact_BAO_Query::buildClause("participant.status_id", 'IN', $this->_formValues['participant_status_id'], 'Int');
187 188 189
          if ($status = CRM_Utils_Array::value('IN', $this->_formValues['participant_status_id'])) {
            $this->_formValues['participant_status_id'] = $status;
          }
monishdeb's avatar
monishdeb committed
190 191
        }
        if (!empty($this->_formValues['participant_role_id'])) {
192
          $escapedRoles = [];
193 194 195
          foreach ((array) $this->_formValues['participant_role_id'] as $participantRole) {
            $escapedRoles[] = CRM_Utils_Type::escape($participantRole, 'String');
          }
196
          $seatClause[] = "( participant.role_id IN ( '" . implode("' , '", $escapedRoles) . "' ) )";
monishdeb's avatar
monishdeb committed
197 198
        }

199 200 201
        // CRM-15379
        if (!empty($this->_formValues['participant_fee_id'])) {
          $participant_fee_id = $this->_formValues['participant_fee_id'];
202
          $val_regexp = [];
203 204
          foreach ($participant_fee_id as $k => &$val) {
            $val = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $val, 'label');
205
            $val_regexp[$k] = CRM_Core_DAO::escapeString(preg_quote(trim($val)));
206 207
            $val = CRM_Core_DAO::escapeString(trim($val));
          }
208
          $feeLabel = implode('|', $val_regexp);
209
          $seatClause[] = "( participant.fee_level REGEXP '{$feeLabel}' )";
totten's avatar
totten committed
210
        }
211

monishdeb's avatar
monishdeb committed
212 213
        $seatClause = implode(' AND ', $seatClause);
        $participantCount = CRM_Event_BAO_Event::eventTotalSeats(array_pop($eventIds), $seatClause);
totten's avatar
totten committed
214 215 216 217
      }
      $this->assign('participantCount', $participantCount);
      $this->assign('lineItems', $lineItems);

218 219
      $taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
      $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
totten's avatar
totten committed
220 221

      if (isset($this->_ssID)) {
222
        $savedSearchValues = [
totten's avatar
totten committed
223 224
          'id' => $this->_ssID,
          'name' => CRM_Contact_BAO_SavedSearch::getName($this->_ssID, 'title'),
225
        ];
totten's avatar
totten committed
226 227 228 229
        $this->assign_by_ref('savedSearch', $savedSearchValues);
        $this->assign('ssID', $this->_ssID);
      }

230
      $this->addTaskMenu($tasks);
totten's avatar
totten committed
231 232 233 234
    }

  }

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  /**
   * Get the label for the sortName field if email searching is on.
   *
   * (email searching is a setting under search preferences).
   *
   * @return string
   */
  protected function getSortNameLabelWithEmail() {
    return ts('Participant Name or Email');
  }

  /**
   * Get the label for the sortName field if email searching is off.
   *
   * (email searching is a setting under search preferences).
   *
   * @return string
   */
  protected function getSortNameLabelWithOutEmail() {
    return ts('Participant Name');
  }

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  /**
   * Get the label for the tag field.
   *
   * We do this in a function so the 'ts' wraps the whole string to allow
   * better translation.
   *
   * @return string
   */
  protected function getTagLabel() {
    return ts('Participant Tag(s)');
  }

  /**
   * Get the label for the group field.
   *
   * @return string
   */
  protected function getGroupLabel() {
    return ts('Participant Group(s)');
  }

  /**
   * Get the label for the group field.
   *
   * @return string
   */
  protected function getContactTypeLabel() {
    return ts('Participant Contact Type');
  }

totten's avatar
totten committed
287
  /**
288 289
   * Test submit the form.
   * @param $formValues
totten's avatar
totten committed
290
   */
291 292 293
  public function testSubmit($formValues) {
    $this->submit($formValues);
  }
totten's avatar
totten committed
294

295 296 297 298 299 300
  /**
   * Submit the search form with given values.
   * @param $formValues
   */
  private function submit($formValues) {
    $this->_formValues = $formValues;
totten's avatar
totten committed
301 302 303 304 305 306 307 308

    $this->fixFormValues();

    if (isset($this->_ssID) && empty($_POST)) {
      // if we are editing / running a saved search and the form has not been posted
      $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID);
    }

309 310
    // We don't show test records in summaries or dashboards
    if (empty($this->_formValues['participant_test']) && $this->_force) {
totten's avatar
totten committed
311 312 313
      $this->_formValues["participant_test"] = 0;
    }

314
    CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues);
totten's avatar
totten committed
315

316
    $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
totten's avatar
totten committed
317 318 319 320 321

    $this->set('formValues', $this->_formValues);
    $this->set('queryParams', $this->_queryParams);

    $buttonName = $this->controller->getButtonName();
322
    if ($buttonName == $this->_actionButtonName) {
totten's avatar
totten committed
323 324 325
      // check actionName and if next, then do not repeat a search, since we are going to the next page

      // hack, make sure we reset the task values
lobo's avatar
lobo committed
326
      $stateMachine = $this->controller->getStateMachine();
totten's avatar
totten committed
327 328 329 330 331 332 333 334 335 336 337 338
      $formName = $stateMachine->getTaskFormName();
      $this->controller->resetPage($formName);
      return;
    }

    $sortID = NULL;
    if ($this->get(CRM_Utils_Sort::SORT_ID)) {
      $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
        $this->get(CRM_Utils_Sort::SORT_DIRECTION)
      );
    }

339
    $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, ['event_id']);
totten's avatar
totten committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

    $selector = new CRM_Event_Selector_Search($this->_queryParams,
      $this->_action,
      NULL,
      $this->_single,
      $this->_limit,
      $this->_context
    );

    $selector->setKey($this->controller->_key);

    $prefix = NULL;
    if ($this->_context == 'user') {
      $prefix = $this->_prefix;
    }

    $this->assign("{$prefix}limit", $this->_limit);
    $this->assign("{$prefix}single", $this->_single);

    $controller = new CRM_Core_Selector_Controller($selector,
      $this->get(CRM_Utils_Pager::PAGE_ID),
      $sortID,
      CRM_Core_Action::VIEW,
      $this,
      CRM_Core_Selector_Controller::SESSION,
      $prefix
    );
    $controller->setEmbedded(TRUE);

lobo's avatar
lobo committed
369
    $query = $selector->getQuery();
totten's avatar
totten committed
370 371 372 373 374 375
    if ($this->_context == 'user') {
      $query->setSkipPermission(TRUE);
    }
    $controller->run();
  }

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  /**
   * The post processing of the form gets done here.
   *
   * Key things done during post processing are
   *      - check for reset or next request. if present, skip post procesing.
   *      - now check if user requested running a saved search, if so, then
   *        the form values associated with the saved search are used for searching.
   *      - if user has done a submit with new values the regular post submissing is
   *        done.
   * The processing consists of using a Selector / Controller framework for getting the
   * search results.
   *
   * @param
   *
   * @return void
   */
  public function postProcess() {
    if ($this->_done) {
      return;
    }

    $this->_done = TRUE;
398
    $formValues = [];
399 400 401

    if (!empty($_POST)) {
      $formValues = $this->controller->exportValues($this->_name);
402
      CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, ['participant_status_id']);
403 404
    }

405
    if (empty($formValues)) {
406 407 408 409 410 411
      $formValues = $this->controller->exportValues($this->_name);
    }

    $this->submit($formValues);
  }

totten's avatar
totten committed
412
  /**
colemanw's avatar
colemanw committed
413
   * add the rules (mainly global rules) for form.
totten's avatar
totten committed
414 415
   * All local rules are added near the element
   *
colemanw's avatar
colemanw committed
416
   * @return void
totten's avatar
totten committed
417 418
   * @see valid_date
   */
419 420
  public function addRules() {
  }
totten's avatar
totten committed
421 422

  /**
Eileen McNaughton's avatar
Eileen McNaughton committed
423
   * Set the default form values.
totten's avatar
totten committed
424 425
   *
   *
426 427
   * @return array
   *   the default array reference
totten's avatar
totten committed
428
   */
429
  public function setDefaultValues() {
430
    $defaults = [];
totten's avatar
totten committed
431 432 433 434
    $defaults = $this->_formValues;
    return $defaults;
  }

435
  public function fixFormValues() {
totten's avatar
totten committed
436 437 438
    // if this search has been forced
    // then see if there are any get values, and if so over-ride the post values
    // note that this means that GET over-rides POST :)
439
    $event = CRM_Utils_Request::retrieve('event', 'Positive');
totten's avatar
totten committed
440 441 442 443 444
    if ($event) {
      $this->_formValues['event_id'] = $event;
      $this->_formValues['event_name'] = CRM_Event_PseudoConstant::event($event, TRUE);
    }

445
    $status = CRM_Utils_Request::retrieve('status', 'String');
totten's avatar
totten committed
446 447 448

    if (isset($status)) {
      if ($status === 'true') {
monishdeb's avatar
monishdeb committed
449
        $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 1");
totten's avatar
totten committed
450 451
      }
      elseif ($status === 'false') {
monishdeb's avatar
monishdeb committed
452
        $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, "is_counted = 0");
totten's avatar
totten committed
453 454
      }
      elseif (is_numeric($status)) {
monishdeb's avatar
monishdeb committed
455
        $statusTypes = (int) $status;
totten's avatar
totten committed
456
      }
457 458 459
      elseif (is_array($status) && !array_key_exists('IN', $status)) {
        $statusTypes = array_keys($status);
      }
460
      $this->_formValues['participant_status_id'] = is_array($statusTypes) ? ['IN' => array_keys($statusTypes)] : $statusTypes;
totten's avatar
totten committed
461 462
    }

463
    $role = CRM_Utils_Request::retrieve('role', 'String');
totten's avatar
totten committed
464 465 466

    if (isset($role)) {
      if ($role === 'true') {
monishdeb's avatar
monishdeb committed
467
        $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 1");
totten's avatar
totten committed
468 469
      }
      elseif ($role === 'false') {
monishdeb's avatar
monishdeb committed
470
        $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, "filter = 0");
totten's avatar
totten committed
471 472
      }
      elseif (is_numeric($role)) {
monishdeb's avatar
monishdeb committed
473
        $roleTypes = (int) $role;
totten's avatar
totten committed
474
      }
monishdeb's avatar
monishdeb committed
475
      $this->_formValues['participant_role_id'] = is_array($roleTypes) ? array_keys($roleTypes) : $roleTypes;
monishdeb's avatar
monishdeb committed
476 477
    }

478
    $type = CRM_Utils_Request::retrieve('type', 'Positive');
totten's avatar
totten committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    if ($type) {
      $this->_formValues['event_type'] = $type;
    }

    $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);

    if ($cid) {
      $cid = CRM_Utils_Type::escape($cid, 'Integer');
      if ($cid > 0) {
        $this->_formValues['contact_id'] = $cid;

        // also assign individual mode to the template
        $this->_single = TRUE;
      }
    }
  }

Eileen McNaughton's avatar
Eileen McNaughton committed
496 497 498
  /**
   * @return null
   */
499
  public function getFormValues() {
totten's avatar
totten committed
500 501 502 503 504 505 506 507 508 509 510
    return NULL;
  }

  /**
   * Return a descriptive name for the page, used in wizard header
   *
   * @return string
   */
  public function getTitle() {
    return ts('Find Participants');
  }
511

totten's avatar
totten committed
512
}