You are here

function registration_checkin_search_form_submit in Entity Registration 7.2

AJAX callback to update the registrant checkin table based on a search query.

1 string reference to 'registration_checkin_search_form_submit'
registration_checkin_search_form in modules/registration_checkin/registration_checkin.module
Form builder for the checkin page search function.

File

modules/registration_checkin/registration_checkin.module, line 231
Entity Registration registrant checkin workflow and UI for registration.

Code

function registration_checkin_search_form_submit($form, &$form_state) {

  // Get host info.
  $entity_type = $form_state['values']['entity_type'];
  $entity_id = $form_state['values']['entity_id'];

  // Query again for all registrants.
  // We need them all since we are want to search the entity label value.
  // Not all entities will have a label, so a load-all basis is needed here.
  $registrations = registration_checkin_get_all_registrations($entity_id, $entity_type);
  $search_keys = preg_split('/\\s+/', $form_state['values']['search_keys']);
  $registration_searchable_fields = array(
    'registrant_mail',
  );

  // Since we coldn't use field conditions before the query, we have to iterate.
  // This is going to slow down as we scale up. A few hundred should be ok.
  $matches = array();
  foreach ($registrations as $rid => $registration) {
    $registration_wrapper = entity_metadata_wrapper('registration', $registration);

    // First search entity label.
    $registrant_type = $registration_wrapper->registrant
      ->type();
    $registrant = $registration_wrapper->registrant
      ->value();
    $registrant_label = entity_label($registrant_type, $registrant);
    foreach ($search_keys as $key) {
      if (stripos($registrant_label, $key) !== FALSE) {
        $matches[$rid] = $registration;
        break;
      }
    }

    // Iterate the the fields we defined as searchable.
    foreach ($registration_searchable_fields as $field) {
      $haystack = $registration_wrapper->{$field}
        ->value();
      foreach ($search_keys as $needle) {
        if (stripos($haystack, $needle) !== FALSE) {

          // A match was found.
          // Save it in the matches bucket and stop examining this registration.
          $matches[$rid] = $registration;
          break 2;
        }
      }
    }
  }
  if (!empty($matches)) {

    // Generate markup for the registrant table.
    $table = array(
      'header' => registration_checkin_get_registration_table_headers(),
      'rows' => registration_checkin_get_registration_table_rows($matches),
    );
    $registrant_markup = theme('table', $table) . theme('pager');
  }
  else {
    $registrant_markup = t('No results.');
  }
  $registrants_build = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => array(
        'registrant-list',
      ),
    ),
    'markup' => array(
      '#markup' => $registrant_markup,
    ),
  );
  return render($registrants_build);
}