Proyectos de Subversion LeadersLinked - Services

Rev

Rev 558 | Rev 620 | 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
    }
183
 
184
    /**
185
     *
186
     * @param  int $topic_id
187
     * @return MicrolearningTopicCapsule[]
188
     */
189
    public function fetchAllActiveByTopicId($topic_id)
190
    {
191
        $prototype = new MicrolearningTopicCapsule();
192
 
193
        $select = $this->sql->select(self::_TABLE);
558 stevensc 194
        $select->where->equalTo('topic_id', $topic_id);
283 www 195
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
558 stevensc 196
        $select->order('added_on DESC');
283 www 197
 
198
        return $this->executeFetchAllObject($select, $prototype);
199
    }
200
 
201
    /**
202
     *
203
     * @param  int $topic_id
204
     * @return MicrolearningTopicCapsule[]
205
     */
206
    public function fetchAllActiveAndPublishByTopicId($topic_id)
207
    {
208
        $prototype = new MicrolearningTopicCapsule();
209
 
210
        $select = $this->sql->select(self::_TABLE);
558 stevensc 211
        $select->where->equalTo('topic_id', $topic_id);
283 www 212
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
213
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
558 stevensc 214
        $select->order('added_on DESC');
283 www 215
 
216
        return $this->executeFetchAllObject($select, $prototype);
217
    }
218
 
219
 
220
    /**
221
     *
222
     * @param MicrolearningTopicCapsule $record
223
     * @return boolean
224
     */
225
    public function insert($record)
226
    {
227
        $hydrator = new ObjectPropertyHydrator();
228
        $values = $hydrator->extract($record);
229
        $values = $this->removeEmpty($values);
230
 
231
 
232
        $insert = $this->sql->insert(self::_TABLE);
233
        $insert->values($values);
234
 
235
        $result = $this->executeInsert($insert);
236
        if($result) {
237
            $record->id = $this->lastInsertId;
238
        }
239
        return $result;
240
    }
241
 
242
    /**
243
     *
244
     * @param MicrolearningTopicCapsule $record
245
     * @return boolean
246
     */
247
    public function update($record)
248
    {
249
        $hydrator = new ObjectPropertyHydrator();
250
        $values = $hydrator->extract($record);
251
        $values = $this->removeEmpty($values);
252
 
253
        $update = $this->sql->update(self::_TABLE);
254
        $update->set($values);
255
        $update->where->equalTo('id', $record->id);
256
 
257
        return $this->executeUpdate($update);
258
    }
259
 
260
    /**
261
     *
262
     * @param MicrolearningTopicCapsule $record
263
     * @return boolean
264
     */
265
    public function delete($record)
266
    {
267
        $delete = $this->sql->delete(self::_TABLE);
268
        $delete->where->equalTo('id', $record->id);
269
 
270
        return $this->executeDelete($delete);
271
    }
272
 
273
 
274
 
275
}