Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

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