Loading Classes/WE/SpreadsheetImport/Annotations/Mapping.php 0 → 100644 +36 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Annotations; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ /** * Defines the column mapping for a property * * @Annotation * @Target({"PROPERTY"}) */ final class Mapping { /** * @var string */ public $labelId = ''; /** * @var boolean */ public $identifier = FALSE; /** * @var string */ public $setter; } Classes/WE/SpreadsheetImport/Domain/Model/SpreadsheetImport.php 0 → 100644 +240 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Model; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use Doctrine\ORM\Mapping as ORM; /** * @Flow\Entity */ class SpreadsheetImport { /** * @var string * @Flow\Validate(type="NotEmpty") */ protected $context; /** * @ORM\OneToOne(cascade={"persist"}) * @var \TYPO3\Flow\Resource\Resource */ protected $file; /** * @var \DateTime */ protected $scheduleDate; /** * @var string */ protected $mapping = ''; /** * @var boolean */ protected $inserting = FALSE; /** * @var boolean */ protected $updating = FALSE; /** * @var boolean */ protected $deleting = FALSE; /** * @var int */ protected $totalInserted = 0; /** * @var int */ protected $totalUpdated = 0; /** * @var int */ protected $totalDeleted = 0; /** * @var int */ protected $totalSkipped = 0; /** * SpreadsheetImport constructor. */ public function __construct() { $this->scheduleDate = new \DateTime(); } /** * @return string */ public function getContext() { return $this->context; } /** * @param string $context */ public function setContext($context) { $this->context = $context; } /** * @return \TYPO3\Flow\Resource\Resource */ public function getFile() { return $this->file; } /** * @param \TYPO3\Flow\Resource\Resource $file */ public function setFile($file) { $this->file = $file; } /** * @return \DateTime */ public function getScheduleDate() { return $this->scheduleDate; } /** * @param \DateTime $scheduleDate */ public function setScheduleDate($scheduleDate) { $this->scheduleDate = $scheduleDate; } /** * @return array */ public function getMapping() { return unserialize($this->mapping); } /** * @param array $mapping */ public function setMapping($mapping) { $this->mapping = serialize($mapping); } /** * @return boolean */ public function isInserting() { return $this->inserting; } /** * @param boolean $inserting */ public function setInserting($inserting) { $this->inserting = $inserting; } /** * @return boolean */ public function isUpdating() { return $this->updating; } /** * @param boolean $updating */ public function setUpdating($updating) { $this->updating = $updating; } /** * @return boolean */ public function isDeleting() { return $this->deleting; } /** * @param boolean $deleting */ public function setDeleting($deleting) { $this->deleting = $deleting; } /** * @return int */ public function getTotalInserted() { return $this->totalInserted; } /** * @param int $totalInserted */ public function setTotalInserted($totalInserted) { $this->totalInserted = $totalInserted; } /** * @return int */ public function getTotalUpdated() { return $this->totalUpdated; } /** * @param int $totalUpdated */ public function setTotalUpdated($totalUpdated) { $this->totalUpdated = $totalUpdated; } /** * @return int */ public function getTotalDeleted() { return $this->totalDeleted; } /** * @param int $totalDeleted */ public function setTotalDeleted($totalDeleted) { $this->totalDeleted = $totalDeleted; } /** * @return int */ public function getTotalSkipped() { return $this->totalSkipped; } /** * @param int $totalSkipped */ public function setTotalSkipped($totalSkipped) { $this->totalSkipped = $totalSkipped; } } Classes/WE/SpreadsheetImport/Domain/Model/SpreadsheetImportTargetInterface.php 0 → 100644 +35 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Model; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ interface SpreadsheetImportTargetInterface { /** * @param boolean $updated * * @return void */ public function setUpdated($updated); /** * @param \DateTime $lastUpdated * * @return void */ public function isUpdated($lastUpdated); /** * @return array */ public static function getAdditionalProperties(); } Classes/WE/SpreadsheetImport/Domain/Repository/SpreadsheetImportRepository.php 0 → 100644 +22 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Repository; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Persistence\Repository; /** * @Flow\Scope("singleton") */ class SpreadsheetImportRepository extends Repository { } Classes/WE/SpreadsheetImport/SpreadsheetImportService.php 0 → 100644 +149 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport; /** * @Flow\Scope("singleton") */ class SpreadsheetImportService { /** * @var SpreadsheetImport */ protected $spreadsheetImport; /** * @var array */ protected $context; /** * @Flow\InjectConfiguration * @var array */ protected $settings; /** * @Flow\Inject * @var \TYPO3\Flow\Reflection\ReflectionService */ protected $reflectionService; /** * @Flow\Inject * @var \TYPO3\Flow\Resource\ResourceManager */ protected $resourceManager; /** * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * * @return $this */ public function init(SpreadsheetImport $spreadsheetImport) { $this->spreadsheetImport = $spreadsheetImport; $this->context = $this->settings[$spreadsheetImport->getContext()]; return $this; } /** * @return array */ public function getDomainMappingProperties() { $domainMappingProperties = array(); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); foreach ($properties as $property) { $domainMappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class); } return $domainMappingProperties; } /** * @return array */ public function getSpreadsheetColumns() { $columns = array(); $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); $sheet = $reader->getActiveSheet(); $highestColumn = $sheet->getHighestDataColumn(); $row = $sheet->getRowIterator(1, 1)->current(); $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(FALSE); /** @var \PHPExcel_Cell $cell */ foreach ($cellIterator as $cell) { $columns[$cell->getColumn()] = $cell->getValue(); if ($cell->getColumn() === $highestColumn) { break; } } return $columns; } /** * @param int $number * * @return object */ public function getObjectByRow($number) { $domain = $this->context['domain']; $newObject = new $domain; // Plus one to skip the headings $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); $row = $reader->getActiveSheet()->getRowIterator($number + 1, $number + 1)->current(); $this->setObjectPropertiesByRow($newObject, $row); return $newObject; } /** * @return array */ public function import() { // TODO: This simply creates the objects for now without update or delete $objects = array(); $domain = $this->context['domain']; $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); /** @var \PHPExcel_Worksheet_Row $row */ foreach ($reader->getActiveSheet()->getRowIterator(2) as $row) { $newObject = new $domain; $this->setObjectPropertiesByRow($newObject, $row); $objects[] = $newObject; } return $objects; } /** * @param object $newObject * @param \PHPExcel_Worksheet_Row $row */ private function setObjectPropertiesByRow($newObject, $row) { // TODO: Cache $domainMappingProperties and $mappings $domainMappingProperties = $this->getDomainMappingProperties(); $mappings = $this->spreadsheetImport->getMapping(); /** @var \PHPExcel_Cell $cell */ foreach ($row->getCellIterator() as $cell) { $column = $cell->getColumn(); if (array_key_exists($column, $mappings)) { $property = $mappings[$column]; /** @var Mapping $mapping */ $mapping = $domainMappingProperties[$property]; $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $newObject->$setter($cell->getValue()); } } } } Loading
Classes/WE/SpreadsheetImport/Annotations/Mapping.php 0 → 100644 +36 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Annotations; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ /** * Defines the column mapping for a property * * @Annotation * @Target({"PROPERTY"}) */ final class Mapping { /** * @var string */ public $labelId = ''; /** * @var boolean */ public $identifier = FALSE; /** * @var string */ public $setter; }
Classes/WE/SpreadsheetImport/Domain/Model/SpreadsheetImport.php 0 → 100644 +240 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Model; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use Doctrine\ORM\Mapping as ORM; /** * @Flow\Entity */ class SpreadsheetImport { /** * @var string * @Flow\Validate(type="NotEmpty") */ protected $context; /** * @ORM\OneToOne(cascade={"persist"}) * @var \TYPO3\Flow\Resource\Resource */ protected $file; /** * @var \DateTime */ protected $scheduleDate; /** * @var string */ protected $mapping = ''; /** * @var boolean */ protected $inserting = FALSE; /** * @var boolean */ protected $updating = FALSE; /** * @var boolean */ protected $deleting = FALSE; /** * @var int */ protected $totalInserted = 0; /** * @var int */ protected $totalUpdated = 0; /** * @var int */ protected $totalDeleted = 0; /** * @var int */ protected $totalSkipped = 0; /** * SpreadsheetImport constructor. */ public function __construct() { $this->scheduleDate = new \DateTime(); } /** * @return string */ public function getContext() { return $this->context; } /** * @param string $context */ public function setContext($context) { $this->context = $context; } /** * @return \TYPO3\Flow\Resource\Resource */ public function getFile() { return $this->file; } /** * @param \TYPO3\Flow\Resource\Resource $file */ public function setFile($file) { $this->file = $file; } /** * @return \DateTime */ public function getScheduleDate() { return $this->scheduleDate; } /** * @param \DateTime $scheduleDate */ public function setScheduleDate($scheduleDate) { $this->scheduleDate = $scheduleDate; } /** * @return array */ public function getMapping() { return unserialize($this->mapping); } /** * @param array $mapping */ public function setMapping($mapping) { $this->mapping = serialize($mapping); } /** * @return boolean */ public function isInserting() { return $this->inserting; } /** * @param boolean $inserting */ public function setInserting($inserting) { $this->inserting = $inserting; } /** * @return boolean */ public function isUpdating() { return $this->updating; } /** * @param boolean $updating */ public function setUpdating($updating) { $this->updating = $updating; } /** * @return boolean */ public function isDeleting() { return $this->deleting; } /** * @param boolean $deleting */ public function setDeleting($deleting) { $this->deleting = $deleting; } /** * @return int */ public function getTotalInserted() { return $this->totalInserted; } /** * @param int $totalInserted */ public function setTotalInserted($totalInserted) { $this->totalInserted = $totalInserted; } /** * @return int */ public function getTotalUpdated() { return $this->totalUpdated; } /** * @param int $totalUpdated */ public function setTotalUpdated($totalUpdated) { $this->totalUpdated = $totalUpdated; } /** * @return int */ public function getTotalDeleted() { return $this->totalDeleted; } /** * @param int $totalDeleted */ public function setTotalDeleted($totalDeleted) { $this->totalDeleted = $totalDeleted; } /** * @return int */ public function getTotalSkipped() { return $this->totalSkipped; } /** * @param int $totalSkipped */ public function setTotalSkipped($totalSkipped) { $this->totalSkipped = $totalSkipped; } }
Classes/WE/SpreadsheetImport/Domain/Model/SpreadsheetImportTargetInterface.php 0 → 100644 +35 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Model; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ interface SpreadsheetImportTargetInterface { /** * @param boolean $updated * * @return void */ public function setUpdated($updated); /** * @param \DateTime $lastUpdated * * @return void */ public function isUpdated($lastUpdated); /** * @return array */ public static function getAdditionalProperties(); }
Classes/WE/SpreadsheetImport/Domain/Repository/SpreadsheetImportRepository.php 0 → 100644 +22 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport\Domain\Repository; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Persistence\Repository; /** * @Flow\Scope("singleton") */ class SpreadsheetImportRepository extends Repository { }
Classes/WE/SpreadsheetImport/SpreadsheetImportService.php 0 → 100644 +149 −0 Original line number Original line Diff line number Diff line <?php namespace WE\SpreadsheetImport; /* * * This script belongs to the Flow package "SpreadsheetImport". * * * * 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 TYPO3 project - inspiring people to share! * * */ use TYPO3\Flow\Annotations as Flow; use WE\SpreadsheetImport\Annotations\Mapping; use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport; /** * @Flow\Scope("singleton") */ class SpreadsheetImportService { /** * @var SpreadsheetImport */ protected $spreadsheetImport; /** * @var array */ protected $context; /** * @Flow\InjectConfiguration * @var array */ protected $settings; /** * @Flow\Inject * @var \TYPO3\Flow\Reflection\ReflectionService */ protected $reflectionService; /** * @Flow\Inject * @var \TYPO3\Flow\Resource\ResourceManager */ protected $resourceManager; /** * @param \WE\SpreadsheetImport\Domain\Model\SpreadsheetImport $spreadsheetImport * * @return $this */ public function init(SpreadsheetImport $spreadsheetImport) { $this->spreadsheetImport = $spreadsheetImport; $this->context = $this->settings[$spreadsheetImport->getContext()]; return $this; } /** * @return array */ public function getDomainMappingProperties() { $domainMappingProperties = array(); $properties = $this->reflectionService->getPropertyNamesByAnnotation($this->context['domain'], Mapping::class); foreach ($properties as $property) { $domainMappingProperties[$property] = $this->reflectionService->getPropertyAnnotation($this->context['domain'], $property, Mapping::class); } return $domainMappingProperties; } /** * @return array */ public function getSpreadsheetColumns() { $columns = array(); $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); $sheet = $reader->getActiveSheet(); $highestColumn = $sheet->getHighestDataColumn(); $row = $sheet->getRowIterator(1, 1)->current(); $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(FALSE); /** @var \PHPExcel_Cell $cell */ foreach ($cellIterator as $cell) { $columns[$cell->getColumn()] = $cell->getValue(); if ($cell->getColumn() === $highestColumn) { break; } } return $columns; } /** * @param int $number * * @return object */ public function getObjectByRow($number) { $domain = $this->context['domain']; $newObject = new $domain; // Plus one to skip the headings $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); $row = $reader->getActiveSheet()->getRowIterator($number + 1, $number + 1)->current(); $this->setObjectPropertiesByRow($newObject, $row); return $newObject; } /** * @return array */ public function import() { // TODO: This simply creates the objects for now without update or delete $objects = array(); $domain = $this->context['domain']; $file = $this->spreadsheetImport->getFile()->createTemporaryLocalCopy(); $reader = \PHPExcel_IOFactory::load($file); /** @var \PHPExcel_Worksheet_Row $row */ foreach ($reader->getActiveSheet()->getRowIterator(2) as $row) { $newObject = new $domain; $this->setObjectPropertiesByRow($newObject, $row); $objects[] = $newObject; } return $objects; } /** * @param object $newObject * @param \PHPExcel_Worksheet_Row $row */ private function setObjectPropertiesByRow($newObject, $row) { // TODO: Cache $domainMappingProperties and $mappings $domainMappingProperties = $this->getDomainMappingProperties(); $mappings = $this->spreadsheetImport->getMapping(); /** @var \PHPExcel_Cell $cell */ foreach ($row->getCellIterator() as $cell) { $column = $cell->getColumn(); if (array_key_exists($column, $mappings)) { $property = $mappings[$column]; /** @var Mapping $mapping */ $mapping = $domainMappingProperties[$property]; $setter = empty($mapping->setter) ? 'set' . ucfirst($property) : $mapping->setter; $newObject->$setter($cell->getValue()); } } } }