Proyectos de Subversion LeadersLinked - Services

Rev

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