Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use LeadersLinked\Model\Comment;
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 Laminas\Db\Sql\Insert;
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
use LeadersLinked\Model\Feed;
15
 
16
 
17
class CommentMapper extends MapperCommon
18
{
19
    const _TABLE = 'tbl_comments';
20
 
21
 
22
    /**
23
     *
24
     * @var CommentMapper
25
     */
26
    private static $_instance;
27
 
28
    /**
29
     *
30
     * @param AdapterInterface $adapter
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
36
 
37
    /**
38
     *
39
     * @param AdapterInterface $adapter
40
     * @param LoggerInterface $logger
41
     * @param int $user_id
42
     * @return \LeadersLinked\Mapper\CommentMapper
43
     */
44
    public static function getInstance($adapter)
45
    {
46
        if (self::$_instance == null) {
47
            self::$_instance = new CommentMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
51
 
52
    /**
53
     *
54
     * @param int $uuid
55
     * @return Comment
56
     */
57
    public function fetchOneByUuid($uuid)
58
    {
59
        $prototype = new Comment;
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('uuid', $uuid);
62
 
63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
65
 
66
 
67
    /**
68
     *
69
     * @param int $feed_id
70
     * @return int
71
     */
72
    public function fetchCountCommentByFeedId($feed_id)
73
    {
74
        $select = $this->sql->select(self::_TABLE);
75
        $select->columns(['total' => new Expression('COUNT(*)')]);
76
        $select->where->equalTo('feed_id', $feed_id);
77
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
78
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
79
 
80
        $record = $this->executeFetchOneArray($select);
81
        return $record['total'];
82
    }
83
 
84
    /**
85
     *
86
     * @param int $knowledge_area_id
87
     * @return int
88
     */
89
    public function fetchCountCommentByKnowledgeAreaId($knowledge_area_id)
90
    {
91
        $select = $this->sql->select(self::_TABLE);
92
        $select->columns(['total' => new Expression('COUNT(*)')]);
93
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
94
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
95
 
96
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
97
 
98
        $record = $this->executeFetchOneArray($select);
99
        return $record['total'];
100
    }
101
 
102
 
103
    /**
104
     *
105
     * @param int $my_coach_answer_id
106
     * @return int
107
     */
108
    public function fetchCountByMyCoachAnswerId($my_coach_answer_id)
109
    {
110
        $select = $this->sql->select(self::_TABLE);
111
        $select->columns(['total' => new Expression('COUNT(*)')]);
112
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
113
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
114
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
115
 
116
        $record = $this->executeFetchOneArray($select);
117
        return $record['total'];
118
    }
119
 
120
 
121
 
122
 
123
    /**
124
     *
125
     * @param int my_coach_question_id
126
     * @return int
127
     */
128
    public function fetchCountByMyCoachQuestionId($my_coach_question_id)
129
    {
130
        $selectIn = $this->sql->select(MyCoachAnswerMapper::_TABLE);
131
        $selectIn->columns(['id']);
132
        $selectIn->where->equalTo('question_id', $my_coach_question_id);
133
 
134
        $select = $this->sql->select(self::_TABLE);
135
        $select->columns(['total' => new Expression('COUNT(*)')]);
136
        $select->where->in('my_coach_answer_id', $selectIn);
137
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
138
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
139
 
140
        $record = $this->executeFetchOneArray($select);
141
        return $record['total'];
142
    }
143
 
144
 
145
 
146
 
147
 
148
 
149
    /**
150
     *
151
     * @param int $post_id
152
     * @return int
153
     */
154
    public function fetchCountCommentByPostId($post_id)
155
    {
156
        $select = $this->sql->select(self::_TABLE);
157
        $select->columns(['total' => new Expression('COUNT(*)')]);
158
        $select->where->equalTo('post_id', $post_id);
159
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
160
 
161
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
162
 
163
        $record = $this->executeFetchOneArray($select);
164
        return $record['total'];
165
    }
166
 
167
 
168
    /**
169
     *
170
     * @param int $feed_id
171
     * @return Comment[]
172
     */
173
    public function fetchAllByFeedId($feed_id)
174
    {
175
        $prototype = new Comment();
176
 
177
        $select = $this->sql->select(self::_TABLE);
178
        $select->where->equalTo('feed_id', $feed_id);
179
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
180
 
181
        return $this->executeFetchAllObject($select, $prototype);
182
    }
183
 
184
    /**
185
     *
186
     * @param int $post_id
187
     * @return Comment[]
188
     */
189
    public function fetchAllByPostId($post_id)
190
    {
191
        $prototype = new Comment();
192
 
193
        $select = $this->sql->select(self::_TABLE);
194
        $select->where->equalTo('post_id', $post_id);
195
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
196
 
197
        return $this->executeFetchAllObject($select, $prototype);
198
    }
199
 
200
    /**
201
     *
202
     * @param int $feed_id
192 efrain 203
     * @param int[] $user_blocked_ids
204
     * @param int[] $abuse_report_comment_ids
1 efrain 205
     * @return Comment[]
206
     */
192 efrain 207
    public function fetchAllPublishedByFeedId($feed_id , $user_blocked_ids  = [], $abuse_report_comment_ids = [])
1 efrain 208
    {
209
        $prototype = new Comment();
210
 
211
        $select = $this->sql->select(self::_TABLE);
212
        $select->where->equalTo('feed_id', $feed_id);
213
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
214
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
215
        $select->where->IsNull('parent_id');
216
 
192 efrain 217
        if($user_blocked_ids) {
218
            $select->where->notIn('user_id', $user_blocked_ids);
219
        }
220
 
221
 
222
        if($abuse_report_comment_ids) {
223
            $select->where->notIn('id', $abuse_report_comment_ids);
224
        }
225
 
1 efrain 226
       //echo $select->getSqlString($this->adapter->platform); exit;
227
 
228
 
229
        return $this->executeFetchAllObject($select, $prototype);
230
    }
231
 
232
 
233
    /**
234
     *
235
     * @param int $post_id
236
     * @return Comment[]
237
     */
238
    public function fetchAllPublishedByKnowledgeAreaId($knowledge_area_id)
239
    {
240
        $prototype = new Comment();
241
 
242
        $select = $this->sql->select(self::_TABLE);
243
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
244
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
245
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
246
        $select->where->IsNull('parent_id');
247
        return $this->executeFetchAllObject($select, $prototype);
248
    }
249
 
250
 
251
 
252
 
253
 
254
    /**
255
     *
256
     * @param int $my_coach_answer_id
257
     * @return Comment[]
258
     */
259
    public function fetchAllPublishedByMyCoachAnswerId($my_coach_answer_id)
260
    {
261
        $prototype = new Comment();
262
 
263
        $select = $this->sql->select(self::_TABLE);
264
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
265
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
266
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
267
        $select->where->IsNull('parent_id');
268
        $select->order('added_on DESC');
269
        return $this->executeFetchAllObject($select, $prototype);
270
    }
271
 
272
 
273
    /**
274
     *
275
     * @param int $post_id
192 efrain 276
     * @param int[] $user_blocked_ids
277
     * @param int[] $abuse_report_comment_ids
1 efrain 278
     * @return Comment[]
279
     */
192 efrain 280
    public function fetchAllPublishedByPostId($post_id, $user_blocked_ids = [], $abuse_report_comment_ids = [])
1 efrain 281
    {
282
        $prototype = new Comment();
283
 
284
        $select = $this->sql->select(self::_TABLE);
285
        $select->where->equalTo('post_id', $post_id);
286
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
287
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
288
        $select->where->IsNull('parent_id');
192 efrain 289
 
290
        if($user_blocked_ids) {
291
            $select->where->notIn('user_id', $user_blocked_ids);
292
        }
293
 
294
 
295
        if($abuse_report_comment_ids) {
296
            $select->where->notIn('id', $abuse_report_comment_ids);
297
        }
298
 
1 efrain 299
        return $this->executeFetchAllObject($select, $prototype);
300
    }
301
 
192 efrain 302
    /**
303
     *
304
     * @param int $comment_id
305
     * @param int[] $user_blocked_ids
306
     * @param int[] $abuse_report_comment_ids
307
     * @return Comment[]
308
     */
309
    public function fetchAllPublishedByCommentId($comment_id, $user_blocked_ids = [], $abuse_report_comment_ids = [])
1 efrain 310
    {
311
        $prototype = new Comment();
312
 
313
        $select = $this->sql->select(self::_TABLE);
314
        $select->where->equalTo('parent_id', $comment_id);
315
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
192 efrain 316
 
317
        if($user_blocked_ids) {
318
            $select->where->notIn('user_id', $user_blocked_ids);
319
        }
320
 
321
 
322
        if($abuse_report_comment_ids) {
323
            $select->where->notIn('id', $abuse_report_comment_ids);
324
        }
325
 
326
 
1 efrain 327
        return $this->executeFetchAllObject($select, $prototype);
328
    }
329
 
330
    /**
331
     *
332
     * @param int $id
333
     * @return Comment
334
     */
335
    public function fetchOne($id)
336
    {
337
        $prototype = new Comment();
338
 
339
        $select = $this->sql->select(self::_TABLE);
340
        $select->where->equalTo('id', $id);
341
 
342
        return $this->executeFetchOneObject($select, $prototype);
343
    }
344
 
345
    /**
346
     *
347
     * @param Comment $comment
348
     * @return boolean
349
     */
350
    public function insert($comment)
351
    {
352
        $hydrator = new ObjectPropertyHydrator();
353
        $values = $hydrator->extract($comment);
354
        $values = $this->removeEmpty($values);
355
 
356
 
357
        $insert = $this->sql->insert(self::_TABLE);
358
        $insert->values($values);
359
 
360
        $result = $this->executeInsert($insert);
361
        if ($result) {
362
            $comment->id = $this->getLastInsertId();
363
        }
364
 
365
        return $result;
366
    }
367
 
368
 
369
    /**
370
     *
371
     * @param Comment $comment
372
     * @return boolean
373
     */
374
    public function update($comment)
375
    {
376
        $hydrator = new ObjectPropertyHydrator();
377
        $values = $hydrator->extract($comment);
378
        $values = $this->removeEmpty($values);
379
 
380
 
381
        $update = $this->sql->update(self::_TABLE);
382
        $update->set($values);
383
        $update->where->equalTo('id', $comment->id);
384
 
385
 
386
 
387
 
388
        return $this->executeUpdate($update);
389
    }
390
 
391
 
392
    /**
393
     *
394
     * @param int $id
395
     * @return boolean
396
     */
397
    public function delete($id)
398
    {
399
 
400
 
401
        $update = $this->sql->update(self::_TABLE);
402
        $update->set([
403
            'status' => Comment::STATUS_DELETED
404
        ]);
405
        $update->where->equalTo('id', $id);
406
 
407
        return $this->executeUpdate($update);
408
    }
409
 
410
    /**
411
     *
412
     * @param int $feed_id
413
     * @return boolean
414
     */
415
    public function deleteAllByFeedId($feed_id)
416
    {
417
 
418
        $update = $this->sql->update(self::_TABLE);
419
        $update->set([
420
            'status' => Comment::STATUS_DELETED
421
        ]);
422
        $update->where->equalTo('feed_id', $feed_id);
423
 
424
        return $this->executeUpdate($update);
425
    }
426
}