You are here

function context_migrate_api_3 in Context 6.3

Helper function to update context 2 objects to context 3.

2 calls to context_migrate_api_3()
context_update_6301 in ./context.install
Update 6301: Update schema.
context_update_6302 in ./context.install
Update 6302: Update old context exportables. This update script may be re-run at any time to update context 2 objects that have been exported.

File

./context.install, line 304

Code

function context_migrate_api_3(&$ret, $contexts) {
  foreach ($contexts as $context) {
    if (!db_result(db_query("SELECT name FROM {context} WHERE name = '%s'", "{$context->namespace}-{$context->attribute}-{$context->value}"))) {
      $new = array(
        'name' => "{$context->namespace}-{$context->attribute}-{$context->value}",
        'description' => isset($context->description) ? $context->description : '',
        'tag' => '',
        'conditions' => array(),
        'reactions' => array(),
      );

      // Migration condition/reaction settings.
      // Some have been renamed. Map them.
      $conditions = array(
        'node' => 'node',
        'user' => 'user',
        'book' => 'book',
        'sitewide' => 'sitewide',
        'path' => 'path',
        'menu_trail' => 'menu',
        'views' => 'views',
        'nodequeue' => 'nodequeue',
      );
      foreach ($conditions as $old_key => $new_key) {
        if (isset($context->{$old_key})) {
          $values = $context->{$old_key};
          $new['conditions'][$new_key] = array(
            'values' => is_array($values) ? $values : array(
              $values,
            ),
            'options' => array(),
          );
        }
      }
      $reactions = array(
        'menu' => 'menu',
        'theme_section' => 'theme',
        'css_injector' => 'css_injector',
        'block' => 'block',
      );
      foreach ($reactions as $old_key => $new_key) {
        if (isset($context->{$old_key})) {

          // Special treatment for blocks.
          if ($old_key === 'block') {
            foreach ($context->block as $block) {
              $block = (array) $block;
              $new['reactions']['block']['blocks'][$block['module'] . '-' . $block['delta']] = $block;
            }
          }
          else {
            $new['reactions'][$new_key] = $context->{$old_key};
          }
        }
      }
      $new['conditions'] = serialize($new['conditions']);
      $new['reactions'] = serialize($new['reactions']);

      // Update_sql does not escape strings properly.
      db_query("INSERT INTO {context} (name,description,tag,conditions,reactions) VALUES ('%s', '%s', '%s', '%s', '%s')", $new['name'], $new['description'], $new['tag'], $new['conditions'], $new['reactions']);

      // Notify the user of any keys that were not migrated.
      $known_keys = array_merge(array_keys($conditions), array_keys($reactions), array(
        'cid',
        'system',
        'namespace',
        'attribute',
        'value',
        'description',
      ));
      $unmigrated = array_diff(array_keys((array) $context), $known_keys);
      if (!empty($unmigrated)) {
        $unmigrated = implode(', ', $unmigrated);
        $ret[] = array(
          'success' => TRUE,
          'query' => "Updated context: {$new['name']}. The following properties could not be migrated: {$unmigrated}.",
        );
      }
      else {
        $ret[] = array(
          'success' => TRUE,
          'query' => "Updated context: {$new['name']}.",
        );
      }
    }
  }
}