You are here

public function NodeImportController::resolutionCenterOperations in Simple Node Importer 8

Provides different operations for failed rows.

1 string reference to 'NodeImportController::resolutionCenterOperations'
simple_node_importer.routing.yml in ./simple_node_importer.routing.yml
simple_node_importer.routing.yml

File

src/Controller/NodeImportController.php, line 323

Class

NodeImportController
Default controller for the simple_node_importer module.

Namespace

Drupal\simple_node_importer\Controller

Code

public function resolutionCenterOperations(NodeInterface $node, $op) {
  $failed_rows = NodeImportController::getFailedRowsInRc($node
    ->id(), NULL);
  if ($failed_rows) {
    $i = 1;
    foreach ($failed_rows as $col_val) {
      unset($col_val['sni_nid']);
      foreach ($col_val as $keycol => $keyfieldval) {
        if (is_array($keyfieldval) && !empty($keyfieldval)) {
          $j = 0;
          foreach ($keyfieldval as $keyfield) {
            if ($j == 0) {
              $col_val[$keycol] = $keyfield;
            }
            elseif (!empty($keyfield)) {
              $col_val[$keycol . "_" . $j] = $keyfield;
            }
            $j++;
          }
        }
        else {
          $col_val[$keycol] = $keyfieldval;
        }
      }
      $rows[] = $col_val;
      $i++;
    }
  }
  $entityType = $node->field_select_entity_type[0]->value;
  if ($op == 'download-csv') {
    if ($entityType == 'user') {
      $filename = 'Import-failed-users.csv';
    }
    else {
      $filename = 'Import-failed-nodes.csv';
    }
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename={$filename}");
    header("Expires: 0");
    header("Pragma: public");
    $fh = @fopen('php://output', 'w');

    // Make sure nothing else is sent, our file is done.
    $header_update = FALSE;
    foreach ($rows as $val) {
      $row = [];
      if (!empty($val['type'])) {
        unset($val['type']);
      }
      if (!empty($val['reference'])) {
        unset($val['reference']);
      }
      foreach ($val as $key => $keyval) {
        if (!$header_update) {
          $headcol[] = ucwords(str_replace("field ", "", str_replace("_", " ", $key)));
        }
        $row[] = $keyval;
      }
      if (!$header_update) {
        fputcsv($fh, $headcol);
        $header_update = TRUE;
      }
      fputcsv($fh, $row);
    }
    fclose($fh);
    exit;
  }
  elseif ($op == 'view-records') {
    $srno = 1;
    $tableheader = [
      [
        'data' => $this
          ->t('Sr no'),
      ],
      [
        'data' => $entityType == 'user' ? $this
          ->t('Username') : $this
          ->t('Title'),
      ],
      [
        'data' => $this
          ->t('Operations'),
      ],
    ];
    $defaultMsg = $this
      ->t("<strong>Value not provided in CSV</strong>");
    foreach ($rows as $val) {
      $row = [];
      foreach ($val as $key => $keyval) {
        if ($key == 'title' || $key == 'name') {
          $row[] = [
            'data' => $srno,
          ];
          $row[] = empty($keyval) ? $defaultMsg : [
            'data' => $keyval,
          ];
          break;
        }
      }

      // Generate add node link.
      if ($entityType == 'user') {
        $addLink = Url::fromRoute('user.admin_create', [
          'entity_type' => $entityType,
          'refkey' => $val['reference'],
          'bundle' => $val['type'],
        ], [
          'absolute' => TRUE,
        ]);
        $generateAddLink = $addLink
          ->toString();
      }
      else {
        $addLink = Url::fromRoute('node.add', [
          'node_type' => $val['type'],
          'entity_type' => $entityType,
          'refkey' => $val['reference'],
          'bundle' => $val['type'],
        ], [
          'absolute' => TRUE,
        ]);
        $generateAddLink = $addLink
          ->toString();
      }
      $row[] = [
        'data' => $this
          ->t('<a href="@addLink">Edit & Save</a>', [
          "@addLink" => $generateAddLink,
        ]),
      ];
      $srno++;
      $failedRows[] = [
        'data' => $row,
      ];
    }

    // Output as table format.
    $output = [
      '#type' => 'table',
      '#header' => $tableheader,
      '#rows' => $failedRows,
    ];
    return $output;
  }
}