123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * @file Helper functions for text_formats_report module.
- */
- /**
- * Helper function to fetch the filter_format filter list for an array of
- * filter formats.
- *
- * @param array $filter_formats
- * Set of filter_formats ids.
- *
- * @return array
- * Rows of filter_list_format() results for each filter_format provided.
- */
- function text_formats_report_filter_format_filter_list($filter_formats = array()) {
- $filter_formats_info = array();
- foreach ($filter_formats as $filter_format) {
- $filter_formats_info[$filter_format] = filter_list_format($filter_format);
- }
- return $filter_formats_info;
- }
- /**
- * Gathers the stats fby filter format.
- *
- * @param $filter_formats all if none passed in.
- * @param $field_names all if none passed in.
- *
- * @return array
- * (
- * 'filter_format' => array(
- * 'bundles',
- * 'fields',
- * 'fields_usage',
- * ),
- * )
- */
- function text_formats_report_fetch_stats($filter_formats = array(), $field_names = array()) {
- if (empty($field_names)) {
- $candiate_fields = text_formats_report_get_field_configs();
- foreach ($candiate_fields as $field) {
- $field_names[] = $field['field_name'];
- }
- }
- $stats = array();
- foreach ($filter_formats as $filter_format) {
- $stats[$filter_format] = array(
- 'bundles' => array(),
- 'fields' => array(),
- 'total_content' => 0,
- );
- }
- foreach ($field_names as $field_name) {
- $records = text_formats_report_get_field_usage($field_name, $filter_formats);
- foreach ($records as $record) {
- foreach ($filter_formats as $filter_format) {
- if ($record->format == $filter_format) {
- $stats[$filter_format]['total_content']++;
- $field_label = l($field_name, 'admin/reports/text-formats/field/' . $field_name, array('query' => array('formats' => $filter_format)));
- $stats[$filter_format]['bundles'][] = $record->bundle;
- $stats[$filter_format]['fields'][] = $field_label;
- }
- }
- }
- }
- // Calculate some stats per filter_format.
- foreach ($stats as $filter_format => $row) {
- $bundles = array_count_values($stats[$filter_format]['bundles']);
- $stats[$filter_format]['bundles'] = $bundles;
- $fields = array_count_values($stats[$filter_format]['fields']);
- $stats[$filter_format]['fields'] = $fields;
- }
- return $stats;
- }
- /**
- * Gathers the stats fby filter format.
- *
- * @param $filter_formats all if none passed in.
- * @param $field_names all if none passed in.
- *
- * @return Array $stats
- */
- function text_formats_report_fetch_stats_for_csv($filter_formats = array(), $field_names = array()) {
- if (empty($field_names)) {
- $candiate_fields = text_formats_report_get_field_configs();
- foreach ($candiate_fields as $field) {
- $field_names[] = $field['field_name'];
- }
- }
- $stats = array();
- $stats[] = array(t('Entity Id'), t('Entity type'), t('Bundle'), t('Field name'), t('Text format'), t('Path'), 'Content');
- foreach ($field_names as $field_name) {
- $records = text_formats_report_get_field_usage($field_name, $filter_formats);
- foreach ($records as $record) {
- $path = '';
- if ($record->entity_type == 'node') {
- $path = '/node/' . $record->entity_id;
- }
- $stats[] = array($record->entity_id, $record->entity_type, $record->bundle, $field_name, $record->format, $path, $record->value);
- }
- }
- return $stats;
- }
- /**
- * Helper function to get available input formats as form options.
- *
- * @return Array filter_format.format => filter_format.name
- */
- function text_formats_report_filter_format_options() {
- // Get list of all input formats.
- $query = db_query('SELECT format, name FROM {filter_format}');
- $records = $query->fetchAll();
- foreach ($records as $record) {
- $options[$record->format] = $record->name;
- }
- return $options;
- }
- /**
- * Helper function to retrieve the custom blocks using specified filter formats.
- *
- * @param array $filter_formats
- * filter_format.format to check for.
- *
- * @return array
- * (
- * array('bid', 'info', 'format'),
- * )
- */
- function text_formats_report_get_custom_blocks($filter_formats) {
- $custom_blocks = array();
- $query = db_select('block_custom', 'b')
- ->condition('b.format', $filter_formats)
- ->fields('b', array('bid', 'info', 'format'))
- ->execute();
- if ($query->rowCount() > 0) {
- while ($row = $query->fetchAssoc()) {
- $custom_blocks[] = $row;
- }
- }
- return $custom_blocks;
- }
- /**
- * Helper function to retrieve the fields configured to use 'text',
- * 'text_with_summary' or'text_long' filter_formats.
- *
- * @return array
- * Rows from the field_config table including field_name, id and type.
- */
- function text_formats_report_get_field_configs() {
- $field_names = array();
- $query = db_select('field_config', 'fc')
- ->condition('fc.type', array('text', 'text_with_summary', 'text_long'))
- ->fields('fc', array('field_name', 'id', 'type'))
- ->execute();
- if ($query->rowCount() > 0) {
- while ($row = $query->fetchAssoc()) {
- $field_names[] = $row;
- }
- }
- return $field_names;
- }
- /**
- * Retrieves entries from the field_data_$field_name tables using the specified
- * filter_formats.
- *
- * @param string $field_name
- * @param array $filter_formats
- * all if none passed in.
- */
- function text_formats_report_get_field_usage($field_name, $filter_formats = array()) {
- $filter_formats = empty($filter_formats) ? array_keys(text_formats_report_filter_format_options()) : $filter_formats;
- $format_column = $field_name . '_format';
- $value_column = $field_name . '_value';
- $filter = "";
- if (!empty($filter_formats)) {
- $formats_query_string = '(';
- foreach ($filter_formats as $format) {
- $formats_query_string .= "'$format',";
- }
- $formats_query_string = rtrim($formats_query_string, ",");
- $formats_query_string .= ')';
- $filter = " WHERE $format_column IN $formats_query_string";
- }
- $result = db_query("SELECT entity_id,entity_type, bundle, $format_column as format, $value_column as value FROM {field_data_$field_name}" . $filter);
- return $result->fetchAll();
- }
- /**
- * Helper function that prepares array for theme_table.
- *
- * @param array $data
- * [row] => header-label => row value.
- * @param array $header
- *
- * @return String output from theme_table.
- */
- function text_formats_report_prepare_table($data, $header = array()) {
- // Construct the table headers array if necessary.
- if (empty($header)) {
- foreach ($data as $row) {
- foreach ($row as $title => $content) {
- $header[] = $title;
- }
- break;
- }
- }
- // Construct the table rows data array.
- $row_id = 0;
- $rows = array();
- foreach ($data as $row) {
- foreach ($row as $title => $content) {
- if ($title == 'value') {
- $content = htmlentities($content);
- }
- $rows[$row_id][] = $content;
- }
- $row_id++;
- }
- return array(
- 'header' => $header,
- 'rows' => $rows,
- );
- }
- /**
- * Helper function that prepares a label => value array for theme_table.
- *
- * @param array $data
- * @param array $header
- *
- * @return String output from theme_table.
- */
- function text_formats_report_prepare_simple_table($data, $header = array()) {
- $data = (array) $data;
- $rows = array();
- foreach ($data as $label => $value) {
- $rows[] = array('<strong>' . $label . '</strong>', $value);
- }
- return array(
- 'header' => $header,
- 'rows' => $rows,
- );
- }
|