Proyectos de Subversion LeadersLinked - Services

Rev

Rev 308 | Ir a la última revisión | | 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
121
     * @return HabitSkill[]
122
     */
123
    public function fetchAllTemplateByCompayIds($company_ids)
124
    {
125
        $prototype = new HabitSkill();
126
 
127
 
128
        $select = $this->sql->select(self::_TABLE);
129
        $select->where->in('company_id', $company_ids);
130
        $select->where->equalTo('template', HabitSkill::TEMPLATE_YES);
131
 
132
        return $this->executeFetchAllObject($select, $prototype);
133
    }
311 www 134
 
135
    /**
136
     *
137
     * @param int $company_id
138
     * @param string $search
139
     * @param int $page
140
     * @param int $records_per_page
141
     * @param string $order_field
142
     * @param string $order_direction
143
     * @return Paginator
144
     */
145
    public function fetchAllDataTableTemplates($company_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
146
    {
147
        $prototype = new HabitSkill();
148
        $select = $this->sql->select(self::_TABLE);
149
 
150
        if($search) {
151
            $select->where->like('name', '%' . $search . '%');
152
        }
153
        $select->where->equalTo('company_id', $company_id);
154
        $select->order($order_field . ' ' . $order_direction);
155
 
156
 
157
 
158
        // echo $select->getSqlString($this->adapter->platform); exit;
159
 
160
        $hydrator   = new ObjectPropertyHydrator();
161
        $resultset  = new HydratingResultSet($hydrator, $prototype);
162
 
163
        $adapter = new DbSelect($select, $this->sql, $resultset);
164
        $paginator = new Paginator($adapter);
165
        $paginator->setItemCountPerPage($records_per_page);
166
        $paginator->setCurrentPageNumber($page);
167
 
168
 
169
        return $paginator;
170
    }
308 www 171
 
304 www 172
 
173
 
174
    /**
175
     *
176
     * @param int $user_id
177
     * @param string $search
178
     * @param int $page
179
     * @param int $records_per_page
180
     * @param string $order_field
181
     * @param string $order_direction
182
     * @return Paginator
183
     */
184
    public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
185
    {
186
        $prototype = new HabitSkill();
187
        $select = $this->sql->select(self::_TABLE);
188
 
189
        if($search) {
190
            $select->where->like('name', '%' . $search . '%');
191
        }
311 www 192
        $select->where->equalTo('user_id', $user_id);
304 www 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
     * @param HabitSkill $habitSkill
212
     * @return boolean
213
     */
214
    public function insert($habitSkill)
215
    {
216
        $hydrator = new ObjectPropertyHydrator();
217
        $values = $hydrator->extract($habitSkill);
218
        $values = $this->removeEmpty($values);
219
 
220
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
221
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
222
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
223
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
224
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
225
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
226
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
227
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
228
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
229
 
311 www 230
 
231
 
232
        $values['monday_time']                = empty($values['monday_time']) ? '' : $values['monday_time'];
233
        $values['tuesday_time']               = empty($values['tuesday_time']) ? '' : $values['tuesday_time'];
234
        $values['wednesday_time']             = empty($values['wednesday_time']) ? '' : $values['wednesday_time'];
235
        $values['thursday_time']              = empty($values['thursday_time']) ? '' : $values['thursday_time'];
236
        $values['friday_time']                = empty($values['friday_time']) ? '' : $values['friday_time'];
237
        $values['saturday_time']              = empty($values['saturday_time']) ? '' : $values['saturday_time'];
238
        $values['sunday_time']                = empty($values['sunday_time']) ? '' : $values['sunday_time'];
239
 
240
 
304 www 241
        $insert = $this->sql->insert(self::_TABLE);
242
        $insert->values($values);
243
 
244
 
245
        $result = $this->executeInsert($insert);
246
        if($result) {
247
            $habitSkill->id = $this->lastInsertId;
248
        }
249
 
250
        return $result;
251
 
252
    }
253
 
254
    /**
255
     *
256
     * @param HabitSkill $habitSkill
257
     * @return boolean
258
     */
259
    public function update($habitSkill)
260
    {
261
        $hydrator = new ObjectPropertyHydrator();
262
        $values = $hydrator->extract($habitSkill);
263
        $values = $this->removeEmpty($values);
264
 
265
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
266
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
267
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
268
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
269
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
270
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
271
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
272
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
273
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
311 www 274
 
275
 
276
        $values['monday_time']                = empty($values['monday_time']) ? '' : $values['monday_time'];
277
        $values['tuesday_time']               = empty($values['tuesday_time']) ? '' : $values['tuesday_time'];
278
        $values['wednesday_time']             = empty($values['wednesday_time']) ? '' : $values['wednesday_time'];
279
        $values['thursday_time']              = empty($values['thursday_time']) ? '' : $values['thursday_time'];
280
        $values['friday_time']                = empty($values['friday_time']) ? '' : $values['friday_time'];
281
        $values['saturday_time']              = empty($values['saturday_time']) ? '' : $values['saturday_time'];
282
        $values['sunday_time']                = empty($values['sunday_time']) ? '' : $values['sunday_time'];
304 www 283
 
284
        $update = $this->sql->update(self::_TABLE);
285
        $update->set($values);
286
        $update->where->equalTo('id', $habitSkill->id);
287
 
288
        return $this->executeUpdate($update);
289
    }
290
 
291
    /**
292
     *
293
     * @param HabitSkill $habitSkill
294
     * @return boolean
295
     */
296
    public function delete($habitSkill)
297
    {
298
        $delete = $this->sql->delete(self::_TABLE);
299
        $delete->where->equalTo('id', $habitSkill->id);
300
 
301
        return $this->executeDelete($delete);
302
 
303
    }
304
 
305
 
306
}