You are here

og_menu.install in Organic Groups Menu (OG Menu) 7.3

Install, update and uninstall functions for the og_menu module.

File

og_menu.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the og_menu module.
 */

/**
 * Implements hook_schema().
 */
function og_menu_schema() {
  $schema = array();
  $schema['og_menu'] = array(
    'description' => 'Stores relationships between organic groups and their custom menus.',
    'fields' => array(
      'menu_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'gid' => array(
        'description' => "The group's unique ID.",
        'type' => 'int',
        'size' => 'normal',
        'not null' => TRUE,
      ),
      'group_type' => array(
        'description' => "The group's entity type (e.g., node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'description' => "The menu weight for each group menu.",
        'type' => 'int',
        'length' => '11',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'menu_name',
    ),
    'indexes' => array(
      'gid_group_type_weight' => array(
        'gid',
        'group_type',
        'weight',
      ),
    ),
    'foreign keys' => array(
      'menu_custom' => array(
        'table' => 'menu_custom',
        'columns' => array(
          'menu_name' => 'menu_name',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function og_menu_uninstall() {

  // List custom vars.
  $vars = array(
    'og_menu_assignment',
    'og_menu_block_links',
    'og_menu_max_menus_per_group',
    'og_menu_show_blocks',
    'og_menu_show_homebox',
    'og_menu_show_menuposition',
    'og_menu_show_nodetype',
    'og_menu_context_limit',
    'og_menu_hide_create_option',
    'og_menu_create_by_default',
    'og_menu_hide_help',
  );

  // List all og_menu related node type settings vars.
  $node_types = node_type_get_types();
  foreach ($node_types as $type) {
    $vars[] = 'og_menu_enable_' . $type->type;
  }

  // Finally, delete all the vars we listed.
  foreach ($vars as $var) {
    variable_del($var);
  }

  // Delete custom menus.
  $result = db_select('og_menu', 'm')
    ->fields('m', array(
    'menu_name',
  ))
    ->execute();
  while ($record = $result
    ->fetchAssoc()) {
    if ($menu = menu_load($record['menu_name'])) {
      menu_delete($menu);
    }
  }
}

/**
 * Update og_menu schema to store group type and use etid instead of deprecated
 * gid.
 */
function og_menu_update_7300(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    db_drop_primary_key('og_menu');
    db_query('ALTER TABLE {og_menu} MODIFY COLUMN menu_name varchar(32) NOT NULL DEFAULT \'\' FIRST');
    db_change_field('og_menu', 'gid', 'gid', array(
      'description' => "The group's unique ID.",
      'type' => 'int',
      'size' => 'normal',
      'not null' => TRUE,
    ));

    // Check for safety, some people had problems with this.
    if (!db_field_exists('og_menu', 'group_type')) {
      db_add_field('og_menu', 'group_type', array(
        'description' => "The group's entity type (e.g., node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => 'node',
      ));
    }
    db_field_set_default('og_menu', 'group_type', '');
    db_add_primary_key('og_menu', array(
      'menu_name',
    ));
    $sandbox['progress'] = 0;
    $sandbox['limit'] = 20;
    $sandbox['max'] = 0;
    $results = db_query('SELECT COUNT(*) FROM {og_menu}')
      ->fetchField();
    if ($results) {
      $sandbox['max'] = $results - 1;
    }

    // Upgrade legacy og table.
    if (db_table_exists('og')) {

      // Since this is a batch update based on gid, cannot update column directly.
      // Create a temporary column.
      db_add_field('og_menu', 'etid', array(
        'description' => 'Temporary column for gid transfer to etid',
        'type' => 'int',
        'size' => 'normal',
        'not null' => TRUE,
        'initial' => 0,
      ));

      // Cannot use a join in db_update().
      $groups = db_query('SELECT og.gid, og.etid FROM {og} as og, {og_menu} as og_menu WHERE og.gid = og_menu.gid and og_menu.etid = 0 ORDER BY og_menu.gid LIMIT ' . $sandbox['limit'] . '');
      foreach ($groups as $group) {
        db_update('og_menu')
          ->fields(array(
          'etid' => $group->etid,
        ))
          ->condition('gid', $group->gid)
          ->execute();
        $sandbox['progress']++;
      }

      // Replace gid contents when complete and remove temporary column.
      if ($sandbox['#finished'] >= 1) {

        // Only update if migrating content from legacy og table.
        db_update('og_menu')
          ->expression('gid', 'etid')
          ->execute();
        db_drop_field('og_menu', 'etid');
      }

      // Calculate progress.
      $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
    }
    else {
      $sandbox['#finished'] = TRUE;
    }
  }
}

/**
 * Enable OG Menu functionality for all group content types (backwards
 * compatibility).
 */
function og_menu_update_7301(&$sandbox) {
  $node_types = node_type_get_types();
  $group_content_types = array();
  foreach ($node_types as $type) {
    if (og_is_group_content_type('node', $type->type)) {
      variable_set('og_menu_enable_' . $type->type, TRUE);
      $group_content_types[$type->type] = $type->name;
    }
  }
  if (!empty($group_content_types)) {
    drupal_set_message(t("For backwards compatibility reasons, this update has\n      enabled OG Menu functionality for the following group content types:\n      %types. You can manually disable this on the !ctypes settings page.", array(
      '%types' => implode(', ', $group_content_types),
      '!ctypes' => l(t('content types'), 'admin/structure/types'),
    )));
  }
}

/**
 * Enable OG Menu Default Links module if needed (backwards compatibility).
 */
function og_menu_update_7302(&$sandbox) {
  $bundles = og_get_all_group_bundle();
  $enable = FALSE;

  // Check if a setting exists.
  if ($bundles) {
    foreach ($bundles as $entity_type => $entity_bundles) {
      foreach ($entity_bundles as $bundle => $bundle_label) {
        $group_type = strtolower('_' . $bundle);
        $temp = variable_get('og_menu_default_links_' . $entity_type . $group_type, FALSE);
        if (!empty($temp)) {
          $enable = TRUE;
        }
      }
    }
  }
  if ($enable) {
    module_enable(array(
      'og_menu_default_links',
    ), TRUE);
  }
}

/**
 * Clear menu cache to enable admin page changes.
 */
function og_menu_update_7303(&$sandbox) {
  menu_cache_clear_all();
}

/**
 * Clear menu cache to ensure changes in hook_menu().
 */
function og_menu_update_7304(&$sandbox) {

  // No need to clear menu cache her, because all caches will be cleared anyway
  // while finishing update process in update_finished().
}

/**
 * Add weight field to og_menu table.
 */
function og_menu_update_7305(&$sandbox) {
  if (!db_field_exists('og_menu', 'weight')) {
    db_add_field('og_menu', 'weight', array(
      'description' => "The menu weight for each group menu.",
      'type' => 'int',
      'length' => '11',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
}

/**
 * Add og_menu.(gid, group_type, weight) index.
 */
function og_menu_update_7306() {
  if (!db_index_exists('og_menu', 'gid_group_type_weight')) {
    db_add_index('og_menu', 'gid_group_type_weight', array(
      'gid',
      'group_type',
      'weight',
    ));
  }
}

/**
 * Clear the menu cache so the renamed items will be picked up.
 */
function og_menu_update_7307() {
  variable_set('menu_rebuild_needed', TRUE);
}

/**
 * Clear cache_menu so the renamed menu item will be picked up.
 */
function og_menu_update_7308() {
  cache_clear_all(NULL, 'cache_menu');
}

Functions

Namesort descending Description
og_menu_schema Implements hook_schema().
og_menu_uninstall Implements hook_uninstall().
og_menu_update_7300 Update og_menu schema to store group type and use etid instead of deprecated gid.
og_menu_update_7301 Enable OG Menu functionality for all group content types (backwards compatibility).
og_menu_update_7302 Enable OG Menu Default Links module if needed (backwards compatibility).
og_menu_update_7303 Clear menu cache to enable admin page changes.
og_menu_update_7304 Clear menu cache to ensure changes in hook_menu().
og_menu_update_7305 Add weight field to og_menu table.
og_menu_update_7306 Add og_menu.(gid, group_type, weight) index.
og_menu_update_7307 Clear the menu cache so the renamed items will be picked up.
og_menu_update_7308 Clear cache_menu so the renamed menu item will be picked up.