5 * Implementation of hook_install().
7 function context_install() {
8 drupal_install_schema('context');
12 * Implementation of hook_uninstall().
14 function context_uninstall() {
15 drupal_uninstall_schema('context');
16 variable_del('context_ui_show_empty_regions');
17 variable_del('context_reaction_block_disable_core');
18 variable_del('context_reaction_block_all_regions');
22 * Implementation of hook_schema().
24 function context_schema() {
26 $schema['context'] = array(
27 'description' => 'Storage for normal (user-defined) contexts.',
30 'identifier' => 'context',
31 'default hook' => 'context_default_contexts', // Function hook name.
32 'status' => 'context_status',
35 'api' => 'context', // Base name for api include files.
36 'minimum_version' => 3,
37 'current_version' => 3,
39 'export callback' => 'context_export',
43 'description' => 'The primary identifier for a context.',
49 'description' => array(
50 'description' => 'Description for this context.',
57 'description' => 'Tag for this context.',
63 'conditions' => array(
64 'description' => 'Serialized storage of all context condition settings.',
69 'description' => 'Serialized storage of all context reaction settings.',
73 'condition_mode' => array(
74 'description' => 'Condition mode for this context.',
79 'primary key' => array('name'),
85 * Update script for context that installs the context schema and migrates
86 * any existing context data from deprecated context_ui tables.
88 function context_update_6001() {
91 if (!db_table_exists('context')) {
92 drupal_install_schema('context');
95 if (db_table_exists('context_ui')) {
96 // Clear the schema cache and rebuild
97 drupal_get_schema(NULL
, TRUE
);
99 // Migrate existing contexts to context table
100 $result = db_query("SELECT * FROM {context_ui}");
101 while ($context = db_fetch_object($result)) {
103 $setter_result = db_query("SELECT * FROM {context_ui_setter} WHERE cid = %d", $context->cid
);
104 while ($row = db_fetch_object($setter_result)) {
105 $context->{$row->type
}[$row->id
] = $row->id
;
108 $getter_result = db_query("SELECT * FROM {context_ui_getter} WHERE cid = %d", $context->cid
);
109 while ($row = db_fetch_object($getter_result)) {
110 $context->{$row->type
} = unserialize($row->data
);
113 $block_result = db_query("SELECT module, delta, region, weight FROM {context_ui_block} WHERE cid = %d", $context->cid
);
114 while ($block = db_fetch_object($block_result)) {
115 if (!isset($context->block
)) {
116 $context->block
= array();
118 $block->bid
= $block->module .
"_".
$block->delta
;
119 $context->block
[$block->bid
] = $block;
121 // Clear out identifier
122 unset($context->cid
);
123 context_save_context($context);
127 module_enable(array('context_contrib'));
133 * Update script for API change in path condition.
135 function context_update_6002() {
136 define('CONTEXT_STORAGE_DEFAULT', 0);
137 define('CONTEXT_STORAGE_OVERRIDDEN', 1);
138 define('CONTEXT_STORAGE_NORMAL', 2);
140 // Iterate through all DB-stored contexts and incorporate path
141 // wildcards into their path conditions. Any exported/default
142 // contexts will need to be updated by hand.
143 $contexts = context_enabled_contexts();
144 foreach ($contexts as
$context) {
145 if (($context->type
== CONTEXT_STORAGE_NORMAL
|| $context->type
== CONTEXT_STORAGE_OVERRIDDEN
) && (!empty($context->path
) && is_array($context->path
))) {
147 foreach ($context->path as
$k => $v) {
148 if ($v != '<front>' && strpos($v, '*') === FALSE
) {
150 $context->path
[$k] = "{$v}*";
154 context_save_context($context);
162 * Remove deprecated tables from context_ui.
164 function context_update_6003() {
166 $tables = array('context_ui', 'context_ui_setter', 'context_ui_getter', 'context_ui_block');
167 foreach ($tables as
$table) {
168 if (db_table_exists($table)) {
169 db_drop_table($ret, $table);
176 * Update 6301: Update schema.
178 function context_update_6301() {
180 drupal_install_modules(array('ctools'));
185 'description' => 'The primary identifier for a context.',
191 'description' => array(
192 'description' => 'Description for this context.',
199 'description' => 'Tag for this context.',
205 'conditions' => array(
206 'description' => 'Serialized storage of all context condition settings.',
210 'reactions' => array(
211 'description' => 'Serialized storage of all context reaction settings.',
216 'primary key' => array('name'),
219 if (db_table_exists('context')) {
220 $result = db_query("SELECT * FROM {context}");
222 // Migrate old contexts into new table.
224 while ($context = db_fetch_object($result)) {
225 $data = unserialize($context->data
);
226 unset($context->data
);
227 foreach ($data as
$k => $v) {
230 $contexts["{$context->namespace}-{$context->attribute}-{$context->value}"] = $context;
233 // Drop the existing context table and create one using the new schema.
234 db_drop_table($ret, 'context');
235 db_create_table($ret, 'context', $schema);
238 context_migrate_api_3($ret, $contexts);
244 * Update 6302: Update old context exportables. This update script may be
245 * re-run at any time to update context 2 objects that have been exported.
247 function context_update_6302() {
249 // Invoke context 2 default hooks so that the contexts can be migrated.
250 foreach (module_invoke_all('context_default_contexts') as
$context) {
251 $context = (object) $context;
252 if (!isset($context->api_version
)) {
253 $contexts["{$context->namespace}-{$context->attribute}-{$context->value}"] = $context;
258 context_migrate_api_3($ret, $contexts);
263 * Update 6303: Add field for context condition mode.
265 function context_update_6303() {
268 'description' => 'Condition mode for this context.',
272 db_add_field($ret, 'context', 'condition_mode', $spec);
277 * Update 6304: Rename variable 'context_ui_show_empty_regions'.
279 function context_update_6304() {
280 if (!db_result(db_query("SELECT name FROM {variable} WHERE name = 'context_reaction_block_all_regions'"))) {
281 db_query("UPDATE {variable} SET name = 'context_reaction_block_all_regions' WHERE name = 'context_ui_show_empty_regions'");
282 return array(array('success' => TRUE
, 'query' => 'Variable renamed successfully.'));
288 * Helper function to update context 2 objects to context 3.
290 function context_migrate_api_3(&$ret, $contexts) {
291 foreach ($contexts as
$context) {
292 if (!db_result(db_query("SELECT name FROM {context} WHERE name = '%s'", "{$context->namespace}-{$context->attribute}-{$context->value}"))) {
294 'name' => "{$context->namespace}-{$context->attribute}-{$context->value}",
295 'description' => isset($context->description
) ?
$context->description
: '',
297 'conditions' => array(),
298 'reactions' => array(),
300 // Migration condition/reaction settings.
301 // Some have been renamed. Map them.
306 'sitewide' => 'sitewide',
308 'menu_trail' => 'menu',
310 'nodequeue' => 'nodequeue'
312 foreach ($conditions as
$old_key => $new_key) {
313 if (isset($context->{$old_key})) {
314 $values = $context->{$old_key};
315 $new['conditions'][$new_key] = array(
316 'values' => is_array($values) ?
$values : array($values),
323 'theme_section' => 'theme',
324 'css_injector' => 'css_injector',
327 foreach ($reactions as
$old_key => $new_key) {
328 if (isset($context->{$old_key})) {
329 // Special treatment for blocks.
330 if ($old_key === 'block') {
331 foreach ($context->block as
$block) {
332 $block = (array)$block;
333 $new['reactions']['block']['blocks'][$block['module'] .
'-'.
$block['delta']] = $block;
337 $new['reactions'][$new_key] = $context->{$old_key};
341 $new['conditions'] = serialize($new['conditions']);
342 $new['reactions'] = serialize($new['reactions']);
344 // Update_sql does not escape strings properly.
345 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']);
347 // Notify the user of any keys that were not migrated.
348 $known_keys = array_merge(array_keys($conditions), array_keys($reactions), array('cid', 'system', 'namespace', 'attribute', 'value', 'description'));
349 $unmigrated = array_diff(array_keys((array) $context), $known_keys);
350 if (!empty($unmigrated)) {
351 $unmigrated = implode(', ', $unmigrated);
354 'query' => "Updated context: {$new['name']}. The following properties could not be migrated: {$unmigrated}."
360 'query' => "Updated context: {$new['name']}."