You are here

function system_patterns_validate in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_components/components/system.inc \system_patterns_validate()

File

patterns_components/components/system.inc, line 182

Code

function system_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  if ($tag == 'theme') {

    // TODO: do this the Drupal7.x way
    $themes = system_rebuild_theme_data();
    if (!array_key_exists($data['theme_default'], $themes)) {
      $status = PATTERNS_ERR;
      $msg = t('%theme is not a valid theme.', array(
        '%theme' => $data['theme_default'],
      ));
    }
  }
  elseif ($tag == 'modules') {
    if (!isset($modules_info) || !is_array($modules_info)) {
      $modules_info = system_rebuild_module_data();

      // list of available modules.
    }
    $modules = module_list();

    // List of enabled modules.
    $delete = $action === PATTERNS_DELETE;
    for ($i = 0; isset($data[$i]) && ($item = $data[$i]); $i++) {
      $module = $item['value'];

      // Ensure a module can be disabled safely.
      if ($delete) {

        // TODO: Move this out of the loop?
        if (array_key_exists($module, $modules_info)) {

          // Module is available.
          if (array_key_exists($module, $modules)) {

            // Module is enabled.

            /*
            * TODO: no more dependents field in Drupal7.x
            foreach ((array)$modules_info[$module]->info['dependents'] as $dependent) { // Dependency check.
            if (array_key_exists($dependent, $modules)) {
            $remove[] = $i;
            drupal_set_message(t('Warning: Could not disable %module because other modules depend on it.', array('%module' => $module)), "warning");
            break;
            }
            }
            */
          }
          else {
            $remove[] = $i;
            $status = PATTERNS_WARN;
            $msg .= t('Warning: Did not disable %module because it is already disabled.', array(
              '%module' => $module,
            )) . '<br/>';
          }
        }
        else {
          $remove[] = $i;
          $status = PATTERNS_WARN;
          $msg .= t('Warning: Could not disable %module because it is missing.', array(
            '%module' => $module,
          )) . '<br/>';
        }
      }
      else {
        if (!array_key_exists($module, $modules)) {

          // Module not yet enabled.
          if (!array_key_exists($module, $modules_info)) {

            // Module does not exist.
            $required[] = $module;
          }
          else {
            foreach ((array) $modules_info[$module]->info['dependencies'] as $dependency) {
              if (!array_key_exists($dependency, $modules) && !array_key_exists($dependency, $modules_info)) {
                $required[] = $dependency;
              }
            }
          }
          if (!empty($required)) {
            $status = PATTERNS_ERR;
            $msg .= t('%module can not be installed because the module or its dependencies are missing. Please download them and try again.', array(
              '%module' => $module,
            )) . t('!title%dependencies', array(
              '!title' => '<br/><b>' . t('Missing module(s): ') . '</b>',
              '%dependencies' => implode(', ', $required),
            ));
          }
        }
        else {
          $status = PATTERNS_WARN;
          $msg .= t('Warning: Did not enable %module because it is already enabled.', array(
            '%module' => $module,
          )) . '<br/>';
        }
      }
    }
    if (!empty($remove)) {
      $result = array();
      foreach ($data as $key => $item) {
        if (!in_array($key, $remove)) {
          $result[] = $item;
        }
      }
      $data = $result;
    }
  }
  elseif ($tag == 'call_php_func') {
    if (empty($data['function'])) {
      $status = PATTERNS_ERR;
      $msg = t("A function is required for this tag");
    }
    elseif (!empty($data['type']) && empty($data['module'])) {
      $status = PATTERNS_ERR;
      $msg = t("If you specify a type you must specify a module. See the documentation for module_load_include.");
    }
    elseif (empty($data['type']) && !empty($data['module'])) {
      $status = PATTERNS_ERR;
      $msg = t("If you specify a module you must specify a type. See the documentation for module_load_include.");
    }
    elseif (!empty($data['filepath']) && !file_exists($data['filepath'])) {
      $status = PATTERNS_ERR;
      $msg = t('The file that you specified does not exist: %file', array(
        '%file' => $data['filepath'],
      ));
    }
  }
  elseif ($tag == 'form') {
    if (!isset($data['form_id'])) {
      $status = PATTERNS_ERR;
      $msg = t('"form_id" is missing.');
    }

    // Attempt to load required include file from menu.
    list($menu, $masks) = menu_router_build();
    foreach ($menu as $item) {
      if (isset($item['page arguments'][0]) && $item['page arguments'][0] == $data['form_id'] && !empty($item['include file'])) {
        $data['include'] = $item['include file'];
        break;
      }
    }
    if (!empty($data['include']) && is_file($data['include'])) {
      require_once $data['include'];
    }
    elseif (is_array($data['include'])) {
      $data['include'] = drupal_get_path('module', $data['include']['module']) . '/' . $data['include']['file'];
    }
    if (!function_exists($data['form_id'])) {
      if (!empty($data['module']) && is_string($data['module'])) {
        $modules = module_list();
        if (in_array($data['module'], $modules)) {

          // try most common include file names
          module_load_include('inc', $data['module']);
          module_load_include('inc', $data['module'], $data['module'] . '.admin');
          module_load_include('inc', $data['module'], $data['module'] . '.page');
        }
      }
    }
    if (!function_exists($data['form_id'])) {
      $status = PATTERNS_ERR;
      $msg = t("Couldn't load the form %form. Check if all required modules are enabled and try to define 'include' or 'module' for this action.", array(
        '%form' => $data['form_id'],
      ));
    }
  }

  // TODO: other tags
  return patterns_results($status, $msg);
}