text_formats_report.page.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. */
  5. module_load_include('inc', 'text_formats_report', 'inc/text_formats_report');
  6. /**
  7. * Menu callback.
  8. *
  9. * Provides input formats report form.
  10. */
  11. function text_formats_report_page_form($form, &$form_state) {
  12. $filter_format_options = text_formats_report_filter_format_options();
  13. $form['formats'] = array(
  14. '#type' => 'checkboxes',
  15. '#title' => t("Select the text format(s) to analyze"),
  16. '#options' => $filter_format_options,
  17. '#required' => TRUE,
  18. '#default_value' => array(),
  19. );
  20. $form['submit'] = array(
  21. '#value' => t("View"),
  22. '#type' => 'submit',
  23. '#ajax' => array(
  24. 'callback' => 'text_formats_report_page_form_submit_ajax_handler',
  25. 'effect' => 'fade',
  26. 'wrapper' => 'js-format-stats',
  27. ),
  28. );
  29. $form['download'] = array(
  30. '#value' => t("Download CSV"),
  31. '#type' => 'submit',
  32. );
  33. $form['js-format-stats'] = array(
  34. '#markup' => '<div id="js-format-stats"></div>',
  35. );
  36. $form['#attached']['library'][] = array('system', 'ui.tabs');
  37. return $form;
  38. }
  39. /**
  40. * Form submit handler.
  41. */
  42. function text_formats_report_page_form_submit($form, &$form_state) {
  43. if ($form_state['values']['op'] == t("Download CSV")) {
  44. text_formats_report_page_form_download($form, $form_state);
  45. }
  46. }
  47. /**
  48. * Form 'View' submit ajax handler.
  49. */
  50. function text_formats_report_page_form_submit_ajax_handler($form, &$form_state) {
  51. $selected_formats = array();
  52. if (!empty($form_state['values']) && isset($form_state['values']['formats'])) {
  53. foreach ($form_state['values']['formats'] as $format) {
  54. if ($format) {
  55. $selected_formats[] = $format;
  56. }
  57. }
  58. }
  59. $content = '<div id="js-format-stats">';
  60. // Selecting formats is required.
  61. if (empty($selected_formats)) {
  62. return array(
  63. '#type' => 'ajax',
  64. '#commands' => array(
  65. ajax_command_replace('#js-format-stats', $content . '</div>'),
  66. ),
  67. );
  68. }
  69. $selected_formats_filter_lists = text_formats_report_filter_format_filter_list($selected_formats);
  70. $custom_blocks = text_formats_report_get_custom_blocks($selected_formats);
  71. $entities = text_formats_report_fetch_stats($selected_formats);
  72. $content .= theme('text_formats_report_stats', array(
  73. 'entities' => $entities,
  74. 'custom_blocks' => $custom_blocks,
  75. 'filter_formats_filter_lists' => $selected_formats_filter_lists,
  76. ));
  77. $content .= '</div>';
  78. $result = array(
  79. '#type' => 'ajax',
  80. '#commands' => array(
  81. ajax_command_replace('#js-format-stats', $content),
  82. ajax_command_invoke('#filter-format-tabs', 'tabs'),
  83. ),
  84. );
  85. return $result;
  86. }
  87. /**
  88. * Serves the data for the selected filter formats as a csv file and exits.
  89. */
  90. function text_formats_report_page_form_download($form, &$form_state) {
  91. $selected_formats = array();
  92. if (!empty($form_state['values']) && isset($form_state['values']['formats'])) {
  93. foreach ($form_state['values']['formats'] as $format) {
  94. if ($format) {
  95. $selected_formats[] = $format;
  96. }
  97. }
  98. }
  99. // @todo include the custom blocks in the csv file.
  100. // $custom_blocks = text_formats_report_get_custom_blocks($selected_formats);
  101. $stats = text_formats_report_fetch_stats_for_csv($selected_formats);
  102. $file_name = "export-" . date("d-m-Y-H-i") . ".csv";
  103. drupal_add_http_header('Content-type', 'application/csv; charset=ISO-8859-1');
  104. drupal_add_http_header('Content-Disposition', "attachment; filename=$file_name");
  105. $output = fopen('php://output', 'w');
  106. fputcsv($output, array(t("Showing data for the filter formats: @formats", array('@formats' => implode(', ', $selected_formats)))));
  107. fputcsv($output, array(''));
  108. foreach ($stats as $row) {
  109. fputcsv($output, $row);
  110. }
  111. drupal_exit();
  112. }