coder_review.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Coder Review module integration.
  5. */
  6. /**
  7. * Implements hook_custom_formatters_form_alter_alter() on behalf of
  8. * coder_review.module.
  9. */
  10. function coder_review_custom_formatters_form_alter_alter(&$form, &$form_state, $form_id) {
  11. if (in_array($form_id, array('ctools_export_ui_edit_item_form', 'ctools_export_ui_edit_item_wizard_form')) && isset($form['#formatters']) && 'php' == $form['info']['mode']['#default_value']) {
  12. drupal_add_css(drupal_get_path('module', 'coder_review') . '/coder_review.css');
  13. $form['engine']['coder_review'] = array(
  14. '#type' => 'fieldset',
  15. '#title' => t('Coder Review'),
  16. '#collapsible' => TRUE,
  17. '#collapsed' => TRUE,
  18. );
  19. $reviews = _coder_review_settings_form(_coder_review_get_default_settings('files'), $system = array(), $files = array());
  20. $form['engine']['coder_review']['reviews'] = array_merge($reviews['coder_reviews_group'], array(
  21. '#collapsed' => TRUE,
  22. ));
  23. $form['engine']['coder_review']['review'] = array(
  24. '#type' => 'container',
  25. '#prefix' => '<div id="coder-review-wrapper">',
  26. '#suffix' => '</div>',
  27. );
  28. $form['engine']['coder_review']['button'] = array(
  29. '#type' => 'button',
  30. '#value' => t('Review'),
  31. '#ajax' => array(
  32. 'callback' => 'custom_formatters_coder_review',
  33. 'wrapper' => 'coder-review-wrapper',
  34. ),
  35. );
  36. }
  37. }
  38. /**
  39. * Ajax callback for Custom Formatters Coder Review integration.
  40. */
  41. function custom_formatters_coder_review(&$form, $form_state) {
  42. $directory = 'public://custom_formatters/coder_review';
  43. if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
  44. global $base_url;
  45. $md5 = md5($form_state['values']['code']);
  46. file_unmanaged_save_data("<?php\n/**\n * @file\n */\n\n{$form_state['values']['code']}", "{$directory}/{$md5}.php", FILE_EXISTS_REPLACE);
  47. $file = str_replace($base_url . base_path(), '', file_create_url("{$directory}/{$md5}.php"));
  48. $form_state = array(
  49. 'storage' => array(
  50. 'coder_reviews' => $form_state['values']['coder_reviews'],
  51. 'coder_severity' => $form_state['values']['coder_severity'],
  52. 'coder_file_list' => $file,
  53. 'coder_files' => 1,
  54. )
  55. );
  56. $review = coder_review_page_form(array(), $form_state, 'files');
  57. $form['engine']['coder_review']['review'][$file] = $review[$file]['output'];
  58. foreach ($form['engine']['coder_review']['review'][$file]['#results'] as &$result) {
  59. // Adjust the line numbering.
  60. $result = preg_replace('/\/\>Line (\d+):(.*?<pre>)/e', '"/>Line " . ($1 - 5) . ":$2"', $result);
  61. }
  62. unset($form['engine']['coder_review']['review'][$file]['#filename']);
  63. file_unmanaged_delete("{$directory}/{$md5}.php");
  64. }
  65. drupal_get_messages(NULL, TRUE);
  66. return $form['engine']['coder_review']['review'];
  67. }