- Correct getChoices() docblock to reflect translated label => enum backing value order instead of value => label - Re-indent Smart Links menu config to match actual array nesting - Align assignment operators in RefreshStatsCommand - Add missing newline at end of RefreshStatsCommand.php
109 lines
3.2 KiB
PHP
109 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
|
|
|
|
use Mautic\CoreBundle\Entity\CommonRepository;
|
|
|
|
/**
|
|
* @extends CommonRepository<SmartLink>
|
|
*/
|
|
class SmartLinkRepository extends CommonRepository
|
|
{
|
|
public function getTableAlias(): string
|
|
{
|
|
return 'sl';
|
|
}
|
|
|
|
/**
|
|
* Loads the page and the form with the links, so that the list doesn't query them one by one.
|
|
*
|
|
* @param array<string, mixed> $args
|
|
*/
|
|
public function getEntities(array $args = [])
|
|
{
|
|
$alias = $this->getTableAlias();
|
|
|
|
// The hit count of each link as a HIDDEN scalar: not hydrated into the entities, but
|
|
// available as an order by column ("sl.hitCount") for the Hits column of the list.
|
|
$hitCount = '(SELECT COUNT(h.id) FROM '.SmartLinkHit::class.' h WHERE h.smartLink = '.$alias.'.id) AS HIDDEN hitCount';
|
|
|
|
$args['qb'] = $this->createQueryBuilder($alias)
|
|
->select($alias, 'pg', 'fm', $hitCount)
|
|
->leftJoin($alias.'.page', 'pg')
|
|
->leftJoin($alias.'.form', 'fm');
|
|
|
|
return parent::getEntities($args);
|
|
}
|
|
|
|
/**
|
|
* True when another link already uses this slug. A link being edited never collides with itself.
|
|
*/
|
|
public function slugExists(string $slug, ?int $ignoreId = null): bool
|
|
{
|
|
if ('' === $slug) {
|
|
return false;
|
|
}
|
|
|
|
$qb = $this->createQueryBuilder($this->getTableAlias())
|
|
->select('COUNT('.$this->getTableAlias().'.id)')
|
|
->where($this->getTableAlias().'.slug = :slug')
|
|
->setParameter('slug', $slug);
|
|
|
|
if (null !== $ignoreId) {
|
|
$qb->andWhere($this->getTableAlias().'.id != :id')
|
|
->setParameter('id', $ignoreId);
|
|
}
|
|
|
|
return (int) $qb->getQuery()->getSingleScalarResult() > 0;
|
|
}
|
|
|
|
/**
|
|
* The list's search box matches the slug and the destination URL.
|
|
*
|
|
* @return array<int, mixed>
|
|
*/
|
|
protected function addCatchAllWhereClause($q, $filter): array
|
|
{
|
|
return $this->addStandardCatchAllWhereClause($q, $filter, [
|
|
$this->getTableAlias().'.slug',
|
|
$this->getTableAlias().'.destinationUrl',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* The Hits column of the list sorts by the number of hits of each link: the virtual
|
|
* "hitCount" order by maps to the HIDDEN hitCount scalar of the list query. Everything
|
|
* else falls back to core's column handling.
|
|
*
|
|
* @param array<string, mixed> $args
|
|
*/
|
|
protected function buildOrderByClause($q, array $args): void
|
|
{
|
|
$alias = $this->getTableAlias();
|
|
$orderBy = (string) ($args['orderBy'] ?? '');
|
|
$virtual = $alias.'.hitCount' === $orderBy || 'hitCount' === $orderBy;
|
|
|
|
if ($virtual && $q instanceof \Doctrine\ORM\QueryBuilder) {
|
|
$orderByDir = $this->sanitize((string) ($args['orderByDir'] ?? 'ASC'));
|
|
|
|
$q->addOrderBy('hitCount', $orderByDir);
|
|
|
|
return;
|
|
}
|
|
|
|
parent::buildOrderByClause($q, $args);
|
|
}
|
|
|
|
/**
|
|
* @return array<array{0: string, 1: string}>
|
|
*/
|
|
protected function getDefaultOrder(): array
|
|
{
|
|
return [
|
|
[$this->getTableAlias().'.slug', 'ASC'],
|
|
];
|
|
}
|
|
}
|