Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6319 | 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\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
 
6319 anderson 21
 
1 www 22
    /**
23
     *
24
     * @var CommentMapper
25
     */
26
    private static $_instance;
6319 anderson 27
 
1 www 28
    /**
29
     *
30
     * @param AdapterInterface $adapter
31
     */
32
    private function __construct($adapter)
33
    {
34
        parent::__construct($adapter);
35
    }
6319 anderson 36
 
1 www 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
    {
6319 anderson 46
        if (self::$_instance == null) {
1 www 47
            self::$_instance = new CommentMapper($adapter);
48
        }
49
        return self::$_instance;
50
    }
6319 anderson 51
 
1 www 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);
6319 anderson 62
 
1 www 63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
6319 anderson 65
 
66
 
1 www 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);
6319 anderson 75
        $select->columns(['total' => new Expression('COUNT(*)')]);
1 www 76
        $select->where->equalTo('feed_id', $feed_id);
4808 efrain 77
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
1 www 78
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 79
 
1 www 80
        $record = $this->executeFetchOneArray($select);
81
        return $record['total'];
82
    }
6319 anderson 83
 
4808 efrain 84
    /**
85
     *
6056 efrain 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);
6319 anderson 92
        $select->columns(['total' => new Expression('COUNT(*)')]);
6056 efrain 93
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
94
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
6319 anderson 95
 
6056 efrain 96
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 97
 
6056 efrain 98
        $record = $this->executeFetchOneArray($select);
99
        return $record['total'];
100
    }
6521 efrain 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);
6319 anderson 133
 
6521 efrain 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
 
6319 anderson 144
 
6521 efrain 145
 
146
 
147
 
148
 
6056 efrain 149
    /**
150
     *
4808 efrain 151
     * @param int $post_id
152
     * @return int
153
     */
154
    public function fetchCountCommentByPostId($post_id)
155
    {
156
        $select = $this->sql->select(self::_TABLE);
6319 anderson 157
        $select->columns(['total' => new Expression('COUNT(*)')]);
4808 efrain 158
        $select->where->equalTo('post_id', $post_id);
159
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
6319 anderson 160
 
4808 efrain 161
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
6319 anderson 162
 
4808 efrain 163
        $record = $this->executeFetchOneArray($select);
164
        return $record['total'];
165
    }
6319 anderson 166
 
167
 
1 www 168
    /**
169
     *
170
     * @param int $feed_id
171
     * @return Comment[]
172
     */
173
    public function fetchAllByFeedId($feed_id)
174
    {
175
        $prototype = new Comment();
6319 anderson 176
 
1 www 177
        $select = $this->sql->select(self::_TABLE);
178
        $select->where->equalTo('feed_id', $feed_id);
4808 efrain 179
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
6319 anderson 180
 
1 www 181
        return $this->executeFetchAllObject($select, $prototype);
182
    }
6319 anderson 183
 
1 www 184
    /**
185
     *
4808 efrain 186
     * @param int $post_id
187
     * @return Comment[]
188
     */
189
    public function fetchAllByPostId($post_id)
190
    {
191
        $prototype = new Comment();
6319 anderson 192
 
4808 efrain 193
        $select = $this->sql->select(self::_TABLE);
194
        $select->where->equalTo('post_id', $post_id);
195
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
6319 anderson 196
 
4808 efrain 197
        return $this->executeFetchAllObject($select, $prototype);
198
    }
6319 anderson 199
 
4808 efrain 200
    /**
201
     *
1 www 202
     * @param int $feed_id
203
     * @return Comment[]
204
     */
205
    public function fetchAllPublishedByFeedId($feed_id)
206
    {
207
        $prototype = new Comment();
6319 anderson 208
 
1 www 209
        $select = $this->sql->select(self::_TABLE);
210
        $select->where->equalTo('feed_id', $feed_id);
211
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
4808 efrain 212
        $select->where->equalTo('relational', Comment::RELATIONAL_FEED);
1682 nelberth 213
        $select->where->IsNull('parent_id');
1 www 214
        return $this->executeFetchAllObject($select, $prototype);
215
    }
6319 anderson 216
 
217
 
4808 efrain 218
    /**
219
     *
220
     * @param int $post_id
221
     * @return Comment[]
222
     */
6056 efrain 223
    public function fetchAllPublishedByKnowledgeAreaId($knowledge_area_id)
224
    {
225
        $prototype = new Comment();
6319 anderson 226
 
6056 efrain 227
        $select = $this->sql->select(self::_TABLE);
228
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
229
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
230
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
231
        $select->where->IsNull('parent_id');
232
        return $this->executeFetchAllObject($select, $prototype);
233
    }
6319 anderson 234
 
6521 efrain 235
 
236
 
237
 
238
 
6056 efrain 239
    /**
240
     *
6521 efrain 241
     * @param int $my_coach_answer_id
242
     * @return Comment[]
243
     */
244
    public function fetchAllPublishedByMyCoachAnswerId($my_coach_answer_id)
245
    {
246
        $prototype = new Comment();
247
 
248
        $select = $this->sql->select(self::_TABLE);
249
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
250
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
251
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
252
        $select->where->IsNull('parent_id');
253
        $select->order('added_on DESC');
254
        return $this->executeFetchAllObject($select, $prototype);
255
    }
256
 
257
 
258
    /**
259
     *
6056 efrain 260
     * @param int $post_id
261
     * @return Comment[]
262
     */
4808 efrain 263
    public function fetchAllPublishedByPostId($post_id)
264
    {
265
        $prototype = new Comment();
6319 anderson 266
 
4808 efrain 267
        $select = $this->sql->select(self::_TABLE);
268
        $select->where->equalTo('post_id', $post_id);
269
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
270
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
271
        $select->where->IsNull('parent_id');
272
        return $this->executeFetchAllObject($select, $prototype);
273
    }
6319 anderson 274
 
1684 nelberth 275
    public function fetchAllPublishedByCommentId($comment_id)
276
    {
277
        $prototype = new Comment();
6319 anderson 278
 
1684 nelberth 279
        $select = $this->sql->select(self::_TABLE);
280
        $select->where->equalTo('parent_id', $comment_id);
281
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
282
        return $this->executeFetchAllObject($select, $prototype);
283
    }
6319 anderson 284
 
1 www 285
    /**
286
     *
287
     * @param int $id
288
     * @return Comment
289
     */
290
    public function fetchOne($id)
291
    {
292
        $prototype = new Comment();
6319 anderson 293
 
1 www 294
        $select = $this->sql->select(self::_TABLE);
295
        $select->where->equalTo('id', $id);
6319 anderson 296
 
1 www 297
        return $this->executeFetchOneObject($select, $prototype);
298
    }
6319 anderson 299
 
1 www 300
    /**
301
     *
302
     * @param Comment $comment
303
     * @return boolean
304
     */
305
    public function insert($comment)
306
    {
307
        $hydrator = new ObjectPropertyHydrator();
308
        $values = $hydrator->extract($comment);
309
        $values = $this->removeEmpty($values);
6319 anderson 310
 
311
 
1 www 312
        $insert = $this->sql->insert(self::_TABLE);
313
        $insert->values($values);
6319 anderson 314
 
1 www 315
        $result = $this->executeInsert($insert);
6319 anderson 316
        if ($result) {
317
            $comment->id = $this->getLastInsertId();
1 www 318
        }
6319 anderson 319
 
1 www 320
        return $result;
321
    }
6319 anderson 322
 
323
 
1 www 324
    /**
325
     *
326
     * @param Comment $comment
327
     * @return boolean
328
     */
329
    public function update($comment)
330
    {
331
        $hydrator = new ObjectPropertyHydrator();
332
        $values = $hydrator->extract($comment);
333
        $values = $this->removeEmpty($values);
6319 anderson 334
 
335
 
1 www 336
        $update = $this->sql->update(self::_TABLE);
337
        $update->set($values);
338
        $update->where->equalTo('id', $comment->id);
339
 
6319 anderson 340
 
341
 
342
 
1 www 343
        return $this->executeUpdate($update);
344
    }
6319 anderson 345
 
346
 
1 www 347
    /**
348
     *
349
     * @param int $id
350
     * @return boolean
351
     */
352
    public function delete($id)
353
    {
6319 anderson 354
 
355
 
1 www 356
        $update = $this->sql->update(self::_TABLE);
357
        $update->set([
358
            'status' => Comment::STATUS_DELETED
359
        ]);
360
        $update->where->equalTo('id', $id);
6319 anderson 361
 
1 www 362
        return $this->executeUpdate($update);
363
    }
6319 anderson 364
 
1 www 365
    /**
366
     *
367
     * @param int $feed_id
368
     * @return boolean
369
     */
370
    public function deleteAllByFeedId($feed_id)
371
    {
6319 anderson 372
 
1 www 373
        $update = $this->sql->update(self::_TABLE);
374
        $update->set([
375
            'status' => Comment::STATUS_DELETED
376
        ]);
377
        $update->where->equalTo('feed_id', $feed_id);
6319 anderson 378
 
1 www 379
        return $this->executeUpdate($update);
380
    }
6319 anderson 381
}