Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | 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
use LeadersLinked\Model\CompanyUser;
7
use LeadersLinked\Mapper\Common\MapperCommon;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Log\LoggerInterface;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
use Laminas\Db\Sql\Expression;
12
 
13
class CompanyUserMapper extends MapperCommon
14
{
15
    const _TABLE = 'tbl_company_users';
16
 
17
 
18
    /**
19
     *
20
     * @var CompanyUserMapper
21
     */
22
    private static $_instance;
23
 
24
    /**
25
     *
26
     * @param AdapterInterface $adapter
27
     */
28
    private function __construct($adapter)
29
    {
30
        parent::__construct($adapter);
31
    }
32
 
33
    /**
34
     *
35
     * @param AdapterInterface $adapter
36
     * @param LoggerInterface $logger
37
     * @param int $user_id
38
     * @return \LeadersLinked\Mapper\CompanyUserMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new CompanyUserMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @param int $company_id
51
     * @param int $user_ids
52
     * @return CompanyUser[]
53
     */
54
    public function fetchAllActivesByCompanyIdAndUserIds($company_id, $user_ids)
55
    {
56
        $prototype = new CompanyUser();
57
        $select = $this->sql->select(self::_TABLE);
58
        $select->where->equalTo('company_id', $company_id);
59
        $select->where->in('user_id', $user_ids);
60
        $select->where->in('status',[
61
            CompanyUser::STATUS_ACCEPTED,
62
            CompanyUser::STATUS_ADMIN_WILL_ADD
63
        ]);
64
 
65
        return $this->executeFetchAllObject($select, $prototype);
66
    }
67
 
68
 
69
 
70
    /**
71
     *
72
     * @param int $company_id
73
     * @return CompanyUser[]
74
     */
75
    public function fetchAllByCompanyId($company_id)
76
    {
77
        $prototype = new CompanyUser();
78
        $select = $this->sql->select(self::_TABLE);
79
        $select->where->equalTo('company_id', $company_id);
80
 
81
        return $this->executeFetchAllObject($select, $prototype);
82
    }
83
 
84
    /**
85
     *
86
     * @param int $company_id
87
     * @return CompanyUser[]
88
     */
89
    public function fetchAllByCompanyIdAndNotUserId($company_id,$user_id)
90
    {
91
        $prototype = new CompanyUser();
92
        $select = $this->sql->select(self::_TABLE);
93
        $select->where->equalTo('company_id', $company_id);
94
        $select->where->notEqualTo('user_id', $user_id);
95
        $select->where->equalTo('owner', CompanyUser::OWNER_NO);
96
        return $this->executeFetchAllObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $company_id
102
     * @return CompanyUser[]
103
     */
104
    public function fetchAllByUserId($user_id)
105
    {
106
        $prototype = new CompanyUser();
107
        $select = $this->sql->select(self::_TABLE);
108
        $select->where->equalTo('user_id', $user_id);
109
 
110
        return $this->executeFetchAllObject($select, $prototype);
111
    }
112
 
113
    /**
114
     *
115
     * @param int $id
116
     * @return CompanyUser
117
     */
118
    public function fetchOne($id)
119
    {
120
        $prototype = new CompanyUser();
121
        $select = $this->sql->select(self::_TABLE);
122
        $select->where->equalTo('id', $id);
123
 
124
        return $this->executeFetchOneObject($select, $prototype);
125
    }
126
 
127
    /**
128
     *
129
     * @param int $company_id
130
     * @return CompanyUser
131
     */
132
    public function fetchOwnerByCompanyId($company_id)
133
    {
134
        $prototype = new CompanyUser();
135
        $select = $this->sql->select(self::_TABLE);
136
        $select->where->equalTo('company_id', $company_id);
137
        $select->where->equalTo('owner', CompanyUser::OWNER_YES);
138
 
139
        return $this->executeFetchOneObject($select, $prototype);
140
    }
141
 
142
    /**
143
     *
144
     * @param int $company_id
145
     * @return CompanyUser
146
     */
147
    public function fetchCreatorByCompanyId($company_id)
148
    {
149
        $prototype = new CompanyUser();
150
        $select = $this->sql->select(self::_TABLE);
151
        $select->where->equalTo('company_id', $company_id);
152
        $select->where->equalTo('creator', CompanyUser::CREATOR_YES);
153
 
154
        return $this->executeFetchOneObject($select, $prototype);
155
    }
156
 
157
    /**
158
     *
159
     * @param int $company_id
160
     * @param int $user_id
161
     * @return CompanyUser
162
     */
163
    public function fetchOneByCompanyIdAndUserId($company_id, $user_id)
164
    {
165
        $prototype = new CompanyUser();
166
        $select = $this->sql->select(self::_TABLE);
167
        $select->where->equalTo('company_id', $company_id);
168
        $select->where->equalTo('user_id', $user_id);
169
 
170
       // echo $select->getSqlString($this->adapter->platform); exit;
171
 
172
        return $this->executeFetchOneObject($select, $prototype);
173
    }
174
 
175
    /**
176
     *
177
     * @param int $company_id
178
     * @param int $user_id
179
     * @return CompanyUser
180
     */
181
    public function fetchOneAcceptedByCompanyIdAndUserId($company_id, $user_id)
182
    {
183
        $prototype = new CompanyUser();
184
        $select = $this->sql->select(self::_TABLE);
185
        $select->where->equalTo('company_id', $company_id);
186
        $select->where->equalTo('user_id', $user_id);
187
        $select->where->equalTo('status', CompanyUser::STATUS_ACCEPTED);
188
 
189
        // echo $select->getSqlString($this->adapter->platform); exit;
190
 
191
        return $this->executeFetchOneObject($select, $prototype);
192
    }
193
 
194
 
195
    /**
196
     *
197
     * @param int $company_id
198
     * @param int $user_id
199
     * @return int
200
     */
201
    public function fetchCountOtherCompaniesByCompanyIdAndUserId($company_id, $user_id)
202
    {
203
        $prototype = new CompanyUser();
204
        $select = $this->sql->select();
205
        $select->columns(['total' => new Expression('COUNT(*)') ]);
206
        $select->from(self::_TABLE);
207
        $select->where->notEqualTo('company_id', $company_id);
208
        $select->where->equalTo('user_id', $user_id);
209
 
210
        $record = $this->executeFetchOneArray($select);
211
 
212
        return $record['total'];
213
    }
214
 
215
    /**
216
     *
217
     * @param CompanyUser $companyUser
218
     * @return boolean
219
     */
220
    public function insert($companyUser)
221
    {
222
        $hydrator = new ObjectPropertyHydrator();
223
        $values = $hydrator->extract($companyUser);
224
        $values = $this->removeEmpty($values);
225
 
226
 
227
        $insert = $this->sql->insert(self::_TABLE);
228
        $insert->values($values);
229
 
230
        $result = $this->executeInsert($insert);
231
        if($result) {
232
            $companyUser->id = $this->lastInsertId;
233
        }
234
 
235
        return $result;
236
    }
237
 
238
    /**
239
     *
240
     * @param CompanyUser $companyUser
241
     * @return boolean
242
     */
243
    public function update($companyUser)
244
    {
245
        $hydrator = new ObjectPropertyHydrator();
246
        $values = $hydrator->extract($companyUser);
247
        $values = $this->removeEmpty($values);
248
 
249
        $update = $this->sql->update(self::_TABLE);
250
        $update->set($values);
251
        $update->where->equalTo('id', $companyUser->id);
252
 
253
        return $this->executeUpdate($update);
254
    }
255
 
256
    /**
257
     *
258
     * @param int $id
259
     * @return boolean
260
     */
261
    public function delete($id)
262
    {
263
        $delete = $this->sql->select(self::_TABLE);
264
        $delete->where->equalTo('id', $id);
265
 
266
        return $this->executeDelete($delete);
267
    }
268
 
269
 
270
 
271
}