You are here

function _coder_settings_form in Coder 6

Same name and namespace in other branches
  1. 5.2 coder.module \_coder_settings_form()
  2. 5 coder.module \_coder_settings_form()
  3. 6.2 coder.module \_coder_settings_form()

Build settings form API array for coder.

Generates a form with the default reviews and default modules/themes to run Coder on.

@note Actual forms may have additional sections added to them, this is simply a base.

Parameters

$settings: Settings array for coder in the format of _coder_get_default_settings().

$system: Array of module and theme information, in form string theme/module name => boolean TRUE if checked by coder already.

$files: Associative array of files, in form string theme/module name => string filename to check.

Return value

Array for form API for the settings box.

2 calls to _coder_settings_form()
coder_admin_settings in ./coder.module
Implementation of settings page for Drupal 5.
coder_page_form in ./coder.module
Implementation of hook_form().

File

./coder.module, line 188
Developer Module that assists with code review and version upgrade that supports a plug-in extensible hook system so contributed modules can define additional review standards.

Code

function _coder_settings_form($settings, &$system, &$files) {

  // Add the javascript.
  $path = drupal_get_path('module', 'coder');
  drupal_add_js($path . '/coder.js');

  // Create the list of review options from the coder review plug-ins.
  // Maintain a secondary list based on #title only, to make sorting possible.
  $reviews = _coder_reviews();
  foreach ($reviews as $name => $review) {
    $review_options[$name] = isset($review['#link']) ? l($review['#title'], $review['#link']) : $review['#title'];
    if (isset($review['#description'])) {
      $review_options[$name] .= ' (' . $review['#description'] . ')';
    }
    $review_sort[$name] = $review['#title'];
  }

  // Sort the reviews by #title.
  asort($review_sort);
  foreach ($review_sort as $name => $review) {
    $review_sort[$name] = $review_options[$name];
  }

  // What reviews should be used?
  $form['coder_reviews_group'] = array(
    '#type' => 'fieldset',
    '#title' => t('Reviews'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['coder_reviews_group']['coder_reviews'] = array(
    '#type' => 'checkboxes',
    '#options' => $review_sort,
    '#description' => t('apply the checked coding reviews'),
    '#default_value' => $settings['coder_reviews'],
  );

  // What severities should be used?
  $form['coder_reviews_group']['coder_severity'] = array(
    '#type' => 'radios',
    '#options' => array(
      SEVERITY_MINOR => 'minor (most)',
      SEVERITY_NORMAL => 'normal',
      SEVERITY_CRITICAL => 'critical (fewest)',
    ),
    '#description' => t('show warnings at or above the severity warning level'),
    '#default_value' => $settings['coder_severity'],
  );
  if (!empty($settings['coder_patches'])) {

    // Display what to review options.
    $form['coder_what'] = array(
      '#type' => 'fieldset',
      '#title' => t('What to review'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['coder_what']['coder_patch_help'] = array(
      '#value' => '<p>' . t('All patches must be in unified format.  It\'s also preferable for the patch to be created with the "-p" option, which shows the function closest to the code change.  Without this, some function dependent tests may not be triggered.  Coder offers no guarantee that the patch will apply cleanly or will function correctly.') . '</p>',
      '#weight' => -4,
    );
    $form['coder_what']['coder_patch_link'] = array(
      '#type' => 'textfield',
      '#title' => t('Link to patch'),
      '#default_value' => isset($settings['coder_patch_link']) ? $settings['coder_patch_link'] : '',
      '#weight' => -3,
    );
    $form['coder_what']['coder_patch_help'] = array(
      '#value' => '<p>' . t('Or') . '</p>',
      '#weight' => -2,
    );
    $form['coder_what']['coder_patch_text'] = array(
      '#type' => 'textarea',
      '#title' => t('Patch text'),
      '#rows' => 20,
      '#weight' => -1,
      '#default_value' => isset($settings['coder_patch_text']) ? $settings['coder_patch_text'] : '',
      '#attributes' => array(
        'wrap' => 'OFF',
      ),
    );
    $form['coder_patches'] = array(
      '#type' => 'value',
      '#value' => 1,
    );
    $in_patch = 0;
    $patch = $link_contents = $textarea_contents = '';
    $patches = array();
    if (!empty($settings['coder_patch_link'])) {
      $link_contents = file_get_contents($settings['coder_patch_link']);
    }
    if (!empty($settings['coder_patch_text'])) {
      $textarea_contents = $settings['coder_patch_text'];
    }
    $patch_contents = $link_contents . "\n" . $textarea_contents;
    $lines = preg_split("/(\r\n|\n)/", $patch_contents);
    foreach ($lines as $line) {
      if ($line == '') {
        continue;
      }
      if (preg_match("/^\\+\\+\\+\\s+([\\w\\.\\-\\/]+\\s)/", $line, $matches)) {
        if ($patch) {
          $patches[$filename . ": " . $patch_line_numbers] = $patch;
          $system[$filename . ": " . $patch_line_numbers] = $filename;
          $patch = '';
        }
        $filename = $matches[1];
      }
      else {
        if (preg_match("/^(@@\\s+\\-\\d+,\\d+\\s+\\+\\d+,\\d+\\s+@@)\\s*(function\\s([\\w]+).*?)*\$/", $line, $matches)) {
          if ($patch) {
            $patches[$filename . ": " . $patch_line_numbers] = $patch;
            $system[$filename . ": " . $patch_line_numbers] = $filename;
            $patch = '';
          }
          if ($matches[3]) {
            $current_function = $matches[3];
            $patch = 'function ' . $current_function . "() {\n";
          }
          $patch_line_numbers = $matches[1];
        }
        else {
          if (preg_match("/^\\+(?!\\+)/", $line)) {
            $patch .= ltrim($line, '+') . "\n";
            $in_patch = 1;
          }
          else {
            if (preg_match("/^\\s/", $line)) {
              $patch .= substr($line, 1) . "\n";
              $in_patch = 1;
            }
            else {
              $in_patch = 0;
            }
          }
        }
      }
    }
    if ($patch) {
      $patches[$filename . ": " . $patch_line_numbers] = $patch;
      $system[$filename . ": " . $patch_line_numbers] = $filename;
      $patch = '';
    }
    $files = $patches;
  }
  else {

    // Get the modules and theme.
    $sql = 'SELECT name, filename, type, status FROM {system} WHERE type=\'module\' OR type=\'theme\' ORDER BY weight ASC, filename ASC';
    $result = db_query($sql);
    $system_modules = array();
    $system_themes = array();
    while ($system = db_fetch_object($result)) {
      $display_name = $system->name;
      if ($system->status) {
        $display_name .= ' (' . t('active') . ')';
        $system_active[$system->name] = $system->name;
      }
      if (_coder_is_drupal_core($system)) {
        $display_name .= ' (' . t('core') . ')';
        $system_core[$system->name] = $system->name;
      }
      if ($system->type == 'module') {
        $system_modules[$system->name] = $system->name;
      }
      else {
        $system_themes[$system->name] = $system->name;
      }
      $system_links[$system->name] = l($display_name, "coder/{$system->name}");
      $files[$system->name] = $system->filename;
    }
    asort($system_links);

    // Display what to review options.
    $form['coder_what'] = array(
      '#type' => 'fieldset',
      '#title' => t('What to review'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    // NOTE: Should rename var.
    $form['coder_what']['coder_active_modules'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($settings['coder_active_modules']) ? $settings['coder_active_modules'] : 0,
      '#title' => t('active modules and themes'),
    );
    $form['coder_what']['coder_core'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($settings['coder_core']) ? $settings['coder_core'] : 0,
      '#title' => t('core files (php, modules, and includes)'),
    );
    $form['coder_what']['coder_includes'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($settings['coder_includes']) ? $settings['coder_includes'] : 0,
      '#title' => t('include files (.inc, .php, .test, .js, and .install files)'),
    );
    $form['coder_what']['coder_includes_exclusion_fieldset'] = array(
      '#type' => 'fieldset',
      '#title' => t('include file exclusions'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['coder_what']['coder_includes_exclusion_fieldset']['coder_includes_exclusions'] = array(
      '#rows' => 3,
      '#type' => 'textarea',
      '#default_value' => isset($settings['coder_includes_exclusions']) ? $settings['coder_includes_exclusions'] : '',
      '#description' => t('List file names or paths, one per line, which should be excluded (only valid if "include files" is checked above).  For example, modules/system/*.php will exclude all php files in the modules/system directory.'),
    );

    // Display the modules in a fieldset.
    $form['coder_what']['coder_modules'] = array(
      '#type' => 'fieldset',
      '#title' => t('Select specific modules'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      'checkboxes' => array(
        '#theme' => 'cols',
        '#cols' => 2,
      ),
    );
    if (isset($settings['coder_all'])) {
      $modules = $system_modules;
    }
    elseif (isset($settings['coder_active_modules']) && $settings['coder_active_modules']) {
      if (isset($settings['coder_core']) && $settings['coder_core']) {
        $modules = array_intersect($system_active, $system_core);
        $modules = array_intersect($modules, $system_modules);
      }
      else {
        $modules = array_intersect($system_active, $system_modules);
      }
    }
    elseif (isset($settings['coder_core']) && $settings['coder_core']) {
      $modules = array_intersect($system_core, $system_modules);
    }
    elseif (isset($settings['coder_active_modules']) && $settings['coder_active_modules']) {
      $modules = array_intersect($system_active, $system_modules);
    }
    elseif (isset($settings['coder_contrib']) && $settings['coder_contrib']) {
      $modules = array_diff($system_modules, array_intersect($system_core, $system_modules));
    }
    else {
      $modules = isset($settings['coder_modules']) && is_array($settings['coder_modules']) ? $settings['coder_modules'] : array();
    }

    // Display the themes in a fieldset.
    $form['coder_what']['coder_themes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Select specific themes'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      'checkboxes' => array(
        '#theme' => 'cols',
      ),
    );
    if (isset($settings['coder_all'])) {
      $themes = $system_themes;
    }
    elseif (isset($settings['coder_active_modules']) && $settings['coder_active_modules']) {
      if (isset($settings['coder_core']) && $settings['coder_core']) {
        $themes = array_intersect($system_active, $system_core);
        $themes = array_intersect($themes, $system_themes);
      }
      else {
        $themes = array_intersect($system_active, $system_themes);
      }
    }
    elseif (isset($settings['coder_core']) && $settings['coder_core']) {
      $themes = array_intersect($system_core, $system_themes);
    }
    elseif (isset($settings['coder_active_modules']) && $settings['coder_active_modules']) {
      $themes = array_intersect($system_active, $system_themes);
    }
    elseif (isset($settings['coder_contrib']) && $settings['coder_contrib']) {
      $themes = array_diff($system_themes, array_intersect($system_core, $system_themes));
    }
    else {
      $themes = isset($settings['coder_themes']) && is_array($settings['coder_themes']) ? $settings['coder_themes'] : array();
    }
    foreach ($system_links as $name => $link) {
      $classes = array();
      if (in_array($name, $system_active)) {
        $classes[] = 'coder-active';
      }
      if (in_array($name, $system_core)) {
        $classes[] = 'coder-core';
      }
      if (in_array($name, $system_themes)) {
        $type = 'theme';
        $default_value = isset($themes[$name]);
      }
      else {
        $type = 'module';
        $default_value = isset($modules[$name]);
      }
      $form['coder_what']["coder_{$type}s"]['checkboxes']["coder_{$type}s-{$name}"] = array(
        '#type' => 'checkbox',
        '#title' => $link,
        '#default_value' => $default_value,
        '#attributes' => array(
          'class' => implode(' ', $classes),
        ),
      );
    }
    $system = array_merge($modules, $themes);
  }
  return $form;
}