Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Ir a la última revisión | | 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);
109
        $select->where->like('corporate_email', '%' . corporate_email . '%');
110
        $select->order('first_name, last_name');
111
 
112
 
113
        $prototype = new DiscoveryContact();
114
 
115
        return $this->executeFetchAllObject($select, $prototype);
116
    }
117
 
118
    /**
119
     *
1 efrain 120
     * @param  int $company_id
121
     * @return DiscoveryContact[]
122
     */
123
    public function fetchAllByCompanyId($company_id)
124
    {
125
 
126
        $select = $this->sql->select(self::_TABLE);
127
        $select->where->equalTo('company_id', $company_id);
128
 
129
        $prototype = new DiscoveryContact();
130
        return $this->executeFetchAllObject($select, $prototype);
131
    }
132
 
133
 
134
 
135
 
136
 
137
    /**
138
     *
139
     * @param string $search
140
     * @param int $company_id
141
     * @param int $page
142
     * @param int $records_per_page
143
     * @param string $order_field
144
     * @param string $order_direction
145
     * @return Paginator
146
     */
147
    public function fetchAllDataTableForCompanyId($search, $company_id,  $page = 1, $records_per_page = 10, $order_field= 'last_name', $order_direction = 'ASC')
148
    {
149
        $prototype = new DiscoveryContact();
150
        $select = $this->sql->select(self::_TABLE);
151
        $select->where->equalTo('company_id', $company_id);
152
 
153
 
154
        if($search) {
155
            $select->where->like('uuid', '%' . $search . '%')
156
            ->or->like('first_name', '%' . $search . '%')
157
            ->or->like('last_name', '%' . $search . '%')
158
            ->or->like('corporate_email', '%' . $search . '%')
159
            ->or->like('company', '%' . $search . '%')
160
            ->or->like('position', '%' . $search . '%')
161
            ->or->like('country', '%' . $search . '%');
162
        }
163
        $select->order($order_field . ' ' . $order_direction);
164
 
165
        $hydrator   = new ObjectPropertyHydrator();
166
        $resultset  = new HydratingResultSet($hydrator, $prototype);
167
 
168
        $adapter = new DbSelect($select, $this->sql, $resultset);
169
        $paginator = new Paginator($adapter);
170
        $paginator->setItemCountPerPage($records_per_page);
171
        $paginator->setCurrentPageNumber($page);
172
 
173
 
174
        return $paginator;
175
    }
176
 
177
 
178
    /**
179
     *
180
     * @param DiscoveryContact $record
181
     * @return boolean
182
     */
183
    public function insert($record)
184
    {
185
        $hydrator = new ObjectPropertyHydrator();
186
        $values = $hydrator->extract($record);
187
        $values = $this->removeEmpty($values);
188
 
189
        $insert = $this->sql->insert(self::_TABLE);
190
        $insert->values($values);
191
        $result = $this->executeInsert($insert);
192
 
193
        if($result) {
194
            $record->id = $this->lastInsertId;
195
        }
196
 
197
        return $result;
198
    }
199
 
200
 
201
    /**
202
     *
203
     * @param DiscoveryContact $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
        $update = $this->sql->update(self::_TABLE);
213
        $update->set($values);
214
        $update->where->equalTo('id', $record->id);
215
 
216
        return $this->executeUpdate($update);
217
    }
218
 
219
 
220
 
221
    /**
222
     *
223
     * @param DiscoveryContact $record
224
     * @return boolean
225
     */
226
    public function delete($record)
227
    {
228
        $delete = $this->sql->delete(self::_TABLE);
229
        $delete->where->equalTo('id', $record->id);
230
 
231
        return $this->executeDelete($delete);
232
 
233
    }
234
 
235
 
236
}