mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 04:23:19 +00:00
Implement save method in collection reference repository
This commit is contained in:
@@ -68,6 +68,18 @@ class CollectionReference
|
||||
return $this->baseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $baseId
|
||||
*/
|
||||
public function setBaseId($baseId)
|
||||
{
|
||||
if ($this->baseId > 0) {
|
||||
throw new \LogicException('Cannot change the baseId of an existing collection reference.');
|
||||
}
|
||||
|
||||
$this->baseId = $baseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
|
@@ -27,4 +27,10 @@ interface CollectionReferenceRepository
|
||||
* @return CollectionReference|null
|
||||
*/
|
||||
public function findByCollectionId($databoxId, $collectionId);
|
||||
|
||||
/**
|
||||
* @param CollectionReference $reference
|
||||
* @return void
|
||||
*/
|
||||
public function save(CollectionReference $reference);
|
||||
}
|
||||
|
@@ -2,15 +2,35 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Collection\Reference;
|
||||
|
||||
use Alchemy\Phrasea\Core\Database\QueryBuilder;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
{
|
||||
|
||||
private static $query = 'SELECT base_id AS baseId, sbas_id AS databoxId, server_coll_id AS collectionId,
|
||||
private static $table = 'bas';
|
||||
|
||||
private static $columns = [
|
||||
'base_id' => 'baseId',
|
||||
'sbas_id' => 'databoxId',
|
||||
'server_coll_id' => 'collectionId',
|
||||
'ord' => 'displayIndex',
|
||||
'active' => 'isActive',
|
||||
'aliases' => 'alias'
|
||||
];
|
||||
|
||||
private static $selectQuery = 'SELECT base_id AS baseId, sbas_id AS databoxId, server_coll_id AS collectionId,
|
||||
ord AS displayIndex, active AS isActive, aliases AS alias
|
||||
FROM bas';
|
||||
|
||||
private static $insertQuery = 'INSERT INTO bas (sbas_id, server_coll_id, ord, active, aliases)
|
||||
VALUES (:databoxId, :collectionId,
|
||||
(SELECT COALESCE(MAX(ord), 0) + 1 AS ord FROM bas WHERE sbas_id = :sbas_id),
|
||||
:isActive, :alias)';
|
||||
|
||||
private static $updateQuery = 'UPDATE bas SET ord = :displayIndex, active = :isActive, aliases = :alias
|
||||
WHERE base_id = :baseId';
|
||||
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
@@ -29,7 +49,7 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
*/
|
||||
public function findAll()
|
||||
{
|
||||
return $this->createManyReferences($this->connection->fetchAll(self::$query));
|
||||
return $this->createManyReferences($this->connection->fetchAll(self::$selectQuery));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +58,7 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
*/
|
||||
public function findAllByDatabox($databoxId)
|
||||
{
|
||||
$query = self::$query . ' WHERE sbas_id = :databoxId';
|
||||
$query = self::$selectQuery . ' WHERE sbas_id = :databoxId';
|
||||
$rows = $this->connection->fetchAll($query, [ ':databoxId' => $databoxId ]);
|
||||
|
||||
return $this->createManyReferences($rows);
|
||||
@@ -50,7 +70,7 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
*/
|
||||
public function find($baseId)
|
||||
{
|
||||
$query = self::$query . ' WHERE base_id = :baseId';
|
||||
$query = self::$selectQuery . ' WHERE base_id = :baseId';
|
||||
$row = $this->connection->fetchAssoc($query, [ ':baseId' => $baseId ]);
|
||||
|
||||
if ($row !== false) {
|
||||
@@ -67,7 +87,7 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
*/
|
||||
public function findByCollectionId($databoxId, $collectionId)
|
||||
{
|
||||
$query = self::$query . ' WHERE sbas_id = :databoxId AND server_coll_id = :collectionId';
|
||||
$query = self::$selectQuery . ' WHERE sbas_id = :databoxId AND server_coll_id = :collectionId';
|
||||
$row = $this->connection->fetchAssoc($query, [ ':databoxId' => $databoxId, ':collectionId' => $collectionId ]);
|
||||
|
||||
if ($row !== false) {
|
||||
@@ -77,6 +97,35 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
|
||||
return null;
|
||||
}
|
||||
|
||||
public function save(CollectionReference $collectionReference)
|
||||
{
|
||||
$query = self::$insertQuery;
|
||||
$isInsert = true;
|
||||
|
||||
$parameters = [
|
||||
'isActive' => $collectionReference->isActive(),
|
||||
'alias' => $collectionReference->getAlias()
|
||||
];
|
||||
|
||||
if ($collectionReference->getBaseId() > 0) {
|
||||
$query = self::$updateQuery;
|
||||
$isInsert = false;
|
||||
|
||||
$parameters['baseId'] = $collectionReference->getBaseId();
|
||||
$parameters['displayIndex'] = $collectionReference->getDisplayIndex();
|
||||
}
|
||||
else {
|
||||
$parameters['databoxId'] = $collectionReference->getDataboxId();
|
||||
$parameters['collectionId'] = $collectionReference->getCollectionId();
|
||||
}
|
||||
|
||||
$this->connection->executeQuery($query, $parameters);
|
||||
|
||||
if ($isInsert) {
|
||||
$collectionReference->setBaseId($this->connection->lastInsertId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $row
|
||||
* @return CollectionReference
|
||||
|
@@ -718,7 +718,7 @@ class DataboxController extends Controller
|
||||
} catch (\Exception $e) {
|
||||
return $this->app->redirectPath('admin_database_submit_collection', [
|
||||
'databox_id' => $databox_id,
|
||||
'error' => 'error',
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -92,18 +92,11 @@ EOT;
|
||||
|
||||
$new_id = (int) $connbas->lastInsertId();
|
||||
|
||||
$sql = "INSERT INTO bas (base_id, active, ord, server_coll_id, sbas_id, aliases)
|
||||
VALUES
|
||||
(null, 1, :ord, :server_coll_id, :sbas_id, '')";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->execute([
|
||||
':server_coll_id' => $new_id,
|
||||
':sbas_id' => $sbas_id,
|
||||
':ord' => self::getNewOrder($conn, $sbas_id),
|
||||
]);
|
||||
$stmt->closeCursor();
|
||||
$repository = $app['repo.collection-references'];
|
||||
$collectionReference = new CollectionReference(0, $sbas_id, $new_id, 0, true, '');
|
||||
|
||||
$repository->save($collectionReference);
|
||||
|
||||
$new_bas = $conn->lastInsertId();
|
||||
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
|
||||
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
|
||||
|
||||
|
Reference in New Issue
Block a user