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