Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use LeadersLinked\Mapper\Common\MapperCommon;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Model\MicrolearningTopicCapsule;
9
use Laminas\Db\Sql\Expression;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Db\ResultSet\HydratingResultSet;
12
use Laminas\Paginator\Paginator;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
 
16
class MicrolearningTopicCapsuleMapper extends MapperCommon
17
{
558 stevensc 18
    const _TABLE = 'tbl_microlearning_topic_capsules';
283 www 19
 
20
    /**
21
     *
22
     * @var MicrolearningTopicCapsuleMapper
23
     */
24
    private static $_instance;
25
    /**
26
     *
27
     * @param AdapterInterface $adapter
28
     */
29
    private function __construct($adapter)
30
    {
31
        parent::__construct($adapter);
32
    }
33
 
34
    /**
35
     *
36
     * @param AdapterInterface $adapter
37
     * @return MicrolearningTopicCapsuleMapper
38
     */
39
    public static function getInstance($adapter)
40
    {
41
        if(self::$_instance == null) {
42
            self::$_instance = new MicrolearningTopicCapsuleMapper($adapter);
43
        }
44
        return self::$_instance;
45
    }
46
 
47
    /**
48
     *
49
     * @param int $company_id
50
     * @param int $topic_id
51
     * @return int
52
     */
53
    public function fetchTotalCountByCompanyIdAndTopicId($company_id, $topic_id)
54
    {
55
        $select = $this->sql->select();
56
        $select->columns(['total' => new Expression('COUNT(*)')]);
57
        $select->from(self::_TABLE);
58
        $select->where->equalTo('company_id', $company_id);
59
        $select->where->equalTo('topic_id', $topic_id);
60
 
61
        $record = $this->executeFetchOneArray($select);
62
        return $record['total'];
63
    }
658 stevensc 64
 
65
    /**
66
     *
67
     * @param int $topic_id
68
     * @return int
69
     */
70
    public function fetchTotalCountByTopicId($topic_id)
71
    {
72
        $select = $this->sql->select();
73
        $select->columns(['total' => new Expression('COUNT(*)')]);
74
        $select->from(self::_TABLE);
75
        $select->where->equalTo('topic_id', $topic_id);
76
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
77
 
78
        $record = $this->executeFetchOneArray($select);
79
        return $record['total'];
80
    }
283 www 81
 
82
    /**
83
     *
84
     * @param int $company_id
85
     * @return int
86
     */
87
    public function fetchTotalCountAllActiveByCompanyId($company_id)
88
    {
89
        $select = $this->sql->select();
90
        $select->columns(['total' => new Expression('COUNT(*)')]);
91
        $select->from(self::_TABLE);
92
        $select->where->equalTo('company_id', $company_id);
93
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
94
 
95
        $record = $this->executeFetchOneArray($select);
96
        return $record['total'];
97
    }
98
 
99
    /**
100
     *
101
     * @param int $id
102
     * @return MicrolearningTopicCapsule
103
     */
104
    public function fetchOne($id)
105
    {
106
        $prototype = new MicrolearningTopicCapsule();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('id', $id);
110
 
111
        return $this->executeFetchOneObject($select, $prototype);
112
    }
113
 
114
    /**
115
     *
116
     * @param int $topic_id
117
     * @param int $capsule_id
118
     * @return MicrolearningTopicCapsule
119
     */
120
    public function fetchOneByTopicIdAndCapsuleId($topic_id, $capsule_id)
121
    {
122
        $prototype = new MicrolearningTopicCapsule();
123
 
124
        $select = $this->sql->select(self::_TABLE);
125
        $select->where->equalTo('topic_id', $topic_id);
126
        $select->where->equalTo('capsule_id', $capsule_id);
127
 
128
        return $this->executeFetchOneObject($select, $prototype);
129
    }
618 stevensc 130
 
131
    /**
132
     * Deletes all topic-capsule relationships for a given topic ID.
133
     *
134
     * @param int $topic_id
135
     * @return boolean
136
     */
137
    public function deleteByTopicId($topic_id)
138
    {
139
        $delete = $this->sql->delete(self::_TABLE);
140
        $delete->where->equalTo('topic_id', $topic_id);
141
        return $this->executeDelete($delete);
142
    }
632 stevensc 143
 
144
    /**
145
     * Deletes all topic-capsule relationships for a given capsule ID.
146
     *
147
     * @param int $capsule_id
148
     * @return boolean
149
     */
150
    public function deleteAllByCapsuleId($capsule_id)
151
    {
152
        $delete = $this->sql->delete(self::_TABLE);
153
        $delete->where->equalTo('capsule_id', $capsule_id);
154
        return $this->executeDelete($delete);
155
    }
283 www 156
 
157
    /**
558 stevensc 158
     * Fetches a single topic-capsule relationship by capsule ID.
159
     * Since a capsule might theoretically be in multiple topics, this fetches the first one found.
160
     *
161
     * @param int $capsule_id
162
     * @return MicrolearningTopicCapsule|null
163
     */
164
    public function fetchOneByCapsuleId($capsule_id)
165
    {
166
        $prototype = new MicrolearningTopicCapsule();
167
 
168
        $select = $this->sql->select(self::_TABLE);
169
        $select->where->equalTo('capsule_id', $capsule_id);
170
        $select->limit(1); // Ensure only one record is fetched
171
 
172
        return $this->executeFetchOneObject($select, $prototype);
173
    }
174
 
175
    /**
283 www 176
     *
177
     * @param int $company_id
178
     * @param int $topic_id
179
     * @return int
180
     */
181
    public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
182
    {
183
 
184
 
185
        $select = $this->sql->select(self::_TABLE);
186
        $select->columns(['total' => new Expression('COUNT(*)')]);
187
        $select->where->equalTo('company_id', $company_id);
188
        $select->where->equalTo('topic_id', $topic_id);
189
 
190
        $record = $this->executeFetchOneArray($select);
191
        return $record['total'];
192
 
193
    }
194
 
195
 
196
 
197
 
198
    /**
199
     *
200
     * @param  int $topic_id
201
     * @return MicrolearningTopicCapsule[]
202
     */
203
    public function fetchAllByTopicId($topic_id)
204
    {
205
        $prototype = new MicrolearningTopicCapsule();
206
 
207
        $select = $this->sql->select(self::_TABLE);
558 stevensc 208
        $select->where->equalTo('topic_id', $topic_id);
209
        $select->order('added_on DESC');
283 www 210
 
211
        return $this->executeFetchAllObject($select, $prototype);
212
    }
620 stevensc 213
 
214
    /**
215
     *
216
     * @param int $company_id
217
     * @param int $topic_id
218
     * @return MicrolearningTopicCapsule[]
219
     */
220
    public function fetchAllByCompanyIdAndTopicId($company_id, $topic_id)
221
    {
222
        $prototype = new MicrolearningTopicCapsule();
223
 
224
        $select = $this->sql->select(self::_TABLE);
225
        $select->where->equalTo('company_id', $company_id);
226
        $select->where->equalTo('topic_id', $topic_id);
227
        $select->order('added_on DESC');
228
 
229
        return $this->executeFetchAllObject($select, $prototype);
230
    }
283 www 231
 
232
    /**
233
     *
234
     * @param  int $topic_id
235
     * @return MicrolearningTopicCapsule[]
236
     */
237
    public function fetchAllActiveByTopicId($topic_id)
238
    {
239
        $prototype = new MicrolearningTopicCapsule();
240
 
241
        $select = $this->sql->select(self::_TABLE);
558 stevensc 242
        $select->where->equalTo('topic_id', $topic_id);
283 www 243
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
558 stevensc 244
        $select->order('added_on DESC');
283 www 245
 
246
        return $this->executeFetchAllObject($select, $prototype);
247
    }
649 stevensc 248
 
249
    /**
250
     *
251
     * @param  int $capsule_id
252
     * @return MicrolearningTopicCapsule[]
253
     */
254
    public function fetchAllByCapsuleId($capsule_id){
255
        $prototype = new MicrolearningTopicCapsule();
256
 
257
        $select = $this->sql->select(self::_TABLE);
258
        $select->where->equalTo('capsule_id', $capsule_id);
259
        $select->order('added_on DESC');
260
 
261
        return $this->executeFetchAllObject($select, $prototype);
262
    }
283 www 263
 
264
    /**
265
     *
266
     * @param  int $topic_id
267
     * @return MicrolearningTopicCapsule[]
268
     */
269
    public function fetchAllActiveAndPublishByTopicId($topic_id)
270
    {
271
        $prototype = new MicrolearningTopicCapsule();
272
 
273
        $select = $this->sql->select(self::_TABLE);
558 stevensc 274
        $select->where->equalTo('topic_id', $topic_id);
283 www 275
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
276
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
558 stevensc 277
        $select->order('added_on DESC');
283 www 278
 
279
        return $this->executeFetchAllObject($select, $prototype);
280
    }
281
 
282
 
283
    /**
284
     *
285
     * @param MicrolearningTopicCapsule $record
286
     * @return boolean
287
     */
288
    public function insert($record)
289
    {
290
        $hydrator = new ObjectPropertyHydrator();
291
        $values = $hydrator->extract($record);
292
        $values = $this->removeEmpty($values);
293
 
294
 
295
        $insert = $this->sql->insert(self::_TABLE);
296
        $insert->values($values);
297
 
298
        $result = $this->executeInsert($insert);
299
        if($result) {
300
            $record->id = $this->lastInsertId;
301
        }
302
        return $result;
303
    }
304
 
305
    /**
306
     *
307
     * @param MicrolearningTopicCapsule $record
308
     * @return boolean
309
     */
310
    public function update($record)
311
    {
312
        $hydrator = new ObjectPropertyHydrator();
313
        $values = $hydrator->extract($record);
314
        $values = $this->removeEmpty($values);
315
 
316
        $update = $this->sql->update(self::_TABLE);
317
        $update->set($values);
318
        $update->where->equalTo('id', $record->id);
319
 
320
        return $this->executeUpdate($update);
321
    }
322
 
323
    /**
324
     *
325
     * @param MicrolearningTopicCapsule $record
326
     * @return boolean
327
     */
328
    public function delete($record)
329
    {
330
        $delete = $this->sql->delete(self::_TABLE);
331
        $delete->where->equalTo('id', $record->id);
332
 
333
        return $this->executeDelete($delete);
334
    }
335
 
336
 
337
 
338
}