popsu-d7/sites/all/modules/menu_token/menu_token.install
Bachir Soussi Chiadmi 1bc61b12ad first import
2015-04-08 11:40:19 +02:00

169 lines
4.3 KiB
Plaintext

<?php
/**
* @file
* Install file for menu_token module.
*/
/**
* Implements hook_update_N().
*/
function menu_token_update_7000(&$sandbox) {
$schema['menu_token'] = array(
'description' => t('Menu token properties'),
'fields' => array (
'mlid' => array (
'description' => t('The menu link {menu_links}.mlid'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'link_path' => array (
'description' => t('The actual path with tokens'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('mlid'),
);
db_create_table('menu_token', $schema['menu_token']);
}
/**
* Implements hook_update_N().
*/
function menu_token_update_7001(&$sandbox) {
// Initializing sandbox variables.
if (!isset($sandbox['progress'])) {
// Preparing array of menu items for batch insert.
foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
$sandbox['items'][] = array('mlid' => $mlid, 'link_path' => $link_path);
}
$sandbox['progress'] = 0;
$sandbox['max'] = count($sandbox['items']);
}
// Insert current record.
if (!empty($sandbox['max'])) {
db_merge('menu_token')
->key(array('mlid' => $sandbox['items'][$sandbox['progress']]['mlid']))
->fields(array('link_path' => $sandbox['items'][$sandbox['progress']]['link_path']))
->execute();
}
$sandbox['progress']++;
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
// Delete variable in case of all queries were executed
if ($sandbox['#finished']) {
variable_del('menu_token_enabled');
}
}
/**
* Implements hook_update_N().
*/
function menu_token_update_7002(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
if (db_table_exists('menu_token')) {
$sandbox['max'] = db_select('menu_token', 'mt')->countQuery()->execute()->fetchField();
}
}
if (!empty($sandbox['max'])) {
$tokens = db_select('menu_token', 'mt')
->fields('mt', array('mlid', 'link_path'))
->orderBy('mlid')
->range($sandbox['progress'], 10)
->execute()
->fetchAllKeyed();
if (!empty($tokens)) {
$links = db_select('menu_links', 'ml')
->fields('ml', array('mlid', 'options'))
->condition('mlid', array_keys($tokens))
->execute()
->fetchAllKeyed();
foreach ($links as $mlid => $options) {
$options = unserialize($options);
$options['menu_token_link_path'] = $tokens[$mlid];
$options['menu_token_link_data'] = array();
db_update('menu_links')
->fields(array('options' => serialize($options)))
->condition('mlid', $mlid)
->execute();
}
}
$sandbox['progress'] += 10;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
if ($sandbox['#finished'] >= 1) {
$sandbox['#finished'] = 1;
// Drop the deprecated menu_token table if it exists.
if (db_table_exists('menu_token')) {
db_drop_table('menu_token');
}
return t('The Menu Token module has been updated successfully.');
}
}
/**
* Drop the deprecated menu_token table if it exists.
*/
function menu_token_update_7003() {
if (db_table_exists('menu_token')) {
db_drop_table('menu_token');
}
}
/**
* Migrate menu items link path, from '<front>' to 'menutoken/[uuid]'.
*/
function menu_token_update_7004() {
$result = db_select('menu_links', 'm')
->fields('m', array('mlid', 'options'))
->condition('link_path', '<front>')
->execute();
foreach ($result as $menu_link) {
$options = unserialize($menu_link->options);
if (isset($options['menu_token_data'])) {
db_update('menu_links')
->fields(array(
'link_path' => 'menutoken/' . uniqid(),
'router_path' => 'menutoken/%',
))
->condition('mlid', $menu_link->mlid)
->execute();
}
}
}
/**
* Implements hook_uninstall().
*/
function menu_token_uninstall() {
$result = db_select('variable', 'v')
->fields('v', array('name'))
->condition('name', 'menu_token_%', '=')
->execute();
foreach ($result as $row) {
variable_del($row->name);
}
}