Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 2350 | Ir a la última revisión | | Comparar con el anterior | 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 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
 
96
    public function fetchCountUnreadMessagesByConversationIdAndReceiverCompanyId($conversation_id, $receiver_company_id)
97
    {
98
        $select = $this->sql->select(self::_TABLE);
99
        $select->columns(['total' => new Expression('COUNT(*)')]);
100
        $select->where->equalTo('conversation_id', $conversation_id);
101
        $select->where->equalTo('receiver_company_id', $receiver_company_id)
102
        ->and->equalTo('receiver_status', Message::STATUS_NORMAL)
103
        ->and->equalTo('read', Message::NO);
104
        $select->limit(1);
105
 
106
        $record = $this->executeFetchOneArray($select);
107
        return $record['total'];
108
    }
1 www 109
 
110
    /**
111
     *
112
     * @param int $receiver_id
113
     * @return int
114
     */
115
    public function fetchCountUnreadMessagesReceiverId($receiver_id)
116
    {
117
        $select = $this->sql->select(self::_TABLE);
118
        $select->columns(['total' => new Expression('COUNT(*)')]);
119
        $select->where->equalTo('receiver_id', $receiver_id)
120
        ->and->equalTo('receiver_status', Message::STATUS_NORMAL)
121
        ->and->equalTo('read', Message::NO);
122
        $select->limit(1);
123
 
124
        $record = $this->executeFetchOneArray($select);
125
        return $record['total'];
126
    }
127
 
128
 
129
    /**
130
     *
131
     * @param int $conversation_id
132
     * @param int $receiver_id
133
     * @return string
134
     */
135
    public function fetchLastUnreadMessagesByConversationIdAndReceiverId($conversation_id, $receiver_id)
136
    {
137
        $select = $this->sql->select(self::_TABLE);
138
        $select->columns(['added_on' => new Expression('MAX(added_on)')  ]);
139
        $select->where->equalTo('conversation_id', $conversation_id);
140
        $select->where->equalTo('receiver_id', $receiver_id)
141
        ->and->equalTo('receiver_status', Message::STATUS_NORMAL)
142
        ->and->equalTo('read', Message::NO);
143
        $select->order('id desc');
144
        $select->limit(1);
145
 
146
        //error_log($select->getSqlString($this->adapter->platform));
147
 
148
        $record = $this->executeFetchOneArray($select);
149
        if($record) {
150
            return $record['added_on'];
151
        }
152
 
153
    }
154
 
155
    /**
156
     *
157
     * @param int $conversation_id
158
     * @param int $receiver_id
159
     * @return string
160
     */
161
    public function fetchLastMessagesByConversationIdAndReceiverId($conversation_id, $receiver_id)
162
    {
163
        $select = $this->sql->select(self::_TABLE);
164
        $select->columns(['added_on' => new Expression('MAX(added_on)')  ]);
165
        $select->where->equalTo('conversation_id', $conversation_id);
166
        $select->where->equalTo('receiver_id', $receiver_id)
167
        ->and->equalTo('receiver_status', Message::STATUS_NORMAL);
168
        $select->order('id desc');
169
        $select->limit(1);
170
 
171
        //error_log($select->getSqlString($this->adapter->platform));
172
 
173
        $record = $this->executeFetchOneArray($select);
174
        if($record) {
175
            return $record['added_on'];
176
        }
177
 
178
    }
2338 nelberth 179
 
180
    public function fetchLastMessagesByConversationIdAndReceiverCompanyId($conversation_id, $receiver_company_id)
181
    {
182
        $select = $this->sql->select(self::_TABLE);
183
        $select->columns(['added_on' => new Expression('MAX(added_on)')  ]);
184
        $select->where->equalTo('conversation_id', $conversation_id);
185
        $select->where->equalTo('receiver_company_id', $receiver_company_id)
186
        ->and->equalTo('receiver_status', Message::STATUS_NORMAL);
187
        $select->order('id desc');
188
        $select->limit(1);
189
 
190
        //error_log($select->getSqlString($this->adapter->platform));
191
 
192
        $record = $this->executeFetchOneArray($select);
193
        if($record) {
194
            return $record['added_on'];
195
        }
196
 
197
    }
1 www 198
 
199
    /**
200
     *
201
     * @param int $chat_id
202
     * @param int $page
203
     * @param int $records_per_page
204
     * @return Paginator
205
     */
206
    public function getAllMessagesPaginatorByConversationId($conversation_id, $page = 1, $records_per_page = 10)
207
    {
208
        $select = $this->sql->select(self::_TABLE);
209
        $select->where->equalTo('conversation_id', $conversation_id);
210
        $select->order('id DESC');
211
 
212
        //echo $select->getSqlString($this->adapter->getPlatform()); exit;
213
 
214
        $prototype  = new Message();
215
        $hydrator   = new ObjectPropertyHydrator();
216
        $resultset  = new HydratingResultSet($hydrator, $prototype);
217
 
218
        $adapter    = new DbSelect($select, $this->sql, $resultset);
219
        $paginator  = new Paginator($adapter);
220
        $paginator->setCurrentPageNumber($page);
221
        $paginator->setItemCountPerPage($records_per_page);
222
 
223
        return $paginator;
224
    }
225
 
226
    /**
227
     *
228
     * @param Message $message
229
     * @return boolean
230
     */
231
    public function insert($message)
232
    {
233
        $hydrator = new ObjectPropertyHydrator();
234
        $values = $hydrator->extract($message);
235
        $values = $this->removeEmpty($values);
236
 
237
        $insert = $this->sql->insert(self::_TABLE);
238
        $insert->values($values);
239
 
240
       // echo $insert->getSqlString($this->adapter->platform); exit;
241
 
242
        $response = $this->executeInsert($insert);
243
        if($response) {
244
            $message->id = $this->lastInsertId;
245
        }
246
 
247
        return $response;
248
    }
249
 
250
 
251
    /**
252
     *
253
     * @param Message $message
254
     * @return boolean
255
     */
256
    public function update($message)
257
    {
258
        $hydrator = new ObjectPropertyHydrator();
259
        $values = $hydrator->extract($message);
260
        $values = $this->removeEmpty($values);
261
 
262
        $update = $this->sql->update(self::_TABLE);
263
        $update->set($values);
264
        $update->where->equalTo('id', $message->id);
265
 
266
        return $this->executeUpdate($update);
267
    }
268
 
269
 
270
    /**
271
     *
272
     * @param int $message_id
273
     * @param int $receiver_id
274
     * @return boolean
275
     */
276
    public function markAsRead($id)
277
    {
278
        $update = $this->sql->update(self::_TABLE);
279
        $update->set([
280
           'read' => Message::YES
281
        ]);
282
        $update->where->equalTo('id', $id);
283
 
284
 
285
        return $this->executeUpdate($update);
286
    }
287
 
288
 
289
}