ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailingLists.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
27  private ilDBInterface $db;
28  private ilObjUser $user;
29  private ?ilMailingList $ml = null;
30 
31  public function __construct(ilObjUser $a_user)
32  {
33  global $DIC;
34 
35  $this->db = $DIC['ilDB'];
36  $this->user = $a_user;
37  }
38 
39  public function isOwner(int $a_ml_id, int $a_usr_id): bool
40  {
41  $res = $this->db->queryF(
42  'SELECT EXISTS(SELECT 1 FROM addressbook_mlist WHERE ml_id = %s AND user_id = %s) cnt',
43  ['integer', 'integer'],
44  [$a_ml_id, $a_usr_id]
45  );
46  $row = $this->db->fetchAssoc($res);
47 
48  return is_array($row) && (int) $row['cnt'] === 1;
49  }
50 
51  public function get(int $id = 0): ilMailingList
52  {
53  return new ilMailingList($this->user, $id);
54  }
55 
60  public function getSelected(array $a_ids = []): array
61  {
62  $entries = [];
63 
64  foreach ($a_ids as $id) {
65  $entries[] = new ilMailingList($this->user, (int) $id);
66  }
67 
68  return $entries;
69  }
70 
71  public function hasAny(): bool
72  {
73  $res = $this->db->queryF(
74  'SELECT EXISTS(SELECT 1 FROM addressbook_mlist WHERE user_id = %s) cnt',
75  ['integer'],
76  [$this->user->getId()]
77  );
78  $row = $this->db->fetchAssoc($res);
79 
80  return (is_array($row) && (int) $row['cnt'] === 1);
81  }
82 
86  public function getAll(): array
87  {
88  $res = $this->db->queryF(
89  'SELECT * FROM addressbook_mlist WHERE user_id = %s',
90  ['integer'],
91  [$this->user->getId()]
92  );
93 
94  $entries = [];
95 
96  $counter = 0;
97  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
98  $tmpObj = new ilMailingList($this->user, 0);
99  $tmpObj->setId((int) $row->ml_id);
100  $tmpObj->setUserId((int) $row->user_id);
101  $tmpObj->setTitle($row->title);
102  $tmpObj->setDescription($row->description);
103  $tmpObj->setCreatedate($row->createdate);
104  $tmpObj->setChangedate($row->changedate);
105  $tmpObj->setMode((int) $row->lmode);
106 
107  $entries[$counter] = $tmpObj;
108 
109  unset($tmpObj);
110 
111  ++$counter;
112  }
113 
114  return $entries;
115  }
116 
117  public function mailingListExists(string $a_list_name): bool
118  {
119  $ml_id = (int) substr($a_list_name, strrpos($a_list_name, '_') + 1);
120  if (!is_numeric($ml_id) || $ml_id <= 0) {
121  return false;
122  }
123 
124  $this->setCurrentMailingList($ml_id);
125 
126  return $this->getCurrentMailingList()->doesExist();
127  }
128 
129  public function setCurrentMailingList(int $id = 0): void
130  {
131  $this->ml = $this->get($id);
132  }
133 
134 
136  {
137  return $this->ml;
138  }
139 
140  public function deleteTemporaryLists(): void
141  {
142  foreach ($this->getAll() as $mlist) {
143  if ($mlist->getMode() === ilMailingList::MODE_TEMPORARY) {
144  $mlist->delete();
145  }
146  }
147  }
148 
149  public function deleteLists(): void
150  {
151  $this->db->manipulateF(
152  'DELETE ass FROM addressbook_mlist_ass ass INNER JOIN addressbook_mlist list ON ass.ml_id = list.ml_id WHERE list.user_id = %s',
153  ['integer'],
154  [$this->user->getId()]
155  );
156  $this->db->manipulateF(
157  'DELETE FROM addressbook_mlist WHERE user_id = %s',
158  ['integer'],
159  [$this->user->getId()]
160  );
161  }
162 
163  public function deleteAssignments(): void
164  {
165  $this->db->manipulate(
166  'DELETE FROM addressbook_mlist_ass WHERE usr_id = ' . $this->db->quote($this->user->getId(), 'integer')
167  );
168  }
169 }
$res
Definition: ltiservices.php:69
global $DIC
Definition: feed.php:28
setCurrentMailingList(int $id=0)
__construct(ilObjUser $a_user)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
mailingListExists(string $a_list_name)
getSelected(array $a_ids=[])
isOwner(int $a_ml_id, int $a_usr_id)