Proyectos de Subversion LeadersLinked - Services

Rev

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

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