You are here

function simplified_modules_system_modules_submit in Simplified Modules 7

Custom submit handler for the system_modules() admin page.

1 string reference to 'simplified_modules_system_modules_submit'
simplified_modules_form_system_modules_alter in ./simplified_modules.module
Implements hook_form_FORM_ID_alter().

File

./simplified_modules.module, line 68
Simplifies the modules page by allowing related modules to be grouped under a single checkbox.

Code

function simplified_modules_system_modules_submit($form, &$form_state) {

  // We only act on the initial form submission; if we are coming from the
  // secondary confirmation form, there is nothing to do here because we have
  // already added our modules to the list on the first pass through.
  if (isset($form_state['storage']['modules'])) {
    return;
  }

  // Define preliminary variables.
  $hidden_submodules = simplified_modules_hidden_submodules();
  $hidden_dependencies = simplified_modules_hidden_dependencies();
  $module_dependencies = _simplified_modules_module_build_dependencies();
  $modules =& $form_state['values']['modules'];

  // Initialize the status (enabled or disabled) we expect each module to have
  // when the form submission is processed.
  $submitted_module_status = array();
  foreach ($modules as $group) {

    // If the module appears on the form, use the status that was submitted.
    foreach ($group as $module => $data) {
      $submitted_module_status[$module] = $data['enable'];
    }
  }
  foreach (array_keys($module_dependencies) as $module) {

    // Otherwise, use the module's current status.
    if (!isset($submitted_module_status[$module])) {
      $submitted_module_status[$module] = module_exists($module);
    }
  }

  // Initialize the final status we expect each module to have after the form
  // submission is finished.
  $final_module_status = _simplified_modules_expected_final_module_status($submitted_module_status, $module_dependencies);

  // Enable any of our hidden submodules when the form submission will result
  // in all the modules it requires being enabled too.
  foreach ($hidden_submodules as $submodule) {
    if (!empty($module_dependencies[$submodule]->requires) && empty($submitted_module_status[$submodule])) {
      $required_by_this_submodule_but_will_be_disabled = array_diff_key($module_dependencies[$submodule]->requires, array_filter($final_module_status));
      if (empty($required_by_this_submodule_but_will_be_disabled)) {
        $final_module_status = _simplified_modules_set_expected_status($submodule, TRUE, $submitted_module_status, $modules, $module_dependencies);
      }
    }
  }

  // Enable any of our hidden dependencies whenever the form submission will
  // result in at least one module that requires it being enabled too.
  foreach ($hidden_dependencies as $dependency) {
    if (isset($submitted_module_status[$dependency]) && !$submitted_module_status[$dependency]) {
      $requires_this_module_and_will_be_enabled = array_intersect_key($module_dependencies[$dependency]->required_by, array_filter($final_module_status));
      if (!empty($requires_this_module_and_will_be_enabled)) {
        $final_module_status = _simplified_modules_set_expected_status($dependency, TRUE, $submitted_module_status, $modules, $module_dependencies);
      }
    }
  }
  do {
    $changed = FALSE;

    // For any module that is scheduled to be disabled, disable its hidden
    // dependents or dependencies as appropriate.
    foreach ($submitted_module_status as $module => $status) {
      if (!$status) {

        // Any hidden submodules that depend on this module must be disabled
        // along with it.
        $hidden_submodules_to_disable = array_intersect($hidden_submodules, array_keys($module_dependencies[$module]->required_by));
        foreach ($hidden_submodules_to_disable as $submodule) {
          if (!empty($submitted_module_status[$submodule])) {
            $final_module_status = _simplified_modules_set_expected_status($submodule, FALSE, $submitted_module_status, $modules, $module_dependencies);
            $changed = TRUE;
          }
        }

        // Any hidden dependencies of this module should be disabled if this
        // was the last module that depended on it, or if the only remaining
        // modules that depend on it are hidden submodules. (In the latter
        // case, we mark the hidden dependency as disabled here, and the hidden
        // submodule will eventually be marked as disabled via the code above.)
        foreach (array_keys($module_dependencies[$module]->requires) as $dependency) {
          if (!empty($submitted_module_status[$dependency]) && in_array($dependency, $hidden_dependencies)) {
            $enabled_submodules_of_dependency = array_intersect_key($module_dependencies[$dependency]->required_by, array_filter($final_module_status));
            if (empty($enabled_submodules_of_dependency) || !array_diff(array_keys($enabled_submodules_of_dependency), $hidden_submodules)) {
              $final_module_status = _simplified_modules_set_expected_status($dependency, FALSE, $submitted_module_status, $modules, $module_dependencies);
              $changed = TRUE;
            }
          }
        }
      }
    }

    // We just looped through all disabled modules in $submitted_module_status,
    // so if that list was changed in any way above, we need to go through the
    // loop again to process the new ones.
  } while ($changed);
}