[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:
Luca Haid
2024-04-10 15:17:02 +02:00
parent 9d5be184a2
commit b42409874b
10 changed files with 651 additions and 3 deletions
@@ -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;
}
}