Proyectos de Subversion LeadersLinked - Services

Rev

Rev 324 | | Comparar con el anterior | 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
{
325 www 18
    const _TABLE = 'tbl_habits_content';
324 www 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
    /**
51
     *
52
     * @param int $id
53
     * @return HabitContent
54
     */
55
    public function fetchOne($id)
56
    {
57
        $prototype = new HabitContent();
58
 
59
        $select = $this->sql->select(self::_TABLE);
60
        $select->where->equalTo('id', $id);
61
 
62
        return $this->executeFetchOneObject($select, $prototype);
63
    }
64
 
65
    /**
66
     *
67
     * @param int $uuid
68
     * @return HabitContent
69
     */
70
    public function fetchOneByUuid($uuid)
71
    {
72
        $prototype = new HabitContent;
73
        $select = $this->sql->select(self::_TABLE);
74
        $select->where->equalTo('uuid', $uuid);
75
 
76
        return $this->executeFetchOneObject($select, $prototype);
77
    }
78
 
325 www 79
 
324 www 80
    /**
81
     *
82
     * @param int $company_id
325 www 83
     * @param int $order
84
     * @return HabitContent
85
     */
86
    public function fetchOneNextAvailableForOrderByCompanyId($company_id, $order)
87
    {
88
        $prototype = new HabitContent;
89
        $select = $this->sql->select(self::_TABLE);
90
        $select->where->equalTo('company_id', $company_id);
91
        $select->where->equalTo('status', HabitContent::STATUS_ACTIVE);
92
        $select->where->greaterThan('order', $order);
93
 
94
        return $this->executeFetchOneObject($select, $prototype);
95
    }
96
 
97
    /**
98
     *
99
     * @param int $company_id
324 www 100
     * @return HabitContent[]
101
     */
102
    public function fetchAllByCompanyId($company_id)
103
    {
104
        $prototype = new HabitContent();
105
 
106
        $select = $this->sql->select(self::_TABLE);
107
        $select->where->equalTo('company_id', $company_id);
108
        $select->order(['order', 'name']);
109
 
110
        return $this->executeFetchAllObject($select, $prototype);
111
    }
112
 
113
 
114
 
115
 
116
    /**
117
     *
118
     * @param int $companyId
119
     * @param string $search
120
     * @param int $page
121
     * @param int $records_per_page
122
     * @param string $order_field
123
     * @param string $order_direction
124
     * @return Paginator
125
     *
126
     */
127
    public function fetchAllDataTableByCompanyId($companyId,   $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
128
    {
129
        $prototype = new HabitContent();
130
        $select = $this->sql->select(self::_TABLE);
131
        $select->where->equalTo('company_id', $companyId);
132
 
133
 
134
        if($search) {
135
            $select->where->like('name', '%' . $search . '%');
136
        }
137
        $select->order($order_field . ' ' . $order_direction);
138
 
139
        //echo $select->getSqlString($this->adapter->platform); exit;
140
 
141
        $hydrator   = new ObjectPropertyHydrator();
142
        $resultset  = new HydratingResultSet($hydrator, $prototype);
143
 
144
        $adapter = new DbSelect($select, $this->sql, $resultset);
145
        $paginator = new Paginator($adapter);
146
        $paginator->setItemCountPerPage($records_per_page);
147
        $paginator->setCurrentPageNumber($page);
148
 
149
 
150
        return $paginator;
151
    }
152
 
153
 
154
    /**
155
     *
156
     * @param HabitContent $record
157
     * @return boolean
158
     */
159
    public function insert($record)
160
    {
161
        $hydrator = new ObjectPropertyHydrator();
162
        $values = $hydrator->extract($record);
163
        $values = $this->removeEmpty($values);
164
 
165
        $insert = $this->sql->insert(self::_TABLE);
166
        $insert->values($values);
167
 
168
        $result = $this->executeInsert($insert);
169
        if($result) {
170
            $record->id = $this->lastInsertId;
171
        }
172
        return $result;
173
    }
174
 
175
    /**
176
     *
177
     * @param HabitContent $record
178
     * @return boolean
179
     */
180
    public function update($record)
181
    {
182
 
183
        $hydrator = new ObjectPropertyHydrator();
184
        $values = $hydrator->extract($record);
185
        $values = $this->removeEmpty($values);
186
 
187
        $update = $this->sql->update(self::_TABLE);
188
        $update->set($values);
189
        $update->where->equalTo('id', $record->id);
190
 
191
 
192
        return $this->executeUpdate($update);
193
    }
194
 
195
    /**
196
     *
197
     * @param HabitContent $record
198
     * @return boolean
199
     */
200
    public function delete($record)
201
    {
202
        $delete = $this->sql->delete(self::_TABLE);
203
        $delete->where->equalTo('id', $record->id);
204
 
205
        return $this->executeDelete($delete);
206
    }
207
 
208
 
209
 
210
}