|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author Carsten Bleek <[email protected]> |
|
9
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
10
|
|
|
* @author Mathias Weitz <weitz@xenon> |
|
11
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace Applications\Repository; |
|
14
|
|
|
|
|
15
|
|
|
use Core\Repository\AbstractRepository; |
|
16
|
|
|
use Applications\Entity\Application as ApplicationEntity; |
|
17
|
|
|
use Applications\Entity\CommentInterface; |
|
18
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
19
|
|
|
use Auth\Entity\UserInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* class for accessing applications |
|
23
|
|
|
* |
|
24
|
|
|
* @todo find better mechanism for loading drafts or applications or both states. |
|
25
|
|
|
* |
|
26
|
|
|
* @package Applications |
|
27
|
|
|
*/ |
|
28
|
|
|
class Application extends AbstractRepository |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
|
View Code Duplication |
public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
37
|
|
|
$criteria['isDraft'] = false; |
|
38
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
39
|
|
|
unset($criteria['isDraft']); |
|
40
|
|
|
} |
|
41
|
|
|
return parent::findBy($criteria, $sort, $limit, $skip); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function findOneBy(array $criteria) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
50
|
|
|
$criteria['isDraft'] = false; |
|
51
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
52
|
|
|
unset($criteria['isDraft']); |
|
53
|
|
|
} |
|
54
|
|
|
return parent::findOneBy($criteria); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function createQueryBuilder($findDrafts = false) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$qb = parent::createQueryBuilder(); |
|
63
|
|
|
if (null !== $findDrafts) { |
|
64
|
|
|
$qb->field('isDraft')->equals($findDrafts); |
|
65
|
|
|
} |
|
66
|
|
|
return $qb; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets a pointer to an application |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $params |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getPaginatorCursor($params) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getPaginationQueryBuilder($params) |
|
77
|
|
|
->getQuery() |
|
78
|
|
|
->execute(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Gets a query builder to search for applications |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $params |
|
85
|
|
|
* @return unknown |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
protected function getPaginationQueryBuilder($params) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$filter = $this->getService('filterManager')->get('PaginationQuery/Applications'); |
|
90
|
|
|
$qb = $filter->filter($params, $this->createQueryBuilder()); |
|
91
|
|
|
|
|
92
|
|
|
return $qb; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets a result list of applications |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $params |
|
99
|
|
|
* @return \Applications\Repository\PaginationList |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getPaginationList($params) |
|
102
|
|
|
{ |
|
103
|
|
|
$qb = $this->getPaginationQueryBuilder($params); |
|
104
|
|
|
$cursor = $qb->hydrate(false) |
|
105
|
|
|
->select('_id') |
|
106
|
|
|
->getQuery() |
|
107
|
|
|
->execute(); |
|
108
|
|
|
|
|
109
|
|
|
$list = new PaginationList(array_keys(ArrayUtils::iteratorToArray($cursor))); |
|
110
|
|
|
return $list; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $job |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function loadApplicationsForJob($job) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->createQueryBuilder() |
|
121
|
|
|
->field("job")->equals(new \MongoId($job->id)) |
|
122
|
|
|
->getQuery() |
|
123
|
|
|
->execute(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get unread applications |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Jobs\Entity\JobInterface $job |
|
130
|
|
|
* @return array|bool|\Doctrine\MongoDB\ArrayIterator|\Doctrine\MongoDB\Cursor|\Doctrine\MongoDB\EagerCursor|mixed|null |
|
131
|
|
|
*/ |
|
132
|
|
|
public function loadUnreadApplicationsForJob($job) |
|
133
|
|
|
{ |
|
134
|
|
|
$auth=$this->getService('AuthenticationService'); |
|
135
|
|
|
$qb=$this->createQueryBuilder() |
|
136
|
|
|
->field("readBy")->notIn(array($auth->getUser()->id)) |
|
137
|
|
|
->field("job")->equals(new \MongoId($job->id)); |
|
|
|
|
|
|
138
|
|
|
return $qb->getQuery()->execute(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get comments of an applications |
|
143
|
|
|
* |
|
144
|
|
|
* @param $commentOrId |
|
145
|
|
|
* @internal param \Application\Entity\Comment $comment | Id |
|
146
|
|
|
* @return \Applications\Entity\Comment|NULL |
|
147
|
|
|
*/ |
|
148
|
|
|
public function findComment($commentOrId) |
|
149
|
|
|
{ |
|
150
|
|
|
if ($commentOrId instanceof CommentInterface) { |
|
151
|
|
|
$commentOrId = $commentOrId->getId(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$application = $this->findOneBy(array('comments.id' => $commentOrId)); |
|
155
|
|
|
/* @var \Applications\Entity\Comment $comment */ |
|
156
|
|
|
foreach ($application->getComments() as $comment) { |
|
157
|
|
|
if ($comment->getId() == $commentOrId) { |
|
158
|
|
|
return $comment; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Gets social profiles of an application |
|
166
|
|
|
* |
|
167
|
|
|
* @param String $profileId |
|
168
|
|
|
* @return $profile|NULL |
|
|
|
|
|
|
169
|
|
|
*/ |
|
170
|
|
|
public function findProfile($profileId) |
|
171
|
|
|
{ |
|
172
|
|
|
$application = $this->findOneBy(array('isDraft' => null, 'profiles._id' => new \MongoId($profileId))); |
|
173
|
|
|
foreach ($application->getProfiles() as $profile) { |
|
174
|
|
|
if ($profile->getId() == $profileId) { |
|
175
|
|
|
return $profile; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return mixed |
|
183
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getStates() |
|
186
|
|
|
{ |
|
187
|
|
|
$qb = $this->createQueryBuilder(); |
|
188
|
|
|
$qb->hydrate(false)->distinct('status.name'); |
|
189
|
|
|
$result = $qb->getQuery()->execute(); |
|
190
|
|
|
return $result; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param $user UserInterface |
|
195
|
|
|
* @param $applyId |
|
196
|
|
|
* @return ApplicationEntity|null |
|
197
|
|
|
*/ |
|
198
|
|
|
public function findDraft($user, $applyId) |
|
199
|
|
|
{ |
|
200
|
|
|
if ($user instanceof UserInterface) { |
|
201
|
|
|
$user = $user->getId(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$documents = $this->findBy( |
|
205
|
|
|
[ |
|
206
|
|
|
'isDraft' => true, |
|
207
|
|
|
'$or' => [ |
|
208
|
|
|
['user' => $user], |
|
209
|
|
|
['permissions.change' => $user] |
|
210
|
|
|
] |
|
211
|
|
|
] |
|
212
|
|
|
); |
|
213
|
|
|
|
|
214
|
|
|
/* @var $document ApplicationEntity */ |
|
215
|
|
|
foreach ($documents as $document) { |
|
216
|
|
|
if ($applyId == $document->getJob()->getApplyId()) { |
|
217
|
|
|
return $document; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return null; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get applications for given user ID |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $userId |
|
228
|
|
|
* @param int $limit |
|
|
|
|
|
|
229
|
|
|
* @return Cursor |
|
230
|
|
|
* @since 0.27 |
|
231
|
|
|
*/ |
|
232
|
|
View Code Duplication |
public function getUserApplications($userId, $limit = null) |
|
|
|
|
|
|
233
|
|
|
{ |
|
234
|
|
|
$qb = $this->createQueryBuilder(null) |
|
|
|
|
|
|
235
|
|
|
->field('user')->equals($userId) |
|
236
|
|
|
->sort(['date' => -1]); |
|
237
|
|
|
|
|
238
|
|
|
if (isset($limit)) { |
|
239
|
|
|
$qb->limit($limit); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $qb->getQuery()->execute(); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.