Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2962 | | 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\Notification;
8
use LeadersLinked\Mapper\Common\MapperCommon;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\Db\Sql\Expression;
12
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
13
 
14
class NotificationMapper extends MapperCommon
15
{
16
    const _TABLE = 'tbl_notifications';
17
 
18
    /**
19
     *
20
     * @var NotificationMapper
21
     */
22
    private static $_instance;
23
 
24
    /**
25
     *
26
     * @param AdapterInterface $adapter
27
     */
28
    private function __construct($adapter)
29
    {
30
        parent::__construct($adapter);
31
    }
32
 
33
 
34
    /**
35
     *
36
     * @param AdapterInterface $adapter
37
     * @return NotificationMapper
38
     */
39
    public static function getInstance($adapter)
40
    {
41
        if(self::$_instance == null) {
42
            self::$_instance = new NotificationMapper($adapter);
43
        }
44
        return self::$_instance;
45
    }
46
 
47
    /**
48
     *
49
     * @param int $user_id
50
     * @return int
51
     */
52
    public function fetchUnreadNotificationsCount($user_id)
53
    {
54
        $select = $this->sql->select(self::_TABLE);
55
        $select->columns(['total' => new Expression('COUNT(*)')]);
56
        $select->where->equalTo('user_id', $user_id)->and->equalTo('read', Notification::NO);
57
 
58
        //echo $select->getSqlString($this->adapter->platform);
59
 
60
        $record = $this->executeFetchOneArray($select);
61
        return $record['total'];
62
    }
63
 
64
    /**
65
     *
3262 efrain 66
     * @param string $uuid
67
     * @return Notification
68
     */
69
    public function fetchOneByUuid($uuid)
70
    {
71
        $prototype = new Notification();
72
 
73
        $select = $this->sql->select(self::_TABLE);
74
        $select->where->equalTo('uuid', $uuid);
75
 
76
        return $this->executeFetchOneObject($select, $prototype);
77
    }
78
 
79
 
80
    /**
81
     *
1 www 82
     * @param int $user_id
83
     * @return Notification[]
84
     */
85
    public function fetchAllsUnreadByUserId($user_id)
86
    {
87
        $prototype = new Notification();
88
 
89
        $select = $this->sql->select(self::_TABLE);
90
        $select->where->equalTo('user_id', $user_id)->and->equalTo('read', Notification::NO);
3262 efrain 91
 
1 www 92
 
93
        return $this->executeFetchAllObject($select, $prototype);
94
    }
2957 kerby 95
 
96
    /**
97
     *
98
     * @param int $user_id
99
     * @return Notification[]
100
     */
3262 efrain 101
    public function fetchAllByUserId($user_id)
2957 kerby 102
    {
103
        $prototype = new Notification();
104
 
105
        $select = $this->sql->select(self::_TABLE);
106
        $select->where->equalTo('user_id', $user_id);
2962 kerby 107
        $select->limit(30);
108
        $select->order(['added_on DESC']);
2957 kerby 109
        return $this->executeFetchAllObject($select, $prototype);
110
    }
1 www 111
 
112
    /**
3262 efrain 113
     *
1 www 114
     * @param int $user_id
115
     * @return boolean
116
     */
3262 efrain 117
    public function markAllAsReadByUserId($user_id)
1 www 118
    {
119
        $update = $this->sql->update(self::_TABLE);
120
        $update->set(['read' => Notification::YES]);
121
        $update->where->equalTo('user_id', $user_id);
122
        $update->where->equalTo('read',  Notification::NO);
123
 
124
        return $this->executeUpdate($update);
125
    }
126
 
127
 
128
    /**
3262 efrain 129
     *
130
     * @param int $id
131
     * @return boolean
132
     */
133
    public function markAsReadById($id)
134
    {
135
        $update = $this->sql->update(self::_TABLE);
136
        $update->set(['read' => Notification::YES]);
137
        $update->where->equalTo('id', $id);
138
 
139
        return $this->executeUpdate($update);
140
    }
141
 
142
    /**
1 www 143
     *
3262 efrain 144
     * @param int $id
145
     * @return boolean
146
     */
147
    public function deleteById($id)
148
    {
149
        $delete = $this->sql->delete(self::_TABLE);
150
        $delete->where->equalTo('id', $id);
151
 
152
        return $this->executeDelete($delete);
153
    }
154
 
155
    /**
156
     *
1 www 157
     * @param int $user_id
3262 efrain 158
     * @return boolean
159
     */
160
    public function deleteAllByUserId($user_id)
161
    {
162
        $delete = $this->sql->delete(self::_TABLE);
163
        $delete->where->equalTo('user_id', $user_id);
164
 
165
        return $this->executeDelete($delete);
166
    }
167
 
168
 
169
    /**
170
     *
171
     * @param int $user_id
1 www 172
     * @param int $company_id
173
     * @return boolean
174
     */
175
    public function markAllNotificationsAsReadByUserIdAndCompanyId($user_id, $company_id)
176
    {
177
        $update = $this->sql->update(self::_TABLE);
178
        $update->set(['read' => Notification::YES]);
179
        $update->where->equalTo('user_id', $user_id);
180
        $update->where->equalTo('company_id', $company_id);
181
        $update->where->equalTo('read',  Notification::NO);
182
 
183
        return $this->executeUpdate($update);
184
    }
185
 
186
    /**
187
     *
188
     * @param int $user_id
189
     * @param int $group_id
190
     * @return boolean
191
     */
192
    public function markAllNotificationsAsReadByUserIdAndGroupId($user_id, $group_id)
193
    {
194
        $update = $this->sql->update(self::_TABLE);
195
        $update->set(['read' => Notification::YES]);
196
        $update->where->equalTo('user_id', $user_id);
197
        $update->where->equalTo('group_id', $group_id);
198
        $update->where->equalTo('read',  Notification::NO);
199
 
200
        return $this->executeUpdate($update);
201
    }
202
 
203
 
204
    /**
205
     *
206
     * @param int $user_id
207
     * @param int $feed_id
208
     * @return boolean
209
     */
210
    public function markAllNotificationsAsReadByUserIdAndFeedId($user_id, $feed_id)
211
    {
212
        $update = $this->sql->update(self::_TABLE);
213
        $update->set(['read' => Notification::YES]);
214
        $update->where->equalTo('user_id', $user_id);
215
        $update->where->equalTo('feed_id', $feed_id);
216
        $update->where->equalTo('read',  Notification::NO);
217
 
218
        return $this->executeUpdate($update);
219
    }
220
 
221
    /**
222
     *
223
     * @param string $type
224
     * @param int $user_id
225
     * @return boolean
226
     */
227
    public function markAllNotificationsAsReadByTypeAndUserId($type, $user_id)
228
    {
229
        $update = $this->sql->update(self::_TABLE);
230
        $update->where->equalTo('type', $type);
231
        $update->where->equalTo('user_id', $user_id);
232
        $update->where->equalTo('read',  Notification::NO);
233
 
234
        return $this->executeUpdate($update);
235
    }
236
 
237
    /**
238
     *
239
     * @param string $type
240
     * @param int $user_id
241
     * @param int $company_id
242
     * @return boolean
243
     */
244
    public function markAllNotificationsAsReadByTypeAndUserIdAndCompanyId($type, $user_id, $company_id)
245
    {
246
        $update = $this->sql->update(self::_TABLE);
247
        $update->where->equalTo('type', $type);
248
        $update->where->equalTo('user_id', $user_id);
249
        $update->where->equalTo('company_id', $company_id);
250
        $update->where->equalTo('read',  Notification::NO);
251
 
252
        return $this->executeUpdate($update);
253
    }
254
 
255
    /**
256
     *
257
     * @param string $type
258
     * @param int $user_id
259
     * @param int $group_id
260
     * @return boolean
261
     */
262
    public function markAllNotificationsAsReadByTypeAndUserIdAndGroupId($type, $user_id, $group_id)
263
    {
264
        $update = $this->sql->update(self::_TABLE);
265
        $update->where->equalTo('type', $type);
266
        $update->where->equalTo('user_id', $user_id);
267
        $update->where->equalTo('group_id', $group_id);
268
        $update->where->equalTo('read',  Notification::NO);
269
 
270
        return $this->executeUpdate($update);
271
    }
272
 
273
    /**
274
     *
275
     * @param string $type
276
     * @param int $user_id
277
     * @param int $feed_id
278
     * @return boolean
279
     */
280
    public function markAllNotificationsAsReadByTypeAndUserIdAndFeedId($type, $user_id, $feed_id)
281
    {
282
        $update = $this->sql->update(self::_TABLE);
283
        $update->where->equalTo('type', $type);
284
        $update->where->equalTo('user_id', $user_id);
285
        $update->where->equalTo('feed_id', $feed_id);
286
        $update->where->equalTo('read',  Notification::NO);
287
 
288
        return $this->executeUpdate($update);
289
    }
290
 
291
 
292
 
293
    /**
294
     *
295
     * @param Notification $notification
296
     * @return boolean
297
     */
298
    public function insert($notification)
299
    {
300
        $hydrator = new ObjectPropertyHydrator();
301
        $values = $hydrator->extract($notification);
302
        $values = $this->removeEmpty($values);
303
 
304
        $insert = $this->sql->insert(self::_TABLE);
305
        $insert->values($values);
306
 
307
        $result = $this->executeInsert($insert);
308
        if($result) {
309
            $notification->id = $this->getLastInsertId();
310
        }
311
 
312
        return $result;
313
 
314
    }
315
 
316
 
317
 
318
 
319
 
320
}