Proyectos de Subversion LeadersLinked - Services

Rev

Rev 618 | 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
    }
283 www 126
 
127
    /**
558 stevensc 128
     * Fetches a single topic-capsule relationship by capsule ID.
129
     * Since a capsule might theoretically be in multiple topics, this fetches the first one found.
130
     *
131
     * @param int $capsule_id
132
     * @return MicrolearningTopicCapsule|null
133
     */
134
    public function fetchOneByCapsuleId($capsule_id)
135
    {
136
        $prototype = new MicrolearningTopicCapsule();
137
 
138
        $select = $this->sql->select(self::_TABLE);
139
        $select->where->equalTo('capsule_id', $capsule_id);
140
        $select->limit(1); // Ensure only one record is fetched
141
 
142
        return $this->executeFetchOneObject($select, $prototype);
143
    }
144
 
145
    /**
283 www 146
     *
147
     * @param int $company_id
148
     * @param int $topic_id
149
     * @return int
150
     */
151
    public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
152
    {
153
 
154
 
155
        $select = $this->sql->select(self::_TABLE);
156
        $select->columns(['total' => new Expression('COUNT(*)')]);
157
        $select->where->equalTo('company_id', $company_id);
158
        $select->where->equalTo('topic_id', $topic_id);
159
 
160
        $record = $this->executeFetchOneArray($select);
161
        return $record['total'];
162
 
163
    }
164
 
165
 
166
 
167
 
168
    /**
169
     *
170
     * @param  int $topic_id
171
     * @return MicrolearningTopicCapsule[]
172
     */
173
    public function fetchAllByTopicId($topic_id)
174
    {
175
        $prototype = new MicrolearningTopicCapsule();
176
 
177
        $select = $this->sql->select(self::_TABLE);
558 stevensc 178
        $select->where->equalTo('topic_id', $topic_id);
179
        $select->order('added_on DESC');
283 www 180
 
181
        return $this->executeFetchAllObject($select, $prototype);
182
    }
620 stevensc 183
 
184
    /**
185
     *
186
     * @param int $company_id
187
     * @param int $topic_id
188
     * @return MicrolearningTopicCapsule[]
189
     */
190
    public function fetchAllByCompanyIdAndTopicId($company_id, $topic_id)
191
    {
192
        $prototype = new MicrolearningTopicCapsule();
193
 
194
        $select = $this->sql->select(self::_TABLE);
195
        $select->where->equalTo('company_id', $company_id);
196
        $select->where->equalTo('topic_id', $topic_id);
197
        $select->order('added_on DESC');
198
 
199
        return $this->executeFetchAllObject($select, $prototype);
200
    }
283 www 201
 
202
    /**
203
     *
204
     * @param  int $topic_id
205
     * @return MicrolearningTopicCapsule[]
206
     */
207
    public function fetchAllActiveByTopicId($topic_id)
208
    {
209
        $prototype = new MicrolearningTopicCapsule();
210
 
211
        $select = $this->sql->select(self::_TABLE);
558 stevensc 212
        $select->where->equalTo('topic_id', $topic_id);
283 www 213
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
558 stevensc 214
        $select->order('added_on DESC');
283 www 215
 
216
        return $this->executeFetchAllObject($select, $prototype);
217
    }
218
 
219
    /**
220
     *
221
     * @param  int $topic_id
222
     * @return MicrolearningTopicCapsule[]
223
     */
224
    public function fetchAllActiveAndPublishByTopicId($topic_id)
225
    {
226
        $prototype = new MicrolearningTopicCapsule();
227
 
228
        $select = $this->sql->select(self::_TABLE);
558 stevensc 229
        $select->where->equalTo('topic_id', $topic_id);
283 www 230
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
231
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
558 stevensc 232
        $select->order('added_on DESC');
283 www 233
 
234
        return $this->executeFetchAllObject($select, $prototype);
235
    }
236
 
237
 
238
    /**
239
     *
240
     * @param MicrolearningTopicCapsule $record
241
     * @return boolean
242
     */
243
    public function insert($record)
244
    {
245
        $hydrator = new ObjectPropertyHydrator();
246
        $values = $hydrator->extract($record);
247
        $values = $this->removeEmpty($values);
248
 
249
 
250
        $insert = $this->sql->insert(self::_TABLE);
251
        $insert->values($values);
252
 
253
        $result = $this->executeInsert($insert);
254
        if($result) {
255
            $record->id = $this->lastInsertId;
256
        }
257
        return $result;
258
    }
259
 
260
    /**
261
     *
262
     * @param MicrolearningTopicCapsule $record
263
     * @return boolean
264
     */
265
    public function update($record)
266
    {
267
        $hydrator = new ObjectPropertyHydrator();
268
        $values = $hydrator->extract($record);
269
        $values = $this->removeEmpty($values);
270
 
271
        $update = $this->sql->update(self::_TABLE);
272
        $update->set($values);
273
        $update->where->equalTo('id', $record->id);
274
 
275
        return $this->executeUpdate($update);
276
    }
277
 
278
    /**
279
     *
280
     * @param MicrolearningTopicCapsule $record
281
     * @return boolean
282
     */
283
    public function delete($record)
284
    {
285
        $delete = $this->sql->delete(self::_TABLE);
286
        $delete->where->equalTo('id', $record->id);
287
 
288
        return $this->executeDelete($delete);
289
    }
290
 
291
 
292
 
293
}