ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailTemplateRepository.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 {
10  protected $db;
11 
16  public function __construct(\ilDBInterface $db = null)
17  {
18  global $DIC;
19 
20  if (null === $db) {
21  $db = $DIC->database();
22  }
23  $this->db = $db;
24  }
25 
29  public function getAll() : array
30  {
31  $templates = [];
32 
33  $res = $this->db->query('SELECT * FROM mail_man_tpl');
34  while ($row = $this->db->fetchAssoc($res)) {
35  $template = new \ilMailTemplate($row);
36  $templates[] = $template;
37  }
38 
39  return $templates;
40  }
41 
47  public function findById(int $templateId) : \ilMailTemplate
48  {
49  $res = $this->db->queryF(
50  'SELECT * FROM mail_man_tpl WHERE tpl_id = %s',
51  ['integer'],
52  [$templateId]
53  );
54 
55  if (1 === (int) $this->db->numRows($res)) {
56  $row = $this->db->fetchAssoc($res);
57  return new \ilMailTemplate($row);
58  }
59 
60  throw new \OutOfBoundsException(sprintf("Could not find template by id: %s", $templateId));
61  }
62 
67  public function findByContextId(string $contextId) : array
68  {
69  return array_filter($this->getAll(), function (\ilMailTemplate $template) use ($contextId) {
70  return $contextId === $template->getContext();
71  });
72  }
73 
77  public function deleteByIds(array $templateIds)
78  {
79  if (count($templateIds) > 0) {
80  $this->db->manipulate(
81  '
82  DELETE FROM mail_man_tpl WHERE ' .
83  $this->db->in('tpl_id', $templateIds, false, 'integer')
84  );
85  }
86  }
87 
91  public function store(\ilMailTemplate $template)
92  {
93  if ($template->getTplId() > 0) {
94  $this->db->update(
95  'mail_man_tpl',
96  [
97  'title' => ['text', $template->getTitle()],
98  'context' => ['text', $template->getContext()],
99  'lang' => ['text', $template->getLang()],
100  'm_subject' => ['text', $template->getSubject()],
101  'm_message' => ['text', $template->getMessage()],
102  'is_default' => ['text', $template->isDefault()],
103  ],
104  [
105  'tpl_id' => ['integer', $template->getTplId()],
106  ]
107  );
108  } else {
109  $nextId = $this->db->nextId('mail_man_tpl');
110  $this->db->insert('mail_man_tpl', [
111  'tpl_id' => ['integer', $nextId],
112  'title' => ['text', $template->getTitle()],
113  'context' => ['text', $template->getContext()],
114  'lang' => ['text', $template->getLang()],
115  'm_subject' => ['text', $template->getSubject()],
116  'm_message' => ['text', $template->getMessage()],
117  'is_default' => ['integer', $template->isDefault()],
118  ]);
119  $template->setTplId($nextId);
120  }
121  }
122 }
__construct(\ilDBInterface $db=null)
ilMailTemplateRepository constructor.
$template
global $DIC
Definition: saml.php:7
Class ilMailTemplate.
foreach($_POST as $key=> $value) $res
$row
setTplId(int $templateId)
Class ilMailTemplateRepository.