Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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