Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6521 | | 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');
7242 efrain 214
 
215
       //echo $select->getSqlString($this->adapter->platform); exit;
216
 
217
 
1 www 218
        return $this->executeFetchAllObject($select, $prototype);
219
    }
6319 anderson 220
 
221
 
4808 efrain 222
    /**
223
     *
224
     * @param int $post_id
225
     * @return Comment[]
226
     */
6056 efrain 227
    public function fetchAllPublishedByKnowledgeAreaId($knowledge_area_id)
228
    {
229
        $prototype = new Comment();
6319 anderson 230
 
6056 efrain 231
        $select = $this->sql->select(self::_TABLE);
232
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
233
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
234
        $select->where->equalTo('relational', Comment::RELATIONAL_KNOWLEDGE_AREA);
235
        $select->where->IsNull('parent_id');
236
        return $this->executeFetchAllObject($select, $prototype);
237
    }
6319 anderson 238
 
6521 efrain 239
 
240
 
241
 
242
 
6056 efrain 243
    /**
244
     *
6521 efrain 245
     * @param int $my_coach_answer_id
246
     * @return Comment[]
247
     */
248
    public function fetchAllPublishedByMyCoachAnswerId($my_coach_answer_id)
249
    {
250
        $prototype = new Comment();
251
 
252
        $select = $this->sql->select(self::_TABLE);
253
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
254
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
255
        $select->where->equalTo('relational', Comment::RELATIONAL_MY_COACH);
256
        $select->where->IsNull('parent_id');
257
        $select->order('added_on DESC');
258
        return $this->executeFetchAllObject($select, $prototype);
259
    }
260
 
261
 
262
    /**
263
     *
6056 efrain 264
     * @param int $post_id
265
     * @return Comment[]
266
     */
4808 efrain 267
    public function fetchAllPublishedByPostId($post_id)
268
    {
269
        $prototype = new Comment();
6319 anderson 270
 
4808 efrain 271
        $select = $this->sql->select(self::_TABLE);
272
        $select->where->equalTo('post_id', $post_id);
273
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
274
        $select->where->equalTo('relational', Comment::RELATIONAL_POST);
275
        $select->where->IsNull('parent_id');
276
        return $this->executeFetchAllObject($select, $prototype);
277
    }
6319 anderson 278
 
1684 nelberth 279
    public function fetchAllPublishedByCommentId($comment_id)
280
    {
281
        $prototype = new Comment();
6319 anderson 282
 
1684 nelberth 283
        $select = $this->sql->select(self::_TABLE);
284
        $select->where->equalTo('parent_id', $comment_id);
285
        $select->where->equalTo('status', Feed::STATUS_PUBLISHED);
286
        return $this->executeFetchAllObject($select, $prototype);
287
    }
6319 anderson 288
 
1 www 289
    /**
290
     *
291
     * @param int $id
292
     * @return Comment
293
     */
294
    public function fetchOne($id)
295
    {
296
        $prototype = new Comment();
6319 anderson 297
 
1 www 298
        $select = $this->sql->select(self::_TABLE);
299
        $select->where->equalTo('id', $id);
6319 anderson 300
 
1 www 301
        return $this->executeFetchOneObject($select, $prototype);
302
    }
6319 anderson 303
 
1 www 304
    /**
305
     *
306
     * @param Comment $comment
307
     * @return boolean
308
     */
309
    public function insert($comment)
310
    {
311
        $hydrator = new ObjectPropertyHydrator();
312
        $values = $hydrator->extract($comment);
313
        $values = $this->removeEmpty($values);
6319 anderson 314
 
315
 
1 www 316
        $insert = $this->sql->insert(self::_TABLE);
317
        $insert->values($values);
6319 anderson 318
 
1 www 319
        $result = $this->executeInsert($insert);
6319 anderson 320
        if ($result) {
321
            $comment->id = $this->getLastInsertId();
1 www 322
        }
6319 anderson 323
 
1 www 324
        return $result;
325
    }
6319 anderson 326
 
327
 
1 www 328
    /**
329
     *
330
     * @param Comment $comment
331
     * @return boolean
332
     */
333
    public function update($comment)
334
    {
335
        $hydrator = new ObjectPropertyHydrator();
336
        $values = $hydrator->extract($comment);
337
        $values = $this->removeEmpty($values);
6319 anderson 338
 
339
 
1 www 340
        $update = $this->sql->update(self::_TABLE);
341
        $update->set($values);
342
        $update->where->equalTo('id', $comment->id);
343
 
6319 anderson 344
 
345
 
346
 
1 www 347
        return $this->executeUpdate($update);
348
    }
6319 anderson 349
 
350
 
1 www 351
    /**
352
     *
353
     * @param int $id
354
     * @return boolean
355
     */
356
    public function delete($id)
357
    {
6319 anderson 358
 
359
 
1 www 360
        $update = $this->sql->update(self::_TABLE);
361
        $update->set([
362
            'status' => Comment::STATUS_DELETED
363
        ]);
364
        $update->where->equalTo('id', $id);
6319 anderson 365
 
1 www 366
        return $this->executeUpdate($update);
367
    }
6319 anderson 368
 
1 www 369
    /**
370
     *
371
     * @param int $feed_id
372
     * @return boolean
373
     */
374
    public function deleteAllByFeedId($feed_id)
375
    {
6319 anderson 376
 
1 www 377
        $update = $this->sql->update(self::_TABLE);
378
        $update->set([
379
            'status' => Comment::STATUS_DELETED
380
        ]);
381
        $update->where->equalTo('feed_id', $feed_id);
6319 anderson 382
 
1 www 383
        return $this->executeUpdate($update);
384
    }
6319 anderson 385
}