You are here

function _remove_duplicates_get_table_output in Remove Duplicates 7

Get a table themed output (Legacy output).

Parameters

string $node_type_machine_name: The fetched node type.

string $node_field_machine_name: The {field} used to find duplicates.

bool $prioritize_published_nodes: If TRUE, the last published node in a set of duplicate nodes will be kept. Otherwise, the first node in a set of duplicate nodes will be kept.

bool $case_sensitive: If TRUE, duplicates detection is case sensitive Otherwise, duplicates detection is case insensitive.

Return value

array An array with 2 keys : #markup An HTML string representing the table themed output. #proceed A boolean indicating whether or not duplicates were found.

1 call to _remove_duplicates_get_table_output()
remove_duplicates_build_confirm_settings_form in ./remove_duplicates.module
Form constructor for the module settings confirmation page (Step 2/2).

File

./remove_duplicates.module, line 1308
Remove duplicate nodes according to node fields or Custom fields.

Code

function _remove_duplicates_get_table_output($node_type_machine_name, $node_field_machine_name, $prioritize_published_nodes, $case_sensitive) {
  $node_types = node_type_get_names();
  $node_types_fields = _remove_duplicates_get_node_types_fields();
  $duplicate_node_groups = _remove_duplicates_get_duplicate_node_groups($node_type_machine_name, $node_field_machine_name, $case_sensitive);
  if (is_array($duplicate_node_groups)) {
    if (isset($duplicate_node_groups['count']) && is_array($duplicate_node_groups['count'])) {
      $count_nodes = array_key_exists('nodes', $duplicate_node_groups['count']) ? $duplicate_node_groups['count']['nodes'] : 0;
      $count_node_groups = array_key_exists('node_groups', $duplicate_node_groups['count']) ? $duplicate_node_groups['count']['node_groups'] : 0;
      watchdog('remove_duplicates', 'Search - Found duplicate nodes : [@count_nodes] | node groups : [@count_node_groups]', array(
        '@count_nodes' => $count_nodes,
        '@count_node_groups' => $count_node_groups,
      ), WATCHDOG_INFO);
      watchdog('remove_duplicates', 'Search - Duplicate nodes to remove estimate : [@nodes_to_remove_estimate]', array(
        '@nodes_to_remove_estimate' => $count_nodes - $count_node_groups,
      ), WATCHDOG_INFO);
    }
    if (isset($duplicate_node_groups['data']) && is_array($duplicate_node_groups['data']) && count($duplicate_node_groups['data'])) {

      // Outputs an table of duplicates found.
      $table_output = NULL;

      // Construction of duplicate node group tables.
      $duplicate_node_group_table_rows = array();
      $duplicate_node_group_table_header = array(
        array(
          'header' => TRUE,
          'data' => t('remove'),
        ),
        array(
          'header' => TRUE,
          'data' => t('title'),
        ),
        array(
          'header' => TRUE,
          'data' => t('author'),
        ),
        array(
          'header' => TRUE,
          'data' => t('published'),
        ),
        array(
          'header' => TRUE,
          'data' => t('updated'),
        ),
        array(
          'header' => TRUE,
          'data' => t('created'),
        ),
      );
      $duplicate_node_group_table_rows[] = array(
        array(
          'header' => TRUE,
          'data' => t('Found Duplicates'),
          'colspan' => count($duplicate_node_group_table_header),
        ),
      );
      $duplicate_node_group_table_rows[] = array(
        array(
          'header' => TRUE,
          'data' => t('For node type'),
        ),
        array(
          'data' => (string) $node_types[$node_type_machine_name],
        ),
        array(
          'header' => TRUE,
          'data' => t('With field name'),
        ),
        array(
          'colspan' => count($duplicate_node_group_table_header) - 3,
          'data' => (string) $node_types_fields[$node_type_machine_name][$node_field_machine_name],
        ),
      );
      foreach ($duplicate_node_groups['data'] as $duplicate_node_group) {

        // Defining the default duplicate group title.
        $node_group_title = $node_group_field_name = $node_group_field_value = NULL;
        if (is_array($duplicate_node_group) && count($duplicate_node_group)) {
          $field_common_name = _remove_duplicates_get_field_common_name($node_field_machine_name);
          $node_group_field_value = array();
          foreach ($duplicate_node_group as $duplicate_node) {
            if (is_object($duplicate_node) && !empty($duplicate_node->{$field_common_name})) {
              $node_group_field_value[(string) $duplicate_node->{$field_common_name}] = (string) $duplicate_node->{$field_common_name};
            }
          }
          $or = t('or');
          $node_group_field_name = (string) $node_types_fields[$node_type_machine_name][$node_field_machine_name];
          $node_group_field_value = '"' . implode('" ' . $or . ' "', $node_group_field_value) . '"';
          $node_group_title = t('Where "@nodetypefield" is', array(
            '@nodetypefield' => $node_group_field_name,
          ));
        }

        // Defining the default kept node among duplicates.
        $keep_node_nid = $keep_node_status = NULL;
        foreach ($duplicate_node_group as $duplicate_node) {

          // Defaults to keeping the first node in the group.
          if (empty($keep_node_nid)) {
            $keep_node_nid = $duplicate_node->nid;
            $keep_node_status = $duplicate_node->status;
          }

          // Defining the node which is going to be kept among duplicates.
          if (isset($keep_node_nid)) {

            // If "Keep at least one published node." is checked
            // keeping the first published node in the group.
            if ($prioritize_published_nodes) {
              if ($duplicate_node->status && !$keep_node_status) {
                $keep_node_nid = $duplicate_node->nid;
                $keep_node_status = $duplicate_node->status;
              }
            }
          }
        }
        reset($duplicate_node_group);

        // Defining the group table first rows.
        $duplicate_node_group_table_rows[] = array(
          array(
            'header' => TRUE,
            'data' => $node_group_title,
          ),
          array(
            'data' => $node_group_field_value,
            'colspan' => count($duplicate_node_group_table_header) - 1,
          ),
        );
        $duplicate_node_group_table_rows[] = $duplicate_node_group_table_header;

        // Construction of duplicate node group table.
        foreach ($duplicate_node_group as $duplicate_node) {
          $duplicate_node_group_table_row = array();

          // Data for 'remove' column :
          if (isset($keep_node_nid) && $keep_node_nid == $duplicate_node->nid) {
            $duplicate_node_group_table_row[] = array(
              'data' => '<span style="color:red;">' . t('No') . '</span>',
            );
          }
          else {
            $duplicate_node_group_table_row[] = array(
              'data' => '<span style="color:green;">' . t('Yes') . '</span>',
            );
          }

          // Data for 'title' column :
          $duplicate_node_group_table_row[] = array(
            'data' => l($duplicate_node->title, 'node/' . $duplicate_node->nid, array(
              'attributes' => array(
                'onclick' => 'window.open(this.href,parseInt(Math.random()*1000));return false;',
              ),
            )),
          );

          // Data for 'author' column :
          $duplicate_node_group_table_row[] = array(
            'data' => $duplicate_node->name,
          );

          // Data for 'status' column :
          if ($duplicate_node->status) {
            $duplicate_node_group_table_row[] = array(
              'data' => '<span style="color:black;">' . t('Yes') . '</span>',
            );
          }
          else {
            $duplicate_node_group_table_row[] = array(
              'data' => '<span style="color:gray;">' . t('No') . '</span>',
            );
          }

          // Data for 'updated' column :
          $duplicate_node_group_table_row[] = array(
            'data' => format_date($duplicate_node->changed),
          );

          // Data for 'created' column :
          $duplicate_node_group_table_row[] = array(
            'data' => format_date($duplicate_node->created),
          );

          // Adding new row to rows array.
          $duplicate_node_group_table_rows[] = $duplicate_node_group_table_row;
        }

        // End of duplicate node group table construction.
      }
      $table_output = theme('table', array(
        'rows' => $duplicate_node_group_table_rows,
      ));
      $output = array(
        '#proceed' => TRUE,
        '#element' => array(
          '#type' => 'item',
          '#title' => t('Results'),
          '#markup' => $table_output,
        ),
      );
      return $output;
    }
  }
  watchdog('remove_duplicates', 'Search - No duplicates found for node [@node_type_machine_name] according to field [@node_field_machine_name]', array(
    '@node_type_machine_name' => $node_type_machine_name,
    '@node_field_machine_name' => $node_field_machine_name,
  ), WATCHDOG_INFO);

  // Outputs a no duplicates found message.
  $text_output = t('No duplicates for node type "@nodetype" according to field "@nodetypefield".', array(
    '@nodetype' => (string) $node_types[$node_type_machine_name],
    '@nodetypefield' => (string) $node_types_fields[$node_type_machine_name][$node_field_machine_name],
  ));
  $html_output = '<div class="description"><p><span style="font-weight:bold;color:red;">' . $text_output . '</span></p></div>';
  $output = array(
    '#proceed' => FALSE,
    '#element' => array(
      '#type' => 'item',
      '#title' => t('Results'),
      '#markup' => $html_output,
    ),
  );
  return $output;
}