Refactor position management with trait

nouveau fichier: core/lib/Thelia/Action/Export.php
	nouveau fichier: core/lib/Thelia/Action/Import.php
	modifié:         core/lib/Thelia/Config/Resources/action.xml
	modifié:         core/lib/Thelia/Config/Resources/routing/admin.xml
	modifié:         core/lib/Thelia/Controller/Admin/ExportController.php
	modifié:         core/lib/Thelia/Controller/Admin/ImportController.php
	modifié:         core/lib/Thelia/Core/Event/TheliaEvents.php
	modifié:         core/lib/Thelia/ImportExport/Export/Type/ProductTaxedPricesExport.php
	modifié:         core/lib/Thelia/Model/Export.php
	modifié:         core/lib/Thelia/Model/ExportCategory.php
	modifié:         core/lib/Thelia/Model/Import.php
	modifié:         core/lib/Thelia/Model/ImportCategory.php
	modifié:         templates/backOffice/default/export.html
This commit is contained in:
Benjamin Perche
2014-08-01 12:25:51 +02:00
parent 0e7a1117b0
commit 396bafd7a9
13 changed files with 379 additions and 539 deletions

View File

@@ -11,84 +11,16 @@ use Thelia\ImportExport\Export\ExportHandler;
use Thelia\ImportExport\Export\ImagesExportInterface;
use Thelia\Model\Base\Export as BaseExport;
use Thelia\Model\Map\ExportTableMap;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
class Export extends BaseExport
{
use PositionManagementTrait;
use ModelEventDispatcherTrait;
protected static $cache;
public function upPosition()
{
if (($position = $this->getPosition()) > 1) {
$previous = ExportQuery::create()
->filterByPosition($position - 1)
->findOneByExportCategoryId($this->getExportCategoryId());
if (null !== $previous) {
$previous->setPosition($position)->save();
}
$this->setPosition($position - 1)->save();
}
return $this;
}
public function downPosition()
{
$max = $this->getMaxPosition();
$count = $this->getExportCategory()->countExports();
if ($count > $max) {
$max = $count;
}
$position = $this->getPosition();
if ($position < $max) {
$next = ExportQuery::create()
->filterByPosition($position + 1)
->findOneByExportCategoryId($this->getExportCategoryId());
if (null !== $next) {
$next->setPosition($position)->save();
}
$this->setPosition($position + 1)->save();
}
return $this;
}
public function updatePosition($position)
{
$reverse = ExportQuery::create()
->findOneByPosition($position)
;
if (null !== $reverse) {
$reverse->setPosition($this->getPosition())->save();
}
$this->setPosition($position)->save();
}
public function setPositionToLast()
{
$max = $this->getMaxPosition();
if (null === $max) {
$this->setPosition(1);
} else {
$this->setPosition($max+1);
}
return $this;
}
/**
* @param ContainerInterface $container
* @return \Thelia\ImportExport\Export\ExportHandler
@@ -134,27 +66,6 @@ class Export extends BaseExport
return static::$cache = $instance;
}
/**
* @param ConnectionInterface $con
*
* Handle the position of other exports
*/
public function delete(ConnectionInterface $con = null)
{
$imports = ExportQuery::create()
->filterByPosition($this->getPosition(), Criteria::GREATER_THAN)
->find()
;
foreach ($imports as $import) {
$import->setPosition($import->getPosition() - 1);
}
$imports->save();
parent::delete($con);
}
public function hasImages(ContainerInterface $container)
{
if (static::$cache === null) {
@@ -173,13 +84,13 @@ class Export extends BaseExport
return static::$cache instanceof DocumentsExportInterface;
}
public function getMaxPosition()
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
return ExportQuery::create()
->filterByExportCategoryId($this->getExportCategoryId())
->orderByPosition(Criteria::DESC)
->select(ExportTableMap::POSITION)
->findOne()
;
$this->setPosition($this->getNextPosition());
return true;
}
}

View File

@@ -6,86 +6,11 @@ use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Model\Base\CategoryQuery;
use Thelia\Model\Base\ExportCategory as BaseExportCategory;
use Thelia\Model\Map\ExportCategoryTableMap;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
class ExportCategory extends BaseExportCategory
{
public function upPosition()
{
if (($position = $this->getPosition()) > 1) {
$previous = ExportCategoryQuery::create()
->findOneByPosition($position - 1)
;
if (null !== $previous) {
$previous->setPosition($position)->save();
}
$this->setPosition($position - 1)->save();
}
return $this;
}
public function downPosition()
{
$max = ExportCategoryQuery::create()
->orderByPosition(Criteria::DESC)
->select(ExportCategoryTableMap::POSITION)
->findOne()
;
$count = CategoryQuery::create()->count();
if ($count > $max) {
$max = $count;
}
$position = $this->getPosition();
if ($position < $max) {
$next = ExportCategoryQuery::create()
->findOneByPosition($position + 1)
;
if (null !== $next) {
$next->setPosition($position)->save();
}
$this->setPosition($position + 1)->save();
}
return $this;
}
public function updatePosition($position)
{
$reverse = ExportCategoryQuery::create()
->findOneByPosition($position)
;
if (null !== $reverse) {
$reverse->setPosition($this->getPosition())->save();
}
$this->setPosition($position)->save();
}
public function setPositionToLast()
{
$max = ExportCategoryQuery::create()
->orderByPosition(Criteria::DESC)
->select(ExportCategoryTableMap::POSITION)
->findOne()
;
if (null === $max) {
$this->setPosition(1);
} else {
$this->setPosition($max+1);
}
return $this;
}
use PositionManagementTrait;
use ModelEventDispatcherTrait;
}

View File

@@ -12,81 +12,13 @@ use Thelia\Core\Translation\Translator;
use Thelia\ImportExport\Import\ImportHandler;
use Thelia\Model\Base\Import as BaseImport;
use Thelia\Model\Map\ImportTableMap;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
class Import extends BaseImport
{
public function upPosition()
{
if (($position = $this->getPosition()) > 1) {
$previous = ImportQuery::create()
->filterByPosition($position - 1)
->findOneByImportCategoryId($this->getImportCategoryId());
if (null !== $previous) {
$previous->setPosition($position)->save();
}
$this->setPosition($position - 1)->save();
}
return $this;
}
public function downPosition()
{
$max = $this->getMaxPosition();
$count = $this->getImportCategory()->countImports();
if ($count > $max) {
$max = $count;
}
$position = $this->getPosition();
if ($position < $max) {
$next = ImportQuery::create()
->filterByPosition($position + 1)
->findOneByImportCategoryId($this->getImportCategoryId());
if (null !== $next) {
$next->setPosition($position)->save();
}
$this->setPosition($position + 1)->save();
}
return $this;
}
public function updatePosition($position)
{
$reverse = ImportQuery::create()
->findOneByPosition($position)
;
if (null !== $reverse) {
$reverse->setPosition($this->getPosition())->save();
}
$this->setPosition($position)->save();
}
public function setPositionToLast()
{
$max = $this->getMaxPosition();
if (null === $max) {
$this->setPosition(1);
} else {
$this->setPosition($max+1);
}
return $this;
}
use PositionManagementTrait;
use ModelEventDispatcherTrait;
/**
* @param ContainerInterface $container
@@ -128,35 +60,4 @@ class Import extends BaseImport
return $instance;
}
/**
* @param ConnectionInterface $con
*
* Handle the position of other imports
*/
public function delete(ConnectionInterface $con = null)
{
$imports = ImportQuery::create()
->filterByPosition($this->getPosition(), Criteria::GREATER_THAN)
->find()
;
foreach ($imports as $import) {
$import->setPosition($import->getPosition() - 1);
}
$imports->save();
parent::delete($con);
}
public function getMaxPosition()
{
return ImportQuery::create()
->filterByImportCategoryId($this->getImportCategoryId())
->orderByPosition(Criteria::DESC)
->select(ImportTableMap::POSITION)
->findOne()
;
}
}

View File

@@ -5,86 +5,11 @@ namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Model\Base\ImportCategory as BaseImportCategory;
use Thelia\Model\Map\ImportCategoryTableMap;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\Model\Tools\PositionManagementTrait;
class ImportCategory extends BaseImportCategory
{
public function upPosition()
{
if (($position = $this->getPosition()) > 1) {
$previous = ImportCategoryQuery::create()
->findOneByPosition($position - 1)
;
if (null !== $previous) {
$previous->setPosition($position)->save();
}
$this->setPosition($position - 1)->save();
}
return $this;
}
public function downPosition()
{
$max = ImportCategoryQuery::create()
->orderByPosition(Criteria::DESC)
->select(ImportCategoryTableMap::POSITION)
->findOne()
;
$count = CategoryQuery::create()->count();
if ($count > $max) {
$max = $count;
}
$position = $this->getPosition();
if ($position < $max) {
$next = ImportCategoryQuery::create()
->findOneByPosition($position + 1)
;
if (null !== $next) {
$next->setPosition($position)->save();
}
$this->setPosition($position + 1)->save();
}
return $this;
}
public function updatePosition($position)
{
$reverse = ImportCategoryQuery::create()
->findOneByPosition($position)
;
if (null !== $reverse) {
$reverse->setPosition($this->getPosition())->save();
}
$this->setPosition($position)->save();
}
public function setPositionToLast()
{
$max = ImportCategoryQuery::create()
->orderByPosition(Criteria::DESC)
->select(ImportCategoryTableMap::POSITION)
->findOne()
;
if (null === $max) {
$this->setPosition(1);
} else {
$this->setPosition($max+1);
}
return $this;
}
use PositionManagementTrait;
use ModelEventDispatcherTrait;
}