ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailTemplateRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 protected ilDBInterface $db;
24
25 public function __construct(?ilDBInterface $db = null)
26 {
27 global $DIC;
28 $this->db = $db ?? $DIC->database();
29 }
30
34 public function getAll(): array
35 {
36 $templates = [];
37
38 $res = $this->db->query('SELECT * FROM mail_man_tpl');
39 while ($row = $this->db->fetchAssoc($res)) {
40 $template = new ilMailTemplate($row);
41 $templates[] = $template;
42 }
43
44 return $templates;
45 }
46
47 public function findById(int $template_id): ilMailTemplate
48 {
49 $res = $this->db->queryF(
50 'SELECT * FROM mail_man_tpl WHERE tpl_id = %s',
52 [$template_id]
53 );
54
55 if ($this->db->numRows($res) === 1) {
56 $row = $this->db->fetchAssoc($res);
57 return new ilMailTemplate($row);
58 }
59
60 throw new OutOfBoundsException(sprintf('Could not find template by id: %s', $template_id));
61 }
62
66 public function findByContextId(string $context_id): array
67 {
68 return array_values(
69 array_filter(
70 $this->getAll(),
71 static fn(ilMailTemplate $template): bool => $context_id === $template->getContext()
72 )
73 );
74 }
75
79 public function deleteByIds(array $template_ids): void
80 {
81 if ($template_ids !== []) {
82 $this->db->manipulate(
83 'DELETE FROM mail_man_tpl WHERE ' . $this->db->in('tpl_id', $template_ids, false, ilDBConstants::T_INTEGER)
84 );
85 }
86 }
87
88 public function store(ilMailTemplate $template): void
89 {
90 if ($template->getTplId() > 0) {
91 $this->db->update(
92 'mail_man_tpl',
93 [
94 'title' => [ilDBConstants::T_TEXT, $template->getTitle()],
95 'context' => [ilDBConstants::T_TEXT, $template->getContext()],
96 'lang' => [ilDBConstants::T_TEXT, $template->getLang()],
97 'm_subject' => [ilDBConstants::T_TEXT, $template->getSubject()],
98 'm_message' => [ilDBConstants::T_TEXT, $template->getMessage()],
99 'is_default' => [ilDBConstants::T_INTEGER, $template->isDefault()],
100 ],
101 [
102 'tpl_id' => [ilDBConstants::T_INTEGER, $template->getTplId()],
103 ]
104 );
105 } else {
106 $next_id = $this->db->nextId('mail_man_tpl');
107 $this->db->insert('mail_man_tpl', [
108 'tpl_id' => [ilDBConstants::T_INTEGER, $next_id],
109 'title' => [ilDBConstants::T_TEXT, $template->getTitle()],
110 'context' => [ilDBConstants::T_TEXT, $template->getContext()],
111 'lang' => [ilDBConstants::T_TEXT, $template->getLang()],
112 'm_subject' => [ilDBConstants::T_TEXT, $template->getSubject()],
113 'm_message' => [ilDBConstants::T_TEXT, $template->getMessage()],
114 'is_default' => [ilDBConstants::T_INTEGER, $template->isDefault()],
115 ]);
116 $template->setTplId($next_id);
117 }
118 }
119}
setTplId(int $template_id)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26