getRepositoryByDatabox($databoxId);
}
public static function create(Application $app, databox $databox, appbox $appbox, $name, User $user = null)
{
$databoxId = $databox->get_sbas_id();
$repository = self::getRepository($app, $databoxId);
$collection = new CollectionVO($databoxId, 0, $name);
$repository->save($collection);
$repository = $app['repo.collection-references'];
$collectionReference = new CollectionReference(0, $databoxId, $collection->getCollectionId(), 0, true, '');
$repository->save($collectionReference);
$databox->delete_data_from_cache(databox::CACHE_COLLECTIONS);
$appbox->delete_data_from_cache(appbox::CACHE_LIST_BASES);
phrasea::reset_baseDatas($appbox);
if (null !== $user) {
$collection->set_admin($collectionReference->getBaseId(), $user);
}
$app['dispatcher']->dispatch(CollectionEvents::CREATED, new CreatedEvent($collection));
return $collection;
}
public static function mount_collection(Application $app, databox $databox, $coll_id, User $user)
{
$reference = new CollectionReference(0, $databox->get_sbas_id(), $coll_id, 0, true, '');
$app['repo.collection-references']->save($reference);
$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);
return $reference->getBaseId();
}
public static function getLogo($base_id, Application $app, $printname = false)
{
$base_id_key = $base_id . '_' . ($printname ? '1' : '0');
if ( ! isset(self::$_logos[$base_id_key])) {
if (is_file($app['root.path'] . '/config/minilogos/' . $base_id)) {
$name = phrasea::bas_labels($base_id, $app);
self::$_logos[$base_id_key] = '
';
} elseif ($printname) {
self::$_logos[$base_id_key] = phrasea::bas_labels($base_id, $app);
}
}
return isset(self::$_logos[$base_id_key]) ? self::$_logos[$base_id_key] : '';
}
public static function getWatermark($base_id)
{
if ( ! isset(self::$_watermarks['base_id'])) {
if (is_file(__DIR__ . '/../../config/wm/' . $base_id))
self::$_watermarks['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 (is_file(__DIR__ . '/../../config/presentation/' . $base_id))
self::$_presentations['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 (is_file(__DIR__ . '/../../config/stamp/' . $base_id))
self::$_stamps['base_id'] = '
';
}
return isset(self::$_stamps['base_id']) ? self::$_stamps['base_id'] : '';
}
public static function purge()
{
self::$_collections = [];
}
/**
* @param Application $app
* @param int $base_id
* @return collection
*/
public static function getByBaseId(Application $app, $base_id)
{
/** @var CollectionReferenceRepository $referenceRepository */
$referenceRepository = $app['repo.collection-references'];
$reference = $referenceRepository->find($base_id);
if (!$reference) {
throw new Exception_Databox_CollectionNotFound(sprintf(
"Collection with base_id %s could not be found",
$base_id
));
}
$repository = self::getRepository($app, $reference->getDataboxId());
$collection = $repository->find($reference->getCollectionId());
if (!$collection) {
throw new Exception_Databox_CollectionNotFound(sprintf(
"Collection with base_id %s could not be found",
$base_id
));
}
if (!$app['conf.restrictions']->isCollectionAvailable($collection)) {
throw new Exception_Databox_CollectionNotFound(sprintf(
'Collection `%d` is not available here.',
$collection->get_base_id()
));
}
return $collection;
}
/**
* @param Application $app
* @param databox $databox
* @param int $coll_id
* @return collection
*/
public static function getByCollectionId(Application $app, databox $databox, $coll_id)
{
assert(is_int($coll_id));
$repository = self::getRepository($app, $databox->get_sbas_id());
$collection = $repository->find($coll_id);
if (!$collection) {
throw new Exception_Databox_CollectionNotFound(sprintf(
"Collection with collection ID %d could not be found",
$coll_id
));
}
if (!$app['conf.restrictions']->isCollectionAvailable($collection)) {
throw new Exception_Databox_CollectionNotFound(sprintf(
'Collection `%d` is not available here.',
$collection->get_base_id()
));
}
return $collection;
}
/**
* @var Application
*/
protected $app;
/**
* @var CollectionService
*/
protected $collectionService;
/**
* @var databox
*/
protected $databox;
/**
* @var CollectionVO
*/
protected $collectionVO;
/**
* @var CollectionReference
*/
protected $reference;
/**
* @param Application $app
* @param CollectionVO $collection
* @param CollectionReference $reference
* @internal param $baseId
* @internal param 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;
}
/**
* @param $eventName
* @param CollectionEvent $event
*/
private function dispatch($eventName, CollectionEvent $event)
{
$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)
{
$this->app = $app;
$this->databox = $app->getApplicationBox()->get_databox($this->reference->getDataboxId());
}
public function __sleep()
{
return array(
'collection',
'reference'
);
}
/**
* @return CollectionReference
*/
public function getReference()
{
return $this->reference;
}
/**
* @return bool
*/
public function is_active()
{
return $this->reference->isActive();
}
/**
*
* @return databox
*/
public function get_databox()
{
return $this->databox;
}
/**
* @return \Doctrine\DBAL\Connection
*/
public function get_connection()
{
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
*/
public function getRootIdentifier()
{
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
*/
public function updateThumbnail($thumbnailType, File $file = null)
{
switch ($thumbnailType) {
case ThumbnailManager::TYPE_WM;
$this->reset_watermark();
break;
case ThumbnailManager::TYPE_LOGO:
$this->update_logo($file);
break;
case ThumbnailManager::TYPE_PRESENTATION:
break;
case ThumbnailManager::TYPE_STAMP:
$this->reset_stamp();
break;
default:
throw new \InvalidArgumentException('Unsupported thumbnail type.');
}
}
/**
* @return int|null
* @throws \Doctrine\DBAL\DBALException
*/
public function get_record_amount()
{
return $this->collectionService->getRecordCount($this->collectionVO);
}
/**
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function get_record_details()
{
return $this->collectionService->getRecordDetails($this->collectionVO);
}
/**
* @param SplFileInfo $pathfile
* @return $this
*/
public function update_logo(\SplFileInfo $pathfile = null)
{
$fileContents = null;
if (! is_null($pathfile)) {
$fileContents = file_get_contents($pathfile->getPathname());
}
$this->collectionVO->setLogo($fileContents);
$this->getCollectionRepository()->save($this->collectionVO);
return $this;
}
/**
* @return $this
* @throws \Doctrine\DBAL\DBALException
*/
public function reset_watermark()
{
$this->collectionService->resetWatermark($this->collectionVO);
return $this;
}
/**
* @param null $record_id
* @return $this
* @throws \Doctrine\DBAL\DBALException
*/
public function reset_stamp($record_id = null)
{
$this->collectionService->resetStamp($this->collectionVO, $record_id);
return $this;
}
/**
* @throws \Doctrine\DBAL\DBALException
*/
public function delete()
{
$this->collectionService->delete($this->databox, $this->collectionVO, $this->reference);
}
/**
* @param Application $app
* @return $this
* @throws \Doctrine\DBAL\DBALException
*/
public function unmount_collection(Application $app)
{
$this->collectionService->unmountCollection($this->reference);
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.
*
* @return boolean
*/
public function isRegistrationEnabled()
{
if (false === $xml = simplexml_load_string($this->get_prefs())) {
return false;
}
$element = $xml->xpath('/baseprefs/caninscript');
if (count($element) === 0) {
return $this->databox->isRegistrationEnabled();
}
foreach ($element as $caninscript) {
if (false !== (bool) (string) $caninscript) {
return true;
}
}
return false;
}
/**
* Gets terms of use.
*
* @return null|string
*/
public function getTermsOfUse()
{
if (false === $xml = simplexml_load_string($this->get_prefs())) {
return null;
}
foreach ($xml->xpath('/baseprefs/cgu') as $sbpcgu) {
return $sbpcgu->saveXML();
}
}
}