Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7109 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6951 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\ResultSet\HydratingResultSet;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Paginator\Paginator;
11
use Laminas\Paginator\Adapter\DbSelect;
12
use LeadersLinked\Model\OrganizationPosition;
13
use LeadersLinked\Mapper\Common\MapperCommon;
14
use Laminas\Db\Sql\Expression;
15
 
16
 
17
 
18
class OrganizationPositionMapper extends MapperCommon
19
{
20
    const _TABLE = 'tbl_organization_positions';
21
 
22
 
23
    /**
24
     *
25
     * @var OrganizationPositionMapper
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 OrganizationPositionMapper
42
     */
43
    public static function getInstance($adapter)
44
    {
45
        if(self::$_instance == null) {
46
            self::$_instance = new OrganizationPositionMapper($adapter);
47
        }
48
        return self::$_instance;
49
    }
50
 
51
 
52
    /**
53
     *
54
     * @param int $id
55
     * @return OrganizationPosition
56
     */
57
    public function fetchOne($id)
58
    {
59
        $select = $this->sql->select(self::_TABLE);
60
        $select->where->equalTo('id', $id);
61
        $select->limit(1);
62
 
63
        $prototype = new OrganizationPosition();
64
        return $this->executeFetchOneObject($select, $prototype);
65
    }
66
 
67
    /**
68
     *
69
     * @param string $uuid
70
     * @return OrganizationPosition
71
     */
72
    public function fetchOneByUuid($uuid)
73
    {
74
        $select = $this->sql->select(self::_TABLE);
75
        $select->where->equalTo('uuid', $uuid);
76
        $select->limit(1);
77
 
78
        $prototype = new OrganizationPosition();
79
        return $this->executeFetchOneObject($select, $prototype);
80
    }
81
 
82
 
83
 
84
    /**
85
     *
86
     * @param OrganizationPosition $position
87
     * @return boolean
88
     */
89
    public function insert($position)
90
    {
91
        $hydrator = new ObjectPropertyHydrator();
92
        $values = $hydrator->extract($position);
93
        $values = $this->removeEmpty($values);
94
 
7109 efrain 95
        $values['boss_id'] = empty($values['boss_id']) ? null : $values['boss_id'];
96
 
6951 efrain 97
        $insert = $this->sql->insert(self::_TABLE);
98
        $insert->values($values);
7109 efrain 99
 
6951 efrain 100
        $result = $this->executeInsert($insert);
101
        if($result) {
102
            $position->id = $this->getLastInsertId();
103
        }
104
        return $result;
105
 
106
    }
107
 
108
    /**
109
     *
110
     * @param OrganizationPosition $position
111
     * @return boolean
112
     */
113
    public function update($position)
114
    {
115
        $hydrator = new ObjectPropertyHydrator();
116
        $values = $hydrator->extract($position);
117
        $values = $this->removeEmpty($values);
118
 
7109 efrain 119
        $values['boss_id'] = empty($values['boss_id']) ? null : $values['boss_id'];
120
 
6951 efrain 121
        $update = $this->sql->update(self::_TABLE);
122
        $update->set($values);
123
        $update->where->equalTo('id', $position->id);
124
 
125
        return $this->executeUpdate($update);
126
    }
127
 
128
    /**
129
     *
130
     * @param OrganizationPosition $position
131
     * @return boolean
132
     */
133
    public function delete($position)
134
    {
135
        $delete = $this->sql->delete(self::_TABLE);
136
        $delete->where->equalTo('id', $position->id);
137
 
138
        return $this->executeDelete($delete);
139
 
140
    }
141
 
142
 
143
    /**
144
     *
145
     * @param int $company_id
146
     * @param int $job_description_id
147
     * @param string $search
148
     * @param int $page
149
     * @param int $records_per_page
150
     * @param string $order_field
151
     * @param string $order_direction
152
     * @return Paginator
153
     */
154
    public function fetchAllDataTableByCompanyIdAndJobDescriptionId($company_id, $job_description_id, $search,  $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
155
    {
156
        $select = $this->sql->select();
157
        $select->from(['p' => OrganizationPositionMapper::_TABLE ]);
158
        $select->columns(['uuid', 'user_id', 'job_description_id', 'status']);
159
        $select->join(['u' => UserMapper::_TABLE], 'p.user_id = u.id', ['user' => new Expression("CONCAT(first_name, ' ', last_name,  ' (', email, ')') ") ]);
160
 
161
        $select->where->equalTo('p.company_id', $company_id);
162
        $select->where->equalTo('p.job_description_id', $job_description_id);
163
 
164
        if($search) {
165
            $select->where->nest()->like('u.first_name', '%' . $search . '%')
166
            ->or->like('u.last_name', '%' . $search . '%')
167
            ->or->like('u.email', '%' . $search . '%')
168
            ->unnest();
169
        }
170
        $select->order($order_field . ' ' . $order_direction);
171
 
172
        //echo $select->getSqlString($this->adapter->platform); exit;
173
 
174
        $resultset  = new HydratingResultSet();
175
 
176
        $adapter = new DbSelect($select, $this->sql, $resultset);
177
        $paginator = new Paginator($adapter);
178
        $paginator->setItemCountPerPage($records_per_page);
179
        $paginator->setCurrentPageNumber($page);
180
 
181
 
182
        return $paginator;
183
    }
184
 
185
 
186
    /**
187
     *
7109 efrain 188
     * @param int $company_id
6951 efrain 189
     * @param int $job_description_id
7109 efrain 190
     * @return OrganizationPosition[]
191
     */
192
    public function fetchAllByCompanyIdAndJobDescriptionId($company_id, $job_description_id )
193
    {
194
        $prototype = new OrganizationPosition();
195
 
196
        $select = $this->sql->select(self::_TABLE);
197
        $select->where->equalTo('company_id', $company_id);
198
        $select->where->equalTo('job_description_id', $job_description_id);
199
 
200
        return $this->executeFetchAllObject($select, $prototype);
201
    }
202
 
203
 
7134 efrain 204
    /**
205
     *
206
     * @param int $employee_id
207
     * @return OrganizationPosition[]
208
     */
209
    public function fetchAllByEmployeeId($employee_id)
210
    {
211
        $prototype = new OrganizationPosition();
212
 
213
        $select = $this->sql->select(self::_TABLE);
214
        $select->where->equalTo('employee_id', $employee_id);
215
 
216
        // echo $select->getSqlString($this->adapter->platform); exit;
217
 
218
 
219
        return $this->executeFetchAllObject($select, $prototype);
220
    }
7109 efrain 221
 
7134 efrain 222
 
7109 efrain 223
    /**
224
     *
6951 efrain 225
     * @param int $company_id
7134 efrain 226
     * @param int $employee_id
227
     * @return OrganizationPosition[]
228
     */
229
    public function fetchAllByCompanyIdAndEmployeeId($company_id, $employee_id)
230
    {
231
        $prototype = new OrganizationPosition();
232
 
233
        $select = $this->sql->select(self::_TABLE);
234
        $select->where->equalTo('company_id', $company_id);
235
        $select->where->equalTo('employee_id', $employee_id);
236
 
237
        // echo $select->getSqlString($this->adapter->platform); exit;
238
 
239
 
240
        return $this->executeFetchAllObject($select, $prototype);
241
    }
242
 
243
 
244
    /**
245
     *
246
     * @param int $company_id
7109 efrain 247
     * @param int $boss_id
6951 efrain 248
     * @return OrganizationPosition[]
249
     */
7109 efrain 250
    public function fetchAllByCompanyIdAndBossId($company_id, $boss_id)
6951 efrain 251
    {
252
        $prototype = new OrganizationPosition();
253
 
7109 efrain 254
        $select = $this->sql->select(self::_TABLE);
6951 efrain 255
        $select->where->equalTo('company_id', $company_id);
7109 efrain 256
 
257
        if($boss_id) {
258
            $select->where->equalTo('boss_id', $boss_id);
259
        } else {
260
            $select->where->isNull('boss_id');
261
        }
262
 
263
       // echo $select->getSqlString($this->adapter->platform); exit;
264
 
265
 
6951 efrain 266
        return $this->executeFetchAllObject($select, $prototype);
267
    }
268
 
269
 
7109 efrain 270
    /**
271
     *
272
     * @param int $employee_id
273
     * @return boolean
274
     */
275
    public function removeParentByEmployeeId($employee_id)
276
    {
277
        $values = [
278
            'boss_id' => null,
279
        ];
280
 
281
        $update = $this->sql->update(self::_TABLE);
282
        $update->set($values);
283
        $update->where->equalTo('boss_id', $employee_id);
284
 
285
        //error_log($update->getSqlString($this->adapter->platform));
286
 
287
        return $this->executeUpdate($update);
288
    }
289
 
6951 efrain 290
 
291
}