ILIAS  release_8 Revision v8.24
class.ilMailTemplateContextService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
30 public static function clearFromXml(string $a_component, array $a_new_templates): void
31 {
32 global $DIC;
33 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
34 return;
35 }
36 $persisted_templates = [];
37 $query = 'SELECT id FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text');
38 $set = $DIC->database()->query($query);
39 while ($row = $DIC->database()->fetchAssoc($set)) {
40 $persisted_templates[] = $row['id'];
41 }
42
43 if (count($persisted_templates)) {
44 if (count($a_new_templates)) {
45 foreach ($persisted_templates as $id) {
46 if (!in_array($id, $a_new_templates, true)) {
47 $DIC->database()->manipulate(
48 'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
49 $a_component,
50 'text'
51 ) . ' AND id = ' . $DIC->database()->quote($id, 'text')
52 );
53 }
54 }
55 } else {
56 $DIC->database()->manipulate(
57 'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
58 $a_component,
59 'text'
60 )
61 );
62 }
63 }
64 }
65
66 public static function insertFromXML(string $a_component, string $a_id, string $a_class, ?string $a_path): void
67 {
68 global $DIC;
69
70 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
71 return;
72 }
73
74 $context = self::getContextInstance($a_component, $a_id, $a_class, $a_path, true);
75 if ($context instanceof ilMailTemplateContext) {
76 self::createEntry($context, $a_component, $a_class, $a_path);
77 }
78 }
79
85 public static function getTemplateContextById(string $a_id): ilMailTemplateContext
86 {
87 $contexts = self::getTemplateContexts([$a_id]);
88 $first_context = current($contexts);
89 if (!($first_context instanceof ilMailTemplateContext) || $first_context->getId() !== $a_id) {
90 throw new ilMailException(sprintf("Could not find a mail template context with id: %s", $a_id));
91 }
92
93 return $first_context;
94 }
95
101 public static function getTemplateContexts(?array $a_id = null): array
102 {
103 global $DIC;
104 $templates = [];
105
106 $query = 'SELECT * FROM mail_tpl_ctx';
107 $where = [];
108 if (is_array($a_id) && count($a_id)) {
109 $where[] = $DIC->database()->in('id', $a_id, false, 'text');
110 }
111 if (count($where)) {
112 $query .= ' WHERE ' . implode(' AND ', $where);
113 }
114
115 $set = $DIC->database()->query($query);
116 while ($row = $DIC->database()->fetchAssoc($set)) {
117 $context = self::getContextInstance($row['component'], $row['id'], $row['class'], $row['path']);
118 if ($context instanceof ilMailTemplateContext) {
119 $templates[$context->getId()] = $context;
120 }
121 }
122
123 return $templates;
124 }
125
126 protected static function getContextInstance(
127 string $a_component,
128 string $a_id,
129 string $a_class,
130 ?string $a_path,
131 bool $isCreationContext = false
133 if (!$a_path) {
134 $a_path = $a_component . '/classes/';
135 }
136 $class_file = $a_path . 'class.' . $a_class . '.php';
137
138 if (class_exists($a_class) && file_exists($class_file)) {
139 if ($isCreationContext) {
140 $reflClass = new ReflectionClass($a_class);
141 $context = $reflClass->newInstanceWithoutConstructor();
142 } else {
143 $context = new $a_class();
144 }
145
146 if (($context instanceof ilMailTemplateContext) && $context->getId() === $a_id) {
147 return $context;
148 }
149 }
150 return null;
151 }
152
153 protected static function createEntry(
154 ilMailTemplateContext $a_context,
155 string $a_component,
156 string $a_class,
157 ?string $a_path
158 ): void {
159 global $DIC;
160
161 $query = "SELECT id FROM mail_tpl_ctx WHERE id = %s";
162 $res = $DIC->database()->queryF($query, ['text'], [$a_context->getId()]);
163 $row = $DIC->database()->fetchAssoc($res);
164 $row_id = $row['id'] ?? null;
165 $context_exists = ($row_id === $a_context->getId());
166
167 if (!$context_exists) {
168 $DIC->database()->insert('mail_tpl_ctx', [
169 'id' => ['text', $a_context->getId()],
170 'component' => ['text', $a_component],
171 'class' => ['text', $a_class],
172 'path' => ['text', $a_path]
173 ]);
174 } else {
175 $DIC->database()->update('mail_tpl_ctx', [
176 'component' => ['text', $a_component],
177 'class' => ['text', $a_class],
178 'path' => ['text', $a_path]
179 ], [
180 'id' => ['text', $a_context->getId()]
181 ]);
182 }
183 }
184}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilMailException.
Class ilMailTemplateContextService.
static clearFromXml(string $a_component, array $a_new_templates)
static getContextInstance(string $a_component, string $a_id, string $a_class, ?string $a_path, bool $isCreationContext=false)
static insertFromXML(string $a_component, string $a_id, string $a_class, ?string $a_path)
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.
Class ilMailTemplateContext.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query
$context
Definition: webdav.php:29