ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailTemplateContextService.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
13 public static function clearFromXml($a_component, array $a_new_templates)
14 {
15 global $DIC;
16
17 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
18 return;
19 }
20
21 $persisted_templates = array();
22 $query = 'SELECT id FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text');
23 $set = $DIC->database()->query($query);
24 while ($row = $DIC->database()->fetchAssoc($set)) {
25 $persisted_templates[] = $row['id'];
26 }
27
28 if (count($persisted_templates)) {
29 if (count($a_new_templates)) {
30 foreach ($persisted_templates as $id) {
31 if (!in_array($id, $a_new_templates)) {
32 $DIC->database()->manipulate(
33 'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
34 $a_component,
35 'text'
36 ) . ' AND id = ' . $DIC->database()->quote($id, 'text')
37 );
38 $DIC['ilLog']->debug("Mail Template XML - Context " . $id . " in class " . $a_component . " deleted.");
39 }
40 }
41 } else {
42 $DIC->database()->manipulate('DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote(
43 $a_component,
44 'text'
45 ));
46 $DIC['ilLog']->debug("Mail Template XML - All contexts deleted for " . $a_component . " as component is inactive.");
47 }
48 }
49 }
50
57 public static function insertFromXML($a_component, $a_id, $a_class, $a_path)
58 {
59 global $DIC;
60
61 if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
62 return;
63 }
64
65 $context = self::getContextInstance($a_component, $a_id, $a_class, $a_path);
66 if ($context instanceof ilMailTemplateContext) {
67 self::createEntry($context, $a_component, $a_class, $a_path);
68 }
69 }
70
76 public static function getTemplateContextById($a_id)
77 {
78 $contexts = self::getTemplateContexts($a_id);
79 $first_context = current($contexts);
80 if (!($first_context instanceof ilMailTemplateContext) || $first_context->getId() != $a_id) {
81 require_once 'Services/Mail/exceptions/class.ilMailException.php';
82 throw new ilMailException(sprintf("Could not find a mail template context with id: %s", $a_id));
83 }
84 return $first_context;
85 }
86
92 public static function getTemplateContexts($a_id = null)
93 {
94 global $DIC;
95
96 $templates = array();
97
98 if ($a_id && !is_array($a_id)) {
99 $a_id = array($a_id);
100 }
101
102 $query = 'SELECT * FROM mail_tpl_ctx';
103 $where = array();
104 if ($a_id) {
105 $where[] = $DIC->database()->in('id', $a_id, false, 'text');
106 }
107 if (count($where)) {
108 $query .= ' WHERE ' . implode(' AND ', $where);
109 }
110
111 $set = $DIC->database()->query($query);
112 while ($row = $DIC->database()->fetchAssoc($set)) {
113 $context = self::getContextInstance($row['component'], $row['id'], $row['class'], $row['path']);
114 if ($context instanceof ilMailTemplateContext) {
115 $templates[$context->getId()] = $context;
116 }
117 }
118
119 return $templates;
120 }
121
122
130 protected static function getContextInstance($a_component, $a_id, $a_class, $a_path)
131 {
132 global $DIC;
133
134 $mess = '';
135
136 if (!$a_path) {
137 $a_path = $a_component . '/classes/';
138 }
139 $class_file = $a_path . 'class.' . $a_class . '.php';
140
141 if (file_exists($class_file)) {
142 require_once $class_file;
143 if (class_exists($a_class)) {
144 $context = new $a_class();
145 if ($context instanceof ilMailTemplateContext) {
146 if ($context->getId() == $a_id) {
147 return $context;
148 } else {
149 $mess .= " - context id mismatch";
150 }
151 } else {
152 $mess .= " - does not extend ilMailTemplateContext";
153 }
154 } else {
155 $mess = "- class not found in file";
156 }
157 } else {
158 $mess = " - class file not found";
159 }
160
161 $DIC['ilLog']->debug("Mail Template XML - Context " . $a_id . " in class " . $a_class . " (" . $class_file . ") is invalid." . $mess);
162 }
163
170 protected static function createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
171 {
172 global $DIC;
173
174 $query = "SELECT id FROM mail_tpl_ctx WHERE id = %s";
175 $res = $DIC->database()->queryF($query, array('text'), array($a_context->getId()));
176 $row = $DIC->database()->fetchAssoc($res);
177 $row_id = $row['id'] ?? null;
178 $context_exists = ($row_id == $a_context->getId());
179
180 if (!$context_exists) {
181 $DIC->database()->insert('mail_tpl_ctx', array(
182 'id' => array('text', $a_context->getId()),
183 'component' => array('text', $a_component),
184 'class' => array('text', $a_class),
185 'path' => array('text', $a_path)
186 ));
187
188 $DIC['ilLog']->debug("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " added.");
189 } else {
190 $DIC->database()->update('mail_tpl_ctx', array(
191 'component' => array('text', $a_component),
192 'class' => array('text', $a_class),
193 'path' => array('text', $a_path)
194 ), array(
195 'id' => array('text', $a_context->getId())
196 ));
197
198 $DIC['ilLog']->debug("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " updated.");
199 }
200 }
201}
An exception for terminatinating execution or to throw for unit testing.
Class ilMailException.
Class ilMailTemplateContextService.
static createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
static clearFromXml($a_component, array $a_new_templates)
static getTemplateContexts($a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
static insertFromXML($a_component, $a_id, $a_class, $a_path)
static getContextInstance($a_component, $a_id, $a_class, $a_path)
Class ilMailTemplateContext.
getId()
Returns a unique (in the context of mail template contexts) id.
global $DIC
Definition: goto.php:24
$query
foreach($_POST as $key=> $value) $res
$context
Definition: webdav.php:26