57 lines
1.4 KiB
PHP
57 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
/**
|
|
* ContactGroup Model
|
|
* Manages groupings/lists of contacts for broadcast campaigns.
|
|
*/
|
|
class ContactGroup extends BaseModel
|
|
{
|
|
protected string $table = 'contact_groups';
|
|
|
|
/**
|
|
* Attach a contact to this group
|
|
*/
|
|
public function attachContact(int $groupId, int $contactId)
|
|
{
|
|
$exists = $this->db->query(
|
|
"SELECT 1 FROM contact_group_relations WHERE group_id = ? AND contact_id = ?",
|
|
[$groupId, $contactId]
|
|
)->fetch();
|
|
|
|
if (!$exists) {
|
|
$this->db->query(
|
|
"INSERT INTO contact_group_relations (group_id, contact_id) VALUES (?, ?)",
|
|
[$groupId, $contactId]
|
|
);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Remove a contact from this group
|
|
*/
|
|
public function detachContact(int $groupId, int $contactId)
|
|
{
|
|
$this->db->query(
|
|
"DELETE FROM contact_group_relations WHERE group_id = ? AND contact_id = ?",
|
|
[$groupId, $contactId]
|
|
);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get all raw contact records for a group (Decryption needed after fetch)
|
|
*/
|
|
public function getRawContacts(int $groupId)
|
|
{
|
|
return $this->db->query(
|
|
"SELECT c.* FROM contacts c
|
|
JOIN contact_group_relations cgr ON c.id = cgr.contact_id
|
|
WHERE cgr.group_id = ?",
|
|
[$groupId]
|
|
)->fetchAll();
|
|
}
|
|
}
|