You are here

menu_token.install in Menu Token 7

Install file for menu_token module.

File

menu_token.install
View source
<?php

/**
 * @file
 * Install file for menu_token module.
 */

/**
 * Implements hook_schema().
 */
function menu_token_schema() {
  $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',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function menu_token_install() {
  db_update('system')
    ->fields(array(
    'weight' => 10,
  ))
    ->condition('type', 'module', '=')
    ->condition('name', 'menu_token', '=')
    ->execute();
}

/**
 * Implements hook_update_N().
 */
function menu_token_update_7000(&$sandbox) {
  if (!db_table_exists('menu_token')) {
    $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_update_N().
 * Update Weight to 10
 */
function menu_token_update_7005() {
  db_update('system')
    ->fields(array(
    'weight' => 10,
  ))
    ->condition('type', 'module', '=')
    ->condition('name', 'menu_token', '=')
    ->execute();
}

/**
 * Rebuild registry to load included file.
 */
function menu_token_update_7006() {
  registry_rebuild();
}

/**
 * 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);
  }
}

Functions

Namesort descending Description
menu_token_install Implements hook_install().
menu_token_schema Implements hook_schema().
menu_token_uninstall Implements hook_uninstall().
menu_token_update_7000 Implements hook_update_N().
menu_token_update_7001 Implements hook_update_N().
menu_token_update_7002 Implements hook_update_N().
menu_token_update_7003 Drop the deprecated menu_token table if it exists.
menu_token_update_7004 Migrate menu items link path, from '<front>' to 'menutoken/[uuid]'.
menu_token_update_7005 Implements hook_update_N(). Update Weight to 10
menu_token_update_7006 Rebuild registry to load included file.