diff --git a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReference.php b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReference.php index 55bf6f3234..5f0f74b3c8 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReference.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReference.php @@ -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 */ diff --git a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php index f9ea3e61dd..b90b02e1de 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php @@ -27,4 +27,10 @@ interface CollectionReferenceRepository * @return CollectionReference|null */ public function findByCollectionId($databoxId, $collectionId); + + /** + * @param CollectionReference $reference + * @return void + */ + public function save(CollectionReference $reference); } diff --git a/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php b/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php index 9694636999..070186dc5c 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php @@ -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 diff --git a/lib/Alchemy/Phrasea/Controller/Admin/DataboxController.php b/lib/Alchemy/Phrasea/Controller/Admin/DataboxController.php index 21dd1a9bf0..5825ef22bc 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/DataboxController.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/DataboxController.php @@ -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(), ]); } } diff --git a/lib/classes/collection.php b/lib/classes/collection.php index fd518d8dd4..efdfeb172a 100644 --- a/lib/classes/collection.php +++ b/lib/classes/collection.php @@ -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);