You are here

function context_ui_item_block in Context 5

Provides simple operations (load/save) on any context-block associations. Parallel usage as context_ui_item().

Parameters

$op: Operation to perform on a context. May be either load or save.

$context: A context object with an array of blocks at $context->block.

Return value

Load returns a context object with block information. Save returns true on success and false on failure.

1 call to context_ui_item_block()
context_ui_context in context_ui/context_ui.module
Provides simple operations (load/insert/update/etc.) on a core context space/key/value definition.

File

context_ui/context_ui.module, line 879

Code

function context_ui_item_block($op = 'load', $context) {
  if ($context->cid) {
    switch ($op) {
      case 'load':
        $result = db_query("SELECT module, delta, region, weight FROM {context_ui_block} WHERE cid = %d", $context->cid);
        $context->block = array();
        while ($block = db_fetch_object($result)) {
          $bid = $block->module . "_" . $block->delta;
          $block->bid = $bid;
          $context->block[$bid] = $block;
        }
        return $context;
        break;
      case 'save':

        // grab the current context-> block associations
        $current = (object) array(
          'cid' => $context->cid,
        );
        $current = context_ui_item_block('load', $current);

        // compare current definition with new definition. remove missing associations from the DB
        if (is_array($current->block)) {
          foreach ($current->block as $block) {
            if (!isset($context->block[$block->bid]) || $current->block[$block->bid] != $context->block[$block->bid]) {
              $result = db_query("DELETE FROM {context_ui_block WHERE cid = %d AND module = '%s' AND delta = '%s'", $context->cid, $block->module, $block->delta);
            }
          }
        }

        // compare new definition with current definition. add missing associations to the DB
        if (is_array($context->block)) {
          foreach ($context->block as $block) {
            $block = (object) $block;
            if (!isset($current->block[$block->bid]) || $current->block[$block->bid] != $context->block[$block->bid]) {
              $args = array(
                'module' => $block->module,
                'delta' => $block->delta,
                'region' => $block->region,
                'weight' => $block->weight,
                'cid' => $context->cid,
              );
              $result = db_query("REPLACE INTO {context_ui_block} (module, delta, region, weight, cid) VALUES ('%s', '%s', '%s', %d, %d)", $args);
            }
          }
        }
        return true;
        break;
    }
  }
  return false;
}