You are here

function regcode_load_single in Registration codes 7

Same name and namespace in other branches
  1. 8 regcode.module \regcode_load_single()
  2. 7.2 regcode.module \regcode_load_single()

Load a registration code.

@example regcode_load(1231); // Loads the regcode with rid=1231 regcode_load(NULL, array('code'=>'foobar')); // Loads the "foobar" regcode

Parameters

int|null $id: The database primary key (rid).

array $conditions: An associative array containing the search conditions.

Return value

object|bool The regcode object or FALSE if the code does not exist.

1 call to regcode_load_single()
regcode_code_validate in ./regcode.module
Validate a regcode.

File

./regcode.module, line 367
Main functionality and hooks of regcode module.

Code

function regcode_load_single($id, $conditions = array()) {

  // Build the query.
  $query = db_select('regcode')
    ->fields('regcode', array(
    'rid',
    'uid',
    'created',
    'lastused',
    'begins',
    'expires',
    'code',
    'is_active',
    'maxuses',
    'uses',
  ))
    ->range(0, 1);

  // Allow mixed search parameters.
  if (!empty($id)) {
    $query
      ->condition('rid', $id);
  }
  else {
    foreach ($conditions as $field => $value) {
      $query
        ->condition($field, $value);
    }
  }

  // Run the query and grab a single regcode.
  $regcode = $query
    ->execute()
    ->fetchObject();
  if (!$regcode) {
    return FALSE;
  }

  // Load the terms.
  $query = db_select('regcode_term')
    ->fields('term_data', array(
    'tid',
    'name',
  ))
    ->condition('regcode_term.rid', $regcode->rid);
  $query
    ->join('taxonomy_term_data', 'term_data', 'regcode_term.tid = term_data.tid');
  $tags = $query
    ->execute();
  $regcode->tags = array();
  foreach ($tags as $tag) {
    $regcode->tags[$tag->tid] = $tag->name;
  }

  /*
   * Entity loaders expect arrays of objects. entity_load and
   * this function both invoke the hook below.
   */
  $reg_codes = array(
    $regcode->rid => $regcode,
  );
  module_invoke_all('regcode_load', $reg_codes);
  return $reg_codes[$regcode->rid];
}