1 |
www |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Mapper;
|
|
|
6 |
|
|
|
7 |
use LeadersLinked\Model\Message;
|
|
|
8 |
use LeadersLinked\Mapper\Common\MapperCommon;
|
|
|
9 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
10 |
use Laminas\Db\Sql\Expression;
|
|
|
11 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
12 |
use Laminas\Db\ResultSet\HydratingResultSet;
|
|
|
13 |
use Laminas\Paginator\Adapter\DbSelect;
|
|
|
14 |
use Laminas\Paginator\Paginator;
|
|
|
15 |
|
|
|
16 |
class MessageMapper extends MapperCommon
|
|
|
17 |
{
|
|
|
18 |
const _TABLE = 'tbl_messages';
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
*
|
|
|
22 |
* @var MessageMapper
|
|
|
23 |
*/
|
|
|
24 |
private static $_instance;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @param AdapterInterface $adapter
|
|
|
29 |
*/
|
|
|
30 |
private function __construct($adapter)
|
|
|
31 |
{
|
|
|
32 |
parent::__construct($adapter);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @param AdapterInterface $adapter
|
|
|
38 |
* @return MessageMapper
|
|
|
39 |
*/
|
|
|
40 |
public static function getInstance($adapter)
|
|
|
41 |
{
|
|
|
42 |
if(self::$_instance == null) {
|
|
|
43 |
self::$_instance = new MessageMapper($adapter);
|
|
|
44 |
}
|
|
|
45 |
return self::$_instance;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
*
|
|
|
50 |
* @param int $id
|
|
|
51 |
* @return Message
|
|
|
52 |
*/
|
|
|
53 |
public function fetchOne($id)
|
|
|
54 |
{
|
|
|
55 |
$select = $this->sql->select(self::_TABLE);
|
|
|
56 |
$select->where->equalTo('id', $id);
|
|
|
57 |
|
|
|
58 |
$prototype = new Message();
|
|
|
59 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
*
|
|
|
64 |
* @param string $uuid
|
|
|
65 |
* @return Message
|
|
|
66 |
*/
|
|
|
67 |
public function fetchOneByUuid($uuid)
|
|
|
68 |
{
|
|
|
69 |
$select = $this->sql->select(self::_TABLE);
|
|
|
70 |
$select->where->equalTo('uuid', $uuid);
|
|
|
71 |
|
|
|
72 |
$prototype = new Message();
|
|
|
73 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
*
|
|
|
78 |
* @param int $message_id
|
|
|
79 |
* @param int $receiver_id
|
|
|
80 |
* @return int
|
|
|
81 |
*/
|
|
|
82 |
public function fetchCountUnreadMessagesByConversationIdAndReceiverId($conversation_id, $receiver_id)
|
|
|
83 |
{
|
|
|
84 |
$select = $this->sql->select(self::_TABLE);
|
|
|
85 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
86 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
87 |
$select->where->equalTo('receiver_id', $receiver_id)
|
|
|
88 |
->and->equalTo('receiver_status', Message::STATUS_NORMAL)
|
|
|
89 |
->and->equalTo('read', Message::NO);
|
|
|
90 |
$select->limit(1);
|
|
|
91 |
|
|
|
92 |
$record = $this->executeFetchOneArray($select);
|
|
|
93 |
return $record['total'];
|
|
|
94 |
}
|
2338 |
nelberth |
95 |
|
2350 |
nelberth |
96 |
|
1 |
www |
97 |
|
|
|
98 |
/**
|
|
|
99 |
*
|
|
|
100 |
* @param int $receiver_id
|
|
|
101 |
* @return int
|
|
|
102 |
*/
|
|
|
103 |
public function fetchCountUnreadMessagesReceiverId($receiver_id)
|
|
|
104 |
{
|
|
|
105 |
$select = $this->sql->select(self::_TABLE);
|
|
|
106 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
107 |
$select->where->equalTo('receiver_id', $receiver_id)
|
|
|
108 |
->and->equalTo('receiver_status', Message::STATUS_NORMAL)
|
|
|
109 |
->and->equalTo('read', Message::NO);
|
|
|
110 |
$select->limit(1);
|
|
|
111 |
|
|
|
112 |
$record = $this->executeFetchOneArray($select);
|
|
|
113 |
return $record['total'];
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
*
|
|
|
119 |
* @param int $conversation_id
|
|
|
120 |
* @param int $receiver_id
|
|
|
121 |
* @return string
|
|
|
122 |
*/
|
|
|
123 |
public function fetchLastUnreadMessagesByConversationIdAndReceiverId($conversation_id, $receiver_id)
|
|
|
124 |
{
|
|
|
125 |
$select = $this->sql->select(self::_TABLE);
|
|
|
126 |
$select->columns(['added_on' => new Expression('MAX(added_on)') ]);
|
|
|
127 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
128 |
$select->where->equalTo('receiver_id', $receiver_id)
|
|
|
129 |
->and->equalTo('receiver_status', Message::STATUS_NORMAL)
|
|
|
130 |
->and->equalTo('read', Message::NO);
|
|
|
131 |
$select->order('id desc');
|
|
|
132 |
$select->limit(1);
|
|
|
133 |
|
|
|
134 |
//error_log($select->getSqlString($this->adapter->platform));
|
|
|
135 |
|
|
|
136 |
$record = $this->executeFetchOneArray($select);
|
|
|
137 |
if($record) {
|
|
|
138 |
return $record['added_on'];
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
*
|
|
|
145 |
* @param int $conversation_id
|
|
|
146 |
* @param int $receiver_id
|
|
|
147 |
* @return string
|
|
|
148 |
*/
|
|
|
149 |
public function fetchLastMessagesByConversationIdAndReceiverId($conversation_id, $receiver_id)
|
|
|
150 |
{
|
|
|
151 |
$select = $this->sql->select(self::_TABLE);
|
|
|
152 |
$select->columns(['added_on' => new Expression('MAX(added_on)') ]);
|
|
|
153 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
154 |
$select->where->equalTo('receiver_id', $receiver_id)
|
|
|
155 |
->and->equalTo('receiver_status', Message::STATUS_NORMAL);
|
|
|
156 |
$select->order('id desc');
|
|
|
157 |
$select->limit(1);
|
|
|
158 |
|
6749 |
efrain |
159 |
// echo $select->getSqlString($this->adapter->platform) . "\r\n";
|
1 |
www |
160 |
|
|
|
161 |
$record = $this->executeFetchOneArray($select);
|
6749 |
efrain |
162 |
|
|
|
163 |
|
|
|
164 |
|
1 |
www |
165 |
if($record) {
|
|
|
166 |
return $record['added_on'];
|
6749 |
efrain |
167 |
} else {
|
|
|
168 |
return '';
|
|
|
169 |
}
|
1 |
www |
170 |
|
|
|
171 |
}
|
6749 |
efrain |
172 |
|
|
|
173 |
/**
|
|
|
174 |
*
|
|
|
175 |
* @param int $conversation_id
|
|
|
176 |
* @return string
|
|
|
177 |
*/
|
|
|
178 |
public function fetchLastMessageByConversation($conversation_id)
|
|
|
179 |
{
|
|
|
180 |
$select = $this->sql->select(self::_TABLE);
|
|
|
181 |
$select->columns(['added_on' => new Expression('MAX(added_on)') ]);
|
|
|
182 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
183 |
$select->where->equalTo('receiver_status', Message::STATUS_NORMAL);
|
|
|
184 |
$select->order('id desc');
|
|
|
185 |
$select->limit(1);
|
|
|
186 |
|
|
|
187 |
// echo $select->getSqlString($this->adapter->platform) . "\r\n";
|
|
|
188 |
|
|
|
189 |
$record = $this->executeFetchOneArray($select);
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
if($record) {
|
|
|
194 |
return $record['added_on'];
|
|
|
195 |
} else {
|
|
|
196 |
return '';
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
*
|
|
|
203 |
* @param int $conversation_id
|
|
|
204 |
* return int
|
|
|
205 |
*/
|
|
|
206 |
public function fetchCountMessagesByConversationId($conversation_id)
|
|
|
207 |
{
|
|
|
208 |
$select = $this->sql->select(self::_TABLE);
|
|
|
209 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
210 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
211 |
|
|
|
212 |
$record = $this->executeFetchOneArray($select);
|
|
|
213 |
return intval($record['total'], 10);
|
|
|
214 |
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
*
|
|
|
219 |
* @param int $conversation_id
|
|
|
220 |
* @param int $message_id
|
|
|
221 |
* return int
|
|
|
222 |
*/
|
|
|
223 |
public function fetchCountMessagesByConversationIdAndGreaterThanMessageId($conversation_id, $message_id)
|
|
|
224 |
{
|
|
|
225 |
$select = $this->sql->select(self::_TABLE);
|
|
|
226 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
227 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
228 |
$select->where->greaterThan('id', $message_id);
|
|
|
229 |
|
|
|
230 |
$record = $this->executeFetchOneArray($select);
|
|
|
231 |
return intval($record['total'], 10);
|
|
|
232 |
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
*
|
|
|
237 |
* @param int $conversation_id
|
|
|
238 |
* @param int $message_id
|
|
|
239 |
* return int
|
|
|
240 |
*/
|
|
|
241 |
public function fetchCountMessagesByConversationIdAndLessThanMessageId($conversation_id, $message_id)
|
|
|
242 |
{
|
|
|
243 |
$select = $this->sql->select(self::_TABLE);
|
|
|
244 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
245 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
246 |
$select->where->lessThan('id', $message_id);
|
|
|
247 |
|
|
|
248 |
$record = $this->executeFetchOneArray($select);
|
|
|
249 |
return intval($record['total'], 10);
|
|
|
250 |
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
*
|
|
|
255 |
* @param int $conversation_id
|
|
|
256 |
* @param int $message_id
|
|
|
257 |
* @return Message[]
|
|
|
258 |
*/
|
|
|
259 |
public function fetchBatchMessagesByConversationIdAndGreaterThanMessageId($conversation_id, $message_id)
|
|
|
260 |
{
|
|
|
261 |
$select = $this->sql->select(self::_TABLE);
|
|
|
262 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
263 |
$select->where->greaterThan('id', $message_id);
|
|
|
264 |
$select->order('id DESC');
|
|
|
265 |
$select->limit(10);
|
|
|
266 |
|
|
|
267 |
$prototype = new Message();
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
*
|
|
|
275 |
* @param int $conversation_id
|
|
|
276 |
* @param int $message_id
|
|
|
277 |
* @return Message[]
|
|
|
278 |
*/
|
|
|
279 |
public function fetchBatchMessagesByConversationIdAndLessThanMessageId($conversation_id, $message_id)
|
|
|
280 |
{
|
|
|
281 |
$select = $this->sql->select(self::_TABLE);
|
|
|
282 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
283 |
$select->where->lessThan('id', $message_id);
|
|
|
284 |
$select->order('id DESC');
|
|
|
285 |
$select->limit(10);
|
|
|
286 |
|
|
|
287 |
$prototype = new Message();
|
|
|
288 |
|
|
|
289 |
|
|
|
290 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
*
|
|
|
296 |
* @param int $conversation_id
|
|
|
297 |
* @param int $message_id
|
|
|
298 |
* @return Message[]
|
|
|
299 |
*/
|
|
|
300 |
public function fetchBatchMessagesByConversationId($conversation_id)
|
|
|
301 |
{
|
|
|
302 |
$select = $this->sql->select(self::_TABLE);
|
|
|
303 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
304 |
$select->order('id DESC');
|
|
|
305 |
$select->limit(10);
|
|
|
306 |
|
|
|
307 |
$prototype = new Message();
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
311 |
}
|
|
|
312 |
|
2338 |
nelberth |
313 |
|
6749 |
efrain |
314 |
|
|
|
315 |
/**
|
|
|
316 |
*
|
|
|
317 |
* @param int $conversation_id
|
|
|
318 |
* @param int $message_id
|
|
|
319 |
* @param int $page
|
|
|
320 |
* @param int $records_per_page
|
|
|
321 |
* @return Paginator
|
|
|
322 |
*/
|
|
|
323 |
public function getAllMessagesPaginatorByConversationIdAndGreaterThanMessageId($conversation_id, $message_id, $page = 1, $records_per_page = 10)
|
|
|
324 |
{
|
|
|
325 |
$select = $this->sql->select(self::_TABLE);
|
|
|
326 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
327 |
$select->where->greaterThan('id', $message_id);
|
|
|
328 |
$select->order('id DESC');
|
|
|
329 |
|
|
|
330 |
//echo $select->getSqlString($this->adapter->getPlatform()); exit;
|
|
|
331 |
|
|
|
332 |
$prototype = new Message();
|
|
|
333 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
334 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
335 |
|
|
|
336 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
337 |
$paginator = new Paginator($adapter);
|
|
|
338 |
$paginator->setCurrentPageNumber($page);
|
|
|
339 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
340 |
|
|
|
341 |
return $paginator;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
*
|
|
|
348 |
* @param int $conversation_id
|
|
|
349 |
* @param int $message_id
|
|
|
350 |
* @param int $page
|
|
|
351 |
* @param int $records_per_page
|
|
|
352 |
* @return Paginator
|
|
|
353 |
*/
|
|
|
354 |
public function getAllMessagesPaginatorByConversationIdAndLessThanMessageId($conversation_id, $message_id, $page = 1, $records_per_page = 10)
|
|
|
355 |
{
|
|
|
356 |
$select = $this->sql->select(self::_TABLE);
|
|
|
357 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
358 |
$select->where->lessThan('id', $message_id);
|
|
|
359 |
$select->order('id DESC');
|
|
|
360 |
|
|
|
361 |
//echo $select->getSqlString($this->adapter->getPlatform()); exit;
|
|
|
362 |
|
|
|
363 |
$prototype = new Message();
|
|
|
364 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
365 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
366 |
|
|
|
367 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
368 |
$paginator = new Paginator($adapter);
|
|
|
369 |
$paginator->setCurrentPageNumber($page);
|
|
|
370 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
371 |
|
|
|
372 |
return $paginator;
|
|
|
373 |
}
|
2350 |
nelberth |
374 |
|
1 |
www |
375 |
|
|
|
376 |
/**
|
|
|
377 |
*
|
6749 |
efrain |
378 |
* @param int $conversation_id
|
1 |
www |
379 |
* @param int $page
|
|
|
380 |
* @param int $records_per_page
|
|
|
381 |
* @return Paginator
|
|
|
382 |
*/
|
|
|
383 |
public function getAllMessagesPaginatorByConversationId($conversation_id, $page = 1, $records_per_page = 10)
|
|
|
384 |
{
|
|
|
385 |
$select = $this->sql->select(self::_TABLE);
|
|
|
386 |
$select->where->equalTo('conversation_id', $conversation_id);
|
|
|
387 |
$select->order('id DESC');
|
|
|
388 |
|
|
|
389 |
$prototype = new Message();
|
|
|
390 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
391 |
$resultset = new HydratingResultSet($hydrator, $prototype);
|
|
|
392 |
|
|
|
393 |
$adapter = new DbSelect($select, $this->sql, $resultset);
|
|
|
394 |
$paginator = new Paginator($adapter);
|
|
|
395 |
$paginator->setCurrentPageNumber($page);
|
|
|
396 |
$paginator->setItemCountPerPage($records_per_page);
|
|
|
397 |
|
|
|
398 |
return $paginator;
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
*
|
|
|
403 |
* @param Message $message
|
|
|
404 |
* @return boolean
|
|
|
405 |
*/
|
|
|
406 |
public function insert($message)
|
|
|
407 |
{
|
|
|
408 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
409 |
$values = $hydrator->extract($message);
|
|
|
410 |
$values = $this->removeEmpty($values);
|
|
|
411 |
|
3018 |
efrain |
412 |
if(!isset($values['message'])) {
|
|
|
413 |
$values['message'] = '';
|
|
|
414 |
}
|
|
|
415 |
|
1 |
www |
416 |
$insert = $this->sql->insert(self::_TABLE);
|
|
|
417 |
$insert->values($values);
|
|
|
418 |
|
|
|
419 |
// echo $insert->getSqlString($this->adapter->platform); exit;
|
|
|
420 |
|
|
|
421 |
$response = $this->executeInsert($insert);
|
|
|
422 |
if($response) {
|
|
|
423 |
$message->id = $this->lastInsertId;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
return $response;
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
|
|
|
430 |
/**
|
|
|
431 |
*
|
|
|
432 |
* @param Message $message
|
|
|
433 |
* @return boolean
|
|
|
434 |
*/
|
|
|
435 |
public function update($message)
|
|
|
436 |
{
|
|
|
437 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
438 |
$values = $hydrator->extract($message);
|
|
|
439 |
$values = $this->removeEmpty($values);
|
|
|
440 |
|
|
|
441 |
$update = $this->sql->update(self::_TABLE);
|
|
|
442 |
$update->set($values);
|
|
|
443 |
$update->where->equalTo('id', $message->id);
|
|
|
444 |
|
|
|
445 |
return $this->executeUpdate($update);
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
|
|
|
449 |
/**
|
|
|
450 |
*
|
|
|
451 |
* @param int $message_id
|
|
|
452 |
* @param int $receiver_id
|
|
|
453 |
* @return boolean
|
|
|
454 |
*/
|
|
|
455 |
public function markAsRead($id)
|
|
|
456 |
{
|
|
|
457 |
$update = $this->sql->update(self::_TABLE);
|
|
|
458 |
$update->set([
|
|
|
459 |
'read' => Message::YES
|
|
|
460 |
]);
|
|
|
461 |
$update->where->equalTo('id', $id);
|
|
|
462 |
|
|
|
463 |
|
|
|
464 |
return $this->executeUpdate($update);
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
|
|
|
468 |
}
|