Proyectos de Subversion LeadersLinked - Services

Rev

Rev 304 | 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
        $select->order('name');
132
 
133
        return $this->executeFetchAllObject($select, $prototype);
134
    }
135
 
304 www 136
 
137
 
138
    /**
139
     *
140
     * @param int $user_id
141
     * @param string $search
142
     * @param int $page
143
     * @param int $records_per_page
144
     * @param string $order_field
145
     * @param string $order_direction
146
     * @return Paginator
147
     */
148
    public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
149
    {
150
        $prototype = new HabitSkill();
151
        $select = $this->sql->select(self::_TABLE);
152
 
153
        if($search) {
154
            $select->where->like('name', '%' . $search . '%');
155
        }
156
        $select->order($order_field . ' ' . $order_direction);
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
    }
171
 
172
    /**
173
     *
174
     * @param HabitSkill $habitSkill
175
     * @return boolean
176
     */
177
    public function insert($habitSkill)
178
    {
179
        $hydrator = new ObjectPropertyHydrator();
180
        $values = $hydrator->extract($habitSkill);
181
        $values = $this->removeEmpty($values);
182
 
183
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
184
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
185
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
186
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
187
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
188
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
189
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
190
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
191
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
192
 
193
        $insert = $this->sql->insert(self::_TABLE);
194
        $insert->values($values);
195
 
196
 
197
        $result = $this->executeInsert($insert);
198
        if($result) {
199
            $habitSkill->id = $this->lastInsertId;
200
        }
201
 
202
        return $result;
203
 
204
    }
205
 
206
    /**
207
     *
208
     * @param HabitSkill $habitSkill
209
     * @return boolean
210
     */
211
    public function update($habitSkill)
212
    {
213
        $hydrator = new ObjectPropertyHydrator();
214
        $values = $hydrator->extract($habitSkill);
215
        $values = $this->removeEmpty($values);
216
 
217
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
218
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
219
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
220
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
221
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
222
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
223
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
224
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
225
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
226
 
227
        $update = $this->sql->update(self::_TABLE);
228
        $update->set($values);
229
        $update->where->equalTo('id', $habitSkill->id);
230
 
231
        return $this->executeUpdate($update);
232
    }
233
 
234
    /**
235
     *
236
     * @param HabitSkill $habitSkill
237
     * @return boolean
238
     */
239
    public function delete($habitSkill)
240
    {
241
        $delete = $this->sql->delete(self::_TABLE);
242
        $delete->where->equalTo('id', $habitSkill->id);
243
 
244
        return $this->executeDelete($delete);
245
 
246
    }
247
 
248
 
249
}