Deploy: 2026-05-21 15:33:14
This commit is contained in:
61
backend/app/Models/Template.php
Normal file
61
backend/app/Models/Template.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Security;
|
||||
|
||||
/**
|
||||
* Template Model
|
||||
* Stores predefined WhatsApp message templates with variables.
|
||||
*/
|
||||
class Template extends BaseModel
|
||||
{
|
||||
protected string $table = 'templates';
|
||||
|
||||
/**
|
||||
* Create template with encrypted media URL if exists
|
||||
*/
|
||||
public function createSecure(array $data)
|
||||
{
|
||||
if (!empty($data['media_url'])) {
|
||||
$data['media_url'] = Security::encrypt($data['media_url']);
|
||||
}
|
||||
return $this->create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve and decrypt templates
|
||||
*/
|
||||
public function findAllByCompany(int $companyId)
|
||||
{
|
||||
$templates = $this->db->query(
|
||||
"SELECT * FROM {$this->table} WHERE company_id = ? ORDER BY id DESC",
|
||||
[$companyId]
|
||||
)->fetchAll();
|
||||
|
||||
foreach ($templates as &$template) {
|
||||
if (!empty($template['media_url'])) {
|
||||
$template['media_url'] = Security::decrypt($template['media_url']);
|
||||
}
|
||||
}
|
||||
|
||||
return $templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single template and decrypt
|
||||
*/
|
||||
public function findByIdAndCompany(int $id, int $companyId)
|
||||
{
|
||||
$template = $this->db->query(
|
||||
"SELECT * FROM {$this->table} WHERE id = ? AND company_id = ? LIMIT 1",
|
||||
[$id, $companyId]
|
||||
)->fetch();
|
||||
|
||||
if ($template && !empty($template['media_url'])) {
|
||||
$template['media_url'] = Security::decrypt($template['media_url']);
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user