ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AutoresponderDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use DateTimeImmutable;
25use DateTimeZone;
28
30{
31 private const string TABLE_NAME = 'mail_auto_responder';
32
33 public function __construct(protected ilDBInterface $db)
34 {
35 }
36
37 public function findBySenderId(int $sender_id): AutoresponderArrayCollection
38 {
39 $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE sender_id = ' . $this->db->quote(
40 $sender_id,
42 );
43
44 $result = $this->db->query($query);
45
46 $auto_responder_results = new AutoresponderArrayCollection();
47 while ($row = $this->db->fetchAssoc($result)) {
48 $auto_responder_results->add(
50 (int) $row['sender_id'],
51 (int) $row['receiver_id'],
52 new DateTimeImmutable($row['sent_time'], new DateTimeZone('UTC'))
53 )
54 );
55 }
56
57 return $auto_responder_results;
58 }
59
60 public function findByReceiverId(int $receiver_id): AutoresponderArrayCollection
61 {
62 $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE receiver_id = ' . $this->db->quote(
63 $receiver_id,
65 );
66
67 $result = $this->db->query($query);
68
69 $auto_responder_results = new AutoresponderArrayCollection();
70 while ($row = $this->db->fetchAssoc($result)) {
71 $auto_responder_results->add(
73 (int) $row['sender_id'],
74 (int) $row['receiver_id'],
75 new DateTimeImmutable($row['sent_time'], new DateTimeZone('UTC'))
76 )
77 );
78 }
79
80 return $auto_responder_results;
81 }
82
86 public function findBySenderIdAndReceiverId(int $sender_id, int $receiver_id): AutoresponderDto
87 {
88 $query = 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE sender_id = ' . $this->db->quote(
89 $sender_id,
91 ) . ' AND receiver_id = ' . $this->db->quote($receiver_id, ilDBConstants::T_INTEGER);
92 $result = $this->db->query($query);
93 $row = $this->db->fetchAssoc($result);
94 if (!$row) {
96 'No auto responder found for sender_id: ' . $sender_id . ' and receiver_id: ' . $receiver_id
97 );
98 }
99
100 return new AutoresponderDto(
101 (int) $row['sender_id'],
102 (int) $row['receiver_id'],
103 new DateTimeImmutable($row['sent_time'], new DateTimeZone('UTC'))
104 );
105 }
106
107 public function store(AutoresponderDto $auto_responder): void
108 {
109 $timestamp_sent_time = $auto_responder->getSentTime()->setTimezone(new DateTimeZone('UTC'))->format(
110 'Y-m-d H:i:s'
111 );
112 $this->db->replace(
113 self::TABLE_NAME,
114 [
115 'sender_id' => [ilDBConstants::T_INTEGER, $auto_responder->getSenderId()],
116 'receiver_id' => [ilDBConstants::T_INTEGER, $auto_responder->getReceiverId()]
117 ],
118 [
119 'sent_time' => [ilDBConstants::T_TIMESTAMP, $timestamp_sent_time]
120 ]
121 );
122 }
123
124 public function delete(AutoresponderDto $auto_responder): void
125 {
126 $this->db->manipulate(
127 'DELETE FROM ' . self::TABLE_NAME . ' WHERE sender_id = ' . $this->db->quote(
128 $auto_responder->getSenderId(),
130 ) . ' AND receiver_id = ' . $this->db->quote($auto_responder->getReceiverId(), ilDBConstants::T_INTEGER)
131 );
132 }
133
134 public function deleteBySenderId(int $sender_id): void
135 {
136 $this->db->manipulate(
137 'DELETE FROM ' . self::TABLE_NAME . ' WHERE sender_id = ' . $this->db->quote(
138 $sender_id,
140 )
141 );
142 }
143
144 public function exists(int $sender_id, int $receiver_id): bool
145 {
146 $query = 'SELECT 1 existing_record FROM ' . self::TABLE_NAME . ' WHERE sender_id = ' . $this->db->quote(
147 $sender_id,
149 ) . ' AND receiver_id = ' . $this->db->quote($receiver_id, ilDBConstants::T_INTEGER);
150 $result = $this->db->query($query);
151
152 if ($row = $this->db->fetchAssoc($result)) {
153 return (int) $row['existing_record'] === 1;
154 }
155
156 return false;
157 }
158}
Class ilDBConstants.
Interface ilDBInterface.