You are here

function redhen_filter_query in RedHen CRM 7

Executes a filter query against the provided attributes.

Used to filter Redhen entity list pages.

Parameters

string $entity_type: Entity type to filter by.

array $header: Header array for sorting. Should match the table header used in output.

string|array $bundle: A single entity bundle or an array of multiple bundles.

array $properties: Associative array of key/value pairs.

array $fields: Entity fields to filter by.

Return value

array Executed query object.

5 calls to redhen_filter_query()
redhen_activity_activity_page in modules/redhen_activity/redhen_activity.module
Page callback for a list of activities.
redhen_contact_page in modules/redhen_contact/includes/redhen_contact.pages.inc
Page callback for contact overview page.
redhen_engagements_page in modules/redhen_engagement/redhen_engagement.module
Page callback for showing a contacts engagements.
redhen_note_notes_page in modules/redhen_note/includes/redhen_note.pages.inc
redhen_org_page in modules/redhen_org/includes/redhen_org.pages.inc
Page callback for org overview page.

File

./redhen.module, line 187
Defines basic functionality common to all parts of the Redhen CRM.

Code

function redhen_filter_query($entity_type, $header, $bundle = NULL, $properties = array(), $fields = array()) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $entity_type, '=')
    ->tablesort($header)
    ->pager(variable_get(REDHEN_ITEMS_PAGE, 10));
  if (!empty($bundle)) {
    $operator = is_array($bundle) ? 'in' : '=';
    $query
      ->entityCondition('bundle', $bundle, $operator);
  }

  // Add property filters.
  foreach ($properties as $key => $value) {
    $property_info = entity_get_all_property_info($entity_type);
    if ($value != '') {

      // Processing for date ranges.
      if ($property_info[$key]['type'] == 'date') {
        if ($value['from'] != '') {
          $conditions = array(
            strtotime($value['from']),
            // Add 24 hours for full day search.
            strtotime($value['to']) + 86400,
          );
          $query
            ->propertyCondition($key, $conditions, 'BETWEEN');
        }
      }
      else {

        // NULL operator will allow literal or array for $value.
        $operator = NULL;
        if (isset($property_info[$key]['filter_operator']) && !is_array($value)) {
          $operator = $property_info[$key]['filter_operator'];
        }
        $query
          ->propertyCondition($key, $value, $operator);
      }
    }
  }

  // Add field filters.
  foreach ($fields as $key => $field) {
    $field_info = field_info_field($key);
    switch ($field_info['type']) {

      // Special handling for date fields, which have a two-value range.
      case 'date':
      case 'datestamp':
      case 'datetime':
        if ($field[LANGUAGE_NONE][0]['value'] != '' && $field[LANGUAGE_NONE][0]['value2'] != '') {
          $value = array(
            $field[LANGUAGE_NONE][0]['value'],
            $field[LANGUAGE_NONE][0]['value2'],
          );
          $query
            ->fieldCondition($key, 'value', $value, 'BETWEEN', $key);
        }
        break;
      case 'list_boolean':
        foreach ($field[LANGUAGE_NONE][0] as $column => $value) {

          // Accommodate the skip option introduced in redhen_filter_form().
          if ($value != 'skip') {
            $query
              ->fieldCondition($key, $column, (int) $value, '=', $key);
          }
        }
        break;
      default:

        // Accept only valid columns. Some field types add non-column data.
        $field[LANGUAGE_NONE][0] = array_intersect_key($field[LANGUAGE_NONE][0], $field_info['columns']);
        foreach ($field[LANGUAGE_NONE][0] as $column => $value) {

          // Only accept fields for which a value has been entered.
          if ($value != '') {
            $query
              ->fieldCondition($key, $column, $value, 'CONTAINS', $key);
          }
        }
        break;
    }
  }
  return $query
    ->execute();
}