Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Ir a la última revisión | | 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
     *
84
     * @param int $my_coach_question_id
85
     * @return array
86
     */
87
    public function fetchCountByMyCoachQuestionId($my_coach_question_id)
88
    {
89
        $selectIn = $this->sql->select(MyCoachAnswerMapper::_TABLE);
90
        $selectIn->columns(['id']);
91
        $selectIn->where->equalTo('question_id', $my_coach_question_id);
92
 
93
 
94
        $select = $this->sql->select(self::_TABLE);
95
        $select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
96
        $select->where->in('my_coach_answer_id', $selectIn);
97
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_MY_COACH);
98
        //$select->group('reaction');
99
 
100
        $record = $this->executeFetchOneArray($select);
101
        return $record['total'];
102
    }
103
 
104
    /**
105
     *
106
     * @param int $my_coach_answer_id
107
     * @return array
108
     */
109
    public function fetchCountByMyCoachAnswerId($my_coach_answer_id)
110
    {
111
        $select = $this->sql->select(self::_TABLE);
112
        $select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
113
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
114
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_MY_COACH);
115
 
116
        $record = $this->executeFetchOneArray($select);
117
        return $record['total'];
118
    }
119
 
120
    /*
121
    public function fetchCountByMyCoachAnswerId($my_coach_answer_id)
122
    {
123
        $select = $this->sql->select(self::_TABLE);
124
        $select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
125
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
126
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_MY_COACH);
127
        $select->group('reaction');
128
 
129
        return $this->executeFetchAllArray($select);
130
    }
131
     */
132
 
133
 
134
    /**
135
     *
136
     * @param int $knowledge_area_id
137
     * @return array
138
     */
139
    public function fetchCountByKnowledgeAreaId($knowledge_area_id)
140
    {
141
        $select = $this->sql->select(self::_TABLE);
142
        $select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
143
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
144
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_KNOWLEDGE_AREA);
145
        $select->group('reaction');
146
 
147
        return $this->executeFetchAllArray($select);
148
    }
149
 
150
 
151
 
152
 
153
    /**
154
     *
155
     * @param int $post_id
156
     * @return array
157
     */
158
    public function fetchCountByPostId($post_id)
159
    {
160
        $select = $this->sql->select(self::_TABLE);
161
        $select->columns(['total' => new Expression('COUNT(*)'), 'reaction']);
162
        $select->where->equalTo('post_id', $post_id);
163
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_POST);
164
        $select->group('reaction');
165
 
166
        return $this->executeFetchAllArray($select);
167
    }
168
 
169
    /**
170
     *
171
     * @param int $feed_id
172
     * @param int $user_id
173
     * @return ContentReaction
174
     */
175
    public function fetchOneByFeedIdAndUserId($feed_id, $user_id)
176
    {
177
        $select = $this->sql->select(self::_TABLE);
178
        $select->where->equalTo('feed_id', $feed_id);
179
        $select->where->equalTo('user_id', $user_id);
180
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_FEED);
181
 
182
        $prototype = new ContentReaction();
183
        return $this->executeFetchOneObject($select, $prototype);
184
    }
185
 
186
 
187
    /**
188
     *
189
     * @param int $post_id
190
     * @param int $user_id
191
     * @return ContentReaction
192
     */
193
    public function fetchOneByPostIdAndUserId($post_id, $user_id)
194
    {
195
        $select = $this->sql->select(self::_TABLE);
196
        $select->where->equalTo('post_id', $post_id);
197
        $select->where->equalTo('user_id', $user_id);
198
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_POST);
199
 
200
        $prototype = new ContentReaction();
201
        return $this->executeFetchOneObject($select, $prototype);
202
    }
203
 
204
    /**
205
     *
206
     * @param int $my_coach_answer_id
207
     * @param int $user_id
208
     * @return ContentReaction
209
     */
210
    public function fetchOneByMyCoachAnswerIdAndUserId($my_coach_answer_id, $user_id)
211
    {
212
        $select = $this->sql->select(self::_TABLE);
213
        $select->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
214
        $select->where->equalTo('user_id', $user_id);
215
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_MY_COACH);
216
 
217
        $prototype = new ContentReaction();
218
        return $this->executeFetchOneObject($select, $prototype);
219
    }
220
 
221
    /**
222
     *
223
     * @param int $knowledge_area_id
224
     * @param int $user_id
225
     * @return ContentReaction
226
     */
227
    public function fetchOneByKnowledgeAreaIdAndUserId($knowledge_area_id, $user_id)
228
    {
229
        $select = $this->sql->select(self::_TABLE);
230
        $select->where->equalTo('knowledge_area_id', $knowledge_area_id);
231
        $select->where->equalTo('user_id', $user_id);
232
        $select->where->equalTo('relational', ContentReaction::RELATIONAL_KNOWLEDGE_AREA);
233
 
234
        $prototype = new ContentReaction();
235
        return $this->executeFetchOneObject($select, $prototype);
236
    }
237
 
238
 
232 efrain 239
 
240
 
1 efrain 241
    /**
242
     *
243
     * @param int $feed_id
244
     * @param int $user_id
245
     * @return boolean
246
     */
247
    public function deleteByFeedIdAndUserId($feed_id, $user_id)
248
    {
249
        $delete = $this->sql->delete(self::_TABLE);
250
        $delete->where->equalTo('feed_id', $feed_id);
251
        $delete->where->equalTo('user_id', $user_id);
252
        $delete->where->equalTo('relational', ContentReaction::RELATIONAL_FEED);
253
 
254
        return $this->executeDelete($delete);
255
    }
256
 
257
    /**
258
     *
259
     * @param int $knowledge_area_id
260
     * @param int $user_id
261
     * @return boolean
262
     */
263
    public function deleteByKnowledgeAreaIdAndUserId($knowledge_area_id, $user_id)
264
    {
265
        $delete = $this->sql->delete(self::_TABLE);
266
        $delete->where->equalTo('knowledge_area_id', $knowledge_area_id);
267
        $delete->where->equalTo('user_id', $user_id);
268
        $delete->where->equalTo('relational', ContentReaction::RELATIONAL_KNOWLEDGE_AREA);
269
 
270
        return $this->executeDelete($delete);
271
    }
272
 
273
    /**
274
     *
275
     * @param int $my_coach_answer_id
276
     * @param int $user_id
277
     * @return boolean
278
     */
279
    public function deleteByByMyCoachAnswerId($my_coach_answer_id, $user_id)
280
    {
281
        $delete = $this->sql->delete(self::_TABLE);
282
        $delete->where->equalTo('my_coach_answer_id', $my_coach_answer_id);
283
        $delete->where->equalTo('user_id', $user_id);
284
        $delete->where->equalTo('relational', ContentReaction::RELATIONAL_MY_COACH);
285
 
286
        return $this->executeDelete($delete);
287
    }
288
 
289
 
290
 
291
 
292
 
293
    /**
294
     *
295
     * @param int $post_id
296
     * @param int $user_id
297
     * @return boolean
298
     */
299
    public function deleteByPostIdAndUserId($post_id, $user_id)
300
    {
301
        $delete = $this->sql->delete(self::_TABLE);
302
        $delete->where->equalTo('post_id', $post_id);
303
        $delete->where->equalTo('user_id', $user_id);
304
        $delete->where->equalTo('relational', ContentReaction::RELATIONAL_POST);
305
 
306
        return $this->executeDelete($delete);
307
    }
308
 
309
    /**
310
     *
311
     * @param ContentReaction $contentReaction
312
     * @return boolean
313
     */
314
    public function insert($contentReaction)
315
    {
316
        $hydrator = new ObjectPropertyHydrator();
317
        $values = $hydrator->extract($contentReaction);
318
        $values = $this->removeEmpty($values);
319
 
320
        $insert = $this->sql->insert(self::_TABLE);
321
        $insert->values($values);
322
 
323
        $response = $this->executeInsert($insert);
324
        if($response) {
325
            $contentReaction->id = $this->getLastInsertId();
326
        }
327
 
328
        return $response;
329
    }
330
 
331
 
332
    /**
333
     *
334
     * @param ContentReaction $contentReaction
335
     * @return boolean
336
     */
337
    public function update($contentReaction)
338
    {
339
        $hydrator = new ObjectPropertyHydrator();
340
        $values = $hydrator->extract($contentReaction);
341
        $values = $this->removeEmpty($values);
342
 
343
        $update = $this->sql->update(self::_TABLE);
344
        $update->set($values);
345
        $update->where->equalTo('id', $contentReaction->id);
346
 
347
        return  $this->executeUpdate($update);
348
 
349
    }
350
}