ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailTemplateContextService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
26 public static function clearFromXml(string $a_component, array $a_new_templates): void
27 {
28 global $DIC;
29 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
30 return;
31 }
32 $persisted_templates = [];
33 $query = 'SELECT id FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text');
34 $set = $DIC->database()->query($query);
35 while ($row = $DIC->database()->fetchAssoc($set)) {
36 $persisted_templates[] = $row['id'];
37 }
38
39 if ($persisted_templates !== []) {
40 if ($a_new_templates !== []) {
41 foreach ($persisted_templates as $id) {
42 if (!in_array($id, $a_new_templates, true)) {
43 $DIC->database()->manipulate(
44 'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
45 $a_component,
46 'text'
47 ) . ' AND id = ' . $DIC->database()->quote($id, 'text')
48 );
49 }
50 }
51 } else {
52 $DIC->database()->manipulate(
53 'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
54 $a_component,
55 'text'
56 )
57 );
58 }
59 }
60 }
61
62 public static function insertFromXML(string $a_component, string $a_id, string $a_class, ?string $a_path): void
63 {
64 global $DIC;
65
66 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
67 return;
68 }
69
70 $context = self::getContextInstance($a_component, $a_id, $a_class, $a_path, true);
71 if ($context instanceof ilMailTemplateContext) {
72 self::createEntry($context, $a_component, $a_class, $a_path);
73 }
74 }
75
79 public static function getTemplateContextById(string $a_id): ilMailTemplateContext
80 {
81 $contexts = self::getTemplateContexts([$a_id]);
82 $first_context = current($contexts);
83 if (!($first_context instanceof ilMailTemplateContext) || $first_context->getId() !== $a_id) {
84 throw new ilMailException(sprintf('Could not find a mail template context with id: %s', $a_id));
85 }
86
87 return $first_context;
88 }
89
95 public static function getTemplateContexts(?array $a_id = null): array
96 {
97 global $DIC;
98 $templates = [];
99
100 $query = 'SELECT * FROM mail_tpl_ctx';
101 $where = [];
102 if (is_array($a_id) && count($a_id) > 0) {
103 $where[] = $DIC->database()->in('id', $a_id, false, 'text');
104 }
105 if ($where !== []) {
106 $query .= ' WHERE ' . implode(' AND ', $where);
107 }
108
109 $set = $DIC->database()->query($query);
110 while ($row = $DIC->database()->fetchAssoc($set)) {
111 $context = self::getContextInstance($row['component'], $row['id'], $row['class'], $row['path']);
112 if ($context instanceof ilMailTemplateContext) {
113 $templates[$context->getId()] = $context;
114 }
115 }
116
117 return $templates;
118 }
119
123 private static function getContextInstance(
124 string $a_component,
125 string $a_id,
126 string $a_class,
127 ?string $a_path,
128 bool $is_creation_context = false
130 if (class_exists($a_class)) {
131 if ($is_creation_context) {
132 $class = new ReflectionClass($a_class);
133 $context = $class->newInstanceWithoutConstructor();
134 } else {
135 $context = new $a_class();
136 }
137
138 if (($context instanceof ilMailTemplateContext) && $context->getId() === $a_id) {
139 return $context;
140 }
141 }
142
143 return null;
144 }
145
149 private static function createEntry(
150 ilMailTemplateContext $a_context,
151 string $a_component,
152 string $a_class,
153 ?string $a_path
154 ): void {
155 global $DIC;
156
157 $query = 'SELECT id FROM mail_tpl_ctx WHERE id = %s';
158 $res = $DIC->database()->queryF($query, ['text'], [$a_context->getId()]);
159 $row = $DIC->database()->fetchAssoc($res);
160 $row_id = $row['id'] ?? null;
161 $context_exists = ($row_id === $a_context->getId());
162
163 if ($context_exists) {
164 $DIC->database()->update('mail_tpl_ctx', [
165 'component' => ['text', $a_component],
166 'class' => ['text', $a_class],
167 'path' => ['text', $a_path]
168 ], [
169 'id' => ['text', $a_context->getId()]
170 ]);
171 } else {
172 $DIC->database()->insert('mail_tpl_ctx', [
173 'id' => ['text', $a_context->getId()],
174 'component' => ['text', $a_component],
175 'class' => ['text', $a_class],
176 'path' => ['text', $a_path]
177 ]);
178 }
179 }
180}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static clearFromXml(string $a_component, array $a_new_templates)
static insertFromXML(string $a_component, string $a_id, string $a_class, ?string $a_path)
static getContextInstance(string $a_component, string $a_id, string $a_class, ?string $a_path, bool $is_creation_context=false)
static createEntry(ilMailTemplateContext $a_context, string $a_component, string $a_class, ?string $a_path)
static getTemplateContexts(?array $a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$context
Definition: webdav.php:31