diff --git a/lib/Alchemy/Phrasea/Collection/Collection.php b/lib/Alchemy/Phrasea/Collection/Collection.php new file mode 100644 index 0000000000..b7e0d3f490 --- /dev/null +++ b/lib/Alchemy/Phrasea/Collection/Collection.php @@ -0,0 +1,229 @@ +databoxId = (int) $databoxId; + $this->collectionId = (int) $collectionId; + $this->name = (string) $name; + $this->preferences = << + + 0 + + +EOT; + } + + /** + * @return int + */ + public function getDataboxId() + { + return $this->databoxId; + } + + /** + * @return int + */ + public function getCollectionId() + { + return $this->collectionId; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $name = trim(strip_tags($name)); + + if ($name === '') { + throw new \InvalidArgumentException(); + } + + $this->name = $name; + } + + /** + * @return \string[] + */ + public function getLabels() + { + return $this->labels; + } + + /** + * @param \string[] $labels + */ + public function setLabels($labels) + { + $this->labels = $labels; + } + + /** + * @param $lang + * @param bool $substitute + * @return string + */ + public function getLabel($lang, $substitute = true) + { + if (!array_key_exists($lang, $this->labels)) { + throw new \InvalidArgumentException(sprintf('Code %s is not defined', $lang)); + } + + if ($substitute) { + return isset($this->labels[$lang]) ? $this->labels[$lang] : $this->name; + } else { + return $this->labels[$lang]; + } + } + + /** + * @param $lang + * @param $label + */ + public function setLabel($lang, $label) + { + if (!array_key_exists($lang, $this->labels)) { + throw new \InvalidArgumentException(sprintf("Language '%s' is not defined.", $lang)); + } + + $this->labels[$lang] = $label; + } + + /** + * @return \int[]|string|null + */ + public function getLogo() + { + return $this->logo; + } + + /** + * @param \int[]|string $logo + */ + public function setLogo($logo) + { + $this->logo = $logo; + } + + /** + * @return \DateTimeInterface + */ + public function getLogoUpdatedAt() + { + return $this->logoUpdatedAt; + } + + /** + * @return string + */ + public function getPublicWatermark() + { + return $this->publicWatermark; + } + + /** + * @param string $publicWatermark + */ + public function setPublicWatermark($publicWatermark) + { + if (! in_array($publicWatermark, ['none', 'wm', 'stamp'])) { + return; + } + + $this->publicWatermark = $publicWatermark; + } + + /** + * @return string + */ + public function getPreferences() + { + return $this->preferences; + } + + /** + * @param string $preferences + */ + public function setPreferences($preferences) + { + $this->preferences = $preferences; + } + + /** + * @return CollectionReference + */ + public function getCollectionReference() + { + return $this->collectionReference; + } + + /** + * @param CollectionReference $collectionReference + */ + public function setCollectionReference($collectionReference) + { + $this->collectionReference = $collectionReference; + } +} diff --git a/lib/Alchemy/Phrasea/Collection/CollectionRepository.php b/lib/Alchemy/Phrasea/Collection/CollectionRepository.php index 1f267a4c50..aadf24148c 100644 --- a/lib/Alchemy/Phrasea/Collection/CollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/CollectionRepository.php @@ -17,8 +17,8 @@ interface CollectionRepository public function find($collectionId); /** - * @param \collection $collection + * @param Collection $collection * @return void */ - public function save(\collection $collection); + public function save(Collection $collection); } diff --git a/lib/Alchemy/Phrasea/Collection/CollectionService.php b/lib/Alchemy/Phrasea/Collection/CollectionService.php new file mode 100644 index 0000000000..3707aa0157 --- /dev/null +++ b/lib/Alchemy/Phrasea/Collection/CollectionService.php @@ -0,0 +1,270 @@ +app = $application; + $this->connection = $connection; + $this->connectionProvider = $connectionProvider; + } + + /** + * @param Collection $collection + * @return int|null + * @throws \Doctrine\DBAL\DBALException + */ + public function getRecordCount(Collection $collection) + { + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $sql = "SELECT COALESCE(COUNT(record_id), 0) AS recordCount FROM record WHERE coll_id = :coll_id"; + $stmt = $connection->prepare($sql); + $stmt->execute([':coll_id' => $collection->getCollectionId()]); + $rowbas = $stmt->fetch(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $amount = $rowbas ? (int) $rowbas["recordCount"] : null; + + return $amount; + } + + /** + * @param Collection $collection + * @return array + */ + public function getRecordDetails(Collection $collection) + { + $sql = "SELECT record.coll_id,name,COALESCE(asciiname, CONCAT('_',record.coll_id)) AS asciiname, + SUM(1) AS n, SUM(size) AS size + FROM record NATURAL JOIN subdef + INNER JOIN coll ON record.coll_id=coll.coll_id AND coll.coll_id = :coll_id + GROUP BY record.coll_id, subdef.name"; + + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $stmt = $connection->prepare($sql); + $stmt->execute([':coll_id' => $collection->getCollectionId()]); + $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $ret = []; + foreach ($rs as $row) { + $ret[] = [ + "coll_id" => (int) $row["coll_id"], + "name" => $row["name"], + "amount" => (int) $row["n"], + "size" => (int) $row["size"]]; + } + + return $ret; + } + + /** + * @param Collection $collection + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ + public function resetWatermark(Collection $collection) + { + $sql = 'SELECT path, file FROM record r INNER JOIN subdef s USING(record_id) + WHERE r.coll_id = :coll_id AND r.type="image" AND s.name="preview"'; + + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $stmt = $connection->prepare($sql); + $stmt->execute([':coll_id' => $collection->getCollectionId()]); + + while ($row2 = $stmt->fetch(\PDO::FETCH_ASSOC)) { + @unlink(\p4string::addEndSlash($row2['path']) . 'watermark_' . $row2['file']); + } + $stmt->closeCursor(); + + return $this; + } + + /** + * @param Collection $collection + * @param int|null $record_id + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ + public function resetStamp(Collection $collection, $record_id = null) + { + $sql = 'SELECT path, file FROM record r INNER JOIN subdef s USING(record_id) + WHERE r.coll_id = :coll_id + AND r.type="image" AND s.name IN ("preview", "document")'; + + + $params = [':coll_id' => $collection->getCollectionId()]; + + if ($record_id) { + $sql .= ' AND record_id = :record_id'; + $params[':record_id'] = $record_id; + } + + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $stmt = $connection->prepare($sql); + $stmt->execute($params); + + while ($row2 = $stmt->fetch(\PDO::FETCH_ASSOC)) { + @unlink(\p4string::addEndSlash($row2['path']) . 'stamp_' . $row2['file']); + } + $stmt->closeCursor(); + + return $this; + } + + /** + * @param \databox $databox + * @param Collection $collection + * @param CollectionReference $reference + * @throws \Doctrine\DBAL\DBALException + */ + public function delete(\databox $databox, Collection $collection, CollectionReference $reference) + { + while ($this->getRecordCount($collection) > 0) { + $this->emptyCollection($databox, $collection); + } + + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $sql = "DELETE FROM coll WHERE coll_id = :coll_id"; + $stmt = $connection->prepare($sql); + $stmt->execute([':coll_id' => $collection->getCollectionId()]); + $stmt->closeCursor(); + + $sql = "DELETE FROM bas WHERE base_id = :base_id"; + $stmt = $this->connection->prepare($sql); + $stmt->execute([':base_id' => $reference->getBaseId()]); + $stmt->closeCursor(); + + $sql = "DELETE FROM basusr WHERE base_id = :base_id"; + $stmt = $this->connection->prepare($sql); + $stmt->execute([':base_id' => $reference->getBaseId()]); + $stmt->closeCursor(); + + $this->app['manipulator.registration']->deleteRegistrationsOnCollection($this); + + return; + } + + /** + * @param \databox $databox + * @param Collection $collection + * @param int $pass_quantity + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ + public function emptyCollection(\databox $databox, Collection $collection, $pass_quantity = 100) + { + $pass_quantity = (int) $pass_quantity > 200 ? 200 : (int) $pass_quantity; + $pass_quantity = (int) $pass_quantity < 10 ? 10 : (int) $pass_quantity; + + $sql = "SELECT record_id FROM record WHERE coll_id = :coll_id + ORDER BY record_id DESC LIMIT 0, " . $pass_quantity; + + $connection = $this->connectionProvider->getConnection($collection->getDataboxId()); + + $stmt = $connection->prepare($sql); + $stmt->execute([':coll_id' => $collection->getCollectionId()]); + $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + foreach ($rs as $row) { + $record = $databox->get_record($row['record_id']); + $record->delete(); + unset($record); + } + + return $this; + } + + /** + * @param CollectionReference $reference + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ + public function unmountCollection(CollectionReference $reference) + { + $params = [':base_id' => $reference->getBaseId()]; + + $query = $this->app['phraseanet.user-query']; + $total = $query->on_base_ids([$reference->getBaseId()]) + ->include_phantoms(false) + ->include_special_users(true) + ->include_invite(true) + ->include_templates(true)->get_total(); + $n = 0; + + while ($n < $total) { + $results = $query->limit($n, 50)->execute()->get_results(); + + foreach ($results as $user) { + $this->app->getAclForUser($user)->delete_data_from_cache(\ACL::CACHE_RIGHTS_SBAS); + $this->app->getAclForUser($user)->delete_data_from_cache(\ACL::CACHE_RIGHTS_BAS); + } + + $n+=50; + } + + $sql = "DELETE FROM basusr WHERE base_id = :base_id"; + $stmt = $this->connection->prepare($sql); + $stmt->execute($params); + $stmt->closeCursor(); + + $sql = "DELETE FROM bas WHERE base_id = :base_id"; + $stmt = $this->connection->prepare($sql); + $stmt->execute($params); + $stmt->closeCursor(); + + $this->app['manipulator.registration']->deleteRegistrationsOnCollection($this); + } + + /** + * @param CollectionReference $reference + * @param User $user + */ + public function grantAdminRights(CollectionReference $reference, User $user) + { + $rights = [ + "canputinalbum" => "1", + "candwnldhd" => "1", + "nowatermark" => "1", + "candwnldpreview" => "1", + "cancmd" => "1", + "canadmin" => "1", + "actif" => "1", + "canreport" => "1", + "canpush" => "1", + "basusr_infousr" => "", + "canaddrecord" => "1", + "canmodifrecord" => "1", + "candeleterecord" => "1", + "chgstatus" => "1", + "imgtools" => "1", + "manage" => "1", + "modify_struct" => "1" + ]; + + $this->app->getAclForUser($user)->update_rights_to_base($reference->getBaseId(), $rights); + } +} diff --git a/lib/Alchemy/Phrasea/Collection/Repository/ArrayCacheCollectionRepository.php b/lib/Alchemy/Phrasea/Collection/Repository/ArrayCacheCollectionRepository.php index 02d62740b1..2902841f6b 100644 --- a/lib/Alchemy/Phrasea/Collection/Repository/ArrayCacheCollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Repository/ArrayCacheCollectionRepository.php @@ -2,6 +2,7 @@ namespace Alchemy\Phrasea\Collection\Repository; +use Alchemy\Phrasea\Collection\Collection; use Alchemy\Phrasea\Collection\CollectionRepository; class ArrayCacheCollectionRepository implements CollectionRepository @@ -48,7 +49,7 @@ class ArrayCacheCollectionRepository implements CollectionRepository return null; } - public function save(\collection $collection) + public function save(Collection $collection) { $this->collectionRepository->save($collection); diff --git a/lib/Alchemy/Phrasea/Collection/Repository/CachedCollectionRepository.php b/lib/Alchemy/Phrasea/Collection/Repository/CachedCollectionRepository.php index 796e6684e5..5dd84f7c53 100644 --- a/lib/Alchemy/Phrasea/Collection/Repository/CachedCollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Repository/CachedCollectionRepository.php @@ -10,6 +10,7 @@ namespace Alchemy\Phrasea\Collection\Repository; use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Collection\Collection; use Alchemy\Phrasea\Collection\CollectionRepository; use Doctrine\Common\Cache\Cache; @@ -86,7 +87,7 @@ final class CachedCollectionRepository implements CollectionRepository return null; } - public function save(\collection $collection) + public function save(Collection $collection) { $this->repository->save($collection); diff --git a/lib/Alchemy/Phrasea/Collection/Repository/DbalCollectionRepository.php b/lib/Alchemy/Phrasea/Collection/Repository/DbalCollectionRepository.php index 3a55b9554d..b261f6f78e 100644 --- a/lib/Alchemy/Phrasea/Collection/Repository/DbalCollectionRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Repository/DbalCollectionRepository.php @@ -2,6 +2,7 @@ namespace Alchemy\Phrasea\Collection\Repository; +use Alchemy\Phrasea\Collection\Collection; use Alchemy\Phrasea\Collection\CollectionFactory; use Alchemy\Phrasea\Collection\CollectionRepository; use Alchemy\Phrasea\Collection\Reference\CollectionReferenceRepository; @@ -33,6 +34,12 @@ class DbalCollectionRepository implements CollectionRepository */ private $collectionFactory; + /** + * @param $databoxId + * @param Connection $connection + * @param CollectionReferenceRepository $referenceRepository + * @param CollectionFactory $collectionFactory + */ public function __construct( $databoxId, Connection $connection, @@ -108,7 +115,7 @@ class DbalCollectionRepository implements CollectionRepository return null; } - public function save(\collection $collection) + public function save(Collection $collection) { } diff --git a/lib/classes/collection.php b/lib/classes/collection.php index efdfeb172a..b1e2c0ac28 100644 --- a/lib/classes/collection.php +++ b/lib/classes/collection.php @@ -10,8 +10,10 @@ */ use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Collection\Collection as CollectionVO; use Alchemy\Phrasea\Collection\CollectionRepository; use Alchemy\Phrasea\Collection\CollectionRepositoryRegistry; +use Alchemy\Phrasea\Collection\CollectionService; use Alchemy\Phrasea\Collection\Reference\CollectionReference; use Alchemy\Phrasea\Collection\Reference\CollectionReferenceRepository; use Alchemy\Phrasea\Core\Event\Collection\CollectionEvent; @@ -20,9 +22,7 @@ use Alchemy\Phrasea\Core\Event\Collection\CreatedEvent; use Alchemy\Phrasea\Core\Event\Collection\NameChangedEvent; use Alchemy\Phrasea\Core\Thumbnail\ThumbnailedElement; use Alchemy\Phrasea\Core\Thumbnail\ThumbnailManager; -use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Model\Entities\User; -use Doctrine\DBAL\Driver\Connection; use Symfony\Component\HttpFoundation\File\File; class collection implements cache_cacheableInterface, ThumbnailedElement @@ -39,17 +39,6 @@ class collection implements cache_cacheableInterface, ThumbnailedElement private static $_presentations = []; private static $_collections = []; - private static function getNewOrder(Connection $conn, $sbas_id) - { - $sql = "SELECT GREATEST(0, MAX(ord)) + 1 AS ord FROM bas WHERE sbas_id = :sbas_id"; - $stmt = $conn->prepare($sql); - $stmt->execute([':sbas_id' => $sbas_id]); - $ord = $stmt->fetch(\PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - return $ord['ord'] ?: 1; - } - /** * @param Application $app * @param $databoxId @@ -65,35 +54,15 @@ class collection implements cache_cacheableInterface, ThumbnailedElement public static function create(Application $app, databox $databox, appbox $appbox, $name, User $user = null) { - $sbas_id = $databox->get_sbas_id(); - $connbas = $databox->get_connection(); - $conn = $appbox->get_connection(); - $new_bas = false; + $databoxId = $databox->get_sbas_id(); - $prefs = << - - 0 - - -EOT; + $repository = self::getRepository($app, $databoxId); + $collection = new CollectionVO($databoxId, 0, $name); - $sql = "INSERT INTO coll (coll_id, asciiname, prefs, logo) - VALUES (null, :name, :prefs, '')"; - - $params = [ - ':name' => $name, - 'prefs' => $prefs, - ]; - - $stmt = $connbas->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $new_id = (int) $connbas->lastInsertId(); + $repository->save($collection); $repository = $app['repo.collection-references']; - $collectionReference = new CollectionReference(0, $sbas_id, $new_id, 0, true, ''); + $collectionReference = new CollectionReference(0, $databoxId, $collection->getCollectionId(), 0, true, ''); $repository->save($collectionReference); @@ -102,10 +71,8 @@ EOT; phrasea::reset_baseDatas($appbox); - $collection = self::getByCollectionId($app, $databox, $new_id); - if (null !== $user) { - $collection->set_admin($new_bas, $user); + $collection->set_admin($collectionReference->getBaseId(), $user); } $app['dispatcher']->dispatch(CollectionEvents::CREATED, new CreatedEvent($collection)); @@ -115,30 +82,21 @@ EOT; public static function mount_collection(Application $app, databox $databox, $coll_id, User $user) { - $sql = "INSERT INTO bas (base_id, active, server_coll_id, sbas_id, aliases, ord) - VALUES - (null, 1, :server_coll_id, :sbas_id, '', :ord)"; - $stmt = $databox->get_appbox()->get_connection()->prepare($sql); - $stmt->execute([ - ':server_coll_id' => $coll_id, - ':sbas_id' => $databox->get_sbas_id(), - ':ord' => self::getNewOrder($databox->get_appbox()->get_connection(), $databox->get_sbas_id()), - ]); - $stmt->closeCursor(); + $reference = new CollectionReference(0, $databox->get_sbas_id(), $coll_id, 0, true, ''); + + $app['repo.collection-references']->save($reference); - $new_bas = $databox->get_appbox()->get_connection()->lastInsertId(); $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, $new_bas); - $coll->set_admin($new_bas, $user); + $coll = self::getByBaseId($app, $reference->getBaseId()); + $coll->set_admin($reference->getBaseId(), $user); - return $new_bas; + return $reference->getBaseId(); } public static function getLogo($base_id, Application $app, $printname = false) @@ -270,76 +228,66 @@ EOT; */ protected $app; + /** + * @var CollectionService + */ + protected $collectionService; + /** * @var databox */ protected $databox; + /** + * @var CollectionVO + */ + protected $collectionVO; + /** * @var CollectionReference */ protected $reference; - /** - * @var string - */ - protected $name; - - /** - * @var string - */ - protected $preferences; - - /** - * @var string - */ - protected $pub_wm; - - /** - * @var string[] - */ - protected $labels = []; - - /** - * @var int[]|string - */ - protected $binary_logo; /** * @param Application $app - * @param $baseId + * @param CollectionVO $collection * @param CollectionReference $reference - * @param array $row + * @internal param $baseId + * @internal param array $row */ - public function __construct(Application $app, $baseId, CollectionReference $reference, array $row) + public function __construct(Application $app, CollectionVO $collection, CollectionReference $reference) { $this->app = $app; $this->databox = $app->getApplicationBox()->get_databox($reference->getDataboxId()); + $this->collection = $collection; $this->reference = $reference; - - $this->name = $row['asciiname']; - $this->available = true; - $this->pub_wm = $row['pub_wm']; - $this->preferences = $row['prefs']; - $this->labels = [ - 'fr' => $row['label_fr'], - 'en' => $row['label_en'], - 'de' => $row['label_de'], - 'nl' => $row['label_nl'], - ]; } - public function __sleep() + /** + * @param $eventName + * @param CollectionEvent $event + */ + private function dispatch($eventName, CollectionEvent $event) { - return array( - 'reference', - 'name', - 'preferences', - 'pub_wm', - 'labels', - 'binary_logo' - ); + $this->app['dispatcher']->dispatch($eventName, $event); + } + + /** + * @return CollectionRepository + */ + private function getCollectionRepository() + { + return self::getRepository($this->app, $this->reference->getDataboxId()); + } + + /** + * @return CollectionReferenceRepository + */ + private function getReferenceRepository() + { + return $this->app['repo.collection-references']; } public function hydrate(Application $app) @@ -348,6 +296,14 @@ EOT; $this->databox = $app->getApplicationBox()->get_databox($this->reference->getDataboxId()); } + public function __sleep() + { + return array( + 'collection', + 'reference' + ); + } + /** * @return CollectionReference */ @@ -356,84 +312,6 @@ EOT; return $this->reference; } - private function dispatch($eventName, CollectionEvent $event) - { - $this->app['dispatcher']->dispatch($eventName, $event); - } - - public function enable(appbox $appbox) - { - $sql = 'UPDATE bas SET active = "1" WHERE base_id = :base_id'; - $stmt = $appbox->get_connection()->prepare($sql); - $stmt->execute([':base_id' => $this->get_base_id()]); - $stmt->closeCursor(); - - $this->reference->enable(); - - $this->delete_data_from_cache(); - $appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES); - $this->databox->delete_data_from_cache(databox::CACHE_COLLECTIONS); - - cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure'); - - return $this; - } - - public function get_ord() - { - return $this->reference->getDisplayIndex(); - } - - public function set_ord($ord) - { - $this->app->getApplicationBox()->set_collection_order($this, $ord); - $this->delete_data_from_cache(); - $this->app->getApplicationBox()->delete_data_from_cache(appbox::CACHE_LIST_BASES); - - $this->reference->setDisplayIndex($ord); - - return $this; - } - - public function disable(appbox $appbox) - { - $sql = 'UPDATE bas SET active=0 WHERE base_id = :base_id'; - $stmt = $appbox->get_connection()->prepare($sql); - $stmt->execute([':base_id' => $this->get_base_id()]); - $stmt->closeCursor(); - - $this->reference->disable(); - - $this->delete_data_from_cache(); - $appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES); - $this->databox->delete_data_from_cache(databox::CACHE_COLLECTIONS); - cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure'); - - return $this; - } - - public function empty_collection($pass_quantity = 100) - { - $pass_quantity = (int) $pass_quantity > 200 ? 200 : (int) $pass_quantity; - $pass_quantity = (int) $pass_quantity < 10 ? 10 : (int) $pass_quantity; - - $sql = "SELECT record_id FROM record WHERE coll_id = :coll_id - ORDER BY record_id DESC LIMIT 0, " . $pass_quantity; - - $stmt = $this->databox->get_connection()->prepare($sql); - $stmt->execute([':coll_id' => $this->get_coll_id()]); - $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - foreach ($rs as $row) { - $record = $this->databox->get_record($row['record_id']); - $record->delete(); - unset($record); - } - - return $this; - } - /** * @return bool */ @@ -459,6 +337,157 @@ EOT; return $this->databox->get_connection(); } + /** + * @param $publi + * @return $this + */ + public function set_public_presentation($publi) + { + $this->collectionVO->setPublicWatermark($publi); + $this->getCollectionRepository()->save($this->collectionVO); + + return $this; + } + + /** + * @param $name + * @return $this + * @throws Exception_InvalidArgument + */ + public function set_name($name) + { + try { + $this->collectionVO->setName($name); + } + catch (\InvalidArgumentException $e) { + throw new Exception_InvalidArgument(); + } + + $this->getCollectionRepository()->save($this->collectionVO); + $this->dispatch(CollectionEvents::NAME_CHANGED, new NameChangedEvent($this)); + + return $this; + } + + /** + * @param $code + * @param $label + * @return $this + */ + public function set_label($code, $label) + { + $this->collectionVO->setLabel($code, $label); + $this->getCollectionRepository()->save($this->collectionVO); + + return $this; + } + + /** + * @param $code + * @param bool $substitute + * @return string + */ + public function get_label($code, $substitute = true) + { + return $this->collectionVO->getLabel($code, $substitute); + } + + /** + * @return int + */ + public function get_ord() + { + return $this->reference->getDisplayIndex(); + } + + /** + * @param $ord + * @return $this + */ + public function set_ord($ord) + { + $this->reference->setDisplayIndex($ord); + $this->getReferenceRepository()->save($this->reference); + + return $this; + } + + /** + * @return int[]|null|string + */ + public function get_binary_minilogos() + { + return $this->collectionVO->getLogo(); + } + + /** + * @return int + */ + public function get_base_id() + { + return $this->reference->getBaseId(); + } + + /** + * @return int + */ + public function get_sbas_id() + { + return $this->reference->getDataboxId(); + } + + /** + * @return int + */ + public function get_coll_id() + { + return $this->reference->getCollectionId(); + } + + /** + * @return string + */ + public function get_prefs() + { + return $this->collectionVO->getPreferences(); + } + + /** + * @param DOMDocument $dom + * @return string + */ + public function set_prefs(DOMDocument $dom) + { + $this->collectionVO->setPreferences($dom->saveXML()); + $this->getCollectionRepository()->save($this->collectionVO); + + return $this->collectionVO->getPreferences(); + } + + /** + * @return string + */ + public function get_name() + { + return $this->collectionVO->getName(); + } + + /** + * @return string + */ + public function get_pub_wm() + { + return $this->collectionVO->getName(); + } + + /** + * @return bool + */ + public function is_available() + { + return true; + } + /** * @return int */ @@ -467,6 +496,44 @@ EOT; return $this->reference->getBaseId(); } + /** + * @return $this + */ + public function disable() + { + $this->reference->disable(); + $this->getReferenceRepository()->save($this->reference); + + cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure'); + + return $this; + } + + /** + * @return $this + */ + public function enable() + { + $this->reference->enable(); + $this->getReferenceRepository()->save($this->reference); + + cache_databox::update($this->app, $this->databox->get_sbas_id(), 'structure'); + + return $this; + } + + /** + * @param int $pass_quantity + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ + public function empty_collection($pass_quantity = 100) + { + $this->collectionService->emptyCollection($this->databox, $this->collectionVO, $pass_quantity); + + return $this; + } + /** * @param string $thumbnailType * @param File $file @@ -490,345 +557,133 @@ EOT; } } - public function set_public_presentation($publi) - { - if (in_array($publi, ['none', 'wm', 'stamp'])) { - $sql = 'UPDATE coll SET pub_wm = :pub_wm WHERE coll_id = :coll_id'; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':pub_wm' => $publi, ':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); - - $this->pub_wm = $publi; - - $this->delete_data_from_cache(); - } - - return $this; - } - - public function set_name($name) - { - $name = trim(strip_tags($name)); - - if ($name === '') - throw new Exception_InvalidArgument (); - - $sql = "UPDATE coll SET asciiname = :asciiname - WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':asciiname' => $name, ':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); - - $this->name = $name; - - $this->delete_data_from_cache(); - - phrasea::reset_baseDatas($this->databox->get_appbox()); - - $this->dispatch(CollectionEvents::NAME_CHANGED, new NameChangedEvent($this)); - - return $this; - } - - public function set_label($code, $label) - { - if (!array_key_exists($code, $this->labels)) { - throw new InvalidArgumentException(sprintf('Code %s is not defined', $code)); - } - - $sql = "UPDATE coll SET label_$code = :label - WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':label' => $label, ':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); - - $this->labels[$code] = $label; - - $this->delete_data_from_cache(); - - phrasea::reset_baseDatas($this->databox->get_appbox()); - - return $this; - } - - public function get_label($code, $substitute = true) - { - if (!array_key_exists($code, $this->labels)) { - throw new InvalidArgumentException(sprintf('Code %s is not defined', $code)); - } - - if ($substitute) { - return isset($this->labels[$code]) ? $this->labels[$code] : $this->name; - } else { - return $this->labels[$code]; - } - } - + /** + * @return int|null + * @throws \Doctrine\DBAL\DBALException + */ public function get_record_amount() { - $sql = "SELECT COUNT(record_id) AS n FROM record WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':coll_id' => $this->get_coll_id()]); - $rowbas = $stmt->fetch(PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - $amount = $rowbas ? (int) $rowbas["n"] : null; - - return $amount; + return $this->collectionService->getRecordCount($this->collectionVO); } + /** + * @return array + * @throws \Doctrine\DBAL\DBALException + */ public function get_record_details() { - $sql = "SELECT record.coll_id,name,COALESCE(asciiname, CONCAT('_',record.coll_id)) AS asciiname, - SUM(1) AS n, SUM(size) AS size - FROM record NATURAL JOIN subdef - INNER JOIN coll ON record.coll_id=coll.coll_id AND coll.coll_id = :coll_id - GROUP BY record.coll_id, subdef.name"; - - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':coll_id' => $this->get_coll_id()]); - $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - $ret = []; - foreach ($rs as $row) { - $ret[] = [ - "coll_id" => (int) $row["coll_id"], - "name" => $row["name"], - "amount" => (int) $row["n"], - "size" => (int) $row["size"]]; - } - - return $ret; + return $this->collectionService->getRecordDetails($this->collectionVO); } + /** + * @param SplFileInfo $pathfile + * @return $this + */ public function update_logo(\SplFileInfo $pathfile = null) { - if (is_null($pathfile)) { - $this->binary_logo = null; - } else { - $this->binary_logo = file_get_contents($pathfile->getPathname()); + $fileContents = null; + + if (! is_null($pathfile)) { + $fileContents = file_get_contents($pathfile->getPathname()); } - $sql = "UPDATE coll SET logo = :logo, majLogo=NOW() WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':logo' => $this->binary_logo, ':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); + $this->collectionVO->setLogo($fileContents); + $this->getCollectionRepository()->save($this->collectionVO); return $this; } + /** + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ public function reset_watermark() { - $sql = 'SELECT path, file FROM record r INNER JOIN subdef s USING(record_id) - WHERE r.coll_id = :coll_id AND r.type="image" AND s.name="preview"'; - - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':coll_id' => $this->get_coll_id()]); - - while ($row2 = $stmt->fetch(PDO::FETCH_ASSOC)) { - @unlink(p4string::addEndSlash($row2['path']) . 'watermark_' . $row2['file']); - } - $stmt->closeCursor(); + $this->collectionService->resetWatermark($this->collectionVO); return $this; } + /** + * @param null $record_id + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ public function reset_stamp($record_id = null) { - $sql = 'SELECT path, file FROM record r INNER JOIN subdef s USING(record_id) - WHERE r.coll_id = :coll_id - AND r.type="image" AND s.name IN ("preview", "document")'; - - $params = [':coll_id' => $this->get_coll_id()]; - - if ($record_id) { - $sql .= ' AND record_id = :record_id'; - $params[':record_id'] = $record_id; - } - - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute($params); - - while ($row2 = $stmt->fetch(PDO::FETCH_ASSOC)) { - @unlink(p4string::addEndSlash($row2['path']) . 'stamp_' . $row2['file']); - } - $stmt->closeCursor(); + $this->collectionService->resetStamp($this->collectionVO, $record_id); return $this; } + /** + * @throws \Doctrine\DBAL\DBALException + */ public function delete() { - while ($this->get_record_amount() > 0) { - $this->empty_collection(); - } - - $sql = "DELETE FROM coll WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); - - $appbox = $this->databox->get_appbox(); - - $sql = "DELETE FROM bas WHERE base_id = :base_id"; - $stmt = $appbox->get_connection()->prepare($sql); - $stmt->execute([':base_id' => $this->get_base_id()]); - $stmt->closeCursor(); - - $sql = "DELETE FROM basusr WHERE base_id = :base_id"; - $stmt = $appbox->get_connection()->prepare($sql); - $stmt->execute([':base_id' => $this->get_base_id()]); - $stmt->closeCursor(); - - $this->app['manipulator.registration']->deleteRegistrationsOnCollection($this); - - $this->get_databox()->delete_data_from_cache(databox::CACHE_COLLECTIONS); - $appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES); - phrasea::reset_baseDatas($appbox); - - return; - } - - public function get_binary_minilogos() - { - return $this->binary_logo; - } - - public function get_base_id() - { - return $this->reference->getBaseId(); - } - - public function get_sbas_id() - { - return $this->reference->getDataboxId(); - } - - public function get_coll_id() - { - return $this->reference->getCollectionId(); - } - - public function get_prefs() - { - return $this->preferences; - } - - public function set_prefs(DOMDocument $dom) - { - $this->preferences = $dom->saveXML(); - - $sql = "UPDATE coll SET prefs = :prefs WHERE coll_id = :coll_id"; - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':prefs' => $this->preferences, ':coll_id' => $this->get_coll_id()]); - $stmt->closeCursor(); - - $this->delete_data_from_cache(); - - return $this->preferences; - } - - public function get_name() - { - return $this->name; - } - - public function get_pub_wm() - { - return $this->pub_wm; - } - - public function is_available() - { - return $this->available; + $this->collectionService->delete($this->databox, $this->collectionVO, $this->reference); } + /** + * @param Application $app + * @return $this + * @throws \Doctrine\DBAL\DBALException + */ public function unmount_collection(Application $app) { - $params = [':base_id' => $this->get_base_id()]; - - $query = $app['phraseanet.user-query']; - $total = $query->on_base_ids([$this->get_base_id()]) - ->include_phantoms(false) - ->include_special_users(true) - ->include_invite(true) - ->include_templates(true)->get_total(); - $n = 0; - while ($n < $total) { - $results = $query->limit($n, 50)->execute()->get_results(); - foreach ($results as $user) { - $app->getAclForUser($user)->delete_data_from_cache(ACL::CACHE_RIGHTS_SBAS); - $app->getAclForUser($user)->delete_data_from_cache(ACL::CACHE_RIGHTS_BAS); - } - $n+=50; - } - - $sql = "DELETE FROM basusr WHERE base_id = :base_id"; - $stmt = $app->getApplicationBox()->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $sql = "DELETE FROM bas WHERE base_id = :base_id"; - $stmt = $app->getApplicationBox()->get_connection()->prepare($sql); - $stmt->execute($params); - $stmt->closeCursor(); - - $this->app['manipulator.registration']->deleteRegistrationsOnCollection($this); - - phrasea::reset_baseDatas($app['phraseanet.appbox']); + $this->collectionService->unmountCollection($this->reference); return $this; } + /** + * @param $base_id + * @param User $user + * @return bool + */ public function set_admin($base_id, User $user) { - - $rights = [ - "canputinalbum" => "1", - "candwnldhd" => "1", - "nowatermark" => "1", - "candwnldpreview" => "1", - "cancmd" => "1", - "canadmin" => "1", - "actif" => "1", - "canreport" => "1", - "canpush" => "1", - "basusr_infousr" => "", - "canaddrecord" => "1", - "canmodifrecord" => "1", - "candeleterecord" => "1", - "chgstatus" => "1", - "imgtools" => "1", - "manage" => "1", - "modify_struct" => "1" - ]; - - $this->app->getAclForUser($user)->update_rights_to_base($base_id, $rights); + $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) { - self::getRepository($this->app, $this->reference->getDataboxId())->save($this); - - return $this->databox->delete_data_from_cache($this->get_cache_key($option)); + $this->getCollectionRepository()->save($this->collectionVO); + $this->databox->delete_data_from_cache($this->get_cache_key($option)); } /** @@ -849,7 +704,7 @@ EOT; } foreach ($element as $caninscript) { - if (false !== (Boolean) (string) $caninscript) { + if (false !== (bool) (string) $caninscript) { return true; } } @@ -860,14 +715,12 @@ EOT; /** * Gets terms of use. * - * @param \collection $collection - * * @return null|string */ public function getTermsOfUse() { if (false === $xml = simplexml_load_string($this->get_prefs())) { - return; + return null; } foreach ($xml->xpath('/baseprefs/cgu') as $sbpcgu) {