Proyectos de Subversion LeadersLinked - Services

Rev

Rev 307 | Ir a la última revisión | | 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
     *
102
     * @return HabitSkill[]
103
     */
104
    public function fetchAll()
105
    {
106
        $prototype = new HabitSkill();
107
        $select = $this->sql->select(self::_TABLE);
108
 
109
        return $this->executeFetchAllObject($select, $prototype);
110
    }
111
 
112
 
113
 
114
    /**
115
     *
116
     * @param int $user_id
117
     * @param string $search
118
     * @param int $page
119
     * @param int $records_per_page
120
     * @param string $order_field
121
     * @param string $order_direction
122
     * @return Paginator
123
     */
124
    public function fetchAllDataTable($user_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
125
    {
126
        $prototype = new HabitSkill();
127
        $select = $this->sql->select(self::_TABLE);
128
 
129
        if($search) {
130
            $select->where->like('name', '%' . $search . '%');
131
        }
132
        $select->order($order_field . ' ' . $order_direction);
133
 
134
       // echo $select->getSqlString($this->adapter->platform); exit;
135
 
136
        $hydrator   = new ObjectPropertyHydrator();
137
        $resultset  = new HydratingResultSet($hydrator, $prototype);
138
 
139
        $adapter = new DbSelect($select, $this->sql, $resultset);
140
        $paginator = new Paginator($adapter);
141
        $paginator->setItemCountPerPage($records_per_page);
142
        $paginator->setCurrentPageNumber($page);
143
 
144
 
145
        return $paginator;
146
    }
147
 
148
    /**
149
     *
150
     * @param HabitSkill $habitSkill
151
     * @return boolean
152
     */
153
    public function insert($habitSkill)
154
    {
155
        $hydrator = new ObjectPropertyHydrator();
156
        $values = $hydrator->extract($habitSkill);
157
        $values = $this->removeEmpty($values);
158
 
159
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
160
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
161
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
162
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
163
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
164
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
165
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
166
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
167
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
168
 
169
        $insert = $this->sql->insert(self::_TABLE);
170
        $insert->values($values);
171
 
172
 
173
        $result = $this->executeInsert($insert);
174
        if($result) {
175
            $habitSkill->id = $this->lastInsertId;
176
        }
177
 
178
        return $result;
179
 
180
    }
181
 
182
    /**
183
     *
184
     * @param HabitSkill $habitSkill
185
     * @return boolean
186
     */
187
    public function update($habitSkill)
188
    {
189
        $hydrator = new ObjectPropertyHydrator();
190
        $values = $hydrator->extract($habitSkill);
191
        $values = $this->removeEmpty($values);
192
 
193
        $values['monday_active']                = empty($values['monday_active']) ? 0 : 1;
194
        $values['tuesday_active']               = empty($values['tuesday_active']) ? 0 : 1;
195
        $values['wednesday_active']             = empty($values['wednesday_active']) ? 0 : 1;
196
        $values['thursday_active']              = empty($values['thursday_active']) ? 0 : 1;
197
        $values['friday_active']                = empty($values['friday_active']) ? 0 : 1;
198
        $values['saturday_active']              = empty($values['saturday_active']) ? 0 : 1;
199
        $values['sunday_active']                = empty($values['sunday_active']) ? 0 : 1;
200
        $values['notification_10min_before']    = empty($values['notification_10min_before']) ? 0 : 1;
201
        $values['notification_30min_before']    = empty($values['notification_30min_before']) ? 0 : 1;
202
 
203
        $update = $this->sql->update(self::_TABLE);
204
        $update->set($values);
205
        $update->where->equalTo('id', $habitSkill->id);
206
 
207
        return $this->executeUpdate($update);
208
    }
209
 
210
    /**
211
     *
212
     * @param HabitSkill $habitSkill
213
     * @return boolean
214
     */
215
    public function delete($habitSkill)
216
    {
217
        $delete = $this->sql->delete(self::_TABLE);
218
        $delete->where->equalTo('id', $habitSkill->id);
219
 
220
        return $this->executeDelete($delete);
221
 
222
    }
223
 
224
 
225
}