Implement save collection VO

This commit is contained in:
Thibaud Fabre
2015-07-10 17:32:09 +02:00
parent 616f57e8ed
commit 569c2ff6d8
12 changed files with 165 additions and 96 deletions

View File

@@ -9,6 +9,8 @@
*/
namespace Alchemy\Phrasea\Application\Helper;
use Alchemy\Phrasea\Collection\CollectionService;
trait ApplicationBoxAware
{
/** @var \appbox|callable */
@@ -61,6 +63,14 @@ trait ApplicationBoxAware
return $this->applicationBox;
}
/**
* @return CollectionService
*/
public function getCollectionService()
{
return $this['services.collection'];
}
/**
* Find a registered Databoxes.
*

View File

@@ -3,6 +3,7 @@
namespace Alchemy\Phrasea\Collection;
use Alchemy\Phrasea\Collection\Reference\CollectionReference;
use PHPExiftool\Exception\LogicException;
class Collection
{
@@ -64,6 +65,14 @@ class Collection
<sugestedValues></sugestedValues>
</baseprefs>
EOT;
$this->logo = '';
$this->labels = array(
'en' => '',
'fr' => '',
'de' => '',
'nl' => ''
);
$this->publicWatermark = '';
}
/**
@@ -82,6 +91,18 @@ EOT;
return $this->collectionId;
}
/**
* @param $collectionId
*/
public function setCollectionId($collectionId)
{
if ($this->collectionId > 0) {
throw new LogicException('Cannot change the ID of an existing collection.');
}
$this->collectionId = (int) $collectionId;
}
/**
* @return string
*/

View File

@@ -33,7 +33,17 @@ class CollectionFactory
throw new \InvalidArgumentException('Reference does not belong to given databoxId.');
}
return new \collection($this->app, $reference->getBaseId(), $reference, $row);
$collection = new Collection($databoxId, $row['coll_id'], $row['asciiname']);
$collection->setLabel('en', $row['label_en']);
$collection->setLabel('fr', $row['label_fr']);
$collection->setLabel('de', $row['label_de']);
$collection->setLabel('nl', $row['label_nl']);
$collection->setLogo($row['logo']);
$collection->setPreferences($row['prefs']);
$collection->setPublicWatermark($row['pub_wm']);
return new \collection($this->app, $collection, $reference, $row);
}
/**

View File

@@ -67,6 +67,11 @@ class CollectionRepositoryRegistry
throw new \OutOfBoundsException('No repository available for given base [baseId: ' . $baseId . ' ].');
}
public function purgeRegistry()
{
$this->baseIdMap = null;
}
private function loadBaseIdMap()
{
$references = $this->referenceRepository->findAll();

View File

@@ -25,7 +25,7 @@ class DbalCollectionReferenceRepository implements CollectionReferenceRepository
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),
(SELECT COALESCE(MAX(b.ord), 0) + 1 AS ord FROM bas b WHERE b.sbas_id = :databoxId),
:isActive, :alias)';
private static $updateQuery = 'UPDATE bas SET ord = :displayIndex, active = :isActive, aliases = :alias

View File

@@ -11,9 +11,15 @@ use Doctrine\DBAL\Connection;
class DbalCollectionRepository implements CollectionRepository
{
private static $query = 'SELECT coll_id, asciiname, label_en, label_fr, label_de, label_nl, prefs, logo, majLogo, pub_wm
private static $selectQuery = 'SELECT coll_id, asciiname, label_en, label_fr, label_de, label_nl, prefs, logo, majLogo, pub_wm
FROM coll';
private static $insertQuery = 'INSERT INTO coll (asciiname, prefs, logo) VALUES (:name, :preferences, :logo)';
private static $updateQuery = 'UPDATE coll SET asciiname = :name, label_en = :labelEn, label_fr = :labelFr,
label_de = :labelDe, label_nl = :labelNl, prefs = :preferences, logo = :logo,
majLogo = :logoTimestamp, pub_wm = :publicWatermark WHERE coll_id = :collectionId';
/**
* @var int
*/
@@ -64,7 +70,7 @@ class DbalCollectionRepository implements CollectionRepository
$params[':id_' . $reference->getCollectionId()] = $reference->getCollectionId();
}
$query = self::$query . sprintf(' WHERE coll_id IN (%s)', implode(', ', array_keys($params)));
$query = self::$selectQuery . sprintf(' WHERE coll_id IN (%s)', implode(', ', array_keys($params)));
$rows = $this->connection->fetchAll($query, $params);
return $this->collectionFactory->createMany($this->databoxId, $references, $rows);
@@ -82,7 +88,7 @@ class DbalCollectionRepository implements CollectionRepository
return null;
}
$query = self::$query . ' WHERE coll_id = :collectionId';
$query = self::$selectQuery . ' WHERE coll_id = :collectionId';
$row = $this->connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
if ($row !== false) {
@@ -105,7 +111,7 @@ class DbalCollectionRepository implements CollectionRepository
return null;
}
$query = self::$query . ' WHERE coll_id = :collectionId';
$query = self::$selectQuery . ' WHERE coll_id = :collectionId';
$row = $this->connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
if ($row !== false) {
@@ -117,6 +123,31 @@ class DbalCollectionRepository implements CollectionRepository
public function save(Collection $collection)
{
$isInsert = true;
$query = self::$insertQuery;
$parameters = array(
'name' => $collection->getName(),
'preferences' => $collection->getPreferences(),
'logo' => $collection->getLogo()
);
if ($collection->getCollectionId() > 0) {
$parameters['collectionId'] = $collection->getCollectionId();
$parameters['labelEn'] = $collection->getLabel('en', false);
$parameters['labelFr'] = $collection->getLabel('fr', false);
$parameters['labelDe'] = $collection->getLabel('de', false);
$parameters['labelNl'] = $collection->getLabel('nl', false);
$parameters['logoTimestamp'] = $collection->getLogoUpdatedAt();
$parameters['publicWatermark'] = $collection->getPublicWatermark();
$query = self::$updateQuery;
$isInsert = false;
}
$this->connection->executeQuery($query, $parameters);
if ($isInsert) {
$collection->setCollectionId($this->connection->lastInsertId());
}
}
}

View File

@@ -474,7 +474,7 @@ class CollectionController extends Controller
if ($collection->get_record_amount() > 0) {
$msg = $this->app->trans('Empty the collection before removing');
} else {
$collection->unmount_collection($this->app);
$collection->unmount();
$collection->delete();
$success = true;
$msg = $this->app->trans('Successful removal');
@@ -525,7 +525,7 @@ class CollectionController extends Controller
$collection = \collection::getByBaseId($this->app, $bas_id);
try {
$collection->unmount_collection($this->app);
$collection->unmount();
$success = true;
} catch (\Exception $e) {

View File

@@ -10,9 +10,11 @@
*/
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Collection\CollectionService;
use Alchemy\Phrasea\Core\Configuration\AccessRestriction;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Core\Version\AppboxVersionRepository;
use Alchemy\Phrasea\Databox\DataboxConnectionProvider;
use Alchemy\Phrasea\Databox\DataboxRepository;
use Doctrine\ORM\Tools\SchemaTool;
use MediaAlchemyst\Alchemyst;
@@ -41,6 +43,10 @@ class appbox extends base
* @var \databox[]
*/
protected $databoxes;
/**
* @var CollectionService
*/
protected $collectionService;
public function __construct(Application $app)
{
@@ -289,6 +295,19 @@ class appbox extends base
parent::delete_data_from_cache($option);
}
public function getCollectionService()
{
if ($this->collectionService === null) {
$this->collectionService = new CollectionService(
$this->app,
$this->connection,
new DataboxConnectionProvider($this)
);
}
return $this->collectionService;
}
/**
* @return AccessRestriction
*/

View File

@@ -25,7 +25,7 @@ use Alchemy\Phrasea\Core\Thumbnail\ThumbnailManager;
use Alchemy\Phrasea\Model\Entities\User;
use Symfony\Component\HttpFoundation\File\File;
class collection implements cache_cacheableInterface, ThumbnailedElement
class collection implements ThumbnailedElement
{
const PIC_LOGO = 'minilogos';
@@ -66,13 +66,12 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
$repository->save($collectionReference);
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
$app['repo.collections-registry']->purgeRegistry();
phrasea::reset_baseDatas($appbox);
$collection = new self($app, $collection, $collectionReference);
if (null !== $user) {
$collection->set_admin($collectionReference->getBaseId(), $user);
$collection->collectionService->grantAdminRights($collectionReference, $user);
}
$app['dispatcher']->dispatch(CollectionEvents::CREATED, new CreatedEvent($collection));
@@ -85,16 +84,10 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
$reference = new CollectionReference(0, $databox->get_sbas_id(), $coll_id, 0, true, '');
$app['repo.collection-references']->save($reference);
$app['repo.collections-registry']->purgeRegistry();
$databox->get_appbox()->delete_data_from_cache(appbox::CACHE_LIST_BASES);
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
cache_databox::update($app, $databox->get_sbas_id(), 'structure');
phrasea::reset_baseDatas($databox->get_appbox());
$coll = self::getByBaseId($app, $reference->getBaseId());
$coll->set_admin($reference->getBaseId(), $user);
$collection = self::getByBaseId($app, $reference->getBaseId());
$collection->collectionService->grantAdminRights($collection->reference, $user);
return $reference->getBaseId();
}
@@ -103,7 +96,7 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$base_id_key = $base_id . '_' . ($printname ? '1' : '0');
if ( ! isset(self::$_logos[$base_id_key])) {
if (!isset(self::$_logos[$base_id_key])) {
if (is_file($app['root.path'] . '/config/minilogos/' . $base_id)) {
$name = phrasea::bas_labels($base_id, $app);
@@ -119,33 +112,36 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public static function getWatermark($base_id)
{
if ( ! isset(self::$_watermarks['base_id'])) {
if (!isset(self::$_watermarks['base_id'])) {
if (is_file(__DIR__ . '/../../config/wm/' . $base_id))
if (is_file(__DIR__ . '/../../config/wm/' . $base_id)) {
self::$_watermarks['base_id'] = '<img src="/custom/wm/' . $base_id . '" />';
}
}
return isset(self::$_watermarks['base_id']) ? self::$_watermarks['base_id'] : '';
}
public static function getPresentation($base_id)
{
if ( ! isset(self::$_presentations['base_id'])) {
if (!isset(self::$_presentations['base_id'])) {
if (is_file(__DIR__ . '/../../config/presentation/' . $base_id))
if (is_file(__DIR__ . '/../../config/presentation/' . $base_id)) {
self::$_presentations['base_id'] = '<img src="/custom/presentation/' . $base_id . '" />';
}
}
return isset(self::$_presentations['base_id']) ? self::$_presentations['base_id'] : '';
}
public static function getStamp($base_id)
{
if ( ! isset(self::$_stamps['base_id'])) {
if (!isset(self::$_stamps['base_id'])) {
if (is_file(__DIR__ . '/../../config/stamp/' . $base_id))
if (is_file(__DIR__ . '/../../config/stamp/' . $base_id)) {
self::$_stamps['base_id'] = '<img src="/custom/stamp/' . $base_id . '" />';
}
}
return isset(self::$_stamps['base_id']) ? self::$_stamps['base_id'] : '';
}
@@ -260,8 +256,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$this->app = $app;
$this->databox = $app->getApplicationBox()->get_databox($reference->getDataboxId());
$this->collectionService = $app->getApplicationBox()->getCollectionService();
$this->collection = $collection;
$this->collectionVO = $collection;
$this->reference = $reference;
}
@@ -294,12 +291,13 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$this->app = $app;
$this->databox = $app->getApplicationBox()->get_databox($this->reference->getDataboxId());
$this->collectionService = $app->getApplicationBox()->getCollectionService();
}
public function __sleep()
{
return array(
'collection',
'collectionVO',
'reference'
);
}
@@ -345,6 +343,7 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$this->collectionVO->setPublicWatermark($publi);
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -358,12 +357,13 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
try {
$this->collectionVO->setName($name);
}
catch (\InvalidArgumentException $e) {
} catch (\InvalidArgumentException $e) {
throw new Exception_InvalidArgument();
}
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
$this->dispatch(CollectionEvents::NAME_CHANGED, new NameChangedEvent($this));
return $this;
@@ -377,7 +377,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function set_label($code, $label)
{
$this->collectionVO->setLabel($code, $label);
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -407,7 +409,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function set_ord($ord)
{
$this->reference->setDisplayIndex($ord);
$this->getReferenceRepository()->save($this->reference);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -459,7 +463,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function set_prefs(DOMDocument $dom)
{
$this->collectionVO->setPreferences($dom->saveXML());
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this->collectionVO->getPreferences();
}
@@ -502,7 +508,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function disable()
{
$this->reference->disable();
$this->getReferenceRepository()->save($this->reference);
$this->app['repo.collections-registry']->purgeRegistry();
cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure');
@@ -515,7 +523,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function enable()
{
$this->reference->enable();
$this->getReferenceRepository()->save($this->reference);
$this->app['repo.collections-registry']->purgeRegistry();
cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure');
@@ -583,12 +593,14 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$fileContents = null;
if (! is_null($pathfile)) {
if (!is_null($pathfile)) {
$fileContents = file_get_contents($pathfile->getPathname());
}
$this->collectionVO->setLogo($fileContents);
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -601,6 +613,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$this->collectionService->resetWatermark($this->collectionVO);
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -613,6 +628,9 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
{
$this->collectionService->resetStamp($this->collectionVO, $record_id);
$this->getCollectionRepository()->save($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
@@ -622,70 +640,25 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
public function delete()
{
$this->collectionService->delete($this->databox, $this->collectionVO, $this->reference);
$this->getCollectionRepository()->delete($this->collectionVO);
$this->app['repo.collections-registry']->purgeRegistry();
}
/**
* @param Application $app
* @return $this
* @throws \Doctrine\DBAL\DBALException
*/
public function unmount_collection(Application $app)
public function unmount()
{
$this->collectionService->unmountCollection($this->reference);
$this->getReferenceRepository()->delete($this->reference);
$this->app['repo.collections-registry']->purgeRegistry();
return $this;
}
/**
* @param $base_id
* @param User $user
* @return bool
*/
public function set_admin($base_id, User $user)
{
$this->collectionService->grantAdminRights($this->reference, $user);
return true;
}
/**
* @param null $option
* @return string
*/
public function get_cache_key($option = null)
{
return 'collection_' . $this->get_coll_id() . ($option ? '_' . $option : '');
}
/**
* @param null $option
* @return string
*/
public function get_data_from_cache($option = null)
{
return $this->databox->get_data_from_cache($this->get_cache_key($option));
}
/**
* @param $value
* @param null $option
* @param int $duration
* @return bool
*/
public function set_data_to_cache($value, $option = null, $duration = 0)
{
return $this->databox->set_data_to_cache($value, $this->get_cache_key($option), $duration);
}
/**
* @param null $option
*/
public function delete_data_from_cache($option = null)
{
$this->getCollectionRepository()->save($this->collectionVO);
$this->databox->delete_data_from_cache($this->get_cache_key($option));
}
/**
* Tells whether registration is activated for provided collection or not.
*
@@ -704,7 +677,7 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
}
foreach ($element as $caninscript) {
if (false !== (bool) (string) $caninscript) {
if (false !== (bool)(string)$caninscript) {
return true;
}
}

View File

@@ -380,7 +380,7 @@ class databox extends base implements \Alchemy\Phrasea\Core\Thumbnail\Thumbnaile
public function unmount_databox()
{
foreach ($this->get_collections() as $collection) {
$collection->unmount_collection($this->app);
$collection->unmount();
}
$query = $this->app['phraseanet.user-query'];

View File

@@ -30,7 +30,7 @@ class AdminCollectionTest extends \PhraseanetAuthenticatedWebTestCase
self::$DI['app']['acl'] = new ACLProvider(self::$DI['app']);
foreach (self::$createdCollections as $collection) {
try {
$collection->unmount_collection(self::$DI['app']);
$collection->unmount();
} catch (\Exception $e) {
}
@@ -440,7 +440,7 @@ class AdminCollectionTest extends \PhraseanetAuthenticatedWebTestCase
$json = $this->getJson(self::$DI['client']->getResponse());
$this->assertTrue($json->success);
$this->assertEquals($collection->get_name(), 'test_rename_coll');
$collection->unmount_collection(self::$DI['app']);
$collection->unmount();
$collection->delete();
}
@@ -469,7 +469,7 @@ class AdminCollectionTest extends \PhraseanetAuthenticatedWebTestCase
$this->assertEquals($collection->get_label('nl'), 'netherlands label');
$this->assertEquals($collection->get_label('fr'), 'label français');
$this->assertEquals($collection->get_label('en'), 'label à l\'anglaise');
$collection->unmount_collection(self::$DI['app']);
$collection->unmount();
$collection->delete();
}

View File

@@ -34,7 +34,7 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
foreach (self::$createdCollections as $collection) {
try {
$collection->unmount_collection(self::$DI['app']);
$collection->unmount();
} catch (\Exception $e) {
}
@@ -515,7 +515,7 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase
$this->setAdmin(true);
$collection = $this->createOneCollection();
$collection->unmount_collection(self::$DI['app']);
$collection->unmount();
self::$DI['client']->request('POST', '/admin/databox/' . $collection->get_sbas_id() . '/collection/' . $collection->get_coll_id() . '/mount/', [
'othcollsel' => self::$DI['collection']->get_base_id()