Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2957 | 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\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
     *
66
     * @param int $user_id
67
     * @return Notification[]
68
     */
69
    public function fetchAllsUnreadByUserId($user_id)
70
    {
71
        $prototype = new Notification();
72
 
73
        $select = $this->sql->select(self::_TABLE);
74
        $select->where->equalTo('user_id', $user_id)->and->equalTo('read', Notification::NO);
75
 
76
        return $this->executeFetchAllObject($select, $prototype);
77
    }
2957 kerby 78
 
79
    /**
80
     *
81
     * @param int $user_id
82
     * @return Notification[]
83
     */
84
    public function fetchAllsReadByUserId($user_id)
85
    {
86
        $prototype = new Notification();
87
 
88
        $select = $this->sql->select(self::_TABLE);
89
        $select->where->equalTo('user_id', $user_id);
2962 kerby 90
        $select->limit(30);
91
        $select->order(['added_on DESC']);
2957 kerby 92
        return $this->executeFetchAllObject($select, $prototype);
93
    }
1 www 94
 
95
    /**
96
     *
97
     * @param int $user_id
98
     * @return boolean
99
     */
100
    public function markAllNotificationsAsReadByUserId($user_id)
101
    {
102
        $update = $this->sql->update(self::_TABLE);
103
        $update->set(['read' => Notification::YES]);
104
        $update->where->equalTo('user_id', $user_id);
105
        $update->where->equalTo('read',  Notification::NO);
106
 
107
        return $this->executeUpdate($update);
108
    }
109
 
110
 
111
    /**
112
     *
113
     * @param int $user_id
114
     * @param int $company_id
115
     * @return boolean
116
     */
117
    public function markAllNotificationsAsReadByUserIdAndCompanyId($user_id, $company_id)
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('company_id', $company_id);
123
        $update->where->equalTo('read',  Notification::NO);
124
 
125
        return $this->executeUpdate($update);
126
    }
127
 
128
    /**
129
     *
130
     * @param int $user_id
131
     * @param int $group_id
132
     * @return boolean
133
     */
134
    public function markAllNotificationsAsReadByUserIdAndGroupId($user_id, $group_id)
135
    {
136
        $update = $this->sql->update(self::_TABLE);
137
        $update->set(['read' => Notification::YES]);
138
        $update->where->equalTo('user_id', $user_id);
139
        $update->where->equalTo('group_id', $group_id);
140
        $update->where->equalTo('read',  Notification::NO);
141
 
142
        return $this->executeUpdate($update);
143
    }
144
 
145
 
146
    /**
147
     *
148
     * @param int $user_id
149
     * @param int $feed_id
150
     * @return boolean
151
     */
152
    public function markAllNotificationsAsReadByUserIdAndFeedId($user_id, $feed_id)
153
    {
154
        $update = $this->sql->update(self::_TABLE);
155
        $update->set(['read' => Notification::YES]);
156
        $update->where->equalTo('user_id', $user_id);
157
        $update->where->equalTo('feed_id', $feed_id);
158
        $update->where->equalTo('read',  Notification::NO);
159
 
160
        return $this->executeUpdate($update);
161
    }
162
 
163
    /**
164
     *
165
     * @param string $type
166
     * @param int $user_id
167
     * @return boolean
168
     */
169
    public function markAllNotificationsAsReadByTypeAndUserId($type, $user_id)
170
    {
171
        $update = $this->sql->update(self::_TABLE);
172
        $update->where->equalTo('type', $type);
173
        $update->where->equalTo('user_id', $user_id);
174
        $update->where->equalTo('read',  Notification::NO);
175
 
176
        return $this->executeUpdate($update);
177
    }
178
 
179
    /**
180
     *
181
     * @param string $type
182
     * @param int $user_id
183
     * @param int $company_id
184
     * @return boolean
185
     */
186
    public function markAllNotificationsAsReadByTypeAndUserIdAndCompanyId($type, $user_id, $company_id)
187
    {
188
        $update = $this->sql->update(self::_TABLE);
189
        $update->where->equalTo('type', $type);
190
        $update->where->equalTo('user_id', $user_id);
191
        $update->where->equalTo('company_id', $company_id);
192
        $update->where->equalTo('read',  Notification::NO);
193
 
194
        return $this->executeUpdate($update);
195
    }
196
 
197
    /**
198
     *
199
     * @param string $type
200
     * @param int $user_id
201
     * @param int $group_id
202
     * @return boolean
203
     */
204
    public function markAllNotificationsAsReadByTypeAndUserIdAndGroupId($type, $user_id, $group_id)
205
    {
206
        $update = $this->sql->update(self::_TABLE);
207
        $update->where->equalTo('type', $type);
208
        $update->where->equalTo('user_id', $user_id);
209
        $update->where->equalTo('group_id', $group_id);
210
        $update->where->equalTo('read',  Notification::NO);
211
 
212
        return $this->executeUpdate($update);
213
    }
214
 
215
    /**
216
     *
217
     * @param string $type
218
     * @param int $user_id
219
     * @param int $feed_id
220
     * @return boolean
221
     */
222
    public function markAllNotificationsAsReadByTypeAndUserIdAndFeedId($type, $user_id, $feed_id)
223
    {
224
        $update = $this->sql->update(self::_TABLE);
225
        $update->where->equalTo('type', $type);
226
        $update->where->equalTo('user_id', $user_id);
227
        $update->where->equalTo('feed_id', $feed_id);
228
        $update->where->equalTo('read',  Notification::NO);
229
 
230
        return $this->executeUpdate($update);
231
    }
232
 
233
 
234
 
235
    /**
236
     *
237
     * @param Notification $notification
238
     * @return boolean
239
     */
240
    public function insert($notification)
241
    {
242
        $hydrator = new ObjectPropertyHydrator();
243
        $values = $hydrator->extract($notification);
244
        $values = $this->removeEmpty($values);
245
 
246
        $insert = $this->sql->insert(self::_TABLE);
247
        $insert->values($values);
248
 
249
        $result = $this->executeInsert($insert);
250
        if($result) {
251
            $notification->id = $this->getLastInsertId();
252
        }
253
 
254
        return $result;
255
 
256
    }
257
 
258
 
259
 
260
 
261
 
262
}