16971 |
efrain |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Controller;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
7 |
|
|
|
8 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
9 |
use Laminas\View\Model\ViewModel;
|
|
|
10 |
use Laminas\View\Model\JsonModel;
|
|
|
11 |
use LeadersLinked\Library\Functions;
|
|
|
12 |
use LeadersLinked\Mapper\DiscoveryContactBlackListReasonMapper;
|
|
|
13 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
14 |
use LeadersLinked\Form\DiscoveryContact\BlackListReasonForm;
|
|
|
15 |
use LeadersLinked\Model\DiscoveryContactBlackListReason;
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
class DiscoveryContactBlackListReasonController extends AbstractActionController
|
|
|
19 |
{
|
|
|
20 |
/**
|
|
|
21 |
*
|
|
|
22 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
23 |
*/
|
|
|
24 |
private $adapter;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
29 |
*/
|
|
|
30 |
private $cache;
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
* @var \Laminas\Log\LoggerInterface
|
|
|
36 |
*/
|
|
|
37 |
private $logger;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @var array
|
|
|
42 |
*/
|
|
|
43 |
private $config;
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
*
|
|
|
48 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
49 |
*/
|
|
|
50 |
private $translator;
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
*
|
|
|
55 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
56 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
57 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
58 |
* @param array $config
|
|
|
59 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
60 |
*/
|
|
|
61 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
62 |
{
|
|
|
63 |
$this->adapter = $adapter;
|
|
|
64 |
$this->cache = $cache;
|
|
|
65 |
$this->logger = $logger;
|
|
|
66 |
$this->config = $config;
|
|
|
67 |
$this->translator = $translator;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
public function indexAction()
|
|
|
71 |
{
|
|
|
72 |
$request = $this->getRequest();
|
|
|
73 |
if($request->isGet()) {
|
|
|
74 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
75 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
76 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
77 |
|
|
|
78 |
$headers = $request->getHeaders();
|
|
|
79 |
|
|
|
80 |
$isJson = false;
|
|
|
81 |
if($headers->has('Accept')) {
|
|
|
82 |
$accept = $headers->get('Accept');
|
|
|
83 |
|
|
|
84 |
$prioritized = $accept->getPrioritized();
|
|
|
85 |
|
|
|
86 |
foreach($prioritized as $key => $value) {
|
|
|
87 |
$raw = trim($value->getRaw());
|
|
|
88 |
|
|
|
89 |
if(!$isJson) {
|
|
|
90 |
$isJson = strpos($raw, 'json');
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if($isJson) {
|
|
|
97 |
$search = $this->params()->fromQuery('search');
|
|
|
98 |
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
|
|
|
99 |
|
|
|
100 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
101 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
102 |
$order = $this->params()->fromQuery('order', []);
|
|
|
103 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
104 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
|
|
|
105 |
|
|
|
106 |
$fields = ['name'];
|
|
|
107 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
|
|
|
108 |
|
|
|
109 |
if(!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
110 |
$order_direction = 'ASC';
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$discoveryContactBlackListReasonMapper = DiscoveryContactBlackListReasonMapper::getInstance($this->adapter);
|
|
|
114 |
$paginator = $discoveryContactBlackListReasonMapper->fetchAllDataTableForCompanyId($search, $currentCompany->id, $page, $records_x_page, $order_field, $order_direction);
|
|
|
115 |
|
|
|
116 |
$items = [];
|
|
|
117 |
$records = $paginator->getCurrentItems();
|
|
|
118 |
foreach($records as $record)
|
|
|
119 |
{
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
$item = [
|
|
|
125 |
'name' => $record->name,
|
|
|
126 |
'status' => $record->status,
|
|
|
127 |
'actions' => [
|
|
|
128 |
'link_edit' => $this->url()->fromRoute('discovery-contacts/blacklist-reasons/edit', ['id' => $record->uuid ]),
|
|
|
129 |
'link_delete' => $this->url()->fromRoute('discovery-contacts/blacklist-reasons/delete', ['id' => $record->uuid ]),
|
|
|
130 |
],
|
|
|
131 |
];
|
|
|
132 |
|
|
|
133 |
array_push($items, $item);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return new JsonModel([
|
|
|
137 |
'success' => true,
|
|
|
138 |
'data' => [
|
|
|
139 |
'items' => $items,
|
|
|
140 |
'total' => $paginator->getTotalItemCount(),
|
|
|
141 |
]
|
|
|
142 |
]);
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
} else {
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
$form = new BlackListReasonForm();
|
|
|
150 |
|
|
|
151 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
152 |
$viewModel = new ViewModel();
|
|
|
153 |
$viewModel->setTemplate('leaders-linked/discovery-contact-blacklist-reasons/index.phtml');
|
|
|
154 |
$viewModel->setVariable('form', $form);
|
|
|
155 |
return $viewModel ;
|
|
|
156 |
}
|
|
|
157 |
} else {
|
|
|
158 |
return new JsonModel([
|
|
|
159 |
'success' => false,
|
|
|
160 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
161 |
]);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
public function addAction()
|
|
|
166 |
{
|
|
|
167 |
|
|
|
168 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
169 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
170 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
171 |
|
|
|
172 |
$request = $this->getRequest();
|
|
|
173 |
if($request->isPost()) {
|
|
|
174 |
$form = new BlackListReasonForm();
|
|
|
175 |
$dataPost = $request->getPost()->toArray();
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
$form->setData($dataPost);
|
|
|
179 |
|
|
|
180 |
if($form->isValid()) {
|
|
|
181 |
$dataPost = (array) $form->getData();
|
|
|
182 |
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : DiscoveryContactBlackListReason::STATUS_INACTIVE;
|
|
|
183 |
|
|
|
184 |
$discoveryContactBlackListReasonMapper = DiscoveryContactBlackListReasonMapper::getInstance($this->adapter);
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
189 |
$discoveryContactBlackListReason = new DiscoveryContactBlackListReason();
|
|
|
190 |
$hydrator->hydrate($dataPost, $discoveryContactBlackListReason);
|
|
|
191 |
|
|
|
192 |
$discoveryContactBlackListReason->company_id = $currentCompany->id;
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
$result = $discoveryContactBlackListReasonMapper->insert($discoveryContactBlackListReason);
|
|
|
196 |
|
|
|
197 |
if($result) {
|
|
|
198 |
$this->logger->info('Se agrego el motivo de lista negra del relevamiento de contacto : ' . $discoveryContactBlackListReason->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
199 |
|
|
|
200 |
$data = [
|
|
|
201 |
'success' => true,
|
|
|
202 |
'data' => 'LABEL_RECORD_ADDED'
|
|
|
203 |
];
|
|
|
204 |
} else {
|
|
|
205 |
$data = [
|
|
|
206 |
'success' => false,
|
|
|
207 |
'data' => $discoveryContactBlackListReasonMapper->getError()
|
|
|
208 |
];
|
|
|
209 |
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
return new JsonModel($data);
|
|
|
213 |
|
|
|
214 |
} else {
|
|
|
215 |
$messages = [];
|
|
|
216 |
$form_messages = (array) $form->getMessages();
|
|
|
217 |
foreach($form_messages as $fieldname => $field_messages)
|
|
|
218 |
{
|
|
|
219 |
|
|
|
220 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
return new JsonModel([
|
|
|
224 |
'success' => false,
|
|
|
225 |
'data' => $messages
|
|
|
226 |
]);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
} else {
|
|
|
230 |
$data = [
|
|
|
231 |
'success' => false,
|
|
|
232 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
233 |
];
|
|
|
234 |
|
|
|
235 |
return new JsonModel($data);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
return new JsonModel($data);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public function editAction()
|
|
|
242 |
{
|
|
|
243 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
244 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
245 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
246 |
|
|
|
247 |
$request = $this->getRequest();
|
|
|
248 |
$uuid = $this->params()->fromRoute('id');
|
|
|
249 |
|
|
|
250 |
|
|
|
251 |
if(!$uuid) {
|
|
|
252 |
$data = [
|
|
|
253 |
'success' => false,
|
|
|
254 |
'data' => 'ERROR_INVALID_PARAMETER'
|
|
|
255 |
];
|
|
|
256 |
|
|
|
257 |
return new JsonModel($data);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
$discoveryContactBlackListReasonMapper = DiscoveryContactBlackListReasonMapper::getInstance($this->adapter);
|
|
|
262 |
$discoveryContactBlackListReason = $discoveryContactBlackListReasonMapper->fetchOneByUuid($uuid);
|
|
|
263 |
if(!$discoveryContactBlackListReason) {
|
|
|
264 |
$data = [
|
|
|
265 |
'success' => false,
|
|
|
266 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
267 |
];
|
|
|
268 |
|
|
|
269 |
return new JsonModel($data);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
if($discoveryContactBlackListReason->company_id != $currentCompany->id) {
|
|
|
274 |
return new JsonModel([
|
|
|
275 |
'success' => false,
|
|
|
276 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
277 |
]);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
if($request->isPost()) {
|
|
|
281 |
$form = new BlackListReasonForm();
|
|
|
282 |
$dataPost = $request->getPost()->toArray();
|
|
|
283 |
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : DiscoveryContactBlackListReason::STATUS_INACTIVE;
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
$form->setData($dataPost);
|
|
|
287 |
|
|
|
288 |
if($form->isValid()) {
|
|
|
289 |
$dataPost = (array) $form->getData();
|
|
|
290 |
|
|
|
291 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
292 |
$hydrator->hydrate($dataPost, $discoveryContactBlackListReason);
|
|
|
293 |
$result = $discoveryContactBlackListReasonMapper->update($discoveryContactBlackListReason);
|
|
|
294 |
|
|
|
295 |
if($result) {
|
|
|
296 |
$this->logger->info('Se actualizo el motivo de lista negra del relevamiento de contacto ' . $discoveryContactBlackListReason->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
297 |
|
|
|
298 |
$data = [
|
|
|
299 |
'success' => true,
|
|
|
300 |
'data' => 'LABEL_RECORD_UPDATED'
|
|
|
301 |
];
|
|
|
302 |
} else {
|
|
|
303 |
$data = [
|
|
|
304 |
'success' => false,
|
|
|
305 |
'data' => $discoveryContactBlackListReasonMapper->getError()
|
|
|
306 |
];
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
return new JsonModel($data);
|
|
|
310 |
|
|
|
311 |
} else {
|
|
|
312 |
$messages = [];
|
|
|
313 |
$form_messages = (array) $form->getMessages();
|
|
|
314 |
foreach($form_messages as $fieldname => $field_messages)
|
|
|
315 |
{
|
|
|
316 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
return new JsonModel([
|
|
|
320 |
'success' => false,
|
|
|
321 |
'data' => $messages
|
|
|
322 |
]);
|
|
|
323 |
}
|
|
|
324 |
} else if ($request->isGet()) {
|
|
|
325 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
326 |
|
|
|
327 |
$data = [
|
|
|
328 |
'success' => true,
|
|
|
329 |
'data' => $hydrator->extract($discoveryContactBlackListReason)
|
|
|
330 |
];
|
|
|
331 |
|
|
|
332 |
return new JsonModel($data);
|
|
|
333 |
} else {
|
|
|
334 |
$data = [
|
|
|
335 |
'success' => false,
|
|
|
336 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
337 |
];
|
|
|
338 |
|
|
|
339 |
return new JsonModel($data);
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
return new JsonModel($data);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
public function deleteAction()
|
|
|
346 |
{
|
|
|
347 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
348 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
349 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
350 |
|
|
|
351 |
$request = $this->getRequest();
|
|
|
352 |
$uuid = $this->params()->fromRoute('id');
|
|
|
353 |
|
|
|
354 |
if(!$uuid) {
|
|
|
355 |
$data = [
|
|
|
356 |
'success' => false,
|
|
|
357 |
'data' => 'ERROR_INVALID_PARAMETER'
|
|
|
358 |
];
|
|
|
359 |
|
|
|
360 |
return new JsonModel($data);
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
$discoveryContactBlackListReasonMapper = DiscoveryContactBlackListReasonMapper::getInstance($this->adapter);
|
|
|
364 |
$discoveryContactBlackListReason = $discoveryContactBlackListReasonMapper->fetchOneByUuid($uuid);
|
|
|
365 |
if(!$discoveryContactBlackListReason) {
|
|
|
366 |
$data = [
|
|
|
367 |
'success' => false,
|
|
|
368 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
|
|
369 |
];
|
|
|
370 |
|
|
|
371 |
return new JsonModel($data);
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
if($discoveryContactBlackListReason->company_id != $currentCompany->id) {
|
|
|
375 |
return new JsonModel([
|
|
|
376 |
'success' => false,
|
|
|
377 |
'data' => 'ERROR_UNAUTHORIZED'
|
|
|
378 |
]);
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
if($request->isPost()) {
|
|
|
382 |
$result = $discoveryContactBlackListReasonMapper->delete($discoveryContactBlackListReason);
|
|
|
383 |
if($result) {
|
|
|
384 |
$this->logger->info('Se borro el motivo de lista negra del relevamiento de contacto ' . $discoveryContactBlackListReason->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
|
|
|
385 |
|
|
|
386 |
$data = [
|
|
|
387 |
'success' => true,
|
|
|
388 |
'data' => 'LABEL_RECORD_DELETED'
|
|
|
389 |
];
|
|
|
390 |
} else {
|
|
|
391 |
|
|
|
392 |
$data = [
|
|
|
393 |
'success' => false,
|
|
|
394 |
'data' => $discoveryContactBlackListReasonMapper->getError()
|
|
|
395 |
];
|
|
|
396 |
|
|
|
397 |
return new JsonModel($data);
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
} else {
|
|
|
401 |
$data = [
|
|
|
402 |
'success' => false,
|
|
|
403 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
404 |
];
|
|
|
405 |
|
|
|
406 |
return new JsonModel($data);
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
return new JsonModel($data);
|
|
|
410 |
}
|
|
|
411 |
}
|