Files
Phraseanet/lib/Alchemy/Phrasea/Model/Repositories/BasketRepository.php
jygaulier fbccec009e PHRAS-3602_shared-baskets-step1 WIP DO NOT MERGE (#3929)
* PHRAS-3602 : migrate validations to baskets
WIP [skip ci]

* PHRAS-3602 : migrate validations code to baskets code (wip)
WIP [skip ci]

* PHRAS-3602 : migrate validations to baskets
WIP [skip ci]

* PHRAS-3602 : migrate validations to baskets
WIP [skip ci]

* PHRAS-3602 : fake commit to run tests

* PHRAS-3602 : migrate validations to baskets
fix typo

* PHRAS-3602 : migrate validations to baskets
fixed (some) tests

* PHRAS-3602 : migrate validations to baskets
fixed (some) tests

* PHRAS-3602 : migrate validations to baskets
fixed (some) tests. need to remove method():return_type signature because of old phpunit which does not mock it

* PHRAS-3602 : migrate validations to baskets
fix

* PHRAS-3602 : migrate validations to baskets
fix err 500 when a "vote" (feedback) is deployed in wz

* PHRAS-3602 : migrate validations to baskets
fix missing votes for element+participant
fix update vote dates on basket

* PHRAS-3602 : ux
bump production-client to 34
WIP [skip ci]

* PHRAS-3602 : rebase (bump production-client to 37)
WIP [skip ci]

* PHRAS-3602 : refacto css/twig/templates/... ; replace many png's by fonts
WIP [skip ci]

* PHRAS-3602 : fix icon / css
WIP

* PHRAS-3602 : better icon align ; back button color
fix : save button after adding a user
WIP [skip ci]

* PHRAS-3602 : restore sharebasket controller
todo : implement modification right ; owner is participant ?
WIP [skip ci]

* PHRAS-3602 : set "canModifiy" ; cleanup
WIP [skip ci]

* PHRAS-3602 : respect "can_modify" on shared basket
WIP [skip ci]

* PHRAS-3602 : fix badge icon bg ; fix button css
WIP [skip ci]

* PHRAS-3602 : fix css & cleanup
WIP [skip ci]

* PHRAS-3602 : fix 500 on preview/feedback tab
WIP [skip ci]

* PHRAS-3602 : fix allow to display dlg without selection

* PHRAS-3602 : bump version to 4.1.6-rc1 ; prodclient=38
WIP [skip ci]

* PHRAS-3602 : factorize baskets menu (wip)
WIP [skip ci]

* PHRAS-3602 : modify already shared or feedback basket (general menu)
4th icon on badges (vote != modify)
quitshare option (todo back)
allow fa-icons in toolbar

WIP [skip ci]

* PHRAS-3602 : add share end-date (todo:db write)
add 4th general toggle button
fix badge selection bug
fix css badges zone (form position)
WIP [skip ci]

* PHRAS-3602 : big refacto to use "sharebasket" vocab.
fusion "feedback/sharebasket" removed "feedback"
adaptative ux: 1 "can_agree" ==> feedback display
bump production-client to v50
todo: save shr/fbk end-dates
todo: rename "pushXXX" to pushAndShare ?
WIP [skip ci]

* PHRAS-3602 : share / feedback expiration dates are saved in db
date pickers with delta menu
better adaptive ux
todo: move select general togglers
WIP [skip ci]

* PHRAS-3602 : revert validation* tables (remove "dead" rename)
drop all foreign keys from validation*
WIP [skip ci]

* PHRAS-3602 : restore deleted fields in validation, regenerate proxies
WIP [skip ci]

* PHRAS-3602 : fix initiator_id
WIP [skip ci]

* fix initiator_id-bis

* PHRAS-3602 : css for input-text with glued button
dynamic load of users-lists (left zone) = less duplicated code
fix : users-lists  works after refresh
WIP [skip ci]

* PHRAS-3602 : fix users-lists manager (orange) : go flex !

* PHRAS-3602 : empty dist to ease rebase
WIP [skip ci]

* PHRAS-3602 : rebase
WIP [skip ci]

* PHRAS-3602 : new ux for "owner"
feedback mode is an independent toggle
fix "missing mandatory parameter"
WIP [skip ci]

* PHRAS-3602 : fix css of owner badge
WIP [skip ci]

* PHRAS-3602 : fix handling of feedback initiator
WIP [skip ci]

* PHRAS-3602 : better wss to follow "skins"
full roboto
removed useless class "with-button"
WIP [skip ci]

* PHRAS-3602 : fix "a token require a validation"
temporary fix 500 due to send email to (null) vote-initiator for simple share
todo: send a specific email for simple share
WIP [skip ci]

* PHRAS-3602 : different emails depending if user can vote or not
WIP [skip ci]

* PHRAS-3602 : expired shared baskets removed from wz
WIP [skip ci]

* PHRAS-3602 : fix logic error on last sql
WIP [skip ci]

* PHRAS-3602 : fix missing shared baskets on wz
WIP [skip ci]

* PHRAS-3602 : fix blinking of wz/basket detail
now only the title:hover displays detail
WIP [skip ci]

* PHRAS-3602 : rebase
WIP [skip ci]

* PHRAS-3602 : update basket proxy
WIP [skip ci]

* PHRAS-3602 : fix forever "unread" basket
WIP [skip ci]

* PHRAS-3602 : fix display of unread basket ; sync "eye" icon with css change
WIP [skip ci]

* PHRAS-3602 : "quitshare" action works
WIP [skip ci]

* PHRAS-3602 : missing dist files
WIP [skip ci]

* PHRAS-3602 : cleanup & run ci

* PHRAS-3602 : disable failing unit test ; remove blue on onread basket

* PHRAS-3602 : separate "vote" & "share" emails templates & u-tests

* PHRAS-3602 : add 2 icons "stack" to icomoon set
WIP [skip ci]

* PHRAS-3602 : fix test

* PHRAS-3602 : fix test ; add test for simple share email notification

* PHRAS-3602 : changed proxy

* PHRAS-3602 : colored basket icons
WIP [skip ci]

* PHRAS-3602 : circle basket icons
WIP [skip ci]

* PHRAS-3602 : fix wz filters, add "share" filter.
todo: fix fr writing (new string)
WIP [skip ci]

* PHRAS-3602 : fix "share" dichotomy : use "shared" for wz filter
WIP [skip ci]

* PHRAS-3602 : fix PHRAS-3624 ; PHRAS-3623 ; now adding/removing a user from user list is immediate (no more save button)
WIP [skip ci]

* PHRAS-3602 : fix PHRAS-3647 ; shared basket are listed in api (for list and related record)

* PHRAS-3602 : fix due to failing test

* PHRAS-3602 : fix: can load a 1000 users list
todo : move slow code to worker

* PHRAS-3602 : fix due to failing test

* PHRAS-3468 : (fixed in 3602) fix basket content still visible when baskets are hidden (wz-filter)

* PHRAS-3602 : add "wip" baskets with notification & lock
todo : move slow code from message to worker

* PHRAS-3602 : fix tests due to accidental rename

* PHRAS-3602 : fix : menu closes when mouse out : bump to 4.1.6-rc3
todo : fast move to another basket makes the menu appear on top ?

* add shareBasket worker

* fix test

* PHRAS-3602 : fix : rights buttons on badges now works for users added from search (did work only from loading list)

* PHRAS-3590

Co-authored-by: aynsix <asr@esokia-webagency.com>
Co-authored-by: Nicolas Maillat <maillat@alchemy.fr>
2022-04-07 17:07:43 +02:00

374 lines
12 KiB
PHP

<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Model\Repositories;
use Alchemy\Phrasea\Model\Entities\Basket;
use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use PDO;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Translation\TranslatorInterface;
class BasketRepository extends EntityRepository
{
const MYBASKETS = 'my baskets';
const RECEIVED = 'received';
const VALIDATION_SENT = 'validation_sent';
const VALIDATION_DONE = 'validation_done';
/**
* @var TranslatorInterface
*/
private $translator;
public function setTranslator(TranslatorInterface $translator = null)
{
$this->translator = $translator;
}
/**
* @param string $id
* @param array $parameters
* @param string $domain
* @param string $locale
* @return string
*/
private function trans($id, $parameters = [], $domain = null, $locale = null)
{
if ($this->translator) {
return $this->translator->trans( /** @Ignore */ $id, $parameters, $domain, $locale);
}
return $id;
}
/**
* Returns all basket for a given user that are not marked as archived
*
* @param User $user
* @param null|string $sort
* @return Basket[]
*/
public function findActiveByUser(User $user, $sort = null)
{
// checked : 4 usages, "b.elements" is useless
$dql = "SELECT b\n"
. " FROM Phraseanet:Basket b\n"
// . " LEFT JOIN b.elements e\n" //
. " WHERE b.user = :usr_id\n"
. " AND b.archived = false";
if ($sort == 'date') {
$dql .= "\n ORDER BY b.created DESC";
}
elseif ($sort == 'name') {
$dql .= "\n ORDER BY b.name ASC";
}
$query = $this->_em->createQuery($dql);
$query->setParameters(['usr_id' => $user->getId()]);
return $query->getResult();
}
/**
* Returns all unread basket for a given user that are not marked as archived
*
* @param User $user
* @return Basket[]
*/
public function findUnreadActiveByUser(User $user)
{
// too bad dql does not support "UNION" so we first get ids in sql...
// grouping the 2 parts as 1 requires "LEFT JOIN"'s , it was really slow.
$sql = "SELECT b.id\n"
. " FROM Baskets b\n"
. " WHERE b.archived = 0\n"
. " AND b.user_id = :usr_id_owner\n"
. " AND b.is_read = 0\n"
. " UNION\n"
. "SELECT b.id\n"
. " FROM Baskets b\n"
. " INNER JOIN BasketParticipants p ON (p.`basket_id` = b.`id`)\n"
. " WHERE b.archived = 0\n"
. " AND b.user_id != :usr_id_ownertwo\n"
. " AND p.user_id = :usr_id_participant\n"
. " AND p.is_aware = 0\n"
// see truth-table in findActiveValidationByUser()
. " AND (\n"
. " b.share_expires IS NULL\n"
. " OR\n"
. " CURRENT_TIMESTAMP() < b.share_expires\n"
. " OR\n"
. " (b.vote_expires IS NOT NULL AND CURRENT_TIMESTAMP() < b.vote_expires)\n"
. " )";
$params = [
'usr_id_owner' => $user->getId(),
'usr_id_ownertwo' => $user->getId(),
'usr_id_participant' => $user->getId()
];
$stmt = $this->_em->getConnection()->executeQuery($sql, $params);
$basket_ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
$stmt->closeCursor();
// ... then we fetch the basket objects in dql
$dql = "SELECT b FROM Phraseanet:Basket b\n"
. " WHERE b.id IN (:basket_ids)";
$query = $this->_em->createQuery($dql);
$query->setParameter('basket_ids', $basket_ids);
return $query->getResult();
}
/**
* Returns all baskets that are not expired (share or vote) and
* where a specified user is participant (not owner)
*
* @param User $user
* @param null|string $sort
* @return Basket[]
*
* 0, 1 or 2 dates on a timeline : V="end-of-vote" ; S="end-of-share)
* .: basket is visible ; _:basket no nore visible
*
* .............. // no dates : always visible
* ......V....... // vote with no end-of-share : always visible
* ......S_______ // hidden after simple end-of-share
* ....V....S____ // hidden after end-of-share
* ....S....V____ // end-of-vote extends end-of-share
* ......VS______ // same date : trivial
*
*/
public function findActiveValidationByUser(User $user, $sort = null)
{
$dql = 'SELECT b
FROM Phraseanet:Basket b
JOIN b.elements e
JOIN b.participants p
WHERE b.user != ?1 AND p.user = ?2
AND (
b.share_expires IS NULL
OR
CURRENT_TIMESTAMP() < b.share_expires
OR
(b.vote_expires IS NOT NULL AND CURRENT_TIMESTAMP() < b.vote_expires)
)';
if ($sort == 'date') {
$dql .= "\nORDER BY b.created DESC";
} elseif ($sort == 'name') {
$dql .= "\nORDER BY b.name ASC";
}
$query = $this->_em->createQuery($dql);
$query->setParameters([1 => $user->getId(), 2 => $user->getId()]);
return $query->getResult();
}
/**
* Find a basket specified by his basket_id and his owner or participant
*
* @param int $basket_id
* @param User $user
* @param $requireOwner // true: the user MUST be the owner ;
* // false: IF THE BASKET IS A FEEDBACK the user can also be simple participant
* @return Basket
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findUserBasket($basket_id, User $user, $requireOwner)
{
// checked : 3 usages, "b.elements e" seems useless
$dql = "SELECT b\n"
. " FROM Phraseanet:Basket b\n"
. " WHERE b.id = :basket_id";
$query = $this->_em->createQuery($dql);
$query->setParameters(['basket_id' => $basket_id]);
$basket = $query->getOneOrNullResult();
if (null === $basket) {
throw new NotFoundHttpException($this->trans('Basket is not found'));
}
/* @var Basket $basket */
if ($basket->getUser()->getId() != $user->getId()) {
$participant = false;
if ($basket->isVoteBasket() && !$requireOwner) {
try {
$basket->getParticipant($user);
$participant = true;
}
catch (\Exception $e) {
// no-op
}
}
if (!$participant) {
throw new AccessDeniedHttpException($this->trans('You have not access to this basket'));
}
}
return $basket;
}
public function findContainingRecordForUser(\record_adapter $record, User $user)
{
$dql = 'SELECT b
FROM Phraseanet:Basket b
JOIN b.elements e
LEFT JOIN b.participants p
WHERE e.record_id = :record_id AND e.sbas_id = :databox_id
AND (
b.user = :usr_id1
OR (
p.user = :usr_id2
AND (
b.share_expires IS NULL
OR
CURRENT_TIMESTAMP() < b.share_expires
OR
(b.vote_expires IS NOT NULL AND CURRENT_TIMESTAMP() < b.vote_expires)
)
)
)
';
$params = [
'record_id' => $record->getRecordId(),
'databox_id'=> $record->getDataboxId(),
'usr_id1' => $user->getId(),
'usr_id2' => $user->getId(),
];
$query = $this->_em->createQuery($dql);
$query->setParameters($params);
return $query->getResult();
}
public function findWorkzoneBasket(User $user, $query, $year, $type, $offset, $perPage)
{
switch ($type) {
case self::RECEIVED:
$dql = "SELECT b\n"
. "FROM Phraseanet:Basket b\n"
. " JOIN b.elements e\n"
. "WHERE b.user = :usr_id AND b.pusher IS NOT NULL";
$params = [
'usr_id' => $user->getId()
];
break;
case self::VALIDATION_DONE:
// todo : check when called, and if "LEFT JOIN b.elements e" is usefull
$dql = "SELECT b\n"
. "FROM Phraseanet:Basket b\n"
. " JOIN b.elements e\n"
. " JOIN b.participants p\n"
. "WHERE b.user != ?1 AND p.user = ?2";
$params = [
1 => $user->getId(),
2 => $user->getId()
];
break;
case self::VALIDATION_SENT: // we expect initiator = owner
$dql = 'SELECT b
FROM Phraseanet:Basket b
WHERE b.vote_initiator = :usr_id1 AND b.user = :usr_id2';
$params = [
'usr_id1' => $user->getId(),
'usr_id2' => $user->getId()
];
break;
case self::MYBASKETS:
$dql = 'SELECT b
FROM Phraseanet:Basket b
LEFT JOIN b.participants p
WHERE (b.user = :usr_id)';
$params = [
'usr_id' => $user->getId()
];
break;
default:
// todo : check when called, and if "LEFT JOIN b.elements e" is usefull
$dql = 'SELECT b
FROM Phraseanet:Basket b
LEFT JOIN b.elements e
LEFT JOIN b.participants p
WHERE (b.user = :usr_id OR b.vote_initiator = :validating_usr_id)'; // !!!!!!!!!!! always same user ?
$params = [
'usr_id' => $user->getId(),
'validating_usr_id' => $user->getId()
];
}
if (ctype_digit($year) && strlen($year) == 4) {
$dql .= ' AND b.created >= :min_date AND b.created <= :max_date ';
$params['min_date'] = sprintf('%d-01-01 00:00:00', $year);
$params['max_date'] = sprintf('%d-12-31 23:59:59', $year);
}
if (trim($query) !== '') {
$dql .= ' AND (b.name LIKE :name OR b.description LIKE :description) ';
$params['name'] = '%' . $query . '%';
$params['description'] = '%' . $query . '%';
}
$dql .= ' ORDER BY b.id DESC';
$query = $this->_em->createQuery($dql);
$query->setParameters($params)
->setFirstResult($offset)
->setMaxResults($perPage);
return new Paginator($query, true);
}
/**
* Return all actives validation where current user is involved and user basket
*
* @param User $user
* @param string $sort
* @return Basket[]
*/
public function findActiveValidationAndBasketByUser(User $user, $sort = null)
{
// todo : check caller and if "LEFT JOIN b.elements e" is usefull
$dql = 'SELECT b
FROM Phraseanet:Basket b
LEFT JOIN b.elements e
LEFT JOIN b.participants p
WHERE (b.user = :usr_id AND b.archived = false)
OR (b.user != :usr_id AND p.user = :usr_id
AND (b.vote_expires IS NULL OR b.vote_expires > CURRENT_TIMESTAMP())
)';
if ($sort == 'date') {
$dql .= ' ORDER BY b.created DESC';
} elseif ($sort == 'name') {
$dql .= ' ORDER BY b.name ASC';
}
$query = $this->_em->createQuery($dql);
$query->setParameters(['usr_id' => $user->getId()]);
return $query->getResult();
}
}