$value) { if (property_exists(get_called_class(), $field)) { $this->$field = $value; } } } //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(); $me = mfValuecache::singleton()->get("me"); if(!$me) { $me = new User(); $me->loadMe(); mfValuecache::singleton()->set("me", $me); } $sql = /** @lang text */ "INSERT INTO `VoiceCallHistory` (`uid`, `voice_account`, `start`, `source`, `destination`, `state`, `billable`, `duration`, `create_by`, `edit_by`, `create`, `edit`) 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'] . ", ". $me->id . ", ". $me->id . ", ". date("U") . ", ". date("U") . ")"; $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['uid']) ? Helper::generateFilterCondition($filters['uid'], "uid") : ""; $sql .= isset($filters['voice_account']) ? Helper::generateFilterCondition($filters['voice_account'], "voice_account") : ""; $sql .= isset($filters['end']) ? " AND `start` <= '" . $filters['end'] . "'" : ""; $sql .= isset($filters['source']) ? Helper::generateFilterCondition($filters['source'], "source") : ""; $sql .= isset($filters['destination']) ? Helper::generateFilterCondition($filters['destination'], "destination") : ""; $sql .= isset($filters['billable']) ? Helper::generateFilterCondition($filters['billable'], "billable") : ""; $sql .= isset($filters['duration']) ? Helper::generateFilterCondition($filters['duration'], "duration") : ""; $sql .= array_key_exists("contract_id", $filters) ? Helper::generateFilterCondition($filters['contract_id'], "contract_id") : ""; return $sql; } public static function getVoiceCallHistory($filters, $limit = null, $offset = 0, $order = null) { $db = FronkDB::singleton(); $sql = "SELECT * FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters); $sql .= $order === null || $order['key'] === null ? " ORDER BY `start` DESC" : " ORDER BY `" . $order['key'] . "` " . $order['order']; $sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset; //mfLoghandler::singleton()->debug($sql);exit; // die($sql); $result = $db->query($sql); $rows = []; while ($row = $result->fetch_assoc()) { $rows[] = new VoiceCallHistoryModel($row); } return $rows; } public static function getVoiceCallHistoryAsEntity($filters, $limit = null, $offset = 0, $order = null) { $db = FronkDB::singleton(); $sql = "SELECT * FROM `VoiceCallHistory` WHERE 1 " . self::getSqlFilter($filters); $sql .= $order === null || $order['key'] === null ? " ORDER BY `start` DESC" : " ORDER BY `" . $order['key'] . "` " . $order['order']; $sql .= $limit === null ? "" : " LIMIT " . $limit . " OFFSET " . $offset; //mfLoghandler::singleton()->debug($sql);exit; // die($sql); $result = $db->query($sql); $rows = []; while ($row = $db->fetch_object($result)) { $rows[] = new VoiceCallHistory($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']; } }