Spaces:
No application file
No application file
File size: 6,090 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
<?php
namespace Mautic\AssetBundle\Entity;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* @extends CommonRepository<Asset>
*/
class AssetRepository extends CommonRepository
{
/**
* Get a list of entities.
*
* @return Paginator
*/
public function getEntities(array $args = [])
{
$q = $this
->createQueryBuilder('a')
->select('a')
->leftJoin('a.category', 'c');
$args['qb'] = $q;
return parent::getEntities($args);
}
/**
* @param string $search
* @param int $limit
* @param int $start
* @param bool|false $viewOther
*
* @return array
*/
public function getAssetList($search = '', $limit = 10, $start = 0, $viewOther = false)
{
$q = $this->createQueryBuilder('a');
$q->select('partial a.{id, title, path, alias, language}');
if (!empty($search)) {
$q->andWhere($q->expr()->like('a.title', ':search'))
->setParameter('search', "%{$search}%");
}
if (!$viewOther) {
$q->andWhere($q->expr()->eq('a.createdBy', ':id'))
->setParameter('id', $this->currentUser->getId());
}
$q->orderBy('a.title');
if (!empty($limit)) {
$q->setFirstResult($start)
->setMaxResults($limit);
}
return $q->getQuery()->getArrayResult();
}
/**
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
*/
protected function addCatchAllWhereClause($q, $filter): array
{
return $this->addStandardCatchAllWhereClause($q, $filter, [
'a.title',
'a.alias',
]);
}
/**
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
*/
protected function addSearchCommandWhereClause($q, $filter): array
{
[$expr, $parameters] = $this->addStandardSearchCommandWhereClause($q, $filter);
if ($expr) {
return [$expr, $parameters];
}
$command = $field = $filter->command;
$unique = $this->generateRandomParameterName();
$returnParameter = false; // returning a parameter that is not used will lead to a Doctrine error
switch ($command) {
case $this->translator->trans('mautic.asset.asset.searchcommand.lang'):
$langUnique = $this->generateRandomParameterName();
$langValue = $filter->string.'_%';
$forceParameters = [
$langUnique => $langValue,
$unique => $filter->string,
];
$expr = $q->expr()->orX(
$q->expr()->eq('a.language', ":$unique"),
$q->expr()->like('a.language', ":$langUnique")
);
$returnParameter = true;
break;
}
if ($expr && $filter->not) {
$expr = $q->expr()->not($expr);
}
if (!empty($forceParameters)) {
$parameters = $forceParameters;
} elseif (!$returnParameter) {
$parameters = [];
} else {
$string = ($filter->strict) ? $filter->string : "%{$filter->string}%";
$parameters = ["$unique" => $string];
}
return [$expr, $parameters];
}
/**
* @return string[]
*/
public function getSearchCommands(): array
{
$commands = [
'mautic.core.searchcommand.ispublished',
'mautic.core.searchcommand.isunpublished',
'mautic.core.searchcommand.isuncategorized',
'mautic.core.searchcommand.ismine',
'mautic.core.searchcommand.category',
'mautic.asset.asset.searchcommand.lang',
];
return array_merge($commands, parent::getSearchCommands());
}
/**
* @return array<array<string>>
*/
protected function getDefaultOrder(): array
{
return [
['a.title', 'ASC'],
];
}
public function getTableAlias(): string
{
return 'a';
}
/**
* Gets the sum size of assets.
*/
public function getAssetSize(array $assets): int
{
$q = $this->_em->getConnection()->createQueryBuilder();
$q->select('sum(a.size) as total_size')
->from(MAUTIC_TABLE_PREFIX.'assets', 'a')
->where('a.id IN (:assetIds)')
->setParameter('assetIds', $assets, \Doctrine\DBAL\ArrayParameterType::INTEGER);
$result = $q->executeQuery()->fetchAllAssociative();
return (int) $result[0]['total_size'];
}
/**
* @param int $increaseBy
* @param bool|false $unique
*/
public function upDownloadCount($id, $increaseBy = 1, $unique = false): void
{
$q = $this->_em->getConnection()->createQueryBuilder();
$q->update(MAUTIC_TABLE_PREFIX.'assets')
->set('download_count', 'download_count + '.(int) $increaseBy)
->where('id = '.(int) $id);
if ($unique) {
$q->set('unique_download_count', 'unique_download_count + '.(int) $increaseBy);
}
$q->executeStatement();
}
/**
* @param int $categoryId
*
* @return Asset
*
* @throws NoResultException
* @throws NonUniqueResultException
*/
public function getLatestAssetForCategory($categoryId)
{
$q = $this->createQueryBuilder($this->getTableAlias());
$q->where($this->getTableAlias().'.category = :categoryId');
$q->andWhere($this->getTableAlias().'.isPublished = TRUE');
$q->setParameter('categoryId', $categoryId);
$q->orderBy($this->getTableAlias().'.dateAdded', \Doctrine\Common\Collections\Criteria::DESC);
$q->setMaxResults(1);
return $q->getQuery()->getSingleResult();
}
}
|