You are here

profile_module_manager.module in Profile Module Manager 7.2

Same filename and directory in other branches
  1. 7 profile_module_manager.module

Alters grouping in admin/modules using hook_system_info_alter

File

profile_module_manager.module
View source
<?php

/**
 * @file
 * Alters grouping in admin/modules using hook_system_info_alter
 */

/**
 * Implements hook_page_alter().
 *
 * @param $page
 */
function profile_module_manager_page_alter(&$page) {
  drupal_add_css(drupal_get_path('module', 'profile_module_manager') . '/css/profile-module-manager.css');
}

/**
 * Implements hook_menu().
 *
 * @return mixed
 */
function profile_module_manager_menu() {

  // Admin/settings ui provided by express_admin or custom solution.
  $items['admin/settings/site-configuration/bundles'] = array(
    'title' => 'Enable Bundles',
    'description' => 'Enable Add-on Bundles for additional functionality',
    'page callback' => 'drupal_goto',
    'page arguments' => array(
      'admin/settings/bundles/list',
    ),
    'access arguments' => array(
      'enable module bundles',
    ),
    'weight' => 50,
  );
  if (variable_get('profile_module_manager_disable_enabling', 0) == 0) {
    $items['admin/settings/bundles/list'] = array(
      'title' => 'Configure Bundles',
      'description' => 'List of bundles.',
      'page callback' => 'profile_module_manager_bundle_list',
      'file' => 'profile_module_manager.admin.inc',
      'access arguments' => array(
        'enable module bundles',
      ),
    );
    $items['admin/settings/bundles/list/core'] = array(
      'title' => 'Core',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10,
    );
    $items['admin/settings/bundles/list/admin'] = array(
      'title' => 'Admin',
      'type' => MENU_LOCAL_TASK,
      'description' => 'Add and request additional bundles',
      'page callback' => 'profile_module_manager_admin_page',
      'page arguments' => array(
        1,
      ),
      'access arguments' => array(
        'enable admin bundles',
      ),
      'weight' => 100,
    );
    $items['admin/settings/bundles/list/enable/%'] = array(
      'title' => 'Enable Bundle',
      'description' => 'Enable a bundle.',
      'page callback' => 'profile_module_manager_bundle_enable',
      'page arguments' => array(
        5,
      ),
      'file' => 'profile_module_manager.admin.inc',
      'access arguments' => array(
        'enable module bundles',
      ),
    );
    $items['admin/settings/bundles/list/confirm/%'] = array(
      'title' => 'Confirm Enabling Bundle',
      'description' => 'Enable a bundle.',
      'page callback' => 'profile_module_manager_bundle_confirm',
      'page arguments' => array(
        5,
      ),
      'file' => 'profile_module_manager.admin.inc',
      'access arguments' => array(
        'enable module bundles',
      ),
    );
  }
  $items['admin/config/system/module-manager'] = array(
    'title' => 'Profile Module Manager',
    'description' => 'Configure Module Manager',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'profile_module_manager_admin_settings',
    ),
    'file' => 'profile_module_manager.admin.inc',
    'access arguments' => array(
      'configure profile module manager',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 *
 * @return array
 */
function profile_module_manager_permission() {
  return array(
    'enable module bundles' => array(
      'title' => t('Enable Module Bundles'),
      'description' => t('Enable modules in bundles without administer modules permission.'),
    ),
    'enable admin bundles' => array(
      'title' => t('Enable Admin Bundles'),
      'description' => t('Enable modules in admin bundles without administer modules permission.'),
    ),
    'configure profile module manager' => array(
      'title' => t('Configure Profile Module Manager'),
      'description' => t('Manage hidden bundles and access to UI.'),
    ),
  );
}

/**
 * Implements hook_system_info_alter().
 *
 * @param $info
 * @param $file
 * @param $type
 */
function profile_module_manager_system_info_alter(&$info, $file, $type) {
  if (!variable_get('profile_module_manager_disable_ui_lock', 0)) {
    require_once DRUPAL_ROOT . '/includes/install.inc';
    $profile = install_profile_info(drupal_get_profile());
    if ($type == 'module' && in_array($file->name, $profile['dependencies'])) {
      $info['required'] = TRUE;
    }
    if ($type == 'module' && $file->name == variable_get('express_core_version', '')) {
      $info['required'] = TRUE;
    }
  }
}

/**
 * Returns an array of projects that are enabled, but not ignored.
 *
 * @return array
 */
function profile_module_manager_is_config_ideal() {
  $reality = module_list();
  $ideal = profile_module_manager_build_ideal();

  // Does the reality match the ideal?
  $diff1 = array_diff_assoc($reality, $ideal);
  $diff2 = array_diff_assoc($ideal, $reality);
  $diff = array_merge($diff1, $diff2);
  return $diff;
}

/**
 * Modifies profile_module_manager_ignore array.
 *
 * @param $new_ignore
 */
function profile_module_manager_add_to_ignore($new_ignore) {
  $current_ignore = variable_get('profile_module_manager_ignore', array());
  variable_set('profile_module_manager_ignore', array_merge($current_ignore, $new_ignore));
}

/**
 * Returns an array of bundles.
 *
 * @param string $status
 * @return mixed
 */
function profile_module_manager_get_bundles($status = 'all') {
  if ($status == 'all') {
    $bundles = db_query("SELECT name, filename FROM {system} WHERE type = 'module' ORDER BY weight ASC, name ASC")
      ->fetchAllAssoc('name');
  }
  if ($status == 'enabled') {
    $bundles = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, name ASC")
      ->fetchAllAssoc('name');
  }

  // Check for _bundle in the machine name, but also look for something in the info file.
  // This will avoid false positives from contrib?
  foreach ($bundles as $key => $bundle) {
    $info_file = str_replace(".module", ".info", $bundle->filename);
    $info = drupal_parse_info_file($info_file);
    $bundle_info[$key] = $info;
    if (!isset($info['bundle_group']) && !strpos($bundle->name, '_bundle')) {
      unset($bundles[$key]);
    }
  }
  return $bundles;
}

/**
 * Returns an array of core modules.
 *
 * @param string $status
 * @return mixed
 */
function profile_module_manager_get_core_modules($status = 'all') {
  if ($status == 'all') {
    $core_modules = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND name LIKE '%_core' ORDER BY weight ASC, name ASC")
      ->fetchAllAssoc('name');
  }
  if ($status == 'enabled') {
    $core_modules = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 AND name LIKE '%_core' ORDER BY weight ASC, name ASC")
      ->fetchAllAssoc('name');
  }
  return $core_modules;
}

/**
 * Returns an array of core modules.
 *
 * @return array
 */
function profile_module_manager_build_ideal() {
  $new_ideal = array();
  require_once DRUPAL_ROOT . '/includes/install.inc';
  $profile_name = drupal_get_profile();
  $profile = install_profile_info($profile_name);

  // Start with the profile
  $ideal = isset($profile['dependencies']) ? $profile['dependencies'] : array();
  $ignore = isset($profile['optional-dependencies']) ? $profile['optional-dependencies'] : array();

  // Add core dependencies to ideal and ignore arrays.
  $core_modules = profile_module_manager_get_core_modules('enabled');
  foreach ($core_modules as $core) {

    // could just replace .module w/ .info from $bundle->filename
    // which is faster?
    $path = drupal_get_path('module', $core->name) . '/' . $core->name . '.info';
    $info = drupal_parse_info_file($path);
    if (isset($info['dependencies'])) {
      $ideal = array_merge($ideal, $info['dependencies']);
    }
    if (isset($info['optional-dependencies'])) {
      $ignore = array_merge($ignore, $info['optional-dependencies']);
    }

    // and the core itself
    $ideal[] = $core->name;
  }

  // Add enabled bundle dependencies to ideal and ignore arrays.
  $bundles = profile_module_manager_get_bundles('enabled');
  foreach ($bundles as $bundle) {

    // could just replace .module w/ .info from $bundle->filename
    // which is faster?
    $path = drupal_get_path('module', $bundle->name) . '/' . $bundle->name . '.info';
    $info = drupal_parse_info_file($path);
    if (isset($info['dependencies'])) {
      $ideal = array_merge($ideal, $info['dependencies']);
    }
    if (isset($info['optional-dependencies'])) {
      $ignore = array_merge($ignore, $info['optional-dependencies']);
    }

    // and the bundle itself
    $ideal[] = $bundle->name;
  }

  // rebuild ideal so keys = value and ignore optional dependencies
  foreach ($ideal as $item) {
    $new_ideal[$item] = $item;
  }

  // add any ignored project to new_ideal
  foreach ($ignore as $item) {
    if (module_exists($item)) {
      $new_ideal[$item] = $item;
    }
  }

  // add the profile itself to the list
  $new_ideal[$profile_name] = $profile_name;
  return $new_ideal;
}
function profile_module_manager_process_batch($modules, &$context) {

  // Enable modules.
  module_enable($modules);

  // Pass bundle name to batch finished function.
  foreach ($modules as $module) {
    if (strpos($module, '_bundle')) {
      $context['results']['bundle'] = $module;
    }
  }
}
function profile_module_manager_batch_finished($success, $results, $operations) {
  if ($success) {

    // Get bundle name from $results and print message.
    $bundle_name = $results['bundle'];
    $bundle_path = drupal_get_path('module', $bundle_name);
    $bundle = drupal_parse_info_file($bundle_path . '/' . $bundle_name . '.info');
    drupal_set_message('The ' . $bundle['name'] . ' Bundle has been successfully enabled on your site. You may now begin using this functionality and go back to editing your site.');

    // Log bundle enable time.
    $start_time = variable_get('profile_module_manager_enable_timer', 0);
    $end_time = microtime(TRUE);
    $total_time = $end_time - $start_time;
    watchdog('profile_module_manager', '!bundle enable time: !time seconds.', array(
      '!bundle' => $bundle['name'],
      '!time' => $total_time,
    ));
  }
  else {
    _revert_bundle_on_error();
  }
}

/**
 * Function to handle bundle enabling errors.
 */
function _revert_bundle_on_error() {
}

/**
 * Implements hook_modules_installed().
 * Run functions after bundles are enabled.
 */
function profile_module_manager_modules_enabled($modules) {
  foreach ($modules as $module) {

    // Only run these functions if module name includes _bundle
    if (strpos($module, '_bundle') !== false) {

      // Get the info file.
      $path = drupal_get_path('module', $module) . '/' . $module . '.info';
      $info = drupal_parse_info_file($path);

      // Rebuild node types.
      node_types_rebuild();

      // Clear caches.
      if (isset($info['bundle_cache_clear']) && $info['bundle_cache_clear']) {
        drupal_flush_all_caches();
      }
      if (isset($info['bundle_context_clear']) && $info['bundle_context_clear']) {
        drupal_flush_all_caches();
      }
    }
  }
}

/**
 * Implements hook_theme().
 *
 * @param $existing
 * @param $type
 * @param $theme
 * @param $path
 * @return array
 */
function profile_module_manager_theme(&$existing, $type, $theme, $path) {
  $registry = array();
  $template_dir = drupal_get_path('module', 'profile_module_manager') . '/templates';
  $registry['profile_module_manager_bundle_page'] = array(
    'template' => 'profile-module-manager-bundle-page',
    'path' => $template_dir,
    'render element' => 'elements',
  );
  $registry['profile_module_manager_bundle_group'] = array(
    'template' => 'profile-module-manager-bundle-group',
    'path' => $template_dir,
    'render element' => 'elements',
  );
  $registry['profile_module_manager_bundle'] = array(
    'template' => 'profile-module-manager-bundle',
    'path' => $template_dir,
    'render element' => 'elements',
  );
  $registry['profile_module_manager_bundle_actions_disabled'] = array(
    'template' => 'profile-module-manager-bundle-actions-disabled',
    'path' => $template_dir,
    'render element' => 'elements',
  );
  $registry['profile_module_manager_bundle_actions_enabled'] = array(
    'template' => 'profile-module-manager-bundle-actions-enabled',
    'path' => $template_dir,
    'render element' => 'elements',
  );
  return $registry;
}

/**
 * Builds out the admin bundles page.
 *
 * @return string
 */
function profile_module_manager_admin_page() {
  $page = '';
  $mod_enabled = array();

  // Get the bundles' info array.
  $bundles = profile_module_manager_get_bundles($status = 'all');
  foreach ($bundles as $key => $bundle) {
    $info_file = str_replace(".module", ".info", $bundle->filename);
    $info = drupal_parse_info_file($info_file);
    $bundle_info[$key] = $info;
    $bundle_machine_name = $bundle->name;
    $bundle_name = $info['name'];
    $bundle_group = isset($info['bundle_group']) ? $info['bundle_group'] : '';
    $bundle_description = isset($info['description']) ? $info['description'] : '';
    if (strpos($bundle->name, '_admin_bundle') || isset($bundle_group) && $bundle_group == 'admin_bundles') {
      $page .= '<div class="bundle-enable" id="edit-' . $bundle_machine_name . '">';
      $page .= '<h2>' . $bundle_name . '</h2>';
      $page .= '<p>' . $bundle_description . '</p>';
      foreach ($bundle_info as $key => $value) {
        $enabled = module_exists($key) ? 1 : 0;
      }
      foreach ($info as $key => $value) {

        //$page .= '<p>' . $key . ': ' . $value . '</p>';
      }
      $action_vars = array();
      if ($enabled) {
        $page .= theme('profile_module_manager_bundle_actions_enabled', array(
          'actions' => $action_vars,
        ));
      }
      else {
        $action_vars['enable_url'] = 'admin/settings/bundles/list/confirm/' . $bundle->name;
        if (isset($info['project_demo_url'])) {
          $action_vars['demo_url'] = $info['project_demo_url'];
        }
        $page .= theme('profile_module_manager_bundle_actions_disabled', $action_vars);
      }
      $page .= '</div>';
    }
  }
  $page .= '<div class="enabled-modules-admin"><h2>Enabled Bundles</h2>';
  foreach ($bundles as $bundle) {
    if (module_exists($bundle->name)) {
      $page .= '<span>' . $bundle->name . '</span><br>';
    }
  }
  $page .= '</div>';
  return $page;
}

Functions

Namesort descending Description
profile_module_manager_add_to_ignore Modifies profile_module_manager_ignore array.
profile_module_manager_admin_page Builds out the admin bundles page.
profile_module_manager_batch_finished
profile_module_manager_build_ideal Returns an array of core modules.
profile_module_manager_get_bundles Returns an array of bundles.
profile_module_manager_get_core_modules Returns an array of core modules.
profile_module_manager_is_config_ideal Returns an array of projects that are enabled, but not ignored.
profile_module_manager_menu Implements hook_menu().
profile_module_manager_modules_enabled Implements hook_modules_installed(). Run functions after bundles are enabled.
profile_module_manager_page_alter Implements hook_page_alter().
profile_module_manager_permission Implements hook_permission().
profile_module_manager_process_batch
profile_module_manager_system_info_alter Implements hook_system_info_alter().
profile_module_manager_theme Implements hook_theme().
_revert_bundle_on_error Function to handle bundle enabling errors.