ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarUserNotification.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
24{
25 public const TYPE_USER = 1;
26 public const TYPE_EMAIL = 2;
27
28 private int $cal_id = 0;
29 private array $rcps = array();
30
31 protected ilLanguage $lng;
32 protected ilDBInterface $db;
34
35 public function __construct(int $a_cal_id = 0)
36 {
37 global $DIC;
38
39 $this->lng = $DIC->language();
40 $this->db = $DIC->database();
41 $this->error = $DIC['ilErr'];
42
43 $this->cal_id = $a_cal_id;
44 $this->read();
45 }
46
47 public static function deleteUser(int $a_usr_id): void
48 {
49 global $DIC;
50
51 $ilDB = $DIC['ilDB'];
52 $query = 'DELETE FROM cal_notification ' .
53 'WHERE user_id = ' . $ilDB->quote($a_usr_id, 'integer');
54 $res = $ilDB->manipulate($query);
55 }
56
57 public static function deleteCalendarEntry(int $a_cal_id): void
58 {
59 global $DIC;
60
61 $ilDB = $DIC['ilDB'];
62 $query = 'DELETE FROM cal_notification ' .
63 'WHERE cal_id = ' . $ilDB->quote($a_cal_id, 'integer');
64 $res = $ilDB->manipulate($query);
65 }
66
67 public function setEntryId(int $a_id): void
68 {
69 $this->cal_id = $a_id;
70 }
71
72 public function getEntryId(): int
73 {
74 return $this->cal_id;
75 }
76
77 public function getRecipients(): array
78 {
79 return $this->rcps;
80 }
81
82 public function validate(): bool
83 {
84 if (!count($this->getRecipients())) {
85 return true;
86 }
87 foreach ($this->getRecipients() as $rcp_data) {
88 if ($rcp_data['type'] == self::TYPE_USER) {
89 continue;
90 } elseif (!ilUtil::is_email($rcp_data['email'])) {
91 $this->error->appendMessage($this->lng->txt('cal_err_invalid_notification_rcps'));
92 return false;
93 }
94 }
95 return true;
96 }
97
98 public function save(): bool
99 {
100 $this->deleteRecipients();
101 foreach ($this->getRecipients() as $rcp) {
102 $query = 'INSERT INTO cal_notification ' .
103 '(notification_id,cal_id, user_type, user_id, email) ' .
104 'VALUES ( ' .
105 $this->db->quote($this->db->nextId('cal_notification'), 'integer') . ', ' .
106 $this->db->quote($this->getEntryId(), 'integer') . ', ' .
107 $this->db->quote((int) $rcp['type'], 'integer') . ', ' .
108 $this->db->quote((int) $rcp['usr_id'], 'integer') . ', ' .
109 $this->db->quote($rcp['email'], 'text') .
110 ')';
111 $this->db->manipulate($query);
112 }
113 return true;
114 }
115
116 public function addRecipient(int $a_type, int $a_usr_id = 0, string $a_email = ''): void
117 {
118 $this->rcps[] = array(
119 'type' => $a_type,
120 'usr_id' => $a_usr_id,
121 'email' => $a_email
122 );
123 }
124
125 public function setRecipients(array $a_rcps): void
126 {
127 $this->rcps = array();
128 }
129
130 public function deleteRecipients(): void
131 {
132 $query = 'DELETE FROM cal_notification ' .
133 'WHERE cal_id = ' . $this->db->quote($this->getEntryId(), 'integer');
134 $res = $this->db->manipulate($query);
135 }
136
137 protected function read(): void
138 {
139 if (!$this->getEntryId()) {
140 return;
141 }
142
143 $query = 'SELECT * FROM cal_notification ' .
144 'WHERE cal_id = ' . $this->db->quote($this->getEntryId(), 'integer');
145 $res = $this->db->query($query);
146
147 $this->rcps = array();
148 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
149 $this->addRecipient(
150 (int) $row->user_type,
151 (int) $row->user_id,
152 $row->email
153 );
154 }
155 }
156
157 public static function createTable(): void
158 {
159 global $DIC;
160
161 $ilDB = $DIC['ilDB'];
162 if ($ilDB->tableExists('cal_notification')) {
163 return;
164 }
165
166 // Create notification table
167 $ilDB->createTable(
168 'cal_notification',
169 array(
170 'notification_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true),
171 'cal_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0),
172 'user_type' => array('type' => 'integer', 'length' => 1, 'notnull' => true, 'default' => 0),
173 'user_id' => array('type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0),
174 'email' => array('type' => 'text', 'length' => 64, 'notnull' => false)
175 )
176 );
177 $ilDB->addPrimaryKey(
178 'cal_notification',
179 array(
180 'notification_id'
181 )
182 );
183 $ilDB->createSequence('cal_notification');
184 $ilDB->addIndex('cal_notification', array('cal_id'), 'i1');
185 }
186}
error(string $a_errmsg)
addRecipient(int $a_type, int $a_usr_id=0, string $a_email='')
Error Handling & global info handling.
language handling
static is_email(string $a_email, ?ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26