Proyectos de Subversion LeadersLinked - Services

Rev

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