Proyectos de Subversion LeadersLinked - Services

Rev

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