ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  if(!$GLOBALS['ilDB']->tableExists('mail_tpl_ctx'))
20  {
21  return;
22  }
23 
24  $persisted_templates = array();
25  $query = 'SELECT id FROM mail_tpl_ctx WHERE component = ' . $GLOBALS['ilDB']->quote($a_component, 'text');
26  $set = $GLOBALS['ilDB']->query($query);
27  while($row = $GLOBALS['ilDB']->fetchAssoc($set))
28  {
29  $persisted_templates[] = $row['id'];
30  }
31 
32  if(count($persisted_templates))
33  {
34  if(count($a_new_templates))
35  {
36  foreach($persisted_templates as $id)
37  {
38  if(!in_array($id, $a_new_templates))
39  {
40  $GLOBALS['ilDB']->manipulate(
41  'DELETE FROM mail_tpl_ctx WHERE component = ' . $GLOBALS['ilDB']->quote($a_component, 'text') . ' AND id = ' . $GLOBALS['ilDB']->quote($id, 'text')
42  );
43  $GLOBALS['ilLog']->write("Mail Template XML - Context " . $id . " in class " . $a_component . " deleted.");
44  }
45  }
46  }
47  else
48  {
49  $GLOBALS['ilDB']->manipulate('DELETE FROM mail_tpl_ctx WHERE component = ' . $GLOBALS['ilDB']->quote($a_component, 'text'));
50  $GLOBALS['ilLog']->write("Mail Template XML - All contexts deleted for " . $a_component . " as component is inactive.");
51  }
52  }
53  }
54 
61  public static function insertFromXML($a_component, $a_id, $a_class, $a_path)
62  {
63  if(!$GLOBALS['ilDB']->tableExists('mail_tpl_ctx'))
64  {
65  return;
66  }
67 
68  $context = self::getContextInstance($a_component, $a_id, $a_class, $a_path);
69  if($context instanceof ilMailTemplateContext)
70  {
71  self::createEntry($context, $a_component, $a_class, $a_path);
72  }
73  }
74 
80  public static function getTemplateContextById($a_id)
81  {
82  $contexts = self::getTemplateContexts($a_id);
83  $first_context = current($contexts);
84  if(!($first_context instanceof ilMailTemplateContext) || $first_context->getId() != $a_id)
85  {
86  require_once 'Services/Mail/exceptions/class.ilMailException.php';
87  throw new ilMailException(sprintf("Could not find a mail template context with id: %s", $a_id));
88  }
89  return $first_context;
90  }
91 
97  public static function getTemplateContexts($a_id = null)
98  {
99  $templates = array();
100 
101  if($a_id && !is_array($a_id))
102  {
103  $a_id = array($a_id);
104  }
105 
106  $query = 'SELECT * FROM mail_tpl_ctx';
107  $where = array();
108  if($a_id)
109  {
110  $where[] = $GLOBALS['ilDB']->in('id', $a_id, false, 'text');
111  }
112  if(count($where))
113  {
114  $query .= ' WHERE '. implode(' AND ', $where);
115  }
116 
117  $set = $GLOBALS['ilDB']->query($query);
118  while($row = $GLOBALS['ilDB']->fetchAssoc($set))
119  {
120  $context = self::getContextInstance($row['component'], $row['id'], $row['class'], $row['path']);
121  if($context instanceof ilMailTemplateContext)
122  {
123  $templates[$context->getId()] = $context;
124  }
125  }
126 
127  return $templates;
128  }
129 
130 
137  protected static function getContextInstance($a_component, $a_id, $a_class, $a_path)
138  {
139  $mess = '';
140 
141  if(!$a_path)
142  {
143  $a_path = $a_component . '/classes/';
144  }
145  $class_file = $a_path . 'class.' . $a_class . '.php';
146 
147  if(file_exists($class_file))
148  {
149  require_once $class_file;
150  if(class_exists($a_class))
151  {
152  $context = new $a_class();
153  if($context instanceof ilMailTemplateContext)
154  {
155  if($context->getId() == $a_id)
156  {
157  return $context;
158  }
159  else
160  {
161  $mess .= " - context id mismatch";
162  }
163  }
164  else
165  {
166  $mess .= " - does not extend ilMailTemplateContext";
167  }
168  }
169  else
170  {
171  $mess = "- class not found in file";
172  }
173  }
174  else
175  {
176  $mess = " - class file not found";
177  }
178 
179  $GLOBALS['ilLog']->write("Mail Template XML - Context " . $a_id . " in class " . $a_class . " (" . $class_file . ") is invalid." . $mess);
180  }
181 
188  protected static function createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
189  {
190  $query = "SELECT id FROM mail_tpl_ctx WHERE id = %s";
191  $res = $GLOBALS['ilDB']->queryF($query, array('text'), array($a_context->getId()));
192  $row = $GLOBALS['ilDB']->fetchAssoc($res);
193  $context_exists = ($row['id'] == $a_context->getId());
194 
195  if(!$context_exists)
196  {
197  $GLOBALS['ilDB']->insert('mail_tpl_ctx', array(
198  'id' => array('text', $a_context->getId()),
199  'component' => array('text', $a_component),
200  'class' => array('text', $a_class),
201  'path' => array('text', $a_path)
202  ));
203 
204  $GLOBALS['ilLog']->write("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " added.");
205  }
206  else
207  {
208  $GLOBALS['ilDB']->update('mail_tpl_ctx', array(
209  'component' => array('text', $a_component),
210  'class' => array('text', $a_class),
211  'path' => array('text', $a_path)
212  ), array(
213  'id' => array('text', $a_context->getId())
214  ));
215 
216  $GLOBALS['ilLog']->write("Mail Template XML - Context " . $a_context->getId() . " in class " . $a_class . " updated.");
217  }
218  }
219 }
static insertFromXML($a_component, $a_id, $a_class, $a_path)
static clearFromXml($a_component, array $a_new_templates)
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
Class ilMailTemplateService.
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. ...
static createEntry(ilMailTemplateContext $a_context, $a_component, $a_class, $a_path)
Class ilMailTemplateContext.
Create styles array
The data for the language used.
static getContextInstance($a_component, $a_id, $a_class, $a_path)