forked from fa/breadcrumb-the-shire
250 lines
9.4 KiB
PHP
250 lines
9.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Repository;
|
||
|
|
|
||
|
|
use MintyPHP\DB;
|
||
|
|
|
||
|
|
class TenantRepository
|
||
|
|
{
|
||
|
|
private static function unwrap(?array $row): ?array
|
||
|
|
{
|
||
|
|
if (!$row) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return $row['tenants'] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function unwrapList($rows): array
|
||
|
|
{
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$list = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$tenant = $row['tenants'] ?? null;
|
||
|
|
if (is_array($tenant)) {
|
||
|
|
$list[] = $tenant;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $list;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function list(): array
|
||
|
|
{
|
||
|
|
$rows = DB::select(
|
||
|
|
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants order by id desc'
|
||
|
|
);
|
||
|
|
return self::unwrapList($rows);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function listIds(): array
|
||
|
|
{
|
||
|
|
$rows = DB::select('select id from tenants');
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$ids = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['tenants'] ?? $row;
|
||
|
|
if (is_array($data) && isset($data['id'])) {
|
||
|
|
$ids[] = (int) $data['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return array_values(array_unique($ids));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function listActiveIdsByIds(array $tenantIds): array
|
||
|
|
{
|
||
|
|
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
|
||
|
|
if (!$tenantIds) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$placeholders = implode(',', array_fill(0, count($tenantIds), '?'));
|
||
|
|
$params = array_map('strval', $tenantIds);
|
||
|
|
$rows = call_user_func_array(
|
||
|
|
['MintyPHP\\DB', 'select'],
|
||
|
|
array_merge(
|
||
|
|
["select id from tenants where status = 'active' and id in ($placeholders)"],
|
||
|
|
$params
|
||
|
|
)
|
||
|
|
);
|
||
|
|
if (!is_array($rows)) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$ids = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$data = $row['tenants'] ?? $row;
|
||
|
|
if (is_array($data) && isset($data['id'])) {
|
||
|
|
$ids[] = (int) $data['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return array_values(array_unique($ids));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function listPaged(array $options): array
|
||
|
|
{
|
||
|
|
$limit = (int) ($options['limit'] ?? 10);
|
||
|
|
if ($limit < 1) {
|
||
|
|
$limit = 10;
|
||
|
|
} elseif ($limit > 100) {
|
||
|
|
$limit = 100;
|
||
|
|
}
|
||
|
|
|
||
|
|
$offset = (int) ($options['offset'] ?? 0);
|
||
|
|
if ($offset < 0) {
|
||
|
|
$offset = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
$search = trim((string) ($options['search'] ?? ''));
|
||
|
|
$order = (string) ($options['order'] ?? 'id');
|
||
|
|
$dir = strtolower((string) ($options['dir'] ?? 'desc'));
|
||
|
|
$allowedOrder = ['id', 'uuid', 'description', 'created', 'modified'];
|
||
|
|
if (!in_array($order, $allowedOrder, true)) {
|
||
|
|
$order = 'id';
|
||
|
|
}
|
||
|
|
if (!in_array($dir, ['asc', 'desc'], true)) {
|
||
|
|
$dir = 'desc';
|
||
|
|
}
|
||
|
|
|
||
|
|
$where = [];
|
||
|
|
$params = [];
|
||
|
|
if ($search !== '') {
|
||
|
|
$like = '%' . $search . '%';
|
||
|
|
$where[] = '(description like ? or uuid like ?)';
|
||
|
|
$params[] = $like;
|
||
|
|
$params[] = $like;
|
||
|
|
}
|
||
|
|
if (!empty($options['tenantUserId'])) {
|
||
|
|
$tenantUserId = (int) $options['tenantUserId'];
|
||
|
|
if ($tenantUserId > 0) {
|
||
|
|
$where[] = 'exists (select 1 from user_tenants ut where ut.user_id = ? and ut.tenant_id = tenants.id)';
|
||
|
|
$params[] = (string) $tenantUserId;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$whereSql = $where ? (' where ' . implode(' and ', $where)) : '';
|
||
|
|
$count = DB::selectValue('select count(*) from tenants' . $whereSql, ...$params);
|
||
|
|
$total = $count ? (int) $count : 0;
|
||
|
|
|
||
|
|
$query = 'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants' .
|
||
|
|
$whereSql .
|
||
|
|
sprintf(' order by `%s` %s limit ? offset ?', $order, $dir);
|
||
|
|
|
||
|
|
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
|
||
|
|
$rows = call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $queryParams));
|
||
|
|
|
||
|
|
return [
|
||
|
|
'total' => $total,
|
||
|
|
'rows' => self::unwrapList($rows),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function find(int $id): ?array
|
||
|
|
{
|
||
|
|
$row = DB::selectOne(
|
||
|
|
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where id = ? limit 1',
|
||
|
|
(string) $id
|
||
|
|
);
|
||
|
|
return self::unwrap($row);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function findByUuid(string $uuid): ?array
|
||
|
|
{
|
||
|
|
$row = DB::selectOne(
|
||
|
|
'select id, uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, status, status_changed_at, status_changed_by, created_by, modified_by, created, modified from tenants where uuid = ? limit 1',
|
||
|
|
$uuid
|
||
|
|
);
|
||
|
|
return self::unwrap($row);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static function uuidV4(): string
|
||
|
|
{
|
||
|
|
$data = random_bytes(16);
|
||
|
|
$data[6] = chr((ord($data[6]) & 0x0f) | 0x40);
|
||
|
|
$data[8] = chr((ord($data[8]) & 0x3f) | 0x80);
|
||
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function create(array $data)
|
||
|
|
{
|
||
|
|
return DB::insert(
|
||
|
|
'insert into tenants (uuid, description, address, postal_code, city, country, region, vat_id, tax_number, phone, fax, email, support_email, support_phone, billing_email, website, privacy_url, imprint_url, primary_color, status, status_changed_at, status_changed_by, created_by, created) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())',
|
||
|
|
$data['uuid'] ?? self::uuidV4(),
|
||
|
|
$data['description'],
|
||
|
|
$data['address'] ?? null,
|
||
|
|
$data['postal_code'] ?? null,
|
||
|
|
$data['city'] ?? null,
|
||
|
|
$data['country'] ?? null,
|
||
|
|
$data['region'] ?? null,
|
||
|
|
$data['vat_id'] ?? null,
|
||
|
|
$data['tax_number'] ?? null,
|
||
|
|
$data['phone'] ?? null,
|
||
|
|
$data['fax'] ?? null,
|
||
|
|
$data['email'] ?? null,
|
||
|
|
$data['support_email'] ?? null,
|
||
|
|
$data['support_phone'] ?? null,
|
||
|
|
$data['billing_email'] ?? null,
|
||
|
|
$data['website'] ?? null,
|
||
|
|
$data['privacy_url'] ?? null,
|
||
|
|
$data['imprint_url'] ?? null,
|
||
|
|
$data['primary_color'] ?? null,
|
||
|
|
$data['status'] ?? 'active',
|
||
|
|
$data['status_changed_at'] ?? null,
|
||
|
|
$data['status_changed_by'] ?? null,
|
||
|
|
$data['created_by'] ?? null
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function update(int $id, array $data): bool
|
||
|
|
{
|
||
|
|
$fields = [
|
||
|
|
'description' => $data['description'],
|
||
|
|
'address' => $data['address'] ?? null,
|
||
|
|
'postal_code' => $data['postal_code'] ?? null,
|
||
|
|
'city' => $data['city'] ?? null,
|
||
|
|
'country' => $data['country'] ?? null,
|
||
|
|
'region' => $data['region'] ?? null,
|
||
|
|
'vat_id' => $data['vat_id'] ?? null,
|
||
|
|
'tax_number' => $data['tax_number'] ?? null,
|
||
|
|
'phone' => $data['phone'] ?? null,
|
||
|
|
'fax' => $data['fax'] ?? null,
|
||
|
|
'email' => $data['email'] ?? null,
|
||
|
|
'support_email' => $data['support_email'] ?? null,
|
||
|
|
'support_phone' => $data['support_phone'] ?? null,
|
||
|
|
'billing_email' => $data['billing_email'] ?? null,
|
||
|
|
'website' => $data['website'] ?? null,
|
||
|
|
'privacy_url' => $data['privacy_url'] ?? null,
|
||
|
|
'imprint_url' => $data['imprint_url'] ?? null,
|
||
|
|
'primary_color' => $data['primary_color'] ?? null,
|
||
|
|
'status' => $data['status'] ?? 'active',
|
||
|
|
];
|
||
|
|
if (array_key_exists('modified_by', $data)) {
|
||
|
|
$fields['modified_by'] = $data['modified_by'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('status_changed_at', $data)) {
|
||
|
|
$fields['status_changed_at'] = $data['status_changed_at'];
|
||
|
|
}
|
||
|
|
if (array_key_exists('status_changed_by', $data)) {
|
||
|
|
$fields['status_changed_by'] = $data['status_changed_by'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$setParts = [];
|
||
|
|
$params = [];
|
||
|
|
foreach ($fields as $field => $value) {
|
||
|
|
$setParts[] = sprintf('`%s` = ?', $field);
|
||
|
|
$params[] = $value;
|
||
|
|
}
|
||
|
|
$params[] = (string) $id;
|
||
|
|
|
||
|
|
$query = 'update tenants set ' . implode(', ', $setParts) . ' where id = ?';
|
||
|
|
$result = DB::update($query, ...$params);
|
||
|
|
return $result !== false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function delete(int $id): bool
|
||
|
|
{
|
||
|
|
$result = DB::delete('delete from tenants where id = ?', (string) $id);
|
||
|
|
return $result !== false;
|
||
|
|
}
|
||
|
|
}
|