Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 112 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 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\CompanyMicrolearningCapsule;
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 CompanyMicrolearningCapsuleMapper extends MapperCommon
17
{
18
    const _TABLE = 'tbl_company_microlearning_capsules';
19
 
20
    /**
21
     *
22
     * @var CompanyMicrolearningCapsuleMapper
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 CompanyMicrolearningCapsuleMapper
38
     */
39
    public static function getInstance($adapter)
40
    {
41
        if(self::$_instance == null) {
42
            self::$_instance = new CompanyMicrolearningCapsuleMapper($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', CompanyMicrolearningCapsule::STATUS_ACTIVE);
77
 
78
        $record = $this->executeFetchOneArray($select);
79
        return $record['total'];
80
    }
81
 
82
    /**
83
     *
84
     * @param int $id
85
     * @return CompanyMicrolearningCapsule
86
     */
87
    public function fetchOne($id)
88
    {
89
        $prototype = new CompanyMicrolearningCapsule();
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 $uuid
100
     * @return CompanyMicrolearningCapsule
101
     */
102
    public function fetchOneByUuid($uuid)
103
    {
104
        $prototype = new CompanyMicrolearningCapsule;
105
        $select = $this->sql->select(self::_TABLE);
106
        $select->where->equalTo('uuid', $uuid);
107
 
108
        return $this->executeFetchOneObject($select, $prototype);
109
    }
110
 
111
    /**
112
     *
113
     * @param int $company_id
114
     * @param int $topic_id
115
     * @return CompanyMicrolearningCapsule[]
116
     */
117
    public function fetchAllActiveByCompanyIdAndTopicId($company_id, $topic_id)
118
    {
119
        $prototype = new CompanyMicrolearningCapsule();
120
 
121
        $select = $this->sql->select(self::_TABLE);
122
        $select->where->equalTo('company_id', $company_id);
123
        $select->where->equalTo('topic_id', $topic_id);
124
        $select->where->equalTo('status', CompanyMicrolearningCapsule::STATUS_ACTIVE);
125
        $select->order(['order', 'name']);
126
 
127
        return $this->executeFetchAllObject($select, $prototype);
128
    }
129
 
130
    /**
131
     *
132
     * @param int $company_id
133
     * @param int $topic_id
134
     * @return CompanyMicrolearningCapsule[]
135
     */
136
    public function fetchAllByCompanyIdAndTopicId($company_id, $topic_id)
137
    {
138
        $prototype = new CompanyMicrolearningCapsule();
139
 
140
        $select = $this->sql->select(self::_TABLE);
141
        $select->where->equalTo('company_id', $company_id);
142
        $select->where->equalTo('topic_id', $topic_id);
143
        $select->order(['order', 'name']);
144
 
145
        //echo $select->getSqlString($this->adapter->platform); exit;
146
 
147
        return $this->executeFetchAllObject($select, $prototype);
148
    }
149
 
150
 
151
    /**
152
     *
153
 
154
     * @return CompanyMicrolearningCapsule[]
155
     */
156
    public function fetchAllActiveAndPublic()
157
    {
158
        $prototype = new CompanyMicrolearningCapsule();
159
 
160
        $select = $this->sql->select(self::_TABLE);
161
 
162
        $select->where->equalTo('status', CompanyMicrolearningCapsule::STATUS_ACTIVE);
163
        $select->where->equalTo('privacy', CompanyMicrolearningCapsule::PRIVACY_PUBLIC);
164
        $select->where->equalTo('type', CompanyMicrolearningCapsule::TYPE_FREE);
165
 
166
        $select->order(['order', 'name']);
167
 
168
        return $this->executeFetchAllObject($select, $prototype);
169
    }
170
 
171
 
172
    /**
173
     *
174
     * @param int $companyId
175
     * @param int $topicId
176
     * @param string $search
177
     * @param int $page
178
     * @param int $records_per_page
179
     * @param string $order_field
180
     * @param string $order_direction
181
     * @return Paginator
182
     */
183
    public function fetchAllDataTableByCompanyIdAndTopicId($companyId, $topicId, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
184
    {
185
        $prototype = new CompanyMicrolearningCapsule();
186
        $select = $this->sql->select(self::_TABLE);
187
        $select->where->equalTo('company_id', $companyId);
188
        $select->where->equalTo('topic_id', $topicId);
189
 
190
        if($search) {
191
            $select->where->like('name', '%' . $search . '%');
192
        }
193
        $select->order($order_field . ' ' . $order_direction);
194
 
195
        //echo $select->getSqlString($this->adapter->platform); exit;
196
 
197
        $hydrator   = new ObjectPropertyHydrator();
198
        $resultset  = new HydratingResultSet($hydrator, $prototype);
199
 
200
        $adapter = new DbSelect($select, $this->sql, $resultset);
201
        $paginator = new Paginator($adapter);
202
        $paginator->setItemCountPerPage($records_per_page);
203
        $paginator->setCurrentPageNumber($page);
204
 
205
 
206
        return $paginator;
207
    }
208
 
209
 
210
    /**
211
     *
212
     * @param CompanyMicrolearningCapsule $capsule
213
     * @return boolean
214
     */
215
    public function insert($capsule)
216
    {
217
        $hydrator = new ObjectPropertyHydrator();
218
        $values = $hydrator->extract($capsule);
219
        $values = $this->removeEmpty($values);
220
        $values['cost'] = isset($values['cost']) ? $values['cost'] : 0;
221
 
222
 
223
        $insert = $this->sql->insert(self::_TABLE);
224
        $insert->values($values);
225
 
226
        $result = $this->executeInsert($insert);
227
        if($result) {
228
            $capsule->id = $this->lastInsertId;
229
        }
230
        return $result;
231
    }
232
 
233
    /**
234
     *
235
     * @param CompanyMicrolearningCapsule $capsule
236
     * @return boolean
237
     */
238
    public function update($capsule)
239
    {
240
        $hydrator = new ObjectPropertyHydrator();
241
        $values = $hydrator->extract($capsule);
242
        $values = $this->removeEmpty($values);
243
        $values['cost'] = isset($values['cost']) ? $values['cost'] : 0;
244
 
245
        $update = $this->sql->update(self::_TABLE);
246
        $update->set($values);
247
        $update->where->equalTo('id', $capsule->id);
248
 
249
        return $this->executeUpdate($update);
250
    }
251
 
252
    /**
253
     *
254
     * @param CompanyMicrolearningCapsule $capsule
255
     * @return boolean
256
     */
257
    public function delete($capsule)
258
    {
259
        $delete = $this->sql->delete(self::_TABLE);
260
        $delete->where->equalTo('id', $capsule->id);
261
 
262
        return $this->executeDelete($delete);
263
    }
264
 
265
 
266
 
267
}