Proyectos de Subversion LeadersLinked - Services

Rev

Rev 632 | Ir a la última revisión | | 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
    }
64
 
65
    /**
66
     *
67
     * @param int $company_id
68
     * @return int
69
     */
70
    public function fetchTotalCountAllActiveByCompanyId($company_id)
71
    {
72
        $select = $this->sql->select();
73
        $select->columns(['total' => new Expression('COUNT(*)')]);
74
        $select->from(self::_TABLE);
75
        $select->where->equalTo('company_id', $company_id);
76
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
77
 
78
        $record = $this->executeFetchOneArray($select);
79
        return $record['total'];
80
    }
81
 
82
    /**
83
     *
84
     * @param int $id
85
     * @return MicrolearningTopicCapsule
86
     */
87
    public function fetchOne($id)
88
    {
89
        $prototype = new MicrolearningTopicCapsule();
90
 
91
        $select = $this->sql->select(self::_TABLE);
92
        $select->where->equalTo('id', $id);
93
 
94
        return $this->executeFetchOneObject($select, $prototype);
95
    }
96
 
97
    /**
98
     *
99
     * @param int $topic_id
100
     * @param int $capsule_id
101
     * @return MicrolearningTopicCapsule
102
     */
103
    public function fetchOneByTopicIdAndCapsuleId($topic_id, $capsule_id)
104
    {
105
        $prototype = new MicrolearningTopicCapsule();
106
 
107
        $select = $this->sql->select(self::_TABLE);
108
        $select->where->equalTo('topic_id', $topic_id);
109
        $select->where->equalTo('capsule_id', $capsule_id);
110
 
111
        return $this->executeFetchOneObject($select, $prototype);
112
    }
618 stevensc 113
 
114
    /**
115
     * Deletes all topic-capsule relationships for a given topic ID.
116
     *
117
     * @param int $topic_id
118
     * @return boolean
119
     */
120
    public function deleteByTopicId($topic_id)
121
    {
122
        $delete = $this->sql->delete(self::_TABLE);
123
        $delete->where->equalTo('topic_id', $topic_id);
124
        return $this->executeDelete($delete);
125
    }
632 stevensc 126
 
127
    /**
128
     * Deletes all topic-capsule relationships for a given capsule ID.
129
     *
130
     * @param int $capsule_id
131
     * @return boolean
132
     */
133
    public function deleteAllByCapsuleId($capsule_id)
134
    {
135
        $delete = $this->sql->delete(self::_TABLE);
136
        $delete->where->equalTo('capsule_id', $capsule_id);
137
        return $this->executeDelete($delete);
138
    }
283 www 139
 
140
    /**
558 stevensc 141
     * Fetches a single topic-capsule relationship by capsule ID.
142
     * Since a capsule might theoretically be in multiple topics, this fetches the first one found.
143
     *
144
     * @param int $capsule_id
145
     * @return MicrolearningTopicCapsule|null
146
     */
147
    public function fetchOneByCapsuleId($capsule_id)
148
    {
149
        $prototype = new MicrolearningTopicCapsule();
150
 
151
        $select = $this->sql->select(self::_TABLE);
152
        $select->where->equalTo('capsule_id', $capsule_id);
153
        $select->limit(1); // Ensure only one record is fetched
154
 
155
        return $this->executeFetchOneObject($select, $prototype);
156
    }
157
 
158
    /**
283 www 159
     *
160
     * @param int $company_id
161
     * @param int $topic_id
162
     * @return int
163
     */
164
    public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
165
    {
166
 
167
 
168
        $select = $this->sql->select(self::_TABLE);
169
        $select->columns(['total' => new Expression('COUNT(*)')]);
170
        $select->where->equalTo('company_id', $company_id);
171
        $select->where->equalTo('topic_id', $topic_id);
172
 
173
        $record = $this->executeFetchOneArray($select);
174
        return $record['total'];
175
 
176
    }
177
 
178
 
179
 
180
 
181
    /**
182
     *
183
     * @param  int $topic_id
184
     * @return MicrolearningTopicCapsule[]
185
     */
186
    public function fetchAllByTopicId($topic_id)
187
    {
188
        $prototype = new MicrolearningTopicCapsule();
189
 
190
        $select = $this->sql->select(self::_TABLE);
558 stevensc 191
        $select->where->equalTo('topic_id', $topic_id);
192
        $select->order('added_on DESC');
283 www 193
 
194
        return $this->executeFetchAllObject($select, $prototype);
195
    }
620 stevensc 196
 
197
    /**
198
     *
199
     * @param int $company_id
200
     * @param int $topic_id
201
     * @return MicrolearningTopicCapsule[]
202
     */
203
    public function fetchAllByCompanyIdAndTopicId($company_id, $topic_id)
204
    {
205
        $prototype = new MicrolearningTopicCapsule();
206
 
207
        $select = $this->sql->select(self::_TABLE);
208
        $select->where->equalTo('company_id', $company_id);
209
        $select->where->equalTo('topic_id', $topic_id);
210
        $select->order('added_on DESC');
211
 
212
        return $this->executeFetchAllObject($select, $prototype);
213
    }
283 www 214
 
215
    /**
216
     *
217
     * @param  int $topic_id
218
     * @return MicrolearningTopicCapsule[]
219
     */
220
    public function fetchAllActiveByTopicId($topic_id)
221
    {
222
        $prototype = new MicrolearningTopicCapsule();
223
 
224
        $select = $this->sql->select(self::_TABLE);
558 stevensc 225
        $select->where->equalTo('topic_id', $topic_id);
283 www 226
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
558 stevensc 227
        $select->order('added_on DESC');
283 www 228
 
229
        return $this->executeFetchAllObject($select, $prototype);
230
    }
649 stevensc 231
 
232
    /**
233
     *
234
     * @param  int $capsule_id
235
     * @return MicrolearningTopicCapsule[]
236
     */
237
    public function fetchAllByCapsuleId($capsule_id){
238
        $prototype = new MicrolearningTopicCapsule();
239
 
240
        $select = $this->sql->select(self::_TABLE);
241
        $select->where->equalTo('capsule_id', $capsule_id);
242
        $select->order('added_on DESC');
243
 
244
        return $this->executeFetchAllObject($select, $prototype);
245
    }
283 www 246
 
247
    /**
248
     *
249
     * @param  int $topic_id
250
     * @return MicrolearningTopicCapsule[]
251
     */
252
    public function fetchAllActiveAndPublishByTopicId($topic_id)
253
    {
254
        $prototype = new MicrolearningTopicCapsule();
255
 
256
        $select = $this->sql->select(self::_TABLE);
558 stevensc 257
        $select->where->equalTo('topic_id', $topic_id);
283 www 258
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
259
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
558 stevensc 260
        $select->order('added_on DESC');
283 www 261
 
262
        return $this->executeFetchAllObject($select, $prototype);
263
    }
264
 
265
 
266
    /**
267
     *
268
     * @param MicrolearningTopicCapsule $record
269
     * @return boolean
270
     */
271
    public function insert($record)
272
    {
273
        $hydrator = new ObjectPropertyHydrator();
274
        $values = $hydrator->extract($record);
275
        $values = $this->removeEmpty($values);
276
 
277
 
278
        $insert = $this->sql->insert(self::_TABLE);
279
        $insert->values($values);
280
 
281
        $result = $this->executeInsert($insert);
282
        if($result) {
283
            $record->id = $this->lastInsertId;
284
        }
285
        return $result;
286
    }
287
 
288
    /**
289
     *
290
     * @param MicrolearningTopicCapsule $record
291
     * @return boolean
292
     */
293
    public function update($record)
294
    {
295
        $hydrator = new ObjectPropertyHydrator();
296
        $values = $hydrator->extract($record);
297
        $values = $this->removeEmpty($values);
298
 
299
        $update = $this->sql->update(self::_TABLE);
300
        $update->set($values);
301
        $update->where->equalTo('id', $record->id);
302
 
303
        return $this->executeUpdate($update);
304
    }
305
 
306
    /**
307
     *
308
     * @param MicrolearningTopicCapsule $record
309
     * @return boolean
310
     */
311
    public function delete($record)
312
    {
313
        $delete = $this->sql->delete(self::_TABLE);
314
        $delete->where->equalTo('id', $record->id);
315
 
316
        return $this->executeDelete($delete);
317
    }
318
 
319
 
320
 
321
}