You are here

function _coder_review_settings_form in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_review/coder_review.module \_coder_review_settings_form()

Form constructor: Builds settings form API array for coder_review.

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

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

Parameters

array $settings: Settings array for coder_review in the format of _coder_review_get_default_settings().

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

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

Return value

array Array for form API for the settings box.

See also

coder_review_page_form()

2 calls to _coder_review_settings_form()
coder_review_admin_settings in coder_review/coder_review.admin.inc
Page callback: Configures administrative settings via system_settings_form().
coder_review_page_form in coder_review/coder_review.module
Implements hook_form().

File

coder_review/coder_review.module, line 156
Developer module to assist with coder reviews and API upgrade suggestions.

Code

function _coder_review_settings_form(array $settings, &$system, &$files) {

  // Bootstrap enough to run.
  if (!function_exists('_coder_review_reviews')) {
    require_once realpath(__DIR__) . '/coder_review.common.inc';
  }

  // Add the javascript.
  if (function_exists('drupal_get_path')) {
    $form['#attached']['js'][] = _coder_review_path() . '/coder_review.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_review_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] .= ' (' . _t($review['#description']) . ')';
    }
    $review_sort[$name] = _t($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'],
  );
  $form['coder_reviews_group']['coder_ignore'] = array(
    '#type' => 'checkbox',
    '#default_value' => $settings['coder_ignore'],
    '#title' => _t('handle code ignores'),
    '#description' => _t('Coder is a helpful assistant, not a Drill Sergant. Since the developer should always know better than Coder, the <a href="!help">Coder @ignore system</a> allows developers to tell coder to ignore certain warnings. Uncheck this field to see all warnings, even those that are ignored.', array(
      '!help' => _drupalnode(1772676),
    )),
  );
  if ($settings['coder_files']) {
    $form['coder_what'] = array(
      '#type' => 'fieldset',
      '#title' => _t('What to review'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['coder_what']['coder_file_list'] = array(
      '#type' => 'textarea',
      '#title' => _t('List of files'),
      '#description' => _t('List each relative file path on a separate line without a / at the start. For example: %example_path', array(
        '%example_path' => 'sites/all/modules/contrib/coder/coder.module',
      )),
      '#default_value' => $settings['coder_file_list'],
    );
    $form['coder_files'] = array(
      '#type' => 'value',
      '#value' => 1,
    );
    foreach (explode("\n", $settings['coder_file_list']) as $line) {
      $line = trim($line);
      $system[$line] = $line;
      $files[$line] = $line;
    }
  }
  elseif ($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(
      '#markup' => '<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'),
      '#size' => 150,
      '#default_value' => $settings['coder_patch_link'],
      '#description' => _t('The path should be relative to the drupal home directory, without a / at the start, for example: %example_path', array(
        '%example_path' => drupal_get_path('module', 'coder') . '/name-of-file.patch',
      )),
      '#weight' => -3,
    );
    $form['coder_what']['coder_patch_or'] = array(
      '#markup' => '<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' => $settings['coder_patch_text'],
      '#description' => _t('Paste the contents of the patch file here.'),
      '#attributes' => array(
        'wrap' => 'OFF',
      ),
    );
    $form['coder_patches'] = array(
      '#type' => 'value',
      '#value' => 1,
    );
    $in_patch = 0;
    $patch = $link_contents = $textarea_contents = '';
    $patches = array();
    if ($settings['coder_patch_link']) {
      $link_contents = file_get_contents($settings['coder_patch_link']);
    }
    if ($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 (!empty($patch)) {
          $patches[$filename . ': ' . $patch_line_numbers] = $patch;
          $system[$filename . ': ' . $patch_line_numbers] = $filename;
          $patch = '';
        }
        $filename = $matches[1];
      }
      elseif (preg_match("/^(@@\\s+\\-\\d+,\\d+\\s+\\+\\d+,\\d+\\s+@@)\\s*((function|(protected|private|public)*\\s*class)\\s([\\w]+).*?)*\$/", $line, $matches)) {
        if (!empty($patch)) {
          $patches["{$filename}: {$patch_line_numbers}"] = $patch;
          $system["{$filename}: {$patch_line_numbers}"] = $filename;
          $patch = '';
        }
        if (isset($matches[3]) && isset($matches[4])) {
          if ($matches[3] == 'function') {
            $function_current = $matches[4];
            $patch = 'function ' . $function_current . "() {\n";
          }
          else {
            $class_current = $matches[4];
            $patch = $matches[2] . "\n";
          }
        }
        $patch_line_numbers = $matches[1];
      }
      elseif (preg_match("/^\\+(?!\\+)/", $line)) {
        $patch .= ltrim($line, '+') . "\n";
        $in_patch = 1;
      }
      elseif (preg_match("/^\\s/", $line)) {
        $patch .= drupal_substr($line, 1) . "\n";
        $in_patch = 1;
      }
      else {
        $in_patch = 0;
      }
    }
    if (!empty($patch)) {
      $patches[$filename . ': ' . $patch_line_numbers] = $patch;
      $system[$filename . ': ' . $patch_line_numbers] = $filename;
      $patch = '';
    }
    $files = $patches;
  }
  elseif (function_exists('db_query')) {

    // 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 = $system_themes = array();
    foreach ($result as $system) {
      $display_name = $system->name;
      if ($system->status) {
        $display_name .= ' (' . _t('active') . ')';
        $system_active[$system->name] = $system->name;
      }
      if (_coder_review_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, "admin/config/development/coder/review/{$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' => $settings['coder_active_modules'],
      '#title' => _t('active modules and themes'),
    );
    $form['coder_what']['coder_core'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['coder_core'],
      '#title' => _t('core files (php, modules, and includes)'),
    );
    $ext = _coder_review_php_ext();
    $form['coder_what']['coder_includes'] = array(
      '#type' => 'checkbox',
      '#default_value' => $settings['coder_includes'],
      // @ignore security_fapi_title
      '#title' => _t('include files (@ext)', array(
        '@ext' => implode(' | ', $ext),
      )),
    );
    $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' => $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' => 'coder_review_table_cols',
        '#cols' => 2,
      ),
    );
    if ($settings['coder_all']) {
      $modules = $system_modules;
    }
    elseif ($settings['coder_active_modules']) {
      if ($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 ($settings['coder_core']) {
      $modules = array_intersect($system_core, $system_modules);
    }
    elseif ($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' => 'coder_review_table_cols',
      ),
    );
    if ($settings['coder_all']) {
      $themes = $system_themes;
    }
    elseif ($settings['coder_active_modules']) {
      if ($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 ($settings['coder_active_modules']) {
      $themes = array_intersect($system_active, $system_themes);
    }
    elseif ($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',
        // @ignore security_fapi_title
        '#title' => $link,
        '#default_value' => $default_value,
        '#attributes' => array(
          'class' => $classes,
        ),
      );
    }
    $system = array_merge($modules, $themes);
  }
  return $form;
}