Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 169 | Rev 543 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\ResultSet\HydratingResultSet;
9
use Laminas\Db\Sql\Expression;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Log\LoggerInterface;
12
use Laminas\Paginator\Paginator;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
use LeadersLinked\Model\Competency;
16
use LeadersLinked\Mapper\Common\MapperCommon;
17
use LeadersLinked\Model\CompetencyType;
18
 
19
 
20
 
21
class CompetencyMapper extends MapperCommon
22
{
23
    const _TABLE = 'tbl_competencies';
24
 
25
 
26
    /**
27
     *
28
     * @var CompetencyMapper
29
     */
30
    private static $_instance;
31
 
32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     */
36
    private function __construct($adapter)
37
    {
38
        parent::__construct($adapter);
39
    }
40
 
41
    /**
42
     *
43
     * @param AdapterInterface $adapter
44
     * @return CompetencyMapper
45
     */
46
    public static function getInstance($adapter)
47
    {
48
        if(self::$_instance == null) {
49
            self::$_instance = new CompetencyMapper($adapter);
50
        }
51
        return self::$_instance;
52
    }
53
 
54
 
55
    /**
56
     *
57
     * @param int $id
58
     * @return Competency
59
     */
60
    public function fetchOne($id)
61
    {
62
        $select = $this->sql->select(self::_TABLE);
63
        $select->where->equalTo('id', $id);
64
        $select->limit(1);
65
 
66
        $prototype = new Competency();
67
        return $this->executeFetchOneObject($select, $prototype);
68
    }
69
 
70
    /**
71
     *
72
     * @param string $uuid
73
     * @return Competency
74
     */
75
    public function fetchOneByUuid($uuid)
76
    {
77
        $select = $this->sql->select(self::_TABLE);
78
        $select->where->equalTo('uuid', $uuid);
79
        $select->limit(1);
80
 
81
        $prototype = new Competency();
82
        return $this->executeFetchOneObject($select, $prototype);
83
    }
84
 
85
    /**
86
     *
169 efrain 87
     * @param int $company_id
88
     * @param $competency_id_default
89
     * @return Competency
90
     */
91
    public function fetchOneByCompanyIdAndCompetencyIdDefault($company_id, $competency_id_default)
92
    {
93
        $select = $this->sql->select(self::_TABLE);
94
        $select->where->equalTo('company_id', $company_id);
95
        $select->where->equalTo('competency_id_default', $competency_id_default);
96
        $select->limit(1);
97
 
98
        $prototype = new Competency();
99
        return $this->executeFetchOneObject($select, $prototype);
100
    }
101
 
192 efrain 102
    /**
103
     *
104
     * @param int $company_id
105
     * @return Competency[]
106
     */
107
    public function fetchAllActiveByCompanyId($company_id)
108
    {
109
        $select = $this->sql->select(self::_TABLE);
110
        $select->where->equalTo('company_id', $company_id);
111
        $select->where->equalTo('status', Competency::STATUS_ACTIVE);
112
 
113
 
114
        $prototype = new Competency();
115
        return $this->executeFetchAllObject($select, $prototype);
116
    }
169 efrain 117
 
192 efrain 118
 
169 efrain 119
    /**
120
     *
121
     * @return Competency[]
122
     */
123
    public function fetchAllByDefault()
124
    {
125
        $select = $this->sql->select(self::_TABLE);
126
        $select->where->isNull('company_id');
127
 
128
 
129
        $prototype = new Competency();
130
        return $this->executeFetchAllObject($select, $prototype);
131
    }
132
 
133
    /**
134
     *
1 www 135
     * @param string $search
136
     * @param int $page
137
     * @param int $records_per_page
138
     * @param string $order_field
139
     * @param string $order_direction
140
     * @return Paginator
141
     */
142
    public function fetchAllDataTable($search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
143
    {
144
        $select = $this->sql->select();
145
        $select->columns(['id', 'uuid', 'name', 'status']);
146
        $select->from(['c' => self::_TABLE]);
147
        $select->join(['ct' => CompetencyTypeMapper::_TABLE], 'c.competency_type_id =  ct.id', ['type' => 'name']);
148
 
149
        if($search) {
150
            $select->where->like('name', '%' . $search . '%')->or->like('type', '%' . $search . '%');
151
        }
152
        $select->order($order_field . ' ' . $order_direction);
153
 
154
        $resultset  = new HydratingResultSet();
155
 
156
        $adapter = new DbSelect($select, $this->sql, $resultset);
157
        $paginator = new Paginator($adapter);
158
        $paginator->setItemCountPerPage($records_per_page);
159
        $paginator->setCurrentPageNumber($page);
160
 
161
 
162
        return $paginator;
163
    }
164
 
165
    /**
166
     *
169 efrain 167
     * @param int $company_id
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($company_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
176
    {
177
        $select = $this->sql->select();
178
        $select->columns(['id', 'uuid', 'name', 'status']);
179
        $select->from(['c' => self::_TABLE]);
180
        $select->join(['ct' => CompetencyTypeMapper::_TABLE], 'c.competency_type_id =  ct.id', ['type' => 'name']);
181
        $select->where->equalTo('c.company_id', $company_id);
182
 
183
 
184
        if($search) {
185
            $select->where->NEST->like('name', '%' . $search . '%')->or
186
            ->like('type', '%' . $search . '%')->UNNEST;
187
        }
188
        $select->order($order_field . ' ' . $order_direction);
189
 
190
        $resultset  = new HydratingResultSet();
191
 
192
        $adapter = new DbSelect($select, $this->sql, $resultset);
193
        $paginator = new Paginator($adapter);
194
        $paginator->setItemCountPerPage($records_per_page);
195
        $paginator->setCurrentPageNumber($page);
196
 
197
 
198
        return $paginator;
199
    }
200
 
201
    /**
202
     *
1 www 203
     * @param Competency $competency
204
     * @return boolean
205
     */
206
    public function insert($competency)
207
    {
208
        $hydrator = new ObjectPropertyHydrator();
209
        $values = $hydrator->extract($competency);
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
            $competency->id = $this->lastInsertId;
218
        }
219
 
220
        return $result;
221
 
222
    }
223
 
224
    /**
225
     *
226
     * @param Competency $competency
227
     * @return boolean
228
     */
229
    public function update($competency)
230
    {
231
        $hydrator = new ObjectPropertyHydrator();
232
        $values = $hydrator->extract($competency);
233
        $values = $this->removeEmpty($values);
234
 
235
        $update = $this->sql->update(self::_TABLE);
236
        $update->set($values);
237
        $update->where->equalTo('id', $competency->id);
238
 
239
        return $this->executeUpdate($update);
240
    }
241
 
242
    /**
243
     *
244
     * @param Competency $competency
245
     * @return boolean
246
     */
247
    public function delete($competency)
248
    {
249
        $delete = $this->sql->delete(self::_TABLE);
250
        $delete->where->equalTo('id', $competency->id);
251
 
252
        return $this->executeDelete($delete);
253
 
254
    }
255
 
256
 
257
 
258
 
259
    /**
260
     *
261
     * @return boolean
262
     */
263
    public function truncate()
264
    {
265
        $sql = 'DELETE FROM ' . self::_TABLE;
266
        if($this->executeSentenceWithParameters($sql)) {
267
            $sql = 'ALTER TABLE ' . self::_TABLE . ' AUTO_INCREMENT = 1 ';
268
            return $this->executeSentenceWithParameters($sql);
269
        }
270
        return false;
271
    }
272
 
273
 
274
 
275
}