Proyectos de Subversion LeadersLinked - Services

Rev

Rev 558 | Ir a la última revisión | | 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
{
18
    const _TABLE = 'tbl_microlearning_capsules';
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
    /**
115
     *
116
     * @param int $company_id
117
     * @param int $topic_id
118
     * @return int
119
     */
120
    public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
121
    {
122
 
123
 
124
        $select = $this->sql->select(self::_TABLE);
125
        $select->columns(['total' => new Expression('COUNT(*)')]);
126
        $select->where->equalTo('company_id', $company_id);
127
        $select->where->equalTo('topic_id', $topic_id);
128
 
129
        $record = $this->executeFetchOneArray($select);
130
        return $record['total'];
131
 
132
    }
133
 
134
 
135
 
136
 
137
    /**
138
     *
139
     * @param  int $topic_id
140
     * @return MicrolearningTopicCapsule[]
141
     */
142
    public function fetchAllByTopicId($topic_id)
143
    {
144
        $prototype = new MicrolearningTopicCapsule();
145
 
146
        $select = $this->sql->select(self::_TABLE);
147
        $select->where->equalTo('topic_id', 'topic_id');
148
        $select->where->order('added_on DESC');
149
 
150
        return $this->executeFetchAllObject($select, $prototype);
151
    }
152
 
153
    /**
154
     *
155
     * @param  int $topic_id
156
     * @return MicrolearningTopicCapsule[]
157
     */
158
    public function fetchAllActiveByTopicId($topic_id)
159
    {
160
        $prototype = new MicrolearningTopicCapsule();
161
 
162
        $select = $this->sql->select(self::_TABLE);
163
        $select->where->equalTo('topic_id', 'topic_id');
164
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
165
        $select->where->order('added_on DESC');
166
 
167
        return $this->executeFetchAllObject($select, $prototype);
168
    }
169
 
170
    /**
171
     *
172
     * @param  int $topic_id
173
     * @return MicrolearningTopicCapsule[]
174
     */
175
    public function fetchAllActiveAndPublishByTopicId($topic_id)
176
    {
177
        $prototype = new MicrolearningTopicCapsule();
178
 
179
        $select = $this->sql->select(self::_TABLE);
180
        $select->where->equalTo('topic_id', 'topic_id');
181
        $select->where->equalTo('status', MicrolearningTopicCapsule::STATUS_ACTIVE);
182
        $select->where->greaterThanOrEqualTo('publish_on', new Expression('CURRENT_DATE()'));
183
        $select->where->order('added_on DESC');
184
 
185
        return $this->executeFetchAllObject($select, $prototype);
186
    }
187
 
188
 
189
    /**
190
     *
191
     * @param MicrolearningTopicCapsule $record
192
     * @return boolean
193
     */
194
    public function insert($record)
195
    {
196
        $hydrator = new ObjectPropertyHydrator();
197
        $values = $hydrator->extract($record);
198
        $values = $this->removeEmpty($values);
199
 
200
 
201
        $insert = $this->sql->insert(self::_TABLE);
202
        $insert->values($values);
203
 
204
        $result = $this->executeInsert($insert);
205
        if($result) {
206
            $record->id = $this->lastInsertId;
207
        }
208
        return $result;
209
    }
210
 
211
    /**
212
     *
213
     * @param MicrolearningTopicCapsule $record
214
     * @return boolean
215
     */
216
    public function update($record)
217
    {
218
        $hydrator = new ObjectPropertyHydrator();
219
        $values = $hydrator->extract($record);
220
        $values = $this->removeEmpty($values);
221
 
222
        $update = $this->sql->update(self::_TABLE);
223
        $update->set($values);
224
        $update->where->equalTo('id', $record->id);
225
 
226
        return $this->executeUpdate($update);
227
    }
228
 
229
    /**
230
     *
231
     * @param MicrolearningTopicCapsule $record
232
     * @return boolean
233
     */
234
    public function delete($record)
235
    {
236
        $delete = $this->sql->delete(self::_TABLE);
237
        $delete->where->equalTo('id', $record->id);
238
 
239
        return $this->executeDelete($delete);
240
    }
241
 
242
 
243
 
244
}