You are here

function context_ui_context in Context 5

Provides simple operations (load/insert/update/etc.) on a core context space/key/value definition.

Parameters

$op: Operation to perform on a context. May be one of load/insert/update/delete.

$context: A context object. Optionally, can be an integer cid for the "load" operation.

Return value

If loading, returns a full context item. All other operations return true on success and false on failure.

11 calls to context_ui_context()
ContextUiTest::tearDown in context_ui/tests/context_ui.test
tearDown implementation, setting back switched modules etc
context_ui_delete_confirm in context_ui/context_ui_admin.inc
Provide a form to confirm deletion of a context definition.
context_ui_delete_confirm_submit in context_ui/context_ui_admin.inc
Submit hook for context_ui delete confirmation form.
context_ui_export in context_ui/context_ui_admin.inc
Provides a form with an exported context definition for use in modules.
context_ui_form in context_ui/context_ui_admin.inc
Generates the omnibus context definition editing form. Note: submission and validation handlers are in context_ui_admin.inc

... See full list

File

context_ui/context_ui.module, line 702

Code

function context_ui_context($op, $context) {
  switch ($op) {
    case 'load':
      static $cache = array();

      // Argument is a cid
      if (is_numeric($context)) {
        if (!isset($cache[$context])) {
          $context = db_fetch_object(db_query("SELECT * FROM {context_ui} WHERE cid = %d", $context));
        }
        else {
          return $cache[$context];
        }
      }
      else {
        if (is_object($context) && isset($context->cid)) {
          if (!$cache[$context->cid]) {
            $context = db_fetch_object(db_query("SELECT * FROM {context_ui} WHERE cid = %d", $context->cid));
          }
          else {
            return $cache[$context->cid];
          }
        }
        else {
          if (is_object($context) && $context->namespace && $context->attribute && $context->value) {
            $args = array(
              $context->namespace,
              $context->attribute,
              $context->value,
            );
            $system = '';
            $status = '';
            if (isset($context->system)) {
              $args[] = $context->system;
              $system = "AND system = '%s'";
            }
            if (isset($context->status)) {
              $args[] = $context->status;
              $status = "AND status = %d";
            }
            $context = db_fetch_object(db_query("SELECT * FROM {context_ui} WHERE namespace = '%s' AND attribute = '%s' AND value = '%s' {$system} {$status}", $args));
          }
        }
      }
      if ($context) {
        $context = context_ui_item('load', $context);
        $context = context_ui_item_block('load', $context);

        // After all that hard work, cache the context
        $cache[$context->cid] = $context;
        return $context;
      }
      return false;
    case 'insert':

      // check for type & existence of context definition
      $existing = context_ui_context('load', $context);
      if (!$existing || $existing->system != $context->system) {
        $context->cid = db_next_id('{context_ui}_cid');
        $values = array(
          'cid' => $context->cid,
          'system' => $context->system,
          'status' => $context->status,
          'namespace' => $context->namespace,
          'attribute' => $context->attribute,
          'value' => $context->value,
        );
        $keys = implode(', ', array_keys($values));
        $args = array_merge(array(
          $keys,
        ), $values);
        $result = db_query("INSERT INTO {context_ui} (%s) VALUES(%d, %d, %d, '%s', '%s', '%s')", $args);
        $result = $result && context_ui_item('save', $context);
        $result = $result && context_ui_item_block('save', $context);
        return $result ? true : false;
      }
      return false;
      break;
    case 'update':
      if ($context->cid) {

        // update core context information
        $values = array(
          'system' => $context->system,
          'status' => $context->status,
          'namespace' => $context->namespace,
          'attribute' => $context->attribute,
          'value' => $context->value,
          'cid' => $context->cid,
        );
        $result = db_query("UPDATE {context_ui} SET system = %d, status = %d, namespace = '%s', attribute = '%s', value = '%s'WHERE cid = %d", $values);
        $result = $result && context_ui_item('save', $context);
        $result = $result && context_ui_item_block('save', $context);
        return $result ? true : false;
      }
      break;
    case 'delete':
      if ($context = context_ui_context('load', $context)) {
        db_query("DELETE FROM {context_ui} WHERE cid = %d", $context->cid);
        db_query("DELETE FROM {context_ui_item} WHERE cid = %d", $context->cid);
        db_query("DELETE FROM {context_ui_block} WHERE cid = %d", $context->cid);
        return true;
      }
      return false;
  }
}