Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2957 | 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\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
    }
78
 
79
    /**
80
     *
81
     * @param int $user_id
82
     * @return boolean
83
     */
84
    public function markAllNotificationsAsReadByUserId($user_id)
85
    {
86
        $update = $this->sql->update(self::_TABLE);
87
        $update->set(['read' => Notification::YES]);
88
        $update->where->equalTo('user_id', $user_id);
89
        $update->where->equalTo('read',  Notification::NO);
90
 
91
        return $this->executeUpdate($update);
92
    }
93
 
94
 
95
    /**
96
     *
97
     * @param int $user_id
98
     * @param int $company_id
99
     * @return boolean
100
     */
101
    public function markAllNotificationsAsReadByUserIdAndCompanyId($user_id, $company_id)
102
    {
103
        $update = $this->sql->update(self::_TABLE);
104
        $update->set(['read' => Notification::YES]);
105
        $update->where->equalTo('user_id', $user_id);
106
        $update->where->equalTo('company_id', $company_id);
107
        $update->where->equalTo('read',  Notification::NO);
108
 
109
        return $this->executeUpdate($update);
110
    }
111
 
112
    /**
113
     *
114
     * @param int $user_id
115
     * @param int $group_id
116
     * @return boolean
117
     */
118
    public function markAllNotificationsAsReadByUserIdAndGroupId($user_id, $group_id)
119
    {
120
        $update = $this->sql->update(self::_TABLE);
121
        $update->set(['read' => Notification::YES]);
122
        $update->where->equalTo('user_id', $user_id);
123
        $update->where->equalTo('group_id', $group_id);
124
        $update->where->equalTo('read',  Notification::NO);
125
 
126
        return $this->executeUpdate($update);
127
    }
128
 
129
 
130
    /**
131
     *
132
     * @param int $user_id
133
     * @param int $feed_id
134
     * @return boolean
135
     */
136
    public function markAllNotificationsAsReadByUserIdAndFeedId($user_id, $feed_id)
137
    {
138
        $update = $this->sql->update(self::_TABLE);
139
        $update->set(['read' => Notification::YES]);
140
        $update->where->equalTo('user_id', $user_id);
141
        $update->where->equalTo('feed_id', $feed_id);
142
        $update->where->equalTo('read',  Notification::NO);
143
 
144
        return $this->executeUpdate($update);
145
    }
146
 
147
    /**
148
     *
149
     * @param string $type
150
     * @param int $user_id
151
     * @return boolean
152
     */
153
    public function markAllNotificationsAsReadByTypeAndUserId($type, $user_id)
154
    {
155
        $update = $this->sql->update(self::_TABLE);
156
        $update->where->equalTo('type', $type);
157
        $update->where->equalTo('user_id', $user_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
     * @param int $company_id
168
     * @return boolean
169
     */
170
    public function markAllNotificationsAsReadByTypeAndUserIdAndCompanyId($type, $user_id, $company_id)
171
    {
172
        $update = $this->sql->update(self::_TABLE);
173
        $update->where->equalTo('type', $type);
174
        $update->where->equalTo('user_id', $user_id);
175
        $update->where->equalTo('company_id', $company_id);
176
        $update->where->equalTo('read',  Notification::NO);
177
 
178
        return $this->executeUpdate($update);
179
    }
180
 
181
    /**
182
     *
183
     * @param string $type
184
     * @param int $user_id
185
     * @param int $group_id
186
     * @return boolean
187
     */
188
    public function markAllNotificationsAsReadByTypeAndUserIdAndGroupId($type, $user_id, $group_id)
189
    {
190
        $update = $this->sql->update(self::_TABLE);
191
        $update->where->equalTo('type', $type);
192
        $update->where->equalTo('user_id', $user_id);
193
        $update->where->equalTo('group_id', $group_id);
194
        $update->where->equalTo('read',  Notification::NO);
195
 
196
        return $this->executeUpdate($update);
197
    }
198
 
199
    /**
200
     *
201
     * @param string $type
202
     * @param int $user_id
203
     * @param int $feed_id
204
     * @return boolean
205
     */
206
    public function markAllNotificationsAsReadByTypeAndUserIdAndFeedId($type, $user_id, $feed_id)
207
    {
208
        $update = $this->sql->update(self::_TABLE);
209
        $update->where->equalTo('type', $type);
210
        $update->where->equalTo('user_id', $user_id);
211
        $update->where->equalTo('feed_id', $feed_id);
212
        $update->where->equalTo('read',  Notification::NO);
213
 
214
        return $this->executeUpdate($update);
215
    }
216
 
217
 
218
 
219
    /**
220
     *
221
     * @param Notification $notification
222
     * @return boolean
223
     */
224
    public function insert($notification)
225
    {
226
        $hydrator = new ObjectPropertyHydrator();
227
        $values = $hydrator->extract($notification);
228
        $values = $this->removeEmpty($values);
229
 
230
        $insert = $this->sql->insert(self::_TABLE);
231
        $insert->values($values);
232
 
233
        $result = $this->executeInsert($insert);
234
        if($result) {
235
            $notification->id = $this->getLastInsertId();
236
        }
237
 
238
        return $result;
239
 
240
    }
241
 
242
 
243
 
244
 
245
 
246
}