You are here

public function NodeImportController::viewResolutionCenter in Simple Node Importer 8

Prepare Resolution Center Page.

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

File

src/Controller/NodeImportController.php, line 208

Class

NodeImportController
Default controller for the simple_node_importer module.

Namespace

Drupal\simple_node_importer\Controller

Code

public function viewResolutionCenter() {
  $tableheader = [
    [
      'data' => $this
        ->t('Sr no'),
    ],
    [
      'data' => $this
        ->t('Bundle'),
    ],
    [
      'data' => $this
        ->t('Date of import'),
    ],
    [
      'data' => $this
        ->t('Successful'),
    ],
    [
      'data' => $this
        ->t('Failures'),
    ],
    [
      'data' => $this
        ->t('Uploaded By'),
    ],
    [
      'data' => $this
        ->t('Operations'),
    ],
  ];

  // A variable to hold the row information for each table row.
  $rows = [];
  $srno = 1;
  $connection = Database::getConnection();
  $connection
    ->query("SET SQL_MODE=''");
  $query_record = $connection
    ->select('node_field_data', 'n');
  $query_record
    ->innerJoin('node_resolution', 'nr', 'n.nid = nr.sni_nid');
  $query_record
    ->fields('n', [
    'nid',
    'uid',
    'type',
    'created',
  ]);
  $query_record
    ->fields('nr', [
    'sni_nid',
    'data',
    'reference',
    'status',
    'created',
    'changed',
  ]);
  $query_record
    ->groupBy('nr.sni_nid');
  $result = $query_record
    ->execute()
    ->fetchAll();
  foreach ($result as $data) {
    $serializData = unserialize($data->data);
    $contentType = $serializData['type'];
    $row = [];
    $row[] = [
      'data' => $srno,
    ];

    // Get the bundle label.
    if ($contentType == 'user') {
      $bundle_label = 'User';
    }
    else {
      $node = \Drupal::entityManager()
        ->getStorage('node')
        ->load($data->nid);
      $bundle_label = \Drupal::entityTypeManager()
        ->getStorage('node_type')
        ->load($contentType)
        ->label();
    }
    $row[] = [
      'data' => $bundle_label,
    ];

    // Convert timestamp to date & time.
    $formatted_date = date('d-M-Y', $data->created);
    $row[] = [
      'data' => $formatted_date,
    ];
    $status = unserialize($data->status);
    $row[] = [
      'data' => $status['success'] ? $status['success'] : 0,
    ];
    $row[] = [
      'data' => $status['fail'],
    ];

    // Pass your uid.
    $account = User::load($data->uid);
    $author = $account
      ->getUsername();
    $row[] = [
      'data' => $author,
    ];

    // Generate download csv link.
    $generateDownloadLink = Url::fromRoute('simple_node_importer.resolution_center_operations', [
      'node' => $data->nid,
      'op' => 'download-csv',
    ], [
      'absolute' => TRUE,
    ]);
    $csvLink = $generateDownloadLink
      ->toString();

    // Generate delete node link.
    $generateDeleteLink = Url::fromRoute('entity.node.delete_form', [
      'node' => $data->nid,
    ], [
      'absolute' => TRUE,
    ]);
    $deleteLink = $generateDeleteLink
      ->toString();

    // Generate view records link.
    $generateViewLink = Url::fromRoute('simple_node_importer.resolution_center_operations', [
      'node' => $data->nid,
      'op' => 'view-records',
    ], [
      'absolute' => TRUE,
    ]);
    $viewLink = $generateViewLink
      ->toString();
    $operationGenerator = new FormattableMarkup('<a href="@csvLink">Download CSV</a> | <a href="@viewLink">View</a> | <a href="@deleteLink">Delete</a>', [
      "@csvLink" => $csvLink,
      "@viewLink" => $viewLink,
      "@deleteLink" => $deleteLink,
    ]);
    $row[] = [
      'data' => $this
        ->t("@operations", [
        "@operations" => $operationGenerator,
      ]),
    ];
    $srno++;
    $rows[] = [
      'data' => $row,
    ];
  }
  if (!empty($rows)) {
    $output = [
      '#type' => 'table',
      '#header' => $tableheader,
      '#rows' => $rows,
    ];
  }
  else {
    $output = [
      '#type' => 'table',
      '#header' => $tableheader,
      '#empty' => $this
        ->t('There are no items yet. <a href="@add-url">Add an item.</a>', [
        '@add-url' => Url::fromRoute('node.add', [
          'node_type' => 'simple_node',
        ])
          ->toString(),
      ]),
    ];
  }
  return $output;
}