Skip to content

Commit

Permalink
Initial commit with export
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-K committed Mar 23, 2018
0 parents commit ead61a3
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 0 deletions.
159 changes: 159 additions & 0 deletions Classes/Controller/DatabaseStorageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
namespace Wegmeister\DatabaseStorage\Controller;

/**
* This file is part of the RadKultur.Wettbewerb package.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;

use Wegmeister\DatabaseStorage\Domain\Repository\DatabaseStorageRepository;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;

/**
* The Database Storage controller
*
* @Flow\Scope("singleton")
*/
class DatabaseStorageController extends ActionController
{
/**
* Array with extension and mime type for spreadsheet writers.
* @var array
*/
protected static $types = [
'Xls' => [
'extension' => 'xls',
'mimeType' => 'application/vnd.ms-excel',
],
'Xlsx' => [
'extension' => 'xlsx',
'mimeType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
],
'Ods' => [
'extension' => 'ods',
'mimeType' => 'application/vnd.oasis.opendocument.spreadsheet',
],
'Csv' => [
'extension' => 'csv',
'mimeType' => '',
],
'Html' => [
'extension' => 'html',
'mimeType' => 'text/html',
],
];

/**
* @Flow\Inject
* @var DatabaseStorageRepository
*/
protected $databaseStorageRepository;


/**
* Show list of identifiers
*
* @return void
*/
public function indexAction()
{
$this->view->assign('identifiers', $this->databaseStorageRepository->findStorageidentifiers());
}


/**
* Export all entries for a specific identifier as xls.
*
* @param string $identifier
* @param string $writerType
*
* @return void
*/
public function exportAction(string $identifier, $writerType = 'Xlsx')
{
if (!isset(self::$types[$writerType])) {
throw new WriterException('No writer available for type ' . $writerType . '.', 1521787983);
}

$entries = $this->databaseStorageRepository->findByStorageidentifier($identifier)->toArray();

$dataArray = [];

$spreadsheet = new Spreadsheet();

$spreadsheet->getProperties()
->setCreator('die wegmeister gmbh')
->setTitle('Database Export')
->setSubject('Database Export');

$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setTitle('Database-Export');

$titles = [];
$columns = 0;
foreach ($entries[0]->getProperties() as $title => $value) {
$titles[] = $title;
$columns++;
}

$dataArray[] = $titles;


foreach ($entries as $entry) {
$values = [];

foreach ($entry->getProperties() as $value) {
$values[] = $value;
}

$dataArray[] = $values;
}

$spreadsheet->getActiveSheet()->fromArray($dataArray);

// TODO: Set headline bold
$prefixIndex = 64;
$prefixKey = '';
for ($i = 0; $i < $columns; $i++) {
$index = $i % 26;
$columnStyle = $spreadsheet->getActiveSheet()->getStyle($prefixKey . chr(65 + $i) . '1');
$columnStyle->getFont()->setBold(true);
$columnStyle->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER)
->setVertical(Alignment::VERTICAL_CENTER);

if ($index + 1 > 25) {
$prefixIndex++;
$prefixKey = chr($prefixIndex);
}
}


if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}

header("Pragma: public"); // required
header("Expires: 0");
header('Cache-Control: max-age=0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header('Content-Type: ' . self::$types[$writerType]['mimeType']);
header(sprintf(
'Content-Disposition: attachment; filename="Database-Storage-%s.%s"',
$identifier,
self::$types[$writerType]['extension']
));
header("Content-Transfer-Encoding: binary");

$writer = IOFactory::createWriter($spreadsheet, $writerType);
$writer->save('php://output');
exit;
}
}
100 changes: 100 additions & 0 deletions Classes/Domain/Model/DatabaseStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace Wegmeister\DatabaseStorage\Domain\Model;

/**
* This file is part of the Wegmeister.DatabaseStorage package.
*/

use Neos\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;

/**
* @Flow\Entity
*/
class DatabaseStorage
{

/**
* @var string
* @Flow\Validate(type="NotEmpty")
* @ORM\Column(length=256)
* @Flow\Validate(type="StringLength", options={ "minimum"=1, "maximum"=256 })
*/
protected $storageidentifier;

/**
* Properties of the current storage
*
* @ORM\Column(type="flow_json_array")
* @var array<mixed>
*/
protected $properties = [];

/**
* @var \DateTime
* @Flow\Validate(type="NotEmpty")
*/
protected $datetime;

/**
* Get identifier
*
* @return string
*/
public function getStorageidentifier()
{
return $this->storageidentifier;
}

/**
* Set the identifier
* @param string $identifier
* @return DatabaseStorage
*/
public function setStorageidentifier(string $identifier)
{
$this->storageidentifier = $identifier;
return $this;
}

/**
* Get properties
* @return array
*/
public function getProperties()
{
return $this->properties;
}

/**
* Set properties
* @param array $properties
* @return DatabaseStorage
*/
public function setProperties(array $properties)
{
$this->properties = $properties;
return $this;
}

/**
* Get datetime
*
* @return \DateTime
*/
public function getDateTime()
{
return $this->datetime;
}

/**
* Set datetime
* @param \DateTime $datetime
* @return DatabaseStorage
*/
public function setDateTime(\DateTime $datetime)
{
$this->datetime = $datetime;
return $this;
}
}
55 changes: 55 additions & 0 deletions Classes/Domain/Repository/DatabaseStorageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Wegmeister\DatabaseStorage\Domain\Repository;

/**
* This file is part of the Wegmeister.DatabaseStorage package.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Repository;
use Neos\Flow\Persistence\QueryInterface;

/**
* @Flow\Scope("singleton")
*/
class DatabaseStorageRepository extends Repository
{

/**
* @var array
*/
protected $defaultOrderings = [
'storageidentifier' => QueryInterface::ORDER_ASCENDING,
'datetime' => QueryInterface::ORDER_DESCENDING
];

/**
* @var string
*/
protected $currentIdentifier = false;

/**
* @var array
*/
protected $identifiers = [];


/**
* Find all identifiers.
*
* @return mixed
*/
public function findStorageidentifiers()
{
if ($this->identifiers === []) {
foreach ($this->findAll() as $item) {
if ($this->currentIdentifier !== $item->getStorageidentifier()) {
$this->identifiers[] = $item->getStorageidentifier();
$this->currentIdentifier = $item->getStorageidentifier();
}
}
}

return $this->identifiers;
}
}
58 changes: 58 additions & 0 deletions Classes/Finishers/DatabaseStorageFinisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Wegmeister\DatabaseStorage\Finishers;

/**
* This script belongs to the Neos Flow package "Wegmeister.DatabaseStorage".
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, either version 3
* of the License, or (at your option) any later version.
*
* The Neos project - inspiring people to share!
*/

use Neos\Flow\Annotations as Flow;
use Neos\Form\Core\Model\AbstractFinisher;
use Neos\Form\Exception\FinisherException;

use Wegmeister\DatabaseStorage\Domain\Model\DatabaseStorage;
use Wegmeister\DatabaseStorage\Domain\Repository\DatabaseStorageRepository;

/**
* A simple finisher that stores data into database
*/
class DatabaseStorageFinisher extends AbstractFinisher
{

/**
* @Flow\Inject
* @var DatabaseStorageRepository
*/
protected $databaseStorageRepository;

/**
* Executes this finisher
* @see AbstractFinisher::execute()
*
* @return void
* @throws FinisherException
*/
protected function executeInternal()
{
$formRuntime = $this->finisherContext->getFormRuntime();
$formValues = $formRuntime->getFormState()->getFormValues();

$identifier = $this->parseOption('identifier');
if (!$identifier) {
$identifier = '__undefined__';
}

$dbStorage = new DatabaseStorage();
$dbStorage
->setStorageidentifier($identifier)
->setProperties($formValues)
->setDateTime(new \DateTime());

$this->databaseStorageRepository->add($dbStorage);
}
}
Loading

0 comments on commit ead61a3

Please sign in to comment.