You are here

static function PartyDefaultDataSet::mergePreviewRows in Party 7

Get the preview rows for a merge.

File

includes/party.data.inc, line 78
Provides the default class for managing party - Attached entity relationships.

Class

PartyDefaultDataSet
Class PartyDefaultDataSet

Code

static function mergePreviewRows($master_controller, $merger_controller) {
  if (!$master_controller
    ->getEntity() && !$merger_controller
    ->getEntity()) {
    return array();
  }
  $entity_type = $master_controller
    ->getDataInfo('entity type');
  $bundle = $master_controller
    ->getDataInfo('entity bundle');
  $instances = field_info_instances($entity_type, $bundle);
  $e1 = $master_controller
    ->getEntity();
  $w1 = entity_metadata_wrapper($entity_type, !empty($e1) ? $e1 : NULL, array(
    'bundle' => $bundle,
  ));
  $e2 = $merger_controller
    ->getEntity();
  $w2 = entity_metadata_wrapper($entity_type, !empty($e2) ? $e2 : NULL, array(
    'bundle' => $bundle,
  ));
  $rows = array();
  foreach ($instances as $field_name => $instance) {
    try {
      $row = array(
        'data' => array(
          array(
            'data' => $instance['label'],
          ),
        ),
      );
      $value1 = $value2 = NULL;
      $instance['display']['default']['label'] = 'hidden';
      if (!empty($e1->{$field_name}[LANGUAGE_NONE])) {
        $value1 = field_view_field($entity_type, $e1, $field_name, $instance['display']['default']);
      }
      if (!empty($e2->{$field_name}[LANGUAGE_NONE])) {
        $value2 = field_view_field($entity_type, $e2, $field_name, $instance['display']['default']);
      }
      if (empty($value1) && empty($value2)) {
        continue;
      }
      $row['data'][] = array(
        'class' => array(
          'entity-merge-ui-primary',
        ),
        'data' => $value1,
      );
      $row['data'][] = array(
        'class' => array(
          'entity-merge-ui-duplicate',
        ),
        'data' => $value2,
      );
      $field_info = field_info_field($field_name);
      if ($field_info['cardinality'] == 1) {
        $v1 = !empty($e1) ? $w1->{$field_name}
          ->value() : NULL;
        $v2 = !empty($e2) ? $w2->{$field_name}
          ->value() : NULL;
        if ($v1 == $v2) {
          $action = 'match';
        }
        else {
          if (empty($v2)) {
            $action = 'use_1';
          }
          else {
            if (empty($v1)) {
              $action = 'use_2';
            }
            else {
              $action = 'conflict';
            }
          }
        }
      }
      else {
        if ($w1->{$field_name}
          ->count() == 0) {
          $action = 'use_2';
        }
        else {
          if ($w2->{$field_name}
            ->count() == 0) {
            $action = 'use_1';
          }
          else {
            $action = 'combine';
          }
        }
      }
      switch ($action) {
        case 'match':
          $row['class'][] = 'entity-merge-ui-match';
          $row['data'][2]['class'][] = 'entity-merge-ui-match';
          $row['data'][1]['class'][] = 'entity-merge-ui-match';
          $row['data'][1]['class'][] = 'entity-merge-ui-use';
          break;
        case 'use_2':
          $row['data'][2]['class'][] = 'entity-merge-ui-use';
          break;
        case 'use_1':
          $row['data'][1]['class'][] = 'entity-merge-ui-use';
          break;
        case 'conflict':
          $row['class'][] = 'entity-merge-ui-conflict';
          $row['data'][1]['class'][] = 'entity-merge-ui-conflict';
          $row['data'][2]['class'][] = 'entity-merge-ui-conflict';
          $row['data'][1]['class'][] = 'entity-merge-ui-use';
          break;
        case 'combine':
          $row['class'][] = 'entity-merge-ui-combine';
          $row['data'][1]['class'][] = 'entity-merge-ui-use';
          $row['data'][2]['class'][] = 'entity-merge-ui-use';
          break;
      }
      $rows[$field_name] = $row;
    } catch (Exception $e) {
    }
  }
  return $rows;
}