Files
nabeh/backend/app/Models/Template.php
2026-05-21 15:33:14 +03:00

62 lines
1.5 KiB
PHP

<?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;
}
}