[VoiceCallHistory]
- implemented VoiceCallHistoryJob for running the jobs - implement /VoiceCallHistory for displaying, importing the Voice Call History - added date-range-picker to tt-table (still todo)
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property mixed|null $name
|
||||
*/
|
||||
class VoiceCallHistory extends mfBaseModel
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
//display errors
|
||||
//ini_set('display_errors', 1);
|
||||
//ini_set('display_startup_errors', 1);
|
||||
//error_reporting(E_ALL);
|
||||
|
||||
class VoiceCallHistoryController extends mfBaseController {
|
||||
private User $me;
|
||||
private string $VOICE_PORTAL_HOST = "vportal.xinon.at";
|
||||
private string $VOICE_PORTAL_API_KEY = "2f9mpw3oamALg7gSgtWUTCKNZ01fFRDh";
|
||||
private string $VOICE_PORTAL_USERNAME = "700342020";
|
||||
|
||||
private KolmisoftMore $kolmisoftMore;
|
||||
|
||||
|
||||
protected function init(): void {
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->layout()->set("me", $me);
|
||||
$this->me = $me;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
$this->kolmisoftMore = new KolmisoftMore($this->VOICE_PORTAL_HOST, $this->VOICE_PORTAL_API_KEY, $this->VOICE_PORTAL_USERNAME);
|
||||
|
||||
}
|
||||
|
||||
protected function indexAction(): void {
|
||||
$this->layout()->setTemplate("VoiceCallHistory/Index");
|
||||
}
|
||||
|
||||
protected function apiAction() {
|
||||
$do = $this->request->do;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
switch ($do) {
|
||||
case "getCalls":
|
||||
$return = $this->getCalls();
|
||||
break;
|
||||
case "importCallsFromToday":
|
||||
$return = $this->importCallsFromToday();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$return) {
|
||||
$return = [
|
||||
"status" => "error",
|
||||
"message" => "Invalid request."
|
||||
];
|
||||
}
|
||||
|
||||
die(json_encode($return));
|
||||
}
|
||||
|
||||
private function importCallsFromToday(): array {
|
||||
$startDate = strtotime(date("Y-m-d 8:00:00"));
|
||||
$endDate = strtotime(date("Y-m-d 9:00:59"));
|
||||
|
||||
$callHistory = $this->kolmisoftMore->getVoiceCallHistory($startDate, $endDate);
|
||||
|
||||
return VoiceCallHistoryModel::importCallsFromKolmisoft($callHistory);
|
||||
}
|
||||
|
||||
private function getCalls(): array {
|
||||
$json = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$filters = $json['filters'] ?? [];
|
||||
$page = $json['pagination']['page'] ?? 1;
|
||||
$perPage = $json['pagination']['per_page'] ?? 10;
|
||||
|
||||
$calls = VoiceCallHistoryModel::getVoiceCallHistory($filters, $perPage, $perPage * $page - $perPage);
|
||||
$totalRows = VoiceCallHistoryModel::countVoiceCallHistory($filters);
|
||||
|
||||
return [
|
||||
"rows" => $calls,
|
||||
"pagination" => [
|
||||
"page" => $page,
|
||||
"total_pages" => ceil($totalRows / $perPage),
|
||||
"per_page" => $perPage,
|
||||
"total_rows" => intval($totalRows)
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?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"));
|
||||
$date = new DateTime($date, new DateTimeZone('UTC'));
|
||||
$date->setTimezone(new DateTimeZone('Europe/Vienna'));
|
||||
$date = $date->format('Y-m-d H:i:s');
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property mixed|null $name
|
||||
*/
|
||||
class VoiceCallHistoryJob extends mfBaseModel
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
//display errors
|
||||
//ini_set('display_errors', 1);
|
||||
//ini_set('display_startup_errors', 1);
|
||||
//error_reporting(E_ALL);
|
||||
|
||||
class VoiceCallHistoryJobController extends mfBaseController {
|
||||
private User $me;
|
||||
private string $VOICE_PORTAL_HOST = "vportal.xinon.at";
|
||||
private string $VOICE_PORTAL_API_KEY = "2f9mpw3oamALg7gSgtWUTCKNZ01fFRDh";
|
||||
private string $VOICE_PORTAL_USERNAME = "700342020";
|
||||
|
||||
private KolmisoftMore $kolmisoftMore;
|
||||
|
||||
|
||||
protected function init(): void {
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->layout()->set("me", $me);
|
||||
$this->me = $me;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
$this->kolmisoftMore = new KolmisoftMore($this->VOICE_PORTAL_HOST, $this->VOICE_PORTAL_API_KEY, $this->VOICE_PORTAL_USERNAME);
|
||||
|
||||
}
|
||||
|
||||
protected function indexAction(): void {
|
||||
$this->layout()->setTemplate("VoiceCallHistoryJob/Index");
|
||||
}
|
||||
|
||||
protected function apiAction() {
|
||||
$do = $this->request->do;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
switch ($do) {
|
||||
case "runJobs":
|
||||
$return = $this->runJobs();
|
||||
break;
|
||||
case "importCallsFromToday":
|
||||
$return = $this->importCallsFromToday();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$return) {
|
||||
$return = [
|
||||
"status" => "error",
|
||||
"message" => "Invalid request."
|
||||
];
|
||||
}
|
||||
|
||||
die(json_encode($return));
|
||||
}
|
||||
|
||||
private function runJobs(): array {
|
||||
VoiceCallHistoryJobModel::createJobsUntilToday();
|
||||
|
||||
$jobs = VoiceCallHistoryJobModel::getJobsNotDone();
|
||||
|
||||
$messages = [
|
||||
"success" => [],
|
||||
"error" => []
|
||||
];
|
||||
foreach ($jobs as $job) {
|
||||
$startDate = strtotime(date("Y-m-d 00:00:00", strtotime($job->date)));
|
||||
$endDate = strtotime(date("Y-m-d 00:00:00", strtotime($job->date . " +1 day")));
|
||||
|
||||
$callHistory = $this->kolmisoftMore->getVoiceCallHistory($startDate, $endDate);
|
||||
|
||||
$importedCalls = VoiceCallHistoryModel::importCallsFromKolmisoft($callHistory);
|
||||
|
||||
if ($importedCalls) {
|
||||
$messages["success"][$job->date] = $importedCalls["message"];
|
||||
VoiceCallHistoryJobModel::updateJobStatus($job->id, "success");
|
||||
} else {
|
||||
$messages["error"][$job->date] = "Failed to import calls for job $job->id.";
|
||||
VoiceCallHistoryJobModel::updateJobStatus($job->id, "failed");
|
||||
}
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
class VoiceCallHistoryJobModel {
|
||||
public $id;
|
||||
public $date;
|
||||
public $status;
|
||||
public $create;
|
||||
public $edit;
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static function createJobsUntilToday(): array {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
// $i = first day of the month; $i <= today; $i += 1 day
|
||||
$values = [];
|
||||
for ($i = strtotime(date("Y-m-01")); $i <= strtotime(date("Y-m-d")); $i += 86400) {
|
||||
$values[] = "('" . date("Y-m-d", $i) . "')";
|
||||
}
|
||||
|
||||
$valueStr = implode(", ", $values);
|
||||
|
||||
$db->query("INSERT INTO `VoiceCallHistoryJob` (`date`) VALUES $valueStr ON DUPLICATE KEY UPDATE date=VALUES(date)");
|
||||
$db->query("UPDATE `VoiceCallHistoryJob` SET `status` = 'created', `finished` = NULL WHERE `date` = '" . date("Y-m-d") . "'");
|
||||
|
||||
return [
|
||||
"message" => "Created " . count($values) . " jobs."
|
||||
];
|
||||
}
|
||||
|
||||
public static function updateJobStatus($id, $status): array {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$escapedStatus = $db->escape($status);
|
||||
$escapedId = $db->escape($id);
|
||||
|
||||
$finished = $status == "success" ? ", `finished` = NOW()" : "";
|
||||
|
||||
$db->query("UPDATE `VoiceCallHistoryJob` SET `status` = '$escapedStatus' $finished WHERE `id` = $escapedId");
|
||||
|
||||
return [
|
||||
"message" => "Updated job $id status to $status."
|
||||
];
|
||||
}
|
||||
|
||||
public static function getJobsNotDone(): array {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$query = $db->query("SELECT * FROM `VoiceCallHistoryJob` WHERE `status` = 'created' OR `status` = 'failed' OR `status` = 'pending' ORDER BY `date`");
|
||||
|
||||
$items = [];
|
||||
if($db->num_rows($query)) {
|
||||
while($data = $db->fetch_object($query)) {
|
||||
$items[] = new VoiceCallHistoryJobModel($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user