<?php
/**
 * Install / Update functions for Entity menu links
 */

/**
 * Implements hook_install().
 */
function entity_menu_links_install() {
  $table = 'menu_links_revision';
  $schema = drupal_get_schema($table);
  db_create_table($table, $schema);

  $schema = drupal_get_schema('menu_links');
  $fields = $schema['fields'];

  if (!db_field_exists('menu_links', 'vid')) {
    db_add_field('menu_links', 'vid', $fields['vid']);
  }
  if (!db_field_exists('menu_links', 'uuid')) {
    db_add_field('menu_links', 'uuid', $fields['uuid']);
  }
  entity_menu_links_install_revisions();
  uuid_sync_all();

  // Ensure entity menu links runs after i18n and entity_translation.
  db_update('system')
    ->fields(array('weight' => 15))
    ->condition('name', 'entity_menu_links')
    ->execute();
}

/**
 * Install initial revisions of existing menu links.
 */
function entity_menu_links_install_revisions() {
  $links = drupal_get_schema('menu_links');
  $revisions = drupal_get_schema('menu_links_revision');

  $rev_fields = $revisions['fields'];
  unset($rev_fields['vuuid'], $rev_fields['timestamp'], $rev_fields['vid']);

  $links_fields = $links['fields'];
  unset($links_fields['uuid'], $links_fields['vid']);

  $menu_links_revision_fields = implode(',', array_keys($rev_fields)) . ', timestamp';
  $menu_links_fields = implode(',', array_keys($links_fields)) . ', ' . time();

  $query = "INSERT INTO {menu_links_revision} ($menu_links_revision_fields) SELECT $menu_links_fields FROM {menu_links} WHERE module = :module";
  db_query($query, array(':module' => 'menu'));

  $query = 'UPDATE {menu_links} SET vid = (SELECT vid FROM {menu_links_revision} WHERE {menu_links}.mlid = mlid) WHERE module = :module';
  db_query($query, array(':module' => 'menu'));
}

/**
 * Implements hook_install().
 */
function entity_menu_links_uninstall() {
  db_drop_table('menu_links_revision');
  db_drop_index('menu_links', 'menu_links_revision');
  db_drop_field('menu_links', 'vid');
  db_drop_field('menu_links', 'uuid');
}

/**
 * Implements module_implements_alter().
 */
function entity_menu_links_module_implements_alter(&$implementations, $hook) {
  // Force our implementation of schema_alter to come last
  // to ensure our menu_links_revision schema includes any
  // alterations made by other modules (eg. i18n_menu)
  if ($hook == 'schema_alter') {
    unset($implementations['entity_menu_links']);
    $implementations['entity_menu_links'] = '';
  }
}

/**
 * Implements hook_schema_alter().
 */
function entity_menu_links_schema_alter(&$schema = array()) {
  $schema['menu_links']['fields']['vid'] = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'description' => 'Revision id.',
  );
  $schema['menu_links']['fields']['uuid'] = array(
    'type' => 'char',
    'length' => 36,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The Universally Unique Identifier.',
  );

  $schema['menu_links_revision'] = $schema['menu_links'];
  $schema['menu_links_revision']['module'] = 'entity_menu_links';
  $schema['menu_links_revision']['name'] = 'menu_links_revision';

  $schema['menu_links_revision']['fields']['mlid'] = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  $schema['menu_links_revision']['fields']['vid'] = array(
    'type' => 'serial',
    'unsigned' => TRUE,
    'not null' => TRUE,
  );
  $schema['menu_links_revision']['fields']['timestamp'] = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  $schema['menu_links_revision']['fields']['vuuid'] = $schema['menu_links_revision']['fields']['uuid'];
  unset($schema['menu_links_revision']['fields']['uuid']);

  $schema['menu_links_revision']['primary key'] = array('vid');
  $schema['menu_links_revision']['foreign keys'] = array(
    'versioned_menu_link' => array(
      'table' => 'menu_links',
      'columns' => array(
        'mlid' => 'mlid',
      ),
    ),
  );
}

/**
 * Set module weight.
 */
function entity_menu_links_update_7101() {
  // Ensure entity menu links runs after i18n and entity_translation.
  db_update('system')
    ->fields(array('weight' => 15))
    ->condition('name', 'entity_menu_links')
    ->execute();
}
