You are here

public static function TermDuplicate::dashTerms in Commerce Bulk 8

File

src/Plugin/Action/TermDuplicate.php, line 194

Class

TermDuplicate
Duplicate term.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public static function dashTerms($file = '', $match = '', $engfile = '', $array = NULL) {
  $files = \Drupal::service('file_system');
  $file = $files
    ->realpath("private://google/taxonomy-with-ids.{$file}.txt");
  if (file_exists($file)) {
    $file = file_get_contents($file);
  }
  if ($match) {
    $num = preg_match_all('/^\\d+\\ -\\ ' . $match . '.*/m', $file, $matches);
    $file = $matches[0];
  }
  $eng = $files
    ->realpath("private://google/taxonomy-with-ids.{$engfile}.txt");
  if ($engfile = file_exists($eng)) {
    $engfile = file_get_contents($eng);
    $engfile = explode(PHP_EOL, trim($engfile));
    $eng = [];
    foreach ($engfile as $line) {
      $line = explode(' - ', $line);
      $eng[$line[0]] = $line[1];
    }
  }
  $file = is_array($file) ? $file : explode(PHP_EOL, trim($file));
  $terms = $keyed = [];
  foreach ($file as $index => $line) {
    $parts = explode(' - ', $line);
    $dash = '';
    $names = explode(' > ', $parts[1]);
    foreach ($names as $term) {
      if (!isset($terms["{$dash}{$term}"])) {
        $engpart = $engfile ? "={$eng[$parts[0]]}" : '';
        $terms["{$dash}{$term}"] = "={$parts[0]}{$engpart}";
        $keyed[$parts[0]] = $term;
      }
      $dash .= '-';
    }
  }
  $file = '';
  foreach ($terms as $key => $value) {
    $file .= "={$key}{$value}" . PHP_EOL;
  }
  return $array ? $keyed : $file;
}