|
|
<?php |
|
|
|
|
|
namespace app\Repositories; |
|
|
|
|
|
use app\Core\Repository; |
|
|
use app\Models\MessageModel; |
|
|
use Exception; |
|
|
|
|
|
class MessageRepository extends Repository |
|
|
{ |
|
|
function config() |
|
|
{ |
|
|
parent::$table = MessageModel::$table; |
|
|
} |
|
|
|
|
|
public function addMessage($uniqueid, $src, $dst, $tipo, $content, $profile_name, $media, $status, $mimetype = null, $file_name = null, $id_provedor = null) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = ''; |
|
|
if ($uniqueid) { |
|
|
$query = "INSERT INTO $table (uniqueid, src, dst, type, content, profile_name, media, status, mimetype, file_name, id_provedor) |
|
|
VALUES(:uniqueid, :src, :dst, :type, :content, :profile_name, :media, :status, :mimetype, :file_name, :id_provedor);"; |
|
|
return $this->db->create($query, [ |
|
|
'uniqueid' => $uniqueid, |
|
|
'src' => $src, |
|
|
'dst' => $dst, |
|
|
'type' => $tipo, |
|
|
'content' => $content, |
|
|
'profile_name' => utf8_decode($profile_name), |
|
|
'media' => $media, |
|
|
'status' => $status, |
|
|
'id_provedor' => $id_provedor, |
|
|
'file_name' => $file_name, |
|
|
'mimetype' => $mimetype |
|
|
]); |
|
|
} else { |
|
|
logger('debug')->info(print_r([ |
|
|
'uniqueid' => $uniqueid, |
|
|
'src' => $src, |
|
|
'dst' => $dst, |
|
|
'type' => $tipo, |
|
|
'content' => $content, |
|
|
'profile_name' => utf8_decode($profile_name), |
|
|
'media' => $media, |
|
|
'status' => $status, |
|
|
'id_provedor' => $id_provedor, |
|
|
'file_name' => $file_name, |
|
|
'mimetype' => $mimetype |
|
|
], true)); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
public function get(array $params) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "SELECT * FROM $table WHERE uniqueid = :uniqueid ORDER BY id"; |
|
|
if (empty($uniqueid['uniqueid'])) { |
|
|
throw new Exception('Par<EFBFBD>metro matricula <EFBFBD> obrigat<EFBFBD>rio'); |
|
|
} |
|
|
return $this->db->read($query, $params)->fetch(); |
|
|
} |
|
|
|
|
|
public function findMessageByNumber($number) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "SELECT * FROM $table WHERE (src = :number OR dst = :number) ORDER BY msg_date ASC, uniqueid "; |
|
|
return $this->db->read($query, ['number' => $number])->fetchAll(); |
|
|
} |
|
|
|
|
|
public function findLastMessage($uniqueid) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "SELECT * FROM $table WHERE uniqueid = :uniqueid "; |
|
|
$query .= " ORDER BY msg_date DESC LIMIT 1"; |
|
|
return $this->db->read($query, ['uniqueid' => $uniqueid])->fetch(); |
|
|
} |
|
|
public function markMessege($uniqueid, $status) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "UPDATE $table SET status = :status WHERE uniqueid = :uniqueid;"; |
|
|
$data['uniqueid'] = $uniqueid; |
|
|
$data['status'] = $status; |
|
|
return $this->db->update($query, $data); |
|
|
} |
|
|
|
|
|
public function getNameCliente($uniqueid, $cliente_id) |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "SELECT profile_name FROM $table mm WHERE uniqueid = :uniqueid AND src = :cliente_id LIMIT 1"; |
|
|
|
|
|
return $this->db->read($query, ['uniqueid' => $uniqueid, 'cliente_id' => $cliente_id])->fetch(); |
|
|
} |
|
|
function list(array $params = []): array |
|
|
{ |
|
|
$table = self::$table; |
|
|
$query = "SELECT * FROM $table WHERE 1=1 "; |
|
|
$dados = []; |
|
|
if (!empty($params['uniqueid'])) { |
|
|
if (strlen($params['uniqueid']) < 32) { |
|
|
throw new Exception("uniqueid invalido", 1); |
|
|
} |
|
|
$query .= " AND uniqueid = :uniqueid "; |
|
|
$dados['uniqueid'] = $params['uniqueid']; |
|
|
} |
|
|
|
|
|
return $this->db->read($query, $dados)->fetchAll(); |
|
|
} |
|
|
} |