Newer
Older
<?php
namespace Drupal\uw_cfg_common\Controller;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\path_alias\AliasManager;
use Drupal\transliterate_filenames\SanitizeName;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* Provides CSV files for specific UW sites.
*/
class UwDownloadCsvController extends ControllerBase {
/**
* Entity type manager from the core.
*
* @var Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var \Drupal\path_alias\AliasManager
*/
protected $pathAlias;
/**
* Sanitize name.
*
* @var Drupal\transliterate_filenames\SanitizeName
*/
protected $sanitizeName;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* CSV Report constructor.
*
* @param Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param Drupal\path_alias\AliasManager $pathAlias
* The path alias.
* @param Drupal\transliterate_filenames\SanitizeName $sanitizeName
* The sanitize name.
* @param Drupal\Core\Config\ConfigFactory $configFactory
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
SanitizeName $sanitizeName,
ConfigFactory $configFactory
$this->entityTypeManager = $entityTypeManager;
$this->pathAlias = $pathAlias;
$this->sanitizeName = $sanitizeName;
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('path_alias.manager'),
$container->get('transliterate_filenames.sanitize_name'),
$container->get('config.factory')
);
}
/**
* Provides a CSV file for content on a site.
*
* @return Symfony\Component\HttpFoundation\Response
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
* The page response.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function uwContentReport() {
// Get all the nodes.
$nodes = $this->entityTypeManager->getStorage('node')->loadMultiple();
// Just in case there are no nodes, there should always
// be a home page though, this is just to ensure no errors.
$response = NULL;
if ($nodes) {
// The header of the CSV.
$data = [
$this->t('Node ID'),
$this->t('Title'),
$this->t('Content type'),
$this->t('Path'),
$this->t('Published'),
$this->t('Created'),
$this->t('Last updated'),
];
// Put header for CSV into format to be used in CSV.
$rows[] = implode(',', $data);
// Step through each of the nodes and get the info.
foreach ($nodes as $node) {
// Pull out the info about the node, making sure the title is CSV-safe.
$data = [
'nid' => $node->id(),
'title' => '"' . str_replace('"', '""', $node->getTitle()) . '"',
'type' => $node->type->entity->label(),
'path' => $this->pathAlias->getAliasByPath('/node/' . $node->id()),
'published' => $node->status->value == 1 ? 'yes' : 'no',
'created' => date('Y-d-m G:i', $node->getCreatedTime()),
'updated' => date('Y-d-m G:i', $node->getChangedTime()),
];
// Store the node info for use later in the CSV.
$rows[] = implode(',', $data);
}
// Put the info into a large CSV format.
$content = implode("\n", $rows);
// Create the response for the CSV page.
$response = new Response($content);
// Determine the filename for the CSV.
$sitename = $this->configFactory->get('system.site')->get('name');
$filename = $sitename . '_content_report.csv';
// Make sure we are using a safe filename.
$filename = $this->sanitizeName->sanitizeFilename($filename);
// Set the headers for the CSV.
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
}
return $response;
}
}