121 lines
4.7 KiB
PHP
121 lines
4.7 KiB
PHP
<?php
|
|
|
|
class DomainContactModel {
|
|
|
|
public $id;
|
|
public $roId;
|
|
public $type;
|
|
public $name;
|
|
public $street;
|
|
public $city;
|
|
public $pc;
|
|
public $cc;
|
|
public $voice;
|
|
public $email;
|
|
public $protection;
|
|
public $usedCount;
|
|
public $verificationStatus;
|
|
|
|
|
|
public function __construct($data = []) {
|
|
foreach ($data as $field => $value) {
|
|
if (property_exists(get_called_class(), $field)) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function importDomainContacts($domainContacts): array {
|
|
$db = FronkDB::singleton();
|
|
|
|
$db->query("TRUNCATE TABLE `DomainContact`");
|
|
|
|
$sql = /** @lang text */
|
|
"INSERT INTO `DomainContact` (`inwxRoId`, `type`, `name`, `street`, `city`, `pc`, `cc`, `voice`, `email`, `protection`, `verificationStatus`, `usedCount`) VALUES ";
|
|
$values = [];
|
|
|
|
foreach ($domainContacts as $domainContact) {
|
|
$valueStr = "(" .
|
|
$domainContact['roId'] . ", '" .
|
|
$domainContact['type'] . "', '" .
|
|
$domainContact['name'] . "', '" .
|
|
$domainContact['street'] . "', '" .
|
|
$domainContact['city'] . "', '" .
|
|
$domainContact['pc'] . "', '" .
|
|
$domainContact['cc'] . "', '" .
|
|
$domainContact['voice'] . "', '" .
|
|
$domainContact['email'] . "', " .
|
|
($domainContact['protection'] ? 1 : 0) . ", '" .
|
|
$domainContact['verificationStatus'] . "', ";
|
|
|
|
$valueStr .= $domainContact['usedCount'] ?? "NULL";
|
|
$valueStr .= ")";
|
|
|
|
$values[] = $valueStr;
|
|
}
|
|
|
|
$sql .= implode(", ", $values);
|
|
$db->query($sql);
|
|
return [
|
|
"message" => "Imported " . count($domainContacts) . " domain contacts."
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Generate SQL Filter condition (space separated) for a given column.
|
|
*
|
|
* @param string|null $filterValue The filter value to match against.
|
|
* @param string $columnName The name of the column in the database table.
|
|
* @return string The SQL condition generated based on the filter value and column name.
|
|
*/
|
|
public static function generateFilterCondition(?string $filterValue, string $columnName): string {
|
|
$sql = "";
|
|
if (!empty($filterValue)) {
|
|
$filterItems = explode(" ", $filterValue);
|
|
foreach ($filterItems as $item) {
|
|
$sql .= " AND `$columnName` LIKE '%" . $item . "%'";
|
|
}
|
|
}
|
|
|
|
return $sql;
|
|
}
|
|
|
|
public static function getSqlFilter($filters): string {
|
|
$sql = isset($filters['roId']) ? " AND `inwxRoId` = " . $filters['roId'] : "";
|
|
$sql .= isset($filters['type']) ? " AND `type` = '" . $filters['type'] . "'" : "";
|
|
$sql .= isset($filters['name']) ? self::generateFilterCondition($filters['name'], "name") : "";
|
|
$sql .= isset($filters['street']) ? self::generateFilterCondition($filters['street'], "street") : "";
|
|
$sql .= isset($filters['city']) ? self::generateFilterCondition($filters['city'], "city") : "";
|
|
$sql .= isset($filters['pc']) ? " AND `pc` = " . $filters['pc'] : "";
|
|
$sql .= isset($filters['cc']) ? " AND `cc` = " . $filters['cc'] : "";
|
|
$sql .= isset($filters['voice']) ? " AND `voice` = " . $filters['voice'] : "";
|
|
$sql .= isset($filters['email']) ? self::generateFilterCondition($filters['email'], "email") : "";
|
|
$sql .= isset($filters['protection']) ? " AND `protection` = " . $filters['protection'] : "";
|
|
$sql .= isset($filters['usedCount']) ? " AND `usedCount` = " . $filters['usedCount'] : "";
|
|
$sql .= isset($filters['verificationStatus']) ? " AND `verificationStatus` = '" . $filters['verificationStatus'] . "'" : "";
|
|
return $sql;
|
|
|
|
}
|
|
|
|
public static function getAllDomainContacts($filters = null, $limit = null, $offset = 0, $raw_array = true): array {
|
|
$db = FronkDB::singleton();
|
|
$sql = "SELECT * FROM `DomainContact` WHERE 1 ";
|
|
$sql .= $filters === null ? "" : self::getSqlFilter($filters);
|
|
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
|
|
|
|
$result = $db->query($sql);
|
|
$rows = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$rows[] = $raw_array ? $row : new DomainContactModel($row);
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public static function countDomainContacts($filters) {
|
|
$db = FronkDB::singleton();
|
|
$sql = "SELECT COUNT(*) as `total_rows` FROM `DomainContact` WHERE 1 " . self::getSqlFilter($filters);
|
|
$result = $db->query($sql);
|
|
return $result->fetch_assoc()['total_rows'];
|
|
}
|
|
} |