You are here

protected function Record::removeDuplicates in Views OAI-PMH 8

Remove all duplicate rows for array considering array keys and key brothers @todo refactor this function to more elegant and faster

Parameters

$array:

Return value

array

File

src/Plugin/views/style/Record.php, line 293

Class

Record
Plugin annotation @ViewsStyle( id = "views_oai_pmh_record", title = @Translation("OAI-PMH"), help = @Translation("Displays rows in OAI-PMH records."), display_types = {"oai_pmh"} )

Namespace

Drupal\views_oai_pmh\Plugin\views\style

Code

protected function removeDuplicates($array) {
  $output = [];
  foreach ($array as $key => $value) {
    foreach ($value as $key_i => $value_i) {
      $value_old = $value_i[0];
      $all_equal = true;
      if (is_array($value_i)) {
        for ($j = 0; $j < count($value_i); $j++) {
          if ($value_i[$j] !== $value_old) {
            $all_equal = false;
            break;
          }
          $value_old = $value_i[$j];
        }
      }
      else {
        $value_old = $value_i;
      }
      $delimiter = '';
      $key_delimiter = '';
      if (strpos($key_i, '>') !== false) {

        //datacite
        $delimiter = '>';
        $key_delimiter = '@';
      }
      else {
        if (strpos($key_i, 'dc') !== false) {

          //dcc
          $delimiter = '@';
        }
      }
      $brothers = $this
        ->getBrothersKey($key_i, $value, $delimiter, $key_delimiter);
      if ($all_equal && empty($brothers)) {

        // all values equals and without brother(s)
        $output[$key][$key_i] = $value_old;
      }
      else {
        if (!$all_equal && empty($brothers)) {

          // not all values are equals and without brother(s) for dc, dcc cases
          for ($j = 0; $j < count($value_i); $j++) {
            if (!in_array($value_i[$j], $output[$key][$key_i])) {
              $output[$key][$key_i][] = $value_i[$j];
            }
          }
        }
        else {

          // has brother key
          $tuples = [];
          $m = 0;
          $nChildren = count($brothers[0][key($brothers[0])]);
          for ($k = 0; $k < $nChildren; $k++) {
            $tuple = "";
            for ($l = 0; $l < count($brothers); $l++) {
              if (is_array($brothers[$l][key($brothers[$l])])) {
                $tuple = $tuple . $brothers[$l][key($brothers[$l])][$m];
              }
              else {
                $tuple = $tuple . $brothers[$l][key($brothers[$l])];
              }
            }
            if (!in_array($tuple, $tuples)) {
              if (is_array($array[$key][$key_i])) {
                $output[$key][$key_i][] = $array[$key][$key_i][$m];
              }
              else {
                $output[$key][$key_i][] = $array[$key][$key_i];
              }
            }
            $m++;
            $tuples[] = $tuple;
          }
        }
      }
    }
  }
  return $output;
}