Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2219 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\ResultSet\HydratingResultSet;
9
use Laminas\Paginator\Adapter\DbSelect;
10
use Laminas\Paginator\Paginator;
11
use Laminas\Log\LoggerInterface;
12
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
13
use LeadersLinked\Model\ChatMessage;
14
use LeadersLinked\Mapper\Common\MapperCommon;
15
 
16
 
17
class ChatMessageMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_chat_messages';
20
 
21
    /**
22
     *
23
     * @var ChatMessageMapper
24
     */
25
    private static $_instance;
26
 
27
    /**
28
     *
29
     * @param AdapterInterface $adapter
30
     * @param int $user_id
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
36
 
37
    /**
38
     *
39
     * @param AdapterInterface $adapter
40
     * @return ChatMessageMapper
41
     */
42
    public static function getInstance($adapter)
43
    {
44
        if(self::$_instance == null) {
45
            self::$_instance = new ChatMessageMapper($adapter);
46
        }
47
        return self::$_instance;
48
    }
49
 
50
    /**
51
     *
52
     * @param int $from_id
53
     * @param int $to_id
54
     * @return boolean
55
     */
56
    public function existNotSeenMessagesByChatIdAndToId($chat_id, $to_id)
57
    {
58
        $prototype = new ChatMessage();
59
 
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('chat_id', $chat_id);
62
        $select->where->equalTo('to_id', $to_id);
63
        $select->where->equalTo('seen', ChatMessage::SEEN_NO);
64
        $select->limit(1);
65
 
66
        $record = $this->executeFetchOneObject($select, $prototype);
67
        return $record ? true : false;
68
 
69
    }
70
 
71
    /**
72
     *
73
     * @param int $from_id
74
     * @param int $to_id
75
     * @return boolean
76
     */
77
    public function existNotReceivedMessagesByChatIdAndToId($chat_id, $to_id)
78
    {
79
        $prototype = new ChatMessage();
80
 
81
        $select = $this->sql->select(self::_TABLE);
82
        $select->where->equalTo('chat_id', $chat_id);
83
        $select->where->equalTo('to_id', $to_id);
84
        $select->where->equalTo('recd', ChatMessage::RECD_NO);
85
        $select->limit(1);
86
 
87
        $record = $this->executeFetchOneObject($select, $prototype);
88
        return $record ? true : false;
89
 
90
    }
91
 
92
 
93
    /**
94
     *
95
     * @param int $from_id
96
     * @param int $to_id
97
     * @return boolean
98
     */
99
    public function markAsReceivedByChatIdAndToId($chat_id, $to_id)
100
    {
101
        $update = $this->sql->update(self::_TABLE);
102
        $update->set(['recd' => ChatMessage::RECD_YES]);
103
        $update->where->equalTo('chat_id', $chat_id);
104
        $update->where->equalTo('to_id', $to_id);
105
        $update->where->equalTo('recd', ChatMessage::RECD_NO);
106
 
107
 
108
        return $this->executeUpdate($update);
109
    }
110
 
111
    /**
112
     *
113
     * @param int $to_id
114
     * @return boolean
115
     */
116
    public function markAsSeenByChatIdAndToId($chat_id, $to_id)
117
    {
118
        $update = $this->sql->update(self::_TABLE);
119
        $update->set(['recd' => ChatMessage::RECD_YES, 'seen' => ChatMessage::SEEN_YES]);
120
        $update->where->equalTo('chat_id', $chat_id);
121
        $update->where->equalTo('to_id', $to_id);
122
        $update->where->equalTo('seen', ChatMessage::SEEN_NO);
123
 
124
        return $this->executeUpdate($update);
125
    }
126
 
127
 
128
    /**
129
     *
130
     * @param int $chat_id
131
     * @return boolean
132
     */
133
    public function deleteAllByChatId($chat_id)
134
    {
135
        $delete = $this->sql->delete(self::_TABLE);
136
        $delete->where->equalTo('chat_id', $chat_id);
137
 
138
        return $this->executeDelete($delete);
139
    }
140
 
141
    /**
142
     *
143
     * @param int $chat_id
144
     * @param int $page
145
     * @param int $records_per_page
146
     * @return Paginator
147
     */
148
    public function getAllMessagesPaginatorByChatId($chat_id, $page = 1, $records_per_page = 10)
149
    {
150
        $select = $this->sql->select(self::_TABLE);
151
        $select->where->equalTo('chat_id', $chat_id);
152
        $select->order('id DESC');
153
 
154
 
155
        //echo $select->getSqlString($this->adapter->getPlatform()); exit;
156
 
157
        $prototype  = new ChatMessage();
158
        $hydrator   = new ObjectPropertyHydrator();
159
        $resultset  = new HydratingResultSet($hydrator, $prototype);
160
 
161
        $adapter    = new DbSelect($select, $this->sql, $resultset);
162
        $paginator  = new Paginator($adapter);
163
        $paginator->setCurrentPageNumber($page);
164
        $paginator->setItemCountPerPage($records_per_page);
165
 
166
        return $paginator;
167
    }
168
 
169
    /**
170
     *
171
     * @param ChatMessage $chatMessage
172
     * @return boolean
173
     */
174
    public function insert($chatMessage)
175
    {
176
        $hydrator = new ObjectPropertyHydrator();
177
        $values = $hydrator->extract($chatMessage);
178
        $values = $this->removeEmpty($values);
179
 
180
        $insert = $this->sql->insert(self::_TABLE);
181
        $insert->values($values);
182
 
183
        $result = $this->executeInsert($insert);
184
        if($result) {
185
            $chatMessage->id = $this->lastInsertId;
186
        }
187
        return $result;
188
    }
189
 
190
 
191
    /**
192
     *
193
     * @param int $id
194
     * @return ChatMessage
195
     */
196
    public function fetchOne($id)
197
    {
198
        $prototype = new ChatMessage();
199
 
200
        $select = $this->sql->select(self::_TABLE);
201
        $select->where->equalTo('id', $id);
202
 
203
        return $this->executeFetchOneObject($select, $prototype);
204
    }
205
}