ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilMailTemplateService.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Mail/classes/class.ilMailTemplateContext.php';
5 
12 {
17  public static function clearFromXml($a_component, array $a_new_templates)
18  {
19  global $DIC;
20 
21  if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
22  return;
23  }
24 
25  $persisted_templates = array();
26  $query = 'SELECT id FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text');
27  $set = $DIC->database()->query($query);
28  while ($row = $DIC->database()->fetchAssoc($set)) {
29  $persisted_templates[] = $row['id'];
30  }
31 
32  if (count($persisted_templates)) {
33  if (count($a_new_templates)) {
34  foreach ($persisted_templates as $id) {
35  if (!in_array($id, $a_new_templates)) {
36  $DIC->database()->manipulate(
37  'DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text') . ' AND id = ' . $DIC->database()->quote($id, 'text')
38  );
39  $DIC['ilLog']->debug("Mail Template XML - Context " . $id . " in class " . $a_component . " deleted.");
40  }
41  }
42  } else {
43  $DIC->database()->manipulate('DELETE FROM mail_tpl_ctx WHERE component = ' . $DIC->database()->quote($a_component, 'text'));
44  $DIC['ilLog']->debug("Mail Template XML - All contexts deleted for " . $a_component . " as component is inactive.");
45  }
46  }
47  }
48 
55  public static function insertFromXML($a_component, $a_id, $a_class, $a_path)
56  {
57  global $DIC;
58 
59  if (!$DIC->database()->tableExists('mail_tpl_ctx')) {
60  return;
61  }
62 
63  $context = self::getContextInstance($a_component, $a_id, $a_class, $a_path);
64  if ($context instanceof ilMailTemplateContext) {
65  self::createEntry($context, $a_component, $a_class, $a_path);
66  }
67  }
68 
74  public static function getTemplateContextById($a_id)
75  {
76  $contexts = self::getTemplateContexts($a_id);
77  $first_context = current($contexts);
78  if (!($first_context instanceof ilMailTemplateContext) || $first_context->getId() != $a_id) {
79  require_once 'Services/Mail/exceptions/class.ilMailException.php';
80  throw new ilMailException(sprintf("Could not find a mail template context with id: %s", $a_id));
81  }
82  return $first_context;
83  }
84 
90  public static function getTemplateContexts($a_id = null)
91  {
92  global $DIC;
93 
94  $templates = array();
95 
96  if ($a_id && !is_array($a_id)) {
97  $a_id = array($a_id);
98  }
99 
100  $query = 'SELECT * FROM mail_tpl_ctx';
101  $where = array();
102  if ($a_id) {
103  $where[] = $DIC->database()->in('id', $a_id, false, 'text');
104  }
105  if (count($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 
120 
128  protected static function getContextInstance($a_component, $a_id, $a_class, $a_path)
129  {
130  global $DIC;
131 
132  $mess = '';
133 
134  if (!$a_path) {
135  $a_path = $a_component . '/classes/';
136  }
137  $class_file = $a_path . 'class.' . $a_class . '.php';
138 
139  if (file_exists($class_file)) {
140  require_once $class_file;
141  if (class_exists($a_class)) {
142  $context = new $a_class();
143  if ($context instanceof ilMailTemplateContext) {
144  if ($context->getId() == $a_id) {
145  return $context;
146  } else {
147  $mess .= " - context id mismatch";
148  }
149  } else {
150  $mess .= " - does not extend ilMailTemplateContext";
151  }
152  } else {
153  $mess = "- class not found in file";
154  }
155  } else {
156  $mess = " - class file not found";
157  }
158 
159  $DIC['ilLog']->debug("Mail Template XML - Context " . $a_id . " in class " . $a_class . " (" . $class_file . ") is invalid." . $mess);
160  }
161 
168  protected static function createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
169  {
170  global $DIC;
171 
172  $query = "SELECT id FROM mail_tpl_ctx WHERE id = %s";
173  $res = $DIC->database()->queryF($query, array('text'), array($a_context->getId()));
174  $row = $DIC->database()->fetchAssoc($res);
175  $context_exists = ($row['id'] == $a_context->getId());
176 
177  if (!$context_exists) {
178  $DIC->database()->insert('mail_tpl_ctx', array(
179  'id' => array('text', $a_context->getId()),
180  'component' => array('text', $a_component),
181  'class' => array('text', $a_class),
182  'path' => array('text', $a_path)
183  ));
184 
185  $DIC['ilLog']->debug("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " added.");
186  } else {
187  $DIC->database()->update('mail_tpl_ctx', array(
188  'component' => array('text', $a_component),
189  'class' => array('text', $a_class),
190  'path' => array('text', $a_path)
191  ), array(
192  'id' => array('text', $a_context->getId())
193  ));
194 
195  $DIC['ilLog']->debug("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " updated.");
196  }
197  }
198 }
static insertFromXML($a_component, $a_id, $a_class, $a_path)
global $DIC
Definition: saml.php:7
static clearFromXml($a_component, array $a_new_templates)
Class ilMailTemplateService.
if(!array_key_exists('StateId', $_REQUEST)) $id
Class ilMailException.
getId()
Returns a unique (in the context of mail template contexts) id.
static getTemplateContexts($a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id. ...
foreach($_POST as $key=> $value) $res
static createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
Class ilMailTemplateContext.
$query
Create styles array
The data for the language used.
static getContextInstance($a_component, $a_id, $a_class, $a_path)