Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3454 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
630 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9
use LeadersLinked\Model\Behavior;
10
use LeadersLinked\Mapper\Common\MapperCommon;
3186 efrain 11
use Laminas\Db\ResultSet\HydratingResultSet;
12
use Laminas\Paginator\Paginator;
13
use Laminas\Paginator\Adapter\DbSelect;
630 efrain 14
 
15
class BehaviorMapper extends MapperCommon {
16
 
17
    const _TABLE = 'tbl_behaviors';
18
 
19
    /**
20
     *
21
     * @var BehaviorMapper
22
     */
23
    private static $_instance;
24
 
25
    /**
26
     *
27
     * @param AdapterInterface $adapter
28
     */
29
    private function __construct($adapter) {
30
        parent::__construct($adapter);
31
    }
32
 
33
    /**
34
     *
35
     * @param AdapterInterface $adapter
36
     * @return BehaviorMapper
37
     */
38
    public static function getInstance($adapter) {
39
        if (self::$_instance == null) {
40
            self::$_instance = new BehaviorMapper($adapter);
41
        }
42
        return self::$_instance;
43
    }
44
 
45
    /**
46
     *
47
     * @param int $id
48
     * @return Behavior
49
     */
50
    public function fetchOne($id) {
51
        $select = $this->sql->select(self::_TABLE);
52
        $select->where->equalTo('id', $id);
53
        $select->limit(1);
54
 
55
        $prototype = new Behavior();
56
        return $this->executeFetchOneObject($select, $prototype);
57
    }
58
 
59
    /**
60
     *
61
     * @param int $company_id
3186 efrain 62
     * @param int $behavior_id_default
630 efrain 63
     * @return Behavior
64
     */
3186 efrain 65
    public function fetchOneByCompanyIdAndIdDefault($company_id, $behavior_id_default) {
630 efrain 66
        $select = $this->sql->select(self::_TABLE);
67
        $select->where->equalTo('company_id', $company_id);
3186 efrain 68
        $select->where->equalTo('behavior_id_default', $behavior_id_default);
630 efrain 69
        $select->limit(1);
70
 
71
        $prototype = new Behavior();
72
        return $this->executeFetchOneObject($select, $prototype);
73
    }
74
 
75
    /**
76
     *
77
     * @param int $company_id
78
     * @param int $behavior_type_id_default
79
     * @return Behavior[]
80
     */
81
    public function fetchAllCompanyId($company_id) {
82
        $select = $this->sql->select(self::_TABLE);
83
        $select->where->equalTo('company_id', $company_id);
84
 
85
        $prototype = new Behavior();
86
        return $this->executeFetchAllObject($select, $prototype);
87
    }
88
 
89
    /**
90
     *
91
     * @return Behavior[]
92
     */
93
    public function fetchAllByDefault() {
94
        $select = $this->sql->select(self::_TABLE);
95
        $select->where->isNull('company_id');
96
 
97
        $prototype = new Behavior();
98
        return $this->executeFetchAllObject($select, $prototype);
99
    }
3186 efrain 100
 
101
    /**
102
     *
103
     * @param int $company_id
104
     * @param int $behavior_type_id_default
105
     * @return Behavior[]
106
     */
107
    public function fetchAllCompanyIdOnlyActives($company_id) {
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('company_id', $company_id);
110
        $select->where->equalTo('status', Behavior::STATUS_ACTIVE);
111
 
112
        $prototype = new Behavior();
113
        return $this->executeFetchAllObject($select, $prototype);
114
    }
115
 
116
    /**
117
     *
118
     * @return Behavior[]
119
     */
120
    public function fetchAllByDefaultOnlyActives() {
121
        $select = $this->sql->select(self::_TABLE);
122
        $select->where->isNull('company_id');
123
        $select->where->equalTo('status', Behavior::STATUS_ACTIVE);
124
 
125
        $prototype = new Behavior();
126
        return $this->executeFetchAllObject($select, $prototype);
127
    }
630 efrain 128
 
129
    /**
130
     *
131
     * @param string $uuid
132
     * @return Behavior
133
     */
134
    public function fetchOneByUuid($uuid) {
135
        $select = $this->sql->select(self::_TABLE);
136
        $select->where->equalTo('uuid', $uuid);
137
        $select->limit(1);
138
 
139
        $prototype = new Behavior();
140
        return $this->executeFetchOneObject($select, $prototype);
141
    }
142
 
143
     /**
144
     *
145
     * @param string $description
146
     * @param string $company_id
147
     * @return Behavior
148
     */
149
    public function fetchOneByDescription($description, $company_id) {
150
        $select = $this->sql->select(self::_TABLE);
151
        $select->where->equalTo('description', $description);
152
        $company_id ?
153
        $select->where->equalTo('company_id', $company_id):
154
         $select->where->isNull('company_id');
155
        $select->limit(1);
156
 
157
        $prototype = new Behavior();
158
        return $this->executeFetchOneObject($select, $prototype);
159
    }
160
 
161
 
162
 
163
    /**
164
     *
3186 efrain 165
     * @param Behavior $behavior
630 efrain 166
     * @return boolean
167
     */
3186 efrain 168
    public function insert($behavior) {
630 efrain 169
        $hydrator = new ObjectPropertyHydrator();
3186 efrain 170
        $values = $hydrator->extract($behavior);
630 efrain 171
 
172
        $insert = $this->sql->insert(self::_TABLE);
173
        $insert->values($values);
174
 
175
 
176
        $result = $this->executeInsert($insert);
177
        if ($result) {
3186 efrain 178
            $behavior->id = $this->lastInsertId;
630 efrain 179
        }
180
 
181
        return $result;
182
    }
183
 
184
    /**
185
     *
3186 efrain 186
     * @param Behavior $behavior
630 efrain 187
     * @return boolean
188
     */
3186 efrain 189
    public function update($behavior) {
630 efrain 190
        $hydrator = new ObjectPropertyHydrator();
3186 efrain 191
        $values = $hydrator->extract($behavior);
630 efrain 192
 
193
        $update = $this->sql->update(self::_TABLE);
194
        $update->set($values);
3186 efrain 195
        $update->where->equalTo('id', $behavior->id);
630 efrain 196
 
197
        return $this->executeUpdate($update);
198
    }
199
 
200
    /**
201
     *
3186 efrain 202
     * @param Behavior $behavior
630 efrain 203
     * @return boolean
204
     */
2827 kerby 205
    public function delete($id) {
206
        $delete = $this->sql->delete(self::_TABLE);
207
        $delete->where->equalTo('id', $id);
630 efrain 208
 
2827 kerby 209
        return $this->executeDelete($delete);
630 efrain 210
    }
211
 
212
    /**
213
     *
214
     * @return boolean
215
     */
216
    public function truncate() {
217
        $sql = 'DELETE FROM ' . self::_TABLE;
218
        if ($this->executeSentenceWithParameters($sql)) {
219
            $sql = 'ALTER TABLE ' . self::_TABLE . ' AUTO_INCREMENT = 1 ';
220
            return $this->executeSentenceWithParameters($sql);
221
        }
222
        return false;
223
    }
224
 
225
    /**
226
     *
227
     * @return Behavior[]
228
     */
3454 efrain 229
    public function fetchAllActive() {
630 efrain 230
        $prototype = new Behavior();
231
        $select = $this->sql->select(self::_TABLE);
232
        $select->where->equalTo('status', Behavior::STATUS_ACTIVE);
233
        $select->order('name ASC');
234
 
235
        return $this->executeFetchAllObject($select, $prototype);
236
    }
237
 
238
    /**
239
     *
240
     * @return Behavior[]
241
     */
3454 efrain 242
    public function fetchAllActiveByDefault() {
630 efrain 243
        $prototype = new Behavior();
244
        $select = $this->sql->select(self::_TABLE);
245
        $select->where->equalTo('status', Behavior::STATUS_ACTIVE);
246
        $select->where->isNull('company_id');
3254 efrain 247
        $select->order('description ASC');
630 efrain 248
 
249
        return $this->executeFetchAllObject($select, $prototype);
250
    }
251
 
252
    /**
253
     *
254
     * @return Behavior[]
255
     */
3454 efrain 256
    public function fetchAllActiveByCompanyId($company_id) {
630 efrain 257
        $prototype = new Behavior();
258
        $select = $this->sql->select(self::_TABLE);
259
        $select->where->equalTo('company_id', $company_id);
260
        $select->where->equalTo('status', Behavior::STATUS_ACTIVE);
3254 efrain 261
        $select->order('description ASC');
630 efrain 262
 
263
        return $this->executeFetchAllObject($select, $prototype);
264
    }
265
 
266
    /**
267
     *
268
     * @return Behavior[]
269
     */
270
    public function fetchAllByCompanyId($company_id) {
271
        $prototype = new Behavior();
272
        $select = $this->sql->select(self::_TABLE);
273
        $select->where->equalTo('company_id', $company_id);
3254 efrain 274
        $select->order('description ASC');
630 efrain 275
 
276
        return $this->executeFetchAllObject($select, $prototype);
277
    }
278
 
279
     /**
280
     *
281
     * @param int $company_id
282
     * @param $behavior_id_default
283
     * @return Behavior
284
     */
285
    public function fetchOneByCompanyIdAndCompetencyIdDefault($company_id, $behavior_id_default)
286
    {
287
        $select = $this->sql->select(self::_TABLE);
288
        $select->where->equalTo('company_id', $company_id);
289
        $select->where->equalTo('behavior_id_default', $behavior_id_default);
290
        $select->limit(1);
291
 
292
        $prototype = new Behavior();
293
        return $this->executeFetchOneObject($select, $prototype);
294
    }
3186 efrain 295
 
296
    /**
297
     *
298
     * @param string $search
299
     * @param int $page
300
     * @param int $records_per_page
301
     * @param string $order_field
302
     * @param string $order_direction
303
     * @return Paginator
304
     */
4632 efrain 305
    public function fetchAllDataTableDefault($search, $page = 1, $records_per_page = 10, $order_field = 'description', $order_direction = 'ASC') {
3186 efrain 306
        $prototype = new Behavior();
307
        $select = $this->sql->select(self::_TABLE);
308
        $select->where->isNull('company_id');
309
 
310
        if ($search) {
311
            $select->where->like('description', '%' . $search . '%');
312
        }
313
        $select->order($order_field . ' ' . $order_direction);
314
 
315
        $hydrator = new ObjectPropertyHydrator();
316
        $resultset = new HydratingResultSet($hydrator, $prototype);
317
 
318
        $adapter = new DbSelect($select, $this->sql, $resultset);
319
        $paginator = new Paginator($adapter);
320
        $paginator->setItemCountPerPage($records_per_page);
321
        $paginator->setCurrentPageNumber($page);
322
 
323
 
324
        return $paginator;
325
    }
326
 
327
    /**
328
     *
329
     * @param int $company_id
330
     * @param string $search
331
     * @param int $page
332
     * @param int $records_per_page
333
     * @param string $order_field
334
     * @param string $order_direction
335
     * @return Paginator
336
     */
337
    public function fetchAllDataTableByCompanyId($company_id, $search, $page = 1, $records_per_page = 10, $order_field = 'description', $order_direction = 'ASC') {
338
        $prototype = new Behavior();
339
        $select = $this->sql->select(self::_TABLE);
340
        $select->where->equalTo('company_id', $company_id);
341
 
342
 
343
        if ($search) {
344
            $select->where->NEST->like('description', '%' . $search . '%')->UNNEST;
345
        }
346
        $select->order($order_field . ' ' . $order_direction);
347
 
348
        $hydrator = new ObjectPropertyHydrator();
349
        $resultset = new HydratingResultSet($hydrator, $prototype);
350
 
351
        $adapter = new DbSelect($select, $this->sql, $resultset);
352
        $paginator = new Paginator($adapter);
353
        $paginator->setItemCountPerPage($records_per_page);
354
        $paginator->setCurrentPageNumber($page);
355
 
356
 
357
        return $paginator;
358
    }
630 efrain 359
 
360
}