Proyectos de Subversion LeadersLinked - Services

Rev

| 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\MicrolearningTopic;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\ResultSet\HydratingResultSet;
11
use Laminas\Paginator\Adapter\DbSelect;
12
use Laminas\Paginator\Paginator;
13
 
14
class MicrolearningTopicMapper extends MapperCommon
15
{
16
    const _TABLE = 'tbl_microlearning_topics';
17
 
18
    /**
19
     *
20
     * @var MicrolearningTopicMapper
21
     */
22
    private static $_instance;
23
 
24
    /**
25
     *
26
     * @param AdapterInterface $adapter
27
     */
28
    private function __construct($adapter)
29
    {
30
        parent::__construct($adapter);
31
    }
32
 
33
    /**
34
     *
35
     * @param AdapterInterface $adapter
36
     * @return MicrolearningTopicMapper
37
     */
38
    public static function getInstance($adapter)
39
    {
40
        if(self::$_instance == null) {
41
            self::$_instance = new MicrolearningTopicMapper($adapter);
42
        }
43
        return self::$_instance;
44
    }
45
 
46
    /**
47
     *
48
     * @param int $id
49
     * @return MicrolearningTopic
50
     */
51
    public function fetchOne($id)
52
    {
53
        $prototype = new MicrolearningTopic();
54
 
55
        $select = $this->sql->select(self::_TABLE);
56
        $select->where->equalTo('id', $id);
57
 
58
        return $this->executeFetchOneObject($select, $prototype);
59
    }
60
 
61
    /**
62
     *
63
     * @param int $uuid
64
     * @return MicrolearningTopic
65
     */
66
    public function fetchOneByUuid($uuid)
67
    {
68
        $prototype = new MicrolearningTopic;
69
        $select = $this->sql->select(self::_TABLE);
70
        $select->where->equalTo('uuid', $uuid);
71
 
72
        return $this->executeFetchOneObject($select, $prototype);
73
    }
74
 
75
 
76
 
77
    /**
78
     *
79
     * @param int $company_id
80
     * @param int[] $ids
81
     * @return MicrolearningTopic[]
82
     */
83
    public function fetchAllActiveByCompanyIdAndIds($company_id, $ids)
84
    {
85
        $ids = empty($ids) ? [0] : $ids;
86
 
87
 
88
        $prototype = new MicrolearningTopic();
89
 
90
        $select = $this->sql->select(self::_TABLE);
91
        $select->where->equalTo('company_id', $company_id);
92
        $select->where->in('id', $ids);
93
        $select->where->equalTo('status', MicrolearningTopic::STATUS_ACTIVE);
94
        $select->order(['order', 'name']);
95
 
96
        return $this->executeFetchAllObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $company_id
102
     * @return MicrolearningTopic[]
103
     */
104
    public function fetchAllActiveByCompanyId($company_id)
105
    {
106
        $prototype = new MicrolearningTopic();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('company_id', $company_id);
110
        $select->where->equalTo('status', MicrolearningTopic::STATUS_ACTIVE);
111
        $select->order(['order', 'name']);
112
 
113
        return $this->executeFetchAllObject($select, $prototype);
114
    }
115
 
116
    /**
117
     *
118
     * @param int $company_id
119
     * @return MicrolearningTopic[]
120
     */
121
    public function fetchAllByCompanyId($company_id)
122
    {
123
        $prototype = new MicrolearningTopic();
124
 
125
        $select = $this->sql->select(self::_TABLE);
126
        $select->where->equalTo('company_id', $company_id);
127
        $select->order(['order', 'name']);
128
 
129
        return $this->executeFetchAllObject($select, $prototype);
130
    }
131
 
132
 
133
    /**
134
     *
135
     * @return MicrolearningTopic[]
136
     */
137
    public function fetchAllActive()
138
    {
139
        $prototype = new MicrolearningTopic();
140
 
141
        $select = $this->sql->select(self::_TABLE);
142
        $select->where->equalTo('status', MicrolearningTopic::STATUS_ACTIVE);
143
        $select->order(['order', 'name']);
144
 
145
        return $this->executeFetchAllObject($select, $prototype);
146
    }
147
 
148
 
149
    /**
150
     *
151
     * @return MicrolearningTopic[]
152
     */
153
    public function fetchAll()
154
    {
155
        $prototype = new MicrolearningTopic();
156
 
157
        $select = $this->sql->select(self::_TABLE);
158
        $select->order(['order', 'name']);
159
 
160
        return $this->executeFetchAllObject($select, $prototype);
161
    }
162
 
163
 
164
 
165
    /**
166
     *
167
     * @param int $companyId
168
     * @param string $search
169
     * @param int $page
170
     * @param int $records_per_page
171
     * @param string $order_field
172
     * @param string $order_direction
173
     * @return Paginator
174
     */
175
    public function fetchAllDataTableByCompanyId($companyId, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
176
    {
177
        $prototype = new MicrolearningTopic();
178
        $select = $this->sql->select(self::_TABLE);
179
        $select->where->equalTo('company_id', $companyId);
180
 
181
        if($search) {
182
            $select->where->like('name', '%' . $search . '%');
183
        }
184
        $select->order($order_field . ' ' . $order_direction);
185
 
186
        //echo $select->getSqlString($this->adapter->platform); exit;
187
 
188
        $hydrator   = new ObjectPropertyHydrator();
189
        $resultset  = new HydratingResultSet($hydrator, $prototype);
190
 
191
        $adapter = new DbSelect($select, $this->sql, $resultset);
192
        $paginator = new Paginator($adapter);
193
        $paginator->setItemCountPerPage($records_per_page);
194
        $paginator->setCurrentPageNumber($page);
195
 
196
 
197
        return $paginator;
198
    }
199
 
200
 
201
    /**
202
     *
203
     * @param MicrolearningTopic $topic
204
     * @return boolean
205
     */
206
    public function insert($topic)
207
    {
208
        $hydrator = new ObjectPropertyHydrator();
209
        $values = $hydrator->extract($topic);
210
        $values = $this->removeEmpty($values);
211
 
212
        $insert = $this->sql->insert(self::_TABLE);
213
        $insert->values($values);
214
 
215
        $result = $this->executeInsert($insert);
216
        if($result) {
217
            $topic->id = $this->lastInsertId;
218
        }
219
 
220
 
221
 
222
        return $result;
223
    }
224
 
225
    /**
226
     *
227
     * @param MicrolearningTopic $topic
228
     * @return boolean
229
     */
230
    public function update($topic)
231
    {
232
        $hydrator = new ObjectPropertyHydrator();
233
        $values = $hydrator->extract($topic);
234
        $values = $this->removeEmpty($values);
235
 
236
        $update = $this->sql->update(self::_TABLE);
237
        $update->set($values);
238
        $update->where->equalTo('id', $topic->id);
239
 
240
 
241
        return $this->executeUpdate($update);
242
    }
243
 
244
    /**
245
     *
246
     * @param MicrolearningTopic $topic
247
     * @return boolean
248
     */
249
    public function delete($topic)
250
    {
251
        $delete = $this->sql->delete(self::_TABLE);
252
        $delete->where->equalTo('id', $topic->id);
253
 
254
        return $this->executeDelete($delete);
255
    }
256
 
257
 
258
}