Files
thetool/application/VoiceCallHistory/VoiceCallHistoryModel.php
2024-04-16 17:36:10 +00:00

143 lines
5.7 KiB
PHP

<?php
class VoiceCallHistoryModel {
public $uid;
public $voice_account;
public $start;
public $source;
public $destination;
public $billable;
public $duration;
public function __construct($data = []) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
/**
* 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;
}
//TODO: combine these two functions into one
public static function importCallsFromKolmisoft($callHistory): array {
{
$modifiedCallHistory = [];
for ($i = 0; $i < count($callHistory); $i++) {
$call = $callHistory[$i];
$date = date("Y-m-d H:i:s", strtotime($call["calldate2"] . " UTC"));
$modifiedCall = [
"uid" => $call["uniqueid"],
"voice_account" => strpos($call["clid"], "Anonymous") !== false ? 'Anonymous' : preg_replace('/[^0-9]/', '', explode(" ", $call["clid"])[0]),
"start" => $date,
"source" => strpos($call["clid"], "nymous") !== false ? 'Anonymous' : str_replace("+", "", $call["src"]),
"destination" => $call["dst"],
"state" => preg_replace('/[^0-9]/', '', explode("(", $call["dispod"])[1]),
"billable" => gettype($call["did"]) === "string" ? 0 : 1,
"duration" => $call["nice_billsec"],
];
$modifiedCallHistory[] = $modifiedCall;
}
$voiceHistoryImport = VoiceCallHistoryModel::importVoiceCallHistory($modifiedCallHistory);
if ($callHistory === false) {
return [
"status" => "error",
"message" => "Failed to import calls."
];
}
return [
"status" => "success",
"message" => $voiceHistoryImport["message"]
];
}
}
public static function importVoiceCallHistory($callHistory): array {
$db = FronkDB::singleton();
$sql = /** @lang text */ "INSERT INTO `VoiceCallHistory` (`uid`, `voice_account`, `start`, `source`, `destination`, `state`, `billable`, `duration`) VALUES ";
$values = [];
foreach ($callHistory as $voiceCall) {
$uid = $voiceCall['uid'];
$valueStr = "(" .
($voiceCall['uid'] === 'Anonymous' ? 'NULL' : "'$uid'") . ", " .
($voiceCall['voice_account'] === 'Anonymous' ? 'NULL' : "'" . $voiceCall['voice_account']. "'" ) . ", '" .
$voiceCall['start'] . "', '" .
($voiceCall['source'] === 'Anonymous' ? "NULL" : $voiceCall['source']) . "', '" .
($voiceCall['destination'] === 'Anonymous' ? "NULL" : $voiceCall['destination'] ) . "', " .
$voiceCall['state'] . ", " .
$voiceCall['billable'] . ", " .
$voiceCall['duration'] . ")";
$values[] = $valueStr;
}
$sql .= implode(", ", $values);
$sql .= " ON DUPLICATE KEY UPDATE duration = VALUES(duration)";
$db->query($sql);
return [
"message" => "Imported " . count($callHistory) . " calls to the history."
];
}
public static function getSqlFilter($filters): string {
$sql = "";
if (isset($filters['start']['from']) && isset($filters['start']['to'])) {
$sql = " AND `start` >= FROM_UNIXTIME(" . $filters['start']['from'] . ") AND `start` <= FROM_UNIXTIME(" . $filters['start']['to'] . ")";
}
$sql .= isset($filters['end']) ? " AND `start` <= '" . $filters['end'] . "'" : "";
$sql .= isset($filters['source']) ? self::generateFilterCondition($filters['source'], "source") : "";
$sql .= isset($filters['destination']) ? self::generateFilterCondition($filters['destination'], "destination") : "";
$sql .= isset($filters['billable']) ? self::generateFilterCondition($filters['billable'], "billable") : "";
$sql .= isset($filters['duration']) ? self::generateFilterCondition($filters['duration'], "duration") : "";
return $sql . " ORDER BY `start` DESC";
}
public static function getVoiceCallHistory($filters, $limit = null, $offset = 0): array {
$db = FronkDB::singleton();
$sql = "SELECT * FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters);
$sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset;
$result = $db->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = new VoiceCallHistoryModel($row);
}
return $rows;
}
public static function countVoiceCallHistory($filters) {
$db = FronkDB::singleton();
$sql = "SELECT COUNT(*) as `total_rows` FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters);
$result = $db->query($sql);
return $result->fetch_assoc()['total_rows'];
}
}