1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* YAWIK |
5
|
|
|
* |
6
|
|
|
* @filesource |
7
|
|
|
* @copyright https://yawik.org/COPYRIGHT.php |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
/** ActionController of Core */ |
12
|
|
|
namespace Jobs\Controller; |
13
|
|
|
|
14
|
|
|
use Geo\Entity\Geometry\Point; |
15
|
|
|
use Jobs\Entity\Location; |
16
|
|
|
use Jobs\Entity\TemplateValues; |
17
|
|
|
use Organizations\Entity\Employee; |
18
|
|
|
use Psr\Container\ContainerInterface; |
19
|
|
|
use Laminas\Json\Json; |
20
|
|
|
use Laminas\Mvc\Controller\AbstractActionController; |
21
|
|
|
use Laminas\ServiceManager\ServiceManager; |
22
|
|
|
use Laminas\View\Model\JsonModel; |
23
|
|
|
use Core\Entity\PermissionsInterface; |
24
|
|
|
use Jobs\Listener\Events\JobEvent; |
25
|
|
|
use Jobs\Listener\Response\JobResponse; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class ImportController extends AbstractActionController |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* |
37
|
|
|
* @var ServiceManager |
38
|
|
|
*/ |
39
|
|
|
private $serviceLocator; |
40
|
|
|
public static function factory(ContainerInterface $container, $requestedName, array $options = null) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$controller = new self(); |
43
|
|
|
$controller->serviceLocator = $container; |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $controller; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* api-interface for transferring jobs |
50
|
|
|
* @return JsonModel |
51
|
|
|
*/ |
52
|
|
|
public function saveAction() |
53
|
|
|
{ |
54
|
|
|
$services = $this->serviceLocator; |
55
|
|
|
|
56
|
|
|
/* @var \Laminas\Http\PhpEnvironment\Request $request */ |
57
|
|
|
$request = $this->getRequest(); |
58
|
|
|
|
59
|
|
|
$params = $this->params(); |
60
|
|
|
$p = $params->fromPost(); |
61
|
|
|
/* @var \Auth\Entity\User $user */ |
62
|
|
|
$user = $services->get('AuthenticationService')->getUser(); |
63
|
|
|
$repositories = $services->get('repositories'); |
64
|
|
|
/* @var \Jobs\Repository\Job $repositoriesJob */ |
65
|
|
|
$repositoriesJob = $repositories->get('Jobs/Job'); |
66
|
|
|
$log = $services->get('Core/Log'); |
67
|
|
|
|
68
|
|
|
$result = array('token' => session_id(), 'isSaved' => false, 'message' => '', 'portals' => array()); |
69
|
|
|
try { |
70
|
|
|
if (isset($user) && !empty($user->getLogin())) { |
71
|
|
|
$formElementManager = $services->get('FormElementManager'); |
72
|
|
|
/* @var \Jobs\Form\Import $form */ |
73
|
|
|
$form = $formElementManager->get('Jobs/Import'); |
74
|
|
|
$id = $params->fromPost('id'); // determine Job from Database |
75
|
|
|
/* @var \Jobs\Entity\Job $entity */ |
76
|
|
|
$entity = null; |
77
|
|
|
$createdJob = true; |
78
|
|
|
|
79
|
|
|
if (empty($id)) { |
80
|
|
|
$applyId = $params->fromPost('applyId'); |
81
|
|
|
if (empty($applyId)) { |
82
|
|
|
$entity = $repositoriesJob->create(); |
83
|
|
|
} else { |
84
|
|
|
$entity = $repositoriesJob->findOneBy(array("applyId" => (string) $applyId)); |
85
|
|
|
if (!isset($entity)) { |
86
|
|
|
// new Job (the more likely branch) |
87
|
|
|
$entity =$repositoriesJob->create(array("applyId" => (string) $applyId)); |
88
|
|
|
} else { |
89
|
|
|
$createdJob = false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$repositoriesJob->find($id); |
94
|
|
|
$createdJob = false; |
95
|
|
|
} |
96
|
|
|
//$services->get('repositories')->get('Jobs/Job')->store($entity); |
97
|
|
|
$form->bind($entity); |
98
|
|
|
if ($request->isPost()) { |
99
|
|
|
$loginSuffix = ''; |
100
|
|
|
$event = $this->getEvent(); |
101
|
|
|
$event->setName('login.getSuffix'); |
102
|
|
|
$loginSuffixResponseCollection = $this->getEventManager()->triggerEvent($event); |
103
|
|
|
if (!$loginSuffixResponseCollection->isEmpty()) { |
104
|
|
|
$loginSuffix = $loginSuffixResponseCollection->last(); |
105
|
|
|
} |
106
|
|
|
$params = $request->getPost(); |
107
|
|
|
$params->datePublishStart = \Datetime::createFromFormat("Y-m-d", $params->datePublishStart); |
|
|
|
|
108
|
|
|
$result['post'] = $_POST; |
109
|
|
|
$form->setData($params); |
110
|
|
|
if ($form->isValid()) { |
111
|
|
|
if (isset($params['description'])) { |
112
|
|
|
$templateValues = new TemplateValues(); |
113
|
|
|
$description = Json::decode($params->description); |
|
|
|
|
114
|
|
|
$entity->setTemplateValues($templateValues->setDescription(strip_tags($description))); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$entity->setStatus($params['status']); |
118
|
|
|
/* |
119
|
|
|
* Search responsible user via contactEmail |
120
|
|
|
* @var \Auth\Repository\User $users |
121
|
|
|
*/ |
122
|
|
|
$users = $repositories->get('Auth/User'); |
123
|
|
|
$responsibleUser = $users->findByEmail($params['contactEmail']); |
124
|
|
|
|
125
|
|
|
$entity->setUser($responsibleUser ?: $user); |
126
|
|
|
|
127
|
|
|
$group = $user->getGroup($entity->getCompany()); |
128
|
|
|
if ($group) { |
129
|
|
|
$entity->getPermissions()->grant($group, PermissionsInterface::PERMISSION_VIEW); |
130
|
|
|
} |
131
|
|
|
$result['isSaved'] = true; |
132
|
|
|
$log->info('Jobs/manage/saveJob [user: ' . $user->getLogin() . ']:' . var_export($p, true)); |
133
|
|
|
|
134
|
|
|
if (!empty($params->companyId)) { |
135
|
|
|
$companyId = $params->companyId . $loginSuffix; |
|
|
|
|
136
|
|
|
$repOrganization = $repositories->get('Organizations/Organization'); |
137
|
|
|
$hydratorManager = $services->get('HydratorManager'); |
138
|
|
|
/* @var \Organizations\Entity\Hydrator\OrganizationHydrator $hydrator */ |
139
|
|
|
$hydrator = $hydratorManager->get('Hydrator\Organization'); |
140
|
|
|
$entityOrganizationFromDB = $repOrganization->findbyRef($companyId); |
141
|
|
|
//$permissions = $entityOrganizationFromDB->getPermissions(); |
142
|
|
|
$data = array( |
143
|
|
|
'externalId' => $companyId, |
144
|
|
|
'organizationName' => $params->company, |
|
|
|
|
145
|
|
|
'image' => $params->logoRef, |
|
|
|
|
146
|
|
|
'user' => $user |
147
|
|
|
); |
148
|
|
|
//$permissions->grant($user, PermissionsInterface::PERMISSION_CHANGE); |
149
|
|
|
|
150
|
|
|
$entityOrganization = $hydrator->hydrate($data, $entityOrganizationFromDB); |
151
|
|
|
|
152
|
|
|
if ($params->companyUrl) { |
|
|
|
|
153
|
|
|
$entityOrganization->getContact()->setWebsite($params->companyUrl); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($responsibleUser && $user !== $responsibleUser) { |
157
|
|
|
/* |
158
|
|
|
* We cannot use custom collections yet |
159
|
|
|
* @todo if we updated Mongo ODM to >1.0.5, we must move this to |
160
|
|
|
* a custom collection class |
161
|
|
|
*/ |
162
|
|
|
$employees = $entityOrganization->getEmployees(); |
163
|
|
|
$contained = false; |
164
|
|
|
/* |
165
|
|
|
* this is o(n) and should propably be optimized when the custom collection is created. |
166
|
|
|
* It's not very performant to load the whole user entity, when all we need is the ID. |
167
|
|
|
* Maybe store the id as reference in the Employees Entity. |
168
|
|
|
*/ |
169
|
|
|
foreach ($employees as $employee) { |
170
|
|
|
if ($employee->getUser()->getId() == $responsibleUser->getId()) { |
171
|
|
|
$contained = true; |
172
|
|
|
break; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
if (!$contained) { |
176
|
|
|
$employees->add(new Employee($responsibleUser)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
$repositories->store($entityOrganization); |
180
|
|
|
$entity->setOrganization($entityOrganization); |
181
|
|
|
} else { |
182
|
|
|
$result['message'] = ''; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!empty($params->locations)) { |
186
|
|
|
$locations = \Laminas\Json\Json::decode($params->locations, \Laminas\Json\Json::TYPE_ARRAY); |
|
|
|
|
187
|
|
|
$jobLocations = $entity->getLocations(); |
188
|
|
|
$jobLocations->clear(); |
189
|
|
|
foreach ($locations as $locData) { |
190
|
|
|
$location = new Location(); |
191
|
|
|
$coords = array_map(function ($i) { |
192
|
|
|
return (float) $i; |
193
|
|
|
}, $locData['coordinates']); |
194
|
|
|
$location->setCountry($locData['country']) |
195
|
|
|
->setRegion($locData['region']) |
196
|
|
|
->setCity($locData['city']) |
197
|
|
|
->setCoordinates(new Point($coords)); |
198
|
|
|
|
199
|
|
|
$jobLocations->add($location); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/* @var \Core\EventManager\EventManager $jobEvents |
204
|
|
|
* @var JobEvent $jobEvent */ |
205
|
|
|
$jobEvents = $services->get('Jobs/Events'); |
206
|
|
|
$jobEvent = $jobEvents->getEvent(JobEvent::EVENT_IMPORT_DATA, $this); |
207
|
|
|
$jobEvent->setJobEntity($entity); |
|
|
|
|
208
|
|
|
|
209
|
|
|
$extra = []; |
210
|
|
|
foreach (array('channels', 'position', 'branches', 'keywords', 'description') as $paramName) { |
211
|
|
|
$data = $params->get($paramName); |
212
|
|
|
if ($data) { |
213
|
|
|
$data = Json::decode($data, Json::TYPE_ARRAY); |
214
|
|
|
$extra[$paramName] = $data; |
215
|
|
|
$jobEvent->setParam($paramName, $data); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$jobEvents->triggerEvent($jobEvent); |
220
|
|
|
$repositoriesJob->store($entity); |
221
|
|
|
|
222
|
|
|
$id = $entity->getId(); |
223
|
|
|
if (!empty($id)) { |
224
|
|
|
$jobEvent = $services->get('Jobs/Event'); // intentinally override |
225
|
|
|
$jobEvent->setJobEntity($entity); |
226
|
|
|
|
227
|
|
|
if (isset($extra['channels'])) { |
228
|
|
|
foreach ($extra['channels'] as $portalName => $trash) { |
229
|
|
|
$jobEvent->addPortal($portalName); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
$jobEvent->setParam('extraData', $extra); |
233
|
|
|
|
234
|
|
|
if ($createdJob || true) { |
235
|
|
|
/* @var $jobEvents \Laminas\EventManager\EventManager */ |
236
|
|
|
$jobEvents = $services->get('Jobs/Events'); |
237
|
|
|
$jobEvent->setName(JobEvent::EVENT_JOB_ACCEPTED); |
238
|
|
|
$jobEvent->setTarget($this); |
239
|
|
|
$responses = $jobEvents->triggerEvent($jobEvent); |
240
|
|
|
|
241
|
|
|
foreach ($responses as $response) { |
242
|
|
|
// responses from the portals |
243
|
|
|
// @TODO, put this in some conclusion and meaningful messages |
244
|
|
|
if (!empty($response)) { |
245
|
|
|
if ($response instanceof JobResponse) { |
246
|
|
|
if (!array_key_exists('log', $result)) { |
247
|
|
|
$result['log'] = ''; |
248
|
|
|
} |
249
|
|
|
//$message = $response->getMessage(); |
250
|
|
|
//$result['log'] .= $response->getMessage() . PHP_EOL; |
251
|
|
|
$status = $response->getStatus(); |
252
|
|
|
$portal = $response->getPortal(); |
253
|
|
|
if (empty($portal)) { |
254
|
|
|
throw new \RuntimeException('Publisher-Events (internal error): There is an unregistered publisher listening'); |
255
|
|
|
} |
256
|
|
|
switch ($status) { |
257
|
|
|
case JobResponse::RESPONSE_FAIL: |
258
|
|
|
case JobResponse::RESPONSE_NOTIMPLEMENTED: |
259
|
|
|
case JobResponse::RESPONSE_ERROR: |
260
|
|
|
$result['isSaved'] = false; |
261
|
|
|
break; |
262
|
|
|
case JobResponse::RESPONSE_DENIED: |
263
|
|
|
case JobResponse::RESPONSE_OK: |
264
|
|
|
case JobResponse::RESPONSE_OKANDSTOP: |
265
|
|
|
case JobResponse::RESPONSE_DEPRECATED: |
266
|
|
|
break; |
267
|
|
|
} |
268
|
|
|
if (array_key_exists($portal, $result['portals'])) { |
269
|
|
|
throw new \RuntimeException('Publisher-Events (internal error): There are two publisher registered for ' . $portal); |
270
|
|
|
} |
271
|
|
|
$result['portals'][$portal] = $status; |
272
|
|
|
} else { |
273
|
|
|
throw new \RuntimeException('Publisher-Events (internal error): Response must be from the class Jobs\Listener\Response\JobResponse'); |
274
|
|
|
} |
275
|
|
|
} else { |
276
|
|
|
throw new \RuntimeException('Publisher-Events (internal error): Response must be set'); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} else { |
282
|
|
|
$log->info('Jobs/manage/saveJob [error: ' . $form->getMessages() . ']:' . var_export($p, true)); |
|
|
|
|
283
|
|
|
$result['valid Error'] = $form->getMessages(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
} else { |
287
|
|
|
$log->info('Jobs/manage/saveJob [error: session lost]:' . var_export($p, true)); |
288
|
|
|
$result['message'] = 'session_id is lost'; |
289
|
|
|
} |
290
|
|
|
} catch (\Exception $e) { |
291
|
|
|
$result['message'] = 'exception occured: ' . $e->getMessage(); |
292
|
|
|
} |
293
|
|
|
//$services->get('Core/Log')->info('Jobs/manage/saveJob result:' . PHP_EOL . var_export($p, True)); |
294
|
|
|
return new JsonModel($result); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.