Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| 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\ChatGroupMessage;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Db\ResultSet\HydratingResultSet;
11
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
12
use Laminas\Paginator\Adapter\DbSelect;
13
use Laminas\Paginator\Paginator;
14
use Laminas\Log\LoggerInterface;
15
use LeadersLinked\Model\ChatGroup;
16
 
17
class ChatGroupMessageMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_chat_group_messages';
20
 
21
    /**
22
     *
23
     * @var ChatGroupMessageMapper
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 ChatGroupMessageMapper
41
     */
42
    public static function getInstance($adapter)
43
    {
44
        if(self::$_instance == null) {
45
            self::$_instance = new ChatGroupMessageMapper($adapter);
46
        }
47
        return self::$_instance;
48
    }
49
 
50
    /**
51
     *
52
     * @param int[] $message_ids
53
     * @return ChatGroupMessage[]
54
     */
55
    public function fetchAllByMessagesIds($message_ids)
56
    {
57
        $prototype = new ChatGroupMessage();
58
 
59
        $select = $this->sql->select(self::_TABLE);
60
        $select->where->in('id', $message_ids);
61
        $select->order('id ASC');
62
 
63
        return $this->executeFetchAllObject($select, $prototype);
64
    }
65
 
66
    /**
67
     *
68
     * @param array $message_ids
69
     * @param string $group_id
70
     * @return ChatGroupMessage[]
71
     */
72
    public function fetchAllByMessagesIdsAndGroupId($message_ids, $group_id)
73
    {
74
        $prototype = new ChatGroupMessage();
75
 
76
        $select = $this->sql->select(self::_TABLE);
77
        $select->where->in('id', $message_ids)->and->equalTo('group_id', $group_id);
78
        $select->order('id ASC');
79
 
80
        return $this->executeFetchAllObject($select, $prototype);
81
    }
82
 
83
    /**
84
     *
85
     * @param int $id
86
     * @return ChatGroupMessage
87
     */
88
    public function fetchOne($id)
89
    {
90
        $prototype = new ChatGroupMessage();
91
 
92
        $select = $this->sql->select(self::_TABLE);
93
        $select->where->equalTo('id', $id);
94
 
95
        return $this->executeFetchOneObject($select, $prototype);
96
    }
97
 
98
 
99
    /**
100
     *
101
     * @param int $id
102
     * @return ChatGroupMessage[]
103
     */
104
    public function fetchAllByGroupId($group_id)
105
    {
106
        $prototype = new ChatGroupMessage();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('group_id', $group_id);
110
        $select->order('ASC');
111
 
112
        return $this->executeFetchAllObject($select, $prototype);
113
    }
114
 
115
    /**
116
     *
117
     * @param array $message_ids
118
     * @param string $group_id
119
     * @param int $page
120
     * @param int $records_per_page
121
     * @return Paginator
122
     */
123
    public function getAllMessagesPaginatorByMessagesIdsAndGroupId($message_ids, $group_id, $page = 1, $records_per_page = 10)
124
    {
125
        $prototype = new ChatGroupMessage();
126
 
127
        $select = $this->sql->select(self::_TABLE);
128
        $select->where->in('id', $message_ids)->and->equalTo('group_id', $group_id);
129
        $select->order('id DESC');
130
 
131
 
132
 
133
        $hydrator   = new ObjectPropertyHydrator();
134
        $resultset  = new HydratingResultSet($hydrator, $prototype);
135
 
136
        $adapter    = new DbSelect($select, $this->sql, $resultset);
137
        $paginator  = new Paginator($adapter);
138
        $paginator->setCurrentPageNumber($page);
139
        $paginator->setItemCountPerPage($records_per_page);
140
 
141
        return $paginator;
142
    }
143
 
144
    /**
145
     *
146
     * @param array $message_ids
147
     * @param string $group_id
148
     * @param int $page
149
     * @param int $records_per_page
150
     * @return Paginator
151
     */
152
    public function getPaginatorByGroupId($group_id, $page = 1, $records_per_page = 10)
153
    {
154
        $prototype = new ChatGroupMessage();
155
 
156
        $select = $this->sql->select(self::_TABLE);
157
        $select->where->equalTo('group_id', $group_id);
158
        $select->order('id DESC');
159
 
160
 
161
 
162
        $hydrator   = new ObjectPropertyHydrator();
163
        $resultset  = new HydratingResultSet($hydrator, $prototype);
164
 
165
        $adapter    = new DbSelect($select, $this->sql, $resultset);
166
        $paginator  = new Paginator($adapter);
167
        $paginator->setCurrentPageNumber($page);
168
        $paginator->setItemCountPerPage($records_per_page);
169
 
170
        return $paginator;
171
    }
172
 
173
    /**
174
     *
175
     * @param ChatGroupMessage $chatGroupMessage
176
     * @return boolean
177
     */
178
    public function insert($chatGroupMessage)
179
    {
180
 
181
        $hydrator = new ObjectPropertyHydrator();
182
        $values = $hydrator->extract($chatGroupMessage);
183
        $values = $this->removeEmpty($values);
184
 
185
        $insert = $this->sql->insert(self::_TABLE);
186
        $insert->values($values);
187
 
188
        $result =  $this->executeInsert($insert);
189
        if($result) {
190
            $chatGroupMessage->id = $this->lastInsertId;
191
        }
192
 
193
 
194
        //echo $insert->getSqlString($this->adapter->getPlatform()); exit;
195
 
196
        return $result;
197
    }
198
 
199
    /**
200
     *
201
     * @param int[] $message_ids
202
     * @return boolean
203
     */
204
    public function deleteAllByMessageIds($message_ids)
205
    {
206
        $delete = $this->sql->delete(self::_TABLE);
207
        $delete->where->in('id', $message_ids);
208
 
209
        return $this->executeDelete($delete);
210
    }
211
 
212
    /**
213
     *
214
     * @param string $group_id
215
     * @return boolean
216
     */
217
    public function deleteAllByGroupId($group_id)
218
    {
219
        $delete = $this->sql->delete(self::_TABLE);
220
        $delete->where->equalTo('group_id', $group_id);
221
 
222
        //echo $delete->getSqlString($this->adapter->platform); exit;
223
 
224
        return $this->executeDelete($delete);
225
    }
226
 
227
}