Files
thetool/application/Domain/DomainModel.php
2024-07-30 17:52:04 +00:00

133 lines
5.8 KiB
PHP

<?php
class DomainModel {
public $inwxRoId;
public $domain;
public $period;
public $crDate;
public $exDate;
public $reDate;
public $upDate;
public $transferLock;
public $status;
public $authCode;
public $registrant;
public $admin;
public $tech;
public $billing;
public $ns;
public $pleskId;
public $pleskHostingType;
public $pleskCreated;
public function __construct($data = []) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
public static function importDomains($domains): array {
$db = FronkDB::singleton();
$db->query("TRUNCATE TABLE `Domain`");
$sql = /** @lang text */
"INSERT INTO `Domain` (`inwxRoId`, `domain`, `period`, `crDate`, `exDate`, `reDate`, `upDate`, `transferLock`, `status`, `authCode`, `registrant`, `admin`, `tech`, `billing`, `ns`, `pleskId`, `pleskHostingType`, `pleskCreated`) VALUES ";
$values = [];
foreach ($domains as $domain) {
$valueStr = "(" .
$domain['roId'] . ", '" .
$domain['domain'] . "', '" .
$domain['period'] . "', " .
$domain['crDate']['timestamp'] . ", " .
(isset($domain['reDate']) ? $domain['reDate']['timestamp'] : "NULL") . ", " .
(isset($domain['reDate']) ? $domain['reDate']['timestamp'] : "NULL") . ", " .
$domain['upDate']['timestamp'] . ", " .
($domain['transferLock'] ? 1 : 0) . ", '" .
$domain['status'] . "', '" .
$domain['authCode'] . "', " .
$domain['registrant'] . ", " .
$domain['admin'] . ", " .
$domain['tech'] . ", " .
$domain['billing'] . ", '" .
implode(", ", $domain['ns']) . "', ";
// Check if 'pleskId' is set
if (isset($domain['plesk']) && is_array($domain['plesk'])) {
$valueStr .= $domain['plesk']['id'] . ", ";
$valueStr .= "'" . $domain['plesk']['hosting_type'] . "', ";
$valueStr .= $domain['plesk']['created'];
} else {
$valueStr .= "NULL, NULL, NULL";
}
$values[] = $valueStr . ")";
}
$sql .= implode(", ", $values);
$db->query($sql);
return [
"message" => "Imported " . count($domains) . " domains."
];
}
public static function getSqlFilter($filters): string {
$sql = isset($filters['domain']) ? Helper::generateFilterCondition($filters['domain'], "domain") : "";
$sql .= isset($filters['crDate']) ? Helper::generateFilterCondition($filters['crDate'], "crDate") : "";
$sql .= isset($filters['exDate']) ? Helper::generateFilterCondition($filters['exDate'], "exDate") : "";
$sql .= isset($filters['reDate']) ? Helper::generateFilterCondition($filters['reDate'], "reDate") : "";
$sql .= isset($filters['upDate']) ? Helper::generateFilterCondition($filters['upDate'], "upDate") : "";
$sql .= isset($filters['status']) ? Helper::generateFilterCondition($filters['status'], "status") : "";
$sql .= isset($filters['transferLock']) ? Helper::generateFilterCondition($filters['transferLock'], "transferLock") : "";
$sql .= isset($filters['authCode']) ? Helper::generateFilterCondition($filters['authCode'], "authCode") : "";
$sql .= isset($filters['registrant']) ? Helper::generateFilterCondition($filters['registrant'], "registrant", true) : "";
$sql .= isset($filters['admin']) ? Helper::generateFilterCondition($filters['admin'], "admin", true) : "";
$sql .= isset($filters['tech']) ? Helper::generateFilterCondition($filters['tech'], "tech", true) : "";
$sql .= isset($filters['billing']) ? Helper::generateFilterCondition($filters['billing'], "billing", true) : "";
$sql .= isset($filters['ns']) ? Helper::generateFilterCondition($filters['ns'], "ns") : "";
$sql .= isset($filters['pleskId']) ? " AND `pleskId` " . ($filters['pleskId'] === "0" ? "IS NULL" : "IS NOT NULL") : "";
return $sql;
}
public static function getAllDomains($filters, $limit = null, $offset = 0, $order = null): array {
$db = FronkDB::singleton();
$sql = "SELECT * FROM `Domain` WHERE 1 " . self::getSqlFilter($filters);
$sql .= $order === null || $order['key'] === null ? "" : " ORDER BY `" . $order['key'] . "` " . $order['order'];
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
$result = $db->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$row['pleskId'] = $row['pleskId'] === null ? 0 : 1;
$rows[] = new DomainModel($row);
}
$newRows = [];
// convert domain contracts to array
$domainContracts = array_map(function ($contract) {
return $contract->toArray();
}, ContractModel::search(['product_id' => 170]));
// search for $domain->domain in $domainContracts['matchcode']
foreach ($rows as $row) {
$contract = array_filter($domainContracts, function ($contract) use ($row) {
return $contract['matchcode'] === $row->domain;
});
$row->contractId = count($contract) > 0 ? array_values($contract)[0]['id'] : null;
$newRows[] = $row;
}
return $newRows;
}
public static function countDomains($filters) {
$db = FronkDB::singleton();
$sql = "SELECT COUNT(*) as `total_rows` FROM `Domain` WHERE 1 " . self::getSqlFilter($filters);
$result = $db->query($sql);
return $result->fetch_assoc()['total_rows'];
}
}