function _coder_review_settings_form in Coder 7
Same name and namespace in other branches
- 7.2 coder_review/coder_review.module \_coder_review_settings_form()
Build 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
$settings: Settings array for coder_review in the format of _coder_review_get_default_settings().
$system: Array of module and theme information, in form string theme/module name => boolean TRUE if checked by coder_review 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_review_settings_form()
- coder_review_admin_settings in coder_review/
coder_review.module - Configuration page.
- coder_review_page_form in coder_review/
coder_review.module - Implements hook_form().
File
- coder_review/
coder_review.module, line 243
Code
function _coder_review_settings_form($settings, &$system, &$files) {
// Add the javascript.
$form['#attached']['js'] = array(
drupal_get_path('module', 'coder_review') . '/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] .= ' (' . $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_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.'),
'#default_value' => isset($settings['coder_file_list']) ? $settings['coder_file_list'] : '',
);
$form['coder_files'] = array(
'#type' => 'value',
'#value' => 1,
);
if (!empty($settings['coder_file_list'])) {
$lines = explode("\n", $settings['coder_file_list']);
foreach ($lines as $line) {
$line = trim($line);
$system[$line] = $line;
$files[$line] = $line;
}
}
}
elseif (!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(
'#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'),
'#default_value' => isset($settings['coder_patch_link']) ? $settings['coder_patch_link'] : '',
'#weight' => -3,
);
$form['coder_what']['coder_patch_help'] = 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' => 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 (!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;
}
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();
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' => 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)'),
);
$ext = variable_get('coder_review_php_ext', array(
'inc',
'php',
'install',
'test',
));
$form['coder_what']['coder_includes'] = array(
'#type' => 'checkbox',
'#default_value' => isset($settings['coder_includes']) ? $settings['coder_includes'] : 0,
'#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' => 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' => 'coder_review_table_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' => 'coder_review_table_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' => $classes,
),
);
}
$system = array_merge($modules, $themes);
}
return $form;
}