Proyectos de Subversion LeadersLinked - Services

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
324 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use LeadersLinked\Mapper\Common\MapperCommon;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Model\HabitContent;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
11
use Laminas\Paginator\Paginator;
12
use Laminas\Db\ResultSet\HydratingResultSet;
13
use Laminas\Paginator\Adapter\DbSelect;
14
 
15
 
16
class HabitContentMapper extends MapperCommon
17
{
18
    const _TABLE = 'tbl_habit_content';
19
 
20
    /**
21
     *
22
     * @var HabitContentMapper
23
     */
24
    private static $_instance;
25
 
26
    /**
27
     *
28
     * @param AdapterInterface $adapter
29
     */
30
    private function __construct($adapter)
31
    {
32
        parent::__construct($adapter);
33
    }
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @return HabitContentMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new HabitContentMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @param int $company_id
51
     * @param int $topic_id
52
     * @param int $capsule_id
53
     * @return int
54
     */
55
    public function fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($company_id, $topic_id, $capsule_id)
56
    {
57
        $select = $this->sql->select();
58
        $select->columns(['total' => new Expression('COUNT(*)')]);
59
        $select->from(self::_TABLE);
60
        $select->where->equalTo('company_id', $company_id);
61
        $select->where->equalTo('topic_id', $topic_id);
62
        $select->where->equalTo('capsule_id', $capsule_id);
63
 
64
        $record = $this->executeFetchOneArray($select);
65
        return $record['total'];
66
    }
67
 
68
    /**
69
     *
70
     * @param int $company_id
71
     * @param int $topic_id
72
     * @return int
73
     */
74
    public function fetchTotalCountByCompanyIdAndTopicId($company_id, $topic_id)
75
    {
76
        $select = $this->sql->select();
77
        $select->columns(['total' => new Expression('COUNT(*)')]);
78
        $select->from(self::_TABLE);
79
        $select->where->equalTo('company_id', $company_id);
80
        $select->where->equalTo('topic_id', $topic_id);
81
 
82
 
83
       // echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
84
 
85
        $record = $this->executeFetchOneArray($select);
86
        return $record['total'];
87
    }
88
 
89
 
90
    /**
91
     *
92
     * @param int $company_id
93
     * @param int $quiz_id
94
     * @return int
95
     */
96
    public function fetchTotalCountByCompanyIdQuizId($company_id, $quiz_id)
97
    {
98
        $select = $this->sql->select();
99
        $select->columns(['total' => new Expression('COUNT(*)')]);
100
        $select->from(self::_TABLE);
101
        $select->where->equalTo('company_id', $company_id);
102
        $select->where->equalTo('quiz_id', $quiz_id);
103
 
104
        $record = $this->executeFetchOneArray($select);
105
        return $record['total'];
106
    }
107
 
108
 
109
    /**
110
     *
111
     * @param int $id
112
     * @return HabitContent
113
     */
114
    public function fetchOne($id)
115
    {
116
        $prototype = new HabitContent();
117
 
118
        $select = $this->sql->select(self::_TABLE);
119
        $select->where->equalTo('id', $id);
120
 
121
        return $this->executeFetchOneObject($select, $prototype);
122
    }
123
 
124
    /**
125
     *
126
     * @param int $uuid
127
     * @return HabitContent
128
     */
129
    public function fetchOneByUuid($uuid)
130
    {
131
        $prototype = new HabitContent;
132
        $select = $this->sql->select(self::_TABLE);
133
        $select->where->equalTo('uuid', $uuid);
134
 
135
        return $this->executeFetchOneObject($select, $prototype);
136
    }
137
 
138
    /**
139
     *
140
     * @param int $company_id
141
     * @return HabitContent[]
142
     */
143
    public function fetchAllByCompanyId($company_id)
144
    {
145
        $prototype = new HabitContent();
146
 
147
        $select = $this->sql->select(self::_TABLE);
148
        $select->where->equalTo('company_id', $company_id);
149
        $select->order(['order', 'name']);
150
 
151
        return $this->executeFetchAllObject($select, $prototype);
152
    }
153
 
154
 
155
 
156
 
157
    /**
158
     *
159
     * @param int $companyId
160
     * @param string $search
161
     * @param int $page
162
     * @param int $records_per_page
163
     * @param string $order_field
164
     * @param string $order_direction
165
     * @return Paginator
166
     *
167
     */
168
    public function fetchAllDataTableByCompanyId($companyId,   $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
169
    {
170
        $prototype = new HabitContent();
171
        $select = $this->sql->select(self::_TABLE);
172
        $select->where->equalTo('company_id', $companyId);
173
 
174
 
175
        if($search) {
176
            $select->where->like('name', '%' . $search . '%');
177
        }
178
        $select->order($order_field . ' ' . $order_direction);
179
 
180
        //echo $select->getSqlString($this->adapter->platform); exit;
181
 
182
        $hydrator   = new ObjectPropertyHydrator();
183
        $resultset  = new HydratingResultSet($hydrator, $prototype);
184
 
185
        $adapter = new DbSelect($select, $this->sql, $resultset);
186
        $paginator = new Paginator($adapter);
187
        $paginator->setItemCountPerPage($records_per_page);
188
        $paginator->setCurrentPageNumber($page);
189
 
190
 
191
        return $paginator;
192
    }
193
 
194
 
195
    /**
196
     *
197
     * @param HabitContent $record
198
     * @return boolean
199
     */
200
    public function insert($record)
201
    {
202
        $hydrator = new ObjectPropertyHydrator();
203
        $values = $hydrator->extract($record);
204
        $values = $this->removeEmpty($values);
205
 
206
        $insert = $this->sql->insert(self::_TABLE);
207
        $insert->values($values);
208
 
209
        $result = $this->executeInsert($insert);
210
        if($result) {
211
            $record->id = $this->lastInsertId;
212
        }
213
        return $result;
214
    }
215
 
216
    /**
217
     *
218
     * @param HabitContent $record
219
     * @return boolean
220
     */
221
    public function update($record)
222
    {
223
 
224
        $hydrator = new ObjectPropertyHydrator();
225
        $values = $hydrator->extract($record);
226
        $values = $this->removeEmpty($values);
227
 
228
        $update = $this->sql->update(self::_TABLE);
229
        $update->set($values);
230
        $update->where->equalTo('id', $record->id);
231
 
232
 
233
        return $this->executeUpdate($update);
234
    }
235
 
236
    /**
237
     *
238
     * @param HabitContent $record
239
     * @return boolean
240
     */
241
    public function delete($record)
242
    {
243
        $delete = $this->sql->delete(self::_TABLE);
244
        $delete->where->equalTo('id', $record->id);
245
 
246
        return $this->executeDelete($delete);
247
    }
248
 
249
 
250
 
251
}