You are here

function salesforce_mapping_object_filter_query in Salesforce Suite 7.3

Return loaded mapping objects from a query.

Parameters

array $header: Table sort header array.

array $properties: Array of properties to include in the query.

int $limit: Number of results to return.

int $mapping_id: Entity ID for the mapping: limits results to a single mapping.

Return value

array Array of loaded entities.

1 call to salesforce_mapping_object_filter_query()
salesforce_mapping_object_overview_table in modules/salesforce_mapping/includes/salesforce_mapping_object.admin.inc
Generate the overview filter form/table for mappings.

File

modules/salesforce_mapping/salesforce_mapping.module, line 802

Code

function salesforce_mapping_object_filter_query($header = FALSE, $properties = array(), $limit = 25, $mapping_id = NULL) {
  $table = 'salesforce_mapping_object';
  $id = 'salesforce_mapping_object_id';
  if ($mapping_id) {
    $table .= '_revision';
    $id = 'revision_id';
  }
  $limit = !empty($limit) ? $limit : 25;
  $query = db_select($table, 'm')
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->fields('m')
    ->limit($limit)
    ->orderByHeader($header);
  if (!$mapping_id) {
    $query
      ->join($table . '_revision', 'r', 'm.salesforce_mapping_object_id = r.salesforce_mapping_object_id');
    $query
      ->groupBy('m.salesforce_mapping_object_id');

    // Add count columns for each status value.
    foreach (salesforce_mapping_object_sync_status() as $status_id => $status) {
      $query
        ->addExpression('count(CASE WHEN r.last_sync_status = ' . $status_id . ' THEN TRUE ELSE NULL END)', 'status_' . $status_id);
    }
  }
  else {
    $query
      ->condition('m.salesforce_mapping_object_id', $mapping_id);
  }

  // Add conditions.
  foreach ($properties as $property => $value) {
    if (isset($value) && $value != '') {
      $operator = is_array($value) ? 'IN' : '=';
      switch ($property) {
        case 'last_sync':

          // Handle from/to.
          if (!empty($value['from'])) {
            $query
              ->condition('m.' . $property, strtotime($value['from']), '>=');
          }
          if (!empty($value['to'])) {
            $query
              ->condition('m.' . $property, strtotime($value['to']), '<=');
          }
          break;
        case 'salesforce_id':
          if (strlen($value) < 18) {

            // Match start of the Salesforce ID if less than the full 18 chars.
            $operator = 'LIKE';
            $value = db_like($value) . '%';
          }
        default:
          $query
            ->condition('m.' . $property, $value, $operator);
      }
    }
  }
  $results = $query
    ->execute()
    ->fetchAllAssoc($id);
  return $results;
}