You are here

function context_prefix_api in Context 5

Provides a simple API for validating, adding, and deleting context defintions.

1 call to context_prefix_api()
context_prefix_form_validate in context_prefix/context_prefix.module
Validation handler for context_prefix_form().

File

context_prefix/context_prefix.module, line 539

Code

function context_prefix_api($op = 'insert', $context) {
  switch ($op) {
    case 'load':
      if (isset($context['provider'])) {
        if ($context['id']) {
          $context = db_fetch_array(db_query("SELECT * FROM {context_prefix} WHERE id = '%s' AND provider = '%s'", $context['id'], $context['provider']));
          if ($context) {
            return $context;
          }
        }
        else {
          if ($context['prefix']) {
            $context = db_fetch_array(db_query("SELECT * FROM {context_prefix} WHERE prefix = '%s' AND provider = '%s'", $context['prefix'], $context['provider']));
            if ($context) {
              return $context;
            }
          }
        }
        return false;
      }
      break;
    case 'validate':
      if (check_plain($context['provider']) && preg_match('!^[a-z0-9_-]+$!', $context['prefix'])) {
        $id = db_result(db_query("SELECT id FROM {context_prefix} WHERE prefix = '%s'", $context['prefix']));
        if ($id && $id == $context['id']) {
          return true;
        }
        else {
          if (!$id) {
            return true;
          }
        }
        return false;
      }
      else {
        return false;
      }
    case 'insert':
      if (context_prefix_api('validate', $context)) {
        $status = db_query("INSERT INTO {context_prefix} (provider, prefix, id) VALUES ('%s', '%s', %d)", $context['provider'], $context['prefix'], $context['id']);
        return $status;
      }
      return false;
    case 'update':
      if (context_prefix_api('validate', $context)) {
        $status = db_query("UPDATE {context_prefix} SET prefix = '%s' WHERE id = '%s' AND provider = '%s'", $context['prefix'], $context['id'], $context['provider']);
        return $status;
      }
    case 'delete':
      if ($context['prefix']) {
        $param = 'prefix';
        $where = $context['prefix'];
      }
      else {
        if ($context['id']) {
          $param = 'id';
          $where = $context['id'];
        }
      }
      $check = db_result(db_query("SELECT id FROM {context_prefix} WHERE provider = '%s' AND {$param} = '%s'", $context['provider'], $where));
      if ($check) {
        $status = db_query("DELETE FROM {context_prefix} WHERE provider = '%s' AND {$param} = '%s'", $context['provider'], $where);
        return $status;
      }
      return false;
  }
  return false;
}