ILIAS  release_8 Revision v8.24
class.ilMailTemplateRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 protected ilDBInterface $db;
27
28 public function __construct(ilDBInterface $db = null)
29 {
30 global $DIC;
31 $this->db = $db ?? $DIC->database();
32 }
33
37 public function getAll(): array
38 {
39 $templates = [];
40
41 $res = $this->db->query('SELECT * FROM mail_man_tpl');
42 while ($row = $this->db->fetchAssoc($res)) {
43 $template = new ilMailTemplate($row);
44 $templates[] = $template;
45 }
46
47 return $templates;
48 }
49
54 public function findById(int $templateId): ilMailTemplate
55 {
56 $res = $this->db->queryF(
57 'SELECT * FROM mail_man_tpl WHERE tpl_id = %s',
59 [$templateId]
60 );
61
62 if (1 === $this->db->numRows($res)) {
63 $row = $this->db->fetchAssoc($res);
64 return new ilMailTemplate($row);
65 }
66
67 throw new OutOfBoundsException(sprintf("Could not find template by id: %s", $templateId));
68 }
69
74 public function findByContextId(string $contextId): array
75 {
76 return array_filter($this->getAll(), static function (ilMailTemplate $template) use ($contextId): bool {
77 return $contextId === $template->getContext();
78 });
79 }
80
84 public function deleteByIds(array $templateIds): void
85 {
86 if (count($templateIds) > 0) {
87 $this->db->manipulate(
88 'DELETE FROM mail_man_tpl WHERE ' . $this->db->in('tpl_id', $templateIds, false, ilDBConstants::T_INTEGER)
89 );
90 }
91 }
92
93 public function store(ilMailTemplate $template): void
94 {
95 if ($template->getTplId() > 0) {
96 $this->db->update(
97 'mail_man_tpl',
98 [
99 'title' => [ilDBConstants::T_TEXT, $template->getTitle()],
100 'context' => [ilDBConstants::T_TEXT, $template->getContext()],
101 'lang' => [ilDBConstants::T_TEXT, $template->getLang()],
102 'm_subject' => [ilDBConstants::T_TEXT, $template->getSubject()],
103 'm_message' => [ilDBConstants::T_TEXT, $template->getMessage()],
104 'is_default' => [ilDBConstants::T_INTEGER, $template->isDefault()],
105 ],
106 [
107 'tpl_id' => [ilDBConstants::T_INTEGER, $template->getTplId()],
108 ]
109 );
110 } else {
111 $nextId = $this->db->nextId('mail_man_tpl');
112 $this->db->insert('mail_man_tpl', [
113 'tpl_id' => [ilDBConstants::T_INTEGER, $nextId],
114 'title' => [ilDBConstants::T_TEXT, $template->getTitle()],
115 'context' => [ilDBConstants::T_TEXT, $template->getContext()],
116 'lang' => [ilDBConstants::T_TEXT, $template->getLang()],
117 'm_subject' => [ilDBConstants::T_TEXT, $template->getSubject()],
118 'm_message' => [ilDBConstants::T_TEXT, $template->getMessage()],
119 'is_default' => [ilDBConstants::T_INTEGER, $template->isDefault()],
120 ]);
121 $template->setTplId($nextId);
122 }
123 }
124}
Class ilMailTemplateRepository.
Class ilMailTemplate.
setTplId(int $templateId)
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69