|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license GPLv3 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** ActionController of Organizations */ |
|
11
|
|
|
namespace Organizations\Controller; |
|
12
|
|
|
|
|
13
|
|
|
use Auth\Exception\UnauthorizedAccessException; |
|
14
|
|
|
use Core\Entity\Collection\ArrayCollection; |
|
15
|
|
|
use Core\Form\SummaryForm; |
|
16
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
|
17
|
|
|
use Organizations\Repository; |
|
18
|
|
|
use Organizations\Form; |
|
19
|
|
|
use Zend\Session\Container as Session; |
|
20
|
|
|
use Zend\View\Model\JsonModel; |
|
21
|
|
|
use Core\Entity\PermissionsInterface; |
|
22
|
|
|
use Zend\View\Model\ViewModel; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Main Action Controller for the Organization. |
|
26
|
|
|
* Responsible for handling the organization form. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Mathias Weitz <[email protected]> |
|
29
|
|
|
* @author Carsten Bleek <[email protected]> |
|
30
|
|
|
* @author Rafal Ksiazek |
|
31
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
32
|
|
|
* |
|
33
|
|
|
* @method \Acl\Controller\Plugin\Acl acl() |
|
34
|
|
|
* @method \Core\Controller\Plugin\PaginationParams paginationParams() |
|
35
|
|
|
* @method \Core\Controller\Plugin\CreatePaginator paginator(string $repositoryName, array $defaultParams = array(), bool $usePostParams = false) |
|
36
|
|
|
* @method \Auth\Controller\Plugin\Auth auth() |
|
37
|
|
|
*/ |
|
38
|
|
|
class IndexController extends AbstractActionController |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* The organization form. |
|
42
|
|
|
* |
|
43
|
|
|
* @var Form\Organizations |
|
44
|
|
|
*/ |
|
45
|
|
|
private $form; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The organization repository. |
|
49
|
|
|
* |
|
50
|
|
|
* @var Repository\Organization |
|
51
|
|
|
*/ |
|
52
|
|
|
private $repository; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Creates an instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Form\Organizations $form Organization form. |
|
58
|
|
|
* @param Repository\Organization $repository Organization repository |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(Form\Organizations $form, Repository\Organization $repository) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->repository = $repository; |
|
63
|
|
|
$this->form = $form; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* attaches further Listeners for generating / processing the output |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function attachDefaultListeners() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
parent::attachDefaultListeners(); |
|
74
|
|
|
|
|
75
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
76
|
|
|
$defaultServices = $serviceLocator->get('DefaultListeners'); |
|
77
|
|
|
$events = $this->getEventManager(); |
|
78
|
|
|
|
|
79
|
|
|
$events->attach($defaultServices); |
|
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Generates a list of organizations |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function indexAction() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
/* @var $request \Zend\Http\Request */ |
|
91
|
|
|
$request = $this->getRequest(); |
|
92
|
|
|
$params = $request->getQuery(); |
|
93
|
|
|
$isRecruiter = $this->acl()->isRole('recruiter'); |
|
94
|
|
|
$params->count = 10; |
|
|
|
|
|
|
95
|
|
|
if ($isRecruiter) { |
|
96
|
|
|
$params->set('by', 'me'); |
|
97
|
|
|
} |
|
98
|
|
|
//default sorting |
|
99
|
|
|
if (!isset($params['sort'])) { |
|
100
|
|
|
$params->set('sort', "-name"); |
|
101
|
|
|
} |
|
102
|
|
|
// save the Params in the Session-Container |
|
103
|
|
|
$this->paginationParams()->setParams('Organizations\Index', $params); |
|
|
|
|
|
|
104
|
|
|
$paginator = $this->paginator('Organizations/Organization', $params); |
|
|
|
|
|
|
105
|
|
|
return array( |
|
106
|
|
|
'script' => 'organizations/index/list', |
|
107
|
|
|
'organizations' => $paginator |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Change (Upsert) organizations |
|
114
|
|
|
* |
|
115
|
|
|
* @return JsonModel |
|
116
|
|
|
* @throws \RuntimeException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function editAction() |
|
119
|
|
|
{ |
|
120
|
|
|
/* @var $request \Zend\Http\Request */ |
|
121
|
|
|
$serviceLocator = $this->getServiceLocator(); |
|
122
|
|
|
$return = null; |
|
123
|
|
|
$request = $this->getRequest(); |
|
124
|
|
|
$params = $this->params(); |
|
125
|
|
|
$formIdentifier = $params->fromQuery('form'); |
|
126
|
|
|
|
|
127
|
|
|
try { |
|
128
|
|
|
/* @var $handler \Organizations\Controller\Plugin\GetOrganizationHandler */ |
|
129
|
|
|
$handler = $this->plugin('Organizations/GetOrganizationHandler'); |
|
130
|
|
|
$org = $handler->process($this->params(), true); |
|
131
|
|
|
} catch (\RuntimeException $e) { |
|
132
|
|
|
return $this->getErrorViewModel('no-parent'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$container = $this->getFormular($org); |
|
136
|
|
|
|
|
137
|
|
|
if (isset($formIdentifier) && $request->isPost()) { |
|
138
|
|
|
/* @var $form \Zend\Form\FormInterface */ |
|
139
|
|
|
$postData = $this->params()->fromPost(); |
|
140
|
|
|
$filesData = $this->params()->fromFiles(); |
|
141
|
|
|
/* due to issues in ZF2 we need to clear the employees collection in the entity, |
|
142
|
|
|
* prior to binding. Otherwise it is not possible to REMOVE an employee, as the |
|
143
|
|
|
* MultiCheckbox Validation will FAIL on empty values! |
|
144
|
|
|
*/ |
|
145
|
|
|
if ("employeesManagement" == $formIdentifier) { |
|
146
|
|
|
$markedEmps = array(); |
|
147
|
|
|
// Check if no permissions are set, and set one, mark this employee and restore it afterwards. |
|
148
|
|
|
foreach ($postData['employees']['employees'] as &$empData) { |
|
149
|
|
|
if (!isset($empData['permissions'])) { |
|
150
|
|
|
$empData['permissions'][] = 16; |
|
151
|
|
|
$markedEmps[] = $empData['user']; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
$org->setEmployees(new ArrayCollection()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$form = $container->get($formIdentifier); |
|
158
|
|
|
$form->setData(array_merge($postData, $filesData)); |
|
159
|
|
|
if (!isset($form)) { |
|
160
|
|
|
throw new \RuntimeException('No form found for "' . $formIdentifier . '"'); |
|
161
|
|
|
} |
|
162
|
|
|
$isValid = $form->isValid(); |
|
163
|
|
|
|
|
164
|
|
|
if ("employeesManagement" == $formIdentifier) { |
|
165
|
|
|
// remove permissions from marked employees |
|
166
|
|
|
foreach ($org->getEmployees() as $emp) { |
|
167
|
|
|
$empId = $emp->getUser()->getId(); |
|
168
|
|
|
if (in_array($empId, $markedEmps)) { |
|
|
|
|
|
|
169
|
|
|
$emp->getPermissions()->revokeAll(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
if ($isValid) { |
|
174
|
|
|
$orgName = $org->getOrganizationName(); |
|
175
|
|
|
if ($orgName && '' !== (string) $orgName->getName()) { |
|
176
|
|
|
$org->setIsDraft(false); |
|
177
|
|
|
} |
|
178
|
|
|
$serviceLocator->get('repositories')->persist($org); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$organization = $container->getEntity(); |
|
182
|
|
|
$serviceLocator->get('repositories')->store($organization); |
|
183
|
|
|
|
|
184
|
|
|
if ('file-uri' === $this->params()->fromPost('return')) { |
|
185
|
|
|
/* @var $hydrator \Core\Entity\Hydrator\FileCollectionUploadHydrator |
|
186
|
|
|
* @var $file \Organizations\Entity\OrganizationImage */ |
|
187
|
|
|
$basepath = $serviceLocator->get('ViewHelperManager')->get('basepath'); |
|
188
|
|
|
$hydrator = $form->getHydrator(); |
|
189
|
|
|
$file = $hydrator->getLastUploadedFile(); |
|
190
|
|
|
$content = $basepath($file->getUri()); |
|
191
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
192
|
|
|
if ($form instanceof SummaryForm) { |
|
193
|
|
|
/* @var $form \Core\Form\SummaryForm */ |
|
194
|
|
|
$form->setRenderMode(SummaryForm::RENDER_SUMMARY); |
|
195
|
|
|
$viewHelper = 'summaryform'; |
|
196
|
|
|
} else { |
|
197
|
|
|
$viewHelper = 'form'; |
|
198
|
|
|
} |
|
199
|
|
|
$content = $serviceLocator->get('ViewHelperManager')->get($viewHelper)->__invoke($form); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return new JsonModel( |
|
203
|
|
|
array( |
|
204
|
|
|
'valid' => $isValid, |
|
205
|
|
|
'content' => $content, |
|
206
|
|
|
) |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (!isset($return)) { |
|
211
|
|
|
$return = array( |
|
212
|
|
|
'form' => $container |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
return $return; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Gets the organization form container. |
|
220
|
|
|
* |
|
221
|
|
|
* @param \Organizations\Entity\OrganizationInterface $organization |
|
222
|
|
|
* |
|
223
|
|
|
* @return \Organizations\Form\Organizations |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getFormular($organization) |
|
226
|
|
|
{ |
|
227
|
|
|
/* @var $container \Organizations\Form\Organizations */ |
|
228
|
|
|
$services = $this->getServiceLocator(); |
|
229
|
|
|
$forms = $services->get('FormElementManager'); |
|
230
|
|
|
$container = $forms->get( |
|
231
|
|
|
'organizations/form', |
|
232
|
|
|
array( |
|
233
|
|
|
'mode' => $organization->getId() ? 'edit' : 'new' |
|
234
|
|
|
) |
|
235
|
|
|
); |
|
236
|
|
|
$container->setEntity($organization); |
|
237
|
|
|
$container->setParam('id', $organization->id); |
|
|
|
|
|
|
238
|
|
|
// $container->setParam('applyId',$job->applyId); |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
if ('__my__' != $this->params('id', '')) { |
|
241
|
|
|
$container->disableForm('employeesManagement') |
|
242
|
|
|
->disableForm('workflowSettings'); |
|
243
|
|
|
} else { |
|
244
|
|
|
$container ->disableForm('organizationLogo') |
|
245
|
|
|
->disableForm('descriptionForm'); |
|
246
|
|
|
} |
|
247
|
|
|
return $container; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
protected function getErrorViewModel($script) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->getResponse()->setStatusCode(500); |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
$model = new ViewModel(); |
|
255
|
|
|
$model->setTemplate("organizations/error/$script"); |
|
256
|
|
|
|
|
257
|
|
|
return $model; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
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.