36 lines
1 KiB
PHP
36 lines
1 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace MailAccountAdmin\Repositories;
|
||
|
|
|
||
|
|
use PDO;
|
||
|
|
|
||
|
|
class AccountRepository extends BaseRepository
|
||
|
|
{
|
||
|
|
public function fetchAccountList(string $filterByDomain = null): array
|
||
|
|
{
|
||
|
|
$queryWhere = '';
|
||
|
|
$queryParams = [];
|
||
|
|
if (!empty($filterByDomain)) {
|
||
|
|
$queryWhere = 'WHERE REGEXP_REPLACE(username, "^.*@", "") LIKE :domain';
|
||
|
|
$queryParams['domain'] = str_replace('*', '%', $filterByDomain);
|
||
|
|
}
|
||
|
|
|
||
|
|
$query = '
|
||
|
|
SELECT
|
||
|
|
mail_users.*,
|
||
|
|
REGEXP_REPLACE(username, "^.*@", "") AS domain,
|
||
|
|
COUNT(alias_id) AS alias_count
|
||
|
|
FROM mail_users
|
||
|
|
LEFT JOIN mail_aliases ON mail_users.user_id = mail_aliases.user_id
|
||
|
|
' . $queryWhere . '
|
||
|
|
GROUP BY username
|
||
|
|
ORDER BY domain, username
|
||
|
|
';
|
||
|
|
|
||
|
|
$statement = $this->pdo->prepare($query);
|
||
|
|
$statement->execute($queryParams);
|
||
|
|
return $statement->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
}
|
||
|
|
}
|