text_formats_report.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @file Helper functions for text_formats_report module.
  4. */
  5. /**
  6. * Helper function to fetch the filter_format filter list for an array of
  7. * filter formats.
  8. *
  9. * @param array $filter_formats
  10. * Set of filter_formats ids.
  11. *
  12. * @return array
  13. * Rows of filter_list_format() results for each filter_format provided.
  14. */
  15. function text_formats_report_filter_format_filter_list($filter_formats = array()) {
  16. $filter_formats_info = array();
  17. foreach ($filter_formats as $filter_format) {
  18. $filter_formats_info[$filter_format] = filter_list_format($filter_format);
  19. }
  20. return $filter_formats_info;
  21. }
  22. /**
  23. * Gathers the stats fby filter format.
  24. *
  25. * @param $filter_formats all if none passed in.
  26. * @param $field_names all if none passed in.
  27. *
  28. * @return array
  29. * (
  30. * 'filter_format' => array(
  31. * 'bundles',
  32. * 'fields',
  33. * 'fields_usage',
  34. * ),
  35. * )
  36. */
  37. function text_formats_report_fetch_stats($filter_formats = array(), $field_names = array()) {
  38. if (empty($field_names)) {
  39. $candiate_fields = text_formats_report_get_field_configs();
  40. foreach ($candiate_fields as $field) {
  41. $field_names[] = $field['field_name'];
  42. }
  43. }
  44. $stats = array();
  45. foreach ($filter_formats as $filter_format) {
  46. $stats[$filter_format] = array(
  47. 'bundles' => array(),
  48. 'fields' => array(),
  49. 'total_content' => 0,
  50. );
  51. }
  52. foreach ($field_names as $field_name) {
  53. $records = text_formats_report_get_field_usage($field_name, $filter_formats);
  54. foreach ($records as $record) {
  55. foreach ($filter_formats as $filter_format) {
  56. if ($record->format == $filter_format) {
  57. $stats[$filter_format]['total_content']++;
  58. $field_label = l($field_name, 'admin/reports/text-formats/field/' . $field_name, array('query' => array('formats' => $filter_format)));
  59. $stats[$filter_format]['bundles'][] = $record->bundle;
  60. $stats[$filter_format]['fields'][] = $field_label;
  61. }
  62. }
  63. }
  64. }
  65. // Calculate some stats per filter_format.
  66. foreach ($stats as $filter_format => $row) {
  67. $bundles = array_count_values($stats[$filter_format]['bundles']);
  68. $stats[$filter_format]['bundles'] = $bundles;
  69. $fields = array_count_values($stats[$filter_format]['fields']);
  70. $stats[$filter_format]['fields'] = $fields;
  71. }
  72. return $stats;
  73. }
  74. /**
  75. * Gathers the stats fby filter format.
  76. *
  77. * @param $filter_formats all if none passed in.
  78. * @param $field_names all if none passed in.
  79. *
  80. * @return Array $stats
  81. */
  82. function text_formats_report_fetch_stats_for_csv($filter_formats = array(), $field_names = array()) {
  83. if (empty($field_names)) {
  84. $candiate_fields = text_formats_report_get_field_configs();
  85. foreach ($candiate_fields as $field) {
  86. $field_names[] = $field['field_name'];
  87. }
  88. }
  89. $stats = array();
  90. $stats[] = array(t('Entity Id'), t('Entity type'), t('Bundle'), t('Field name'), t('Text format'), t('Path'), 'Content');
  91. foreach ($field_names as $field_name) {
  92. $records = text_formats_report_get_field_usage($field_name, $filter_formats);
  93. foreach ($records as $record) {
  94. $path = '';
  95. if ($record->entity_type == 'node') {
  96. $path = '/node/' . $record->entity_id;
  97. }
  98. $stats[] = array($record->entity_id, $record->entity_type, $record->bundle, $field_name, $record->format, $path, $record->value);
  99. }
  100. }
  101. return $stats;
  102. }
  103. /**
  104. * Helper function to get available input formats as form options.
  105. *
  106. * @return Array filter_format.format => filter_format.name
  107. */
  108. function text_formats_report_filter_format_options() {
  109. // Get list of all input formats.
  110. $query = db_query('SELECT format, name FROM {filter_format}');
  111. $records = $query->fetchAll();
  112. foreach ($records as $record) {
  113. $options[$record->format] = $record->name;
  114. }
  115. return $options;
  116. }
  117. /**
  118. * Helper function to retrieve the custom blocks using specified filter formats.
  119. *
  120. * @param array $filter_formats
  121. * filter_format.format to check for.
  122. *
  123. * @return array
  124. * (
  125. * array('bid', 'info', 'format'),
  126. * )
  127. */
  128. function text_formats_report_get_custom_blocks($filter_formats) {
  129. $custom_blocks = array();
  130. $query = db_select('block_custom', 'b')
  131. ->condition('b.format', $filter_formats)
  132. ->fields('b', array('bid', 'info', 'format'))
  133. ->execute();
  134. if ($query->rowCount() > 0) {
  135. while ($row = $query->fetchAssoc()) {
  136. $custom_blocks[] = $row;
  137. }
  138. }
  139. return $custom_blocks;
  140. }
  141. /**
  142. * Helper function to retrieve the fields configured to use 'text',
  143. * 'text_with_summary' or'text_long' filter_formats.
  144. *
  145. * @return array
  146. * Rows from the field_config table including field_name, id and type.
  147. */
  148. function text_formats_report_get_field_configs() {
  149. $field_names = array();
  150. $query = db_select('field_config', 'fc')
  151. ->condition('fc.type', array('text', 'text_with_summary', 'text_long'))
  152. ->fields('fc', array('field_name', 'id', 'type'))
  153. ->execute();
  154. if ($query->rowCount() > 0) {
  155. while ($row = $query->fetchAssoc()) {
  156. $field_names[] = $row;
  157. }
  158. }
  159. return $field_names;
  160. }
  161. /**
  162. * Retrieves entries from the field_data_$field_name tables using the specified
  163. * filter_formats.
  164. *
  165. * @param string $field_name
  166. * @param array $filter_formats
  167. * all if none passed in.
  168. */
  169. function text_formats_report_get_field_usage($field_name, $filter_formats = array()) {
  170. $filter_formats = empty($filter_formats) ? array_keys(text_formats_report_filter_format_options()) : $filter_formats;
  171. $format_column = $field_name . '_format';
  172. $value_column = $field_name . '_value';
  173. $filter = "";
  174. if (!empty($filter_formats)) {
  175. $formats_query_string = '(';
  176. foreach ($filter_formats as $format) {
  177. $formats_query_string .= "'$format',";
  178. }
  179. $formats_query_string = rtrim($formats_query_string, ",");
  180. $formats_query_string .= ')';
  181. $filter = " WHERE $format_column IN $formats_query_string";
  182. }
  183. $result = db_query("SELECT entity_id,entity_type, bundle, $format_column as format, $value_column as value FROM {field_data_$field_name}" . $filter);
  184. return $result->fetchAll();
  185. }
  186. /**
  187. * Helper function that prepares array for theme_table.
  188. *
  189. * @param array $data
  190. * [row] => header-label => row value.
  191. * @param array $header
  192. *
  193. * @return String output from theme_table.
  194. */
  195. function text_formats_report_prepare_table($data, $header = array()) {
  196. // Construct the table headers array if necessary.
  197. if (empty($header)) {
  198. foreach ($data as $row) {
  199. foreach ($row as $title => $content) {
  200. $header[] = $title;
  201. }
  202. break;
  203. }
  204. }
  205. // Construct the table rows data array.
  206. $row_id = 0;
  207. $rows = array();
  208. foreach ($data as $row) {
  209. foreach ($row as $title => $content) {
  210. if ($title == 'value') {
  211. $content = htmlentities($content);
  212. }
  213. $rows[$row_id][] = $content;
  214. }
  215. $row_id++;
  216. }
  217. return array(
  218. 'header' => $header,
  219. 'rows' => $rows,
  220. );
  221. }
  222. /**
  223. * Helper function that prepares a label => value array for theme_table.
  224. *
  225. * @param array $data
  226. * @param array $header
  227. *
  228. * @return String output from theme_table.
  229. */
  230. function text_formats_report_prepare_simple_table($data, $header = array()) {
  231. $data = (array) $data;
  232. $rows = array();
  233. foreach ($data as $label => $value) {
  234. $rows[] = array('<strong>' . $label . '</strong>', $value);
  235. }
  236. return array(
  237. 'header' => $header,
  238. 'rows' => $rows,
  239. );
  240. }