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