Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
304 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Db\ResultSet\HydratingResultSet;
10
use Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Paginator\Paginator;
12
 
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
14
 
15
use LeadersLinked\Model\HabitSkill;
16
use LeadersLinked\Mapper\Common\MapperCommon;
17
 
18
 
19
class HabitSkillMapper extends MapperCommon
20
{
21
    const _TABLE = 'tbl_habits_skills';
22
 
23
    /**
24
     *
25
     * @var HabitSkillMapper
26
     */
27
    private static $_instance;
28
 
29
    /**
30
     *
31
     * @param AdapterInterface $adapter
32
     */
33
    private function __construct($adapter)
34
    {
35
        parent::__construct($adapter);
36
    }
37
 
38
    /**
39
     *
40
     * @param AdapterInterface $adapter
41
     * @return HabitSkillMapper
42
     */
43
    public static function getInstance($adapter)
44
    {
45
        if(self::$_instance == null) {
46
            self::$_instance = new HabitSkillMapper($adapter);
47
        }
48
        return self::$_instance;
49
    }
50
 
51
    /**
52
     *
53
     * @param int $id
54
     * @return HabitSkill
55
     */
56
    public function fetchOne($id)
57
    {
58
        $select = $this->sql->select(self::_TABLE);
59
        $select->where->equalTo('id', $id);
60
        $select->limit(1);
61
 
62
        $prototype = new HabitSkill();
63
        return $this->executeFetchOneObject($select, $prototype);
64
    }
65
 
66
    /**
67
     *
68
     * @param string $uuid
69
     * @return HabitSkill
70
     */
71
    public function fetchOneByUuid($uuid)
72
    {
73
        $select = $this->sql->select(self::_TABLE);
74
        $select->where->equalTo('uuid', $uuid);
75
        $select->limit(1);
76
 
77
        $prototype = new HabitSkill();
78
        return $this->executeFetchOneObject($select, $prototype);
79
    }
80
 
81
    /**
82
     *
83
     * @param string $uuid
84
     * @param string $network_id
85
     * @return HabitSkill
86
     */
87
    public function fetchOneByUuidAndNetworkId($uuid, $network_id)
88
    {
89
        $select = $this->sql->select(self::_TABLE);
90
        $select->where->equalTo('uuid', $uuid);
91
        $select->where->equalTo('network_id', $network_id);
92
        $select->limit(1);
93
 
94
 
95
 
96
        $prototype = new HabitSkill();
97
        return $this->executeFetchOneObject($select, $prototype);
98
    }
99
 
100
    /**
101
     *
307 www 102
     * @param int $user_id
304 www 103
     * @return HabitSkill[]
104
     */
307 www 105
    public function fetchAllByUserId($user_id)
304 www 106
    {
307 www 107
 
304 www 108
        $prototype = new HabitSkill();
307 www 109
 
110
 
304 www 111
        $select = $this->sql->select(self::_TABLE);
307 www 112
        $select->where->equalTo('user_id', $user_id);
113
        $select->order('name');
304 www 114
 
115
        return $this->executeFetchAllObject($select, $prototype);
116
    }
117
 
307 www 118
    /**
119
     *
120
     * @param int[] $company_ids
321 www 121
     * @param string $search
307 www 122
     * @return HabitSkill[]
123
     */
321 www 124
    public function searchAllTemplateByCompayIds($company_ids, $search)
125
    {
126
        $prototype = new HabitSkill();
127
 
128
 
129
        $select = $this->sql->select(self::_TABLE);
130
        $select->where->in('company_id', $company_ids);
131
        $select->where->like('name', '%' . $search . '%');
132
        $select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
133
 
134
        return $this->executeFetchAllObject($select, $prototype);
135
    }
136
 
137
 
138
 
139
    /**
140
     *
141
     * @param int[] $company_ids
142
     * @return HabitSkill[]
143
     */
307 www 144
    public function fetchAllTemplateByCompayIds($company_ids)
145
    {
146
        $prototype = new HabitSkill();
147
 
148
 
149
        $select = $this->sql->select(self::_TABLE);
150
        $select->where->in('company_id', $company_ids);
151
        $select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
152
 
153
        return $this->executeFetchAllObject($select, $prototype);
154
    }
311 www 155
 
156
    /**
157
     *
158
     * @param int $company_id
159
     * @param string $search
160
     * @param int $page
161
     * @param int $records_per_page
162
     * @param string $order_field
163
     * @param string $order_direction
164
     * @return Paginator
165
     */
166
    public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
167
    {
168
        $prototype = new HabitSkill();
169
        $select = $this->sql->select(self::_TABLE);
170
 
171
        if($search) {
172
            $select->where->like('name', '%' . $search . '%');
173
        }
174
        $select->where->equalTo('company_id', $company_id);
175
        $select->order($order_field . ' ' . $order_direction);
176
 
177
 
178
 
179
        // echo $select->getSqlString($this->adapter->platform); exit;
180
 
181
        $hydrator   = new ObjectPropertyHydrator();
182
        $resultset  = new HydratingResultSet($hydrator, $prototype);
183
 
184
        $adapter = new DbSelect($select, $this->sql, $resultset);
185
        $paginator = new Paginator($adapter);
186
        $paginator->setItemCountPerPage($records_per_page);
187
        $paginator->setCurrentPageNumber($page);
188
 
189
 
190
        return $paginator;
191
    }
308 www 192
 
304 www 193
 
194
 
195
    /**
196
     *
197
     * @param int $user_id
198
     * @param string $search
199
     * @param int $page
200
     * @param int $records_per_page
201
     * @param string $order_field
202
     * @param string $order_direction
203
     * @return Paginator
204
     */
205
    public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
206
    {
207
        $prototype = new HabitSkill();
208
        $select = $this->sql->select(self::_TABLE);
209
 
210
        if($search) {
211
            $select->where->like('name', '%' . $search . '%');
212
        }
311 www 213
        $select->where->equalTo('user_id', $user_id);
304 www 214
        $select->order($order_field . ' ' . $order_direction);
215
 
216
       // echo $select->getSqlString($this->adapter->platform); exit;
217
 
218
        $hydrator   = new ObjectPropertyHydrator();
219
        $resultset  = new HydratingResultSet($hydrator, $prototype);
220
 
221
        $adapter = new DbSelect($select, $this->sql, $resultset);
222
        $paginator = new Paginator($adapter);
223
        $paginator->setItemCountPerPage($records_per_page);
224
        $paginator->setCurrentPageNumber($page);
225
 
226
 
227
        return $paginator;
228
    }
229
 
230
    /**
231
     *
232
     * @param HabitSkill $habitSkill
233
     * @return boolean
234
     */
235
    public function insert($habitSkill)
236
    {
237
        $hydrator = new ObjectPropertyHydrator();
238
        $values = $hydrator->extract($habitSkill);
239
        $values = $this->removeEmpty($values);
240
 
241
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
242
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
243
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
244
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
245
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
246
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
247
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
248
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
249
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
250
 
311 www 251
 
252
 
253
        $values['monday_time']                = empty($values['monday_time']) ? '' : $values['monday_time'];
254
        $values['tuesday_time']               = empty($values['tuesday_time']) ? '' : $values['tuesday_time'];
255
        $values['wednesday_time']             = empty($values['wednesday_time']) ? '' : $values['wednesday_time'];
256
        $values['thursday_time']              = empty($values['thursday_time']) ? '' : $values['thursday_time'];
257
        $values['friday_time']                = empty($values['friday_time']) ? '' : $values['friday_time'];
258
        $values['saturday_time']              = empty($values['saturday_time']) ? '' : $values['saturday_time'];
259
        $values['sunday_time']                = empty($values['sunday_time']) ? '' : $values['sunday_time'];
260
 
261
 
304 www 262
        $insert = $this->sql->insert(self::_TABLE);
263
        $insert->values($values);
264
 
265
 
266
        $result = $this->executeInsert($insert);
267
        if($result) {
268
            $habitSkill->id = $this->lastInsertId;
269
        }
270
 
271
        return $result;
272
 
273
    }
274
 
275
    /**
276
     *
277
     * @param HabitSkill $habitSkill
278
     * @return boolean
279
     */
280
    public function update($habitSkill)
281
    {
282
        $hydrator = new ObjectPropertyHydrator();
283
        $values = $hydrator->extract($habitSkill);
284
        $values = $this->removeEmpty($values);
285
 
286
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
287
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
288
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
289
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
290
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
291
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
292
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
293
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
294
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
311 www 295
 
296
 
297
        $values['monday_time']                = empty($values['monday_time']) ? '' : $values['monday_time'];
298
        $values['tuesday_time']               = empty($values['tuesday_time']) ? '' : $values['tuesday_time'];
299
        $values['wednesday_time']             = empty($values['wednesday_time']) ? '' : $values['wednesday_time'];
300
        $values['thursday_time']              = empty($values['thursday_time']) ? '' : $values['thursday_time'];
301
        $values['friday_time']                = empty($values['friday_time']) ? '' : $values['friday_time'];
302
        $values['saturday_time']              = empty($values['saturday_time']) ? '' : $values['saturday_time'];
303
        $values['sunday_time']                = empty($values['sunday_time']) ? '' : $values['sunday_time'];
304 www 304
 
305
        $update = $this->sql->update(self::_TABLE);
306
        $update->set($values);
307
        $update->where->equalTo('id', $habitSkill->id);
308
 
309
        return $this->executeUpdate($update);
310
    }
311
 
312
    /**
313
     *
314
     * @param HabitSkill $habitSkill
315
     * @return boolean
316
     */
317
    public function delete($habitSkill)
318
    {
319
        $delete = $this->sql->delete(self::_TABLE);
320
        $delete->where->equalTo('id', $habitSkill->id);
321
 
322
        return $this->executeDelete($delete);
323
 
324
    }
325
 
326
 
327
}