16974 |
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\Log\LoggerInterface;
|
|
|
10 |
use Laminas\View\Model\ViewModel;
|
|
|
11 |
use Laminas\View\Model\JsonModel;
|
|
|
12 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
13 |
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
14 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
15 |
use LeadersLinked\Form\DiscoveryContact\ContactDownloadForm;
|
|
|
16 |
use LeadersLinked\Mapper\DiscoveryContactMapper;
|
|
|
17 |
use Laminas\Mvc\I18n\Translator;
|
|
|
18 |
use LeadersLinked\Library\Functions;
|
|
|
19 |
use Laminas\Http\Response;
|
|
|
20 |
use LeadersLinked\Mapper\DiscoveryContactBlackListMapper;
|
|
|
21 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
22 |
use Laminas\Db\Sql\Expression;
|
|
|
23 |
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
|
|
|
24 |
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
|
|
|
25 |
use LeadersLinked\Model\DiscoveryContact;
|
|
|
26 |
|
|
|
27 |
class DiscoveryContactReportUserController extends AbstractActionController
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
*
|
|
|
31 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
32 |
*/
|
|
|
33 |
private $adapter;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
38 |
*/
|
|
|
39 |
private $cache;
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
*
|
|
|
44 |
* @var \Laminas\Log\LoggerInterface
|
|
|
45 |
*/
|
|
|
46 |
private $logger;
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
*
|
|
|
50 |
* @var array
|
|
|
51 |
*/
|
|
|
52 |
private $config;
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
*
|
|
|
57 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
58 |
*/
|
|
|
59 |
private $translator;
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
*
|
|
|
64 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
65 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
66 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
67 |
* @param array $config
|
|
|
68 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
69 |
*/
|
|
|
70 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
71 |
{
|
|
|
72 |
$this->adapter = $adapter;
|
|
|
73 |
$this->cache = $cache;
|
|
|
74 |
$this->logger = $logger;
|
|
|
75 |
$this->config = $config;
|
|
|
76 |
$this->translator = $translator;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
public function indexAction()
|
|
|
81 |
{
|
|
|
82 |
|
|
|
83 |
$request = $this->getRequest();
|
|
|
84 |
if($request->isGet()) {
|
|
|
85 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
86 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
87 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
88 |
|
|
|
89 |
$headers = $request->getHeaders();
|
|
|
90 |
|
|
|
91 |
$isJson = false;
|
|
|
92 |
if($headers->has('Accept')) {
|
|
|
93 |
$accept = $headers->get('Accept');
|
|
|
94 |
|
|
|
95 |
$prioritized = $accept->getPrioritized();
|
|
|
96 |
|
|
|
97 |
foreach($prioritized as $key => $value) {
|
|
|
98 |
$raw = trim($value->getRaw());
|
|
|
99 |
|
|
|
100 |
if(!$isJson) {
|
|
|
101 |
$isJson = strpos($raw, 'json');
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if($isJson) {
|
|
|
108 |
$user_uuid = $this->params()->fromQuery('user');
|
|
|
109 |
if(!$user_uuid) {
|
|
|
110 |
|
|
|
111 |
return new JsonModel([
|
|
|
112 |
'success' => false,
|
|
|
113 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
114 |
]);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
118 |
$user = $userMapper->fetchOneByUuid($user_uuid);
|
|
|
119 |
|
|
|
120 |
if(!$user) {
|
|
|
121 |
return new JsonModel([
|
|
|
122 |
'success' => false,
|
|
|
123 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
124 |
]);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
$startDate = $this->params()->fromQuery('startDate');
|
|
|
129 |
if (empty($startDate)) {
|
|
|
130 |
$startDate = date('Y-m-d');
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
$endDate = $this->params()->fromQuery('endDate');
|
|
|
134 |
if (empty($endDate)) {
|
|
|
135 |
$endDate = date('Y-m-d');
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
|
|
|
139 |
$dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
if (!$dtStartDate || !$dtEndDate) {
|
|
|
144 |
$startDate = date('Y-m-d');
|
|
|
145 |
$endDate = date('Y-m-d');
|
|
|
146 |
} else {
|
|
|
147 |
|
|
|
148 |
if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
|
|
|
149 |
$startDate = date('Y-m-d');
|
|
|
150 |
$endDate = date('Y-m-d');
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
$discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
|
|
|
158 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
|
|
|
159 |
if(!$discoveryContactInteractionType) {
|
|
|
160 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
$discoveryContactBlackListMapper = DiscoveryContactBlackListMapper::getInstance($this->adapter);
|
|
|
168 |
$blacklist_ids = $discoveryContactBlackListMapper->fetchAllContactIdsByCompanyId($currentCompany->id);
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
173 |
$select = $queryMapper->getSql()->select();
|
|
|
174 |
$select->from(['a' => DiscoveryContactMapper::_TABLE]);
|
|
|
175 |
$select->join(['b' => DiscoveryContactInteractionMapper::_TABLE], 'a.id = b.contact_id', []);
|
|
|
176 |
$select->columns([
|
|
|
177 |
'first_name',
|
|
|
178 |
'last_name',
|
|
|
179 |
'personal_email',
|
|
|
180 |
'corporate_email',
|
|
|
181 |
'company',
|
|
|
182 |
'position',
|
|
|
183 |
'country',
|
|
|
184 |
'state',
|
|
|
185 |
'city',
|
|
|
186 |
'phone',
|
|
|
187 |
'phone_extension',
|
|
|
188 |
'celular',
|
|
|
189 |
'whatsapp',
|
|
|
190 |
'linkedin',
|
|
|
191 |
'sector',
|
|
|
192 |
'scholarship',
|
|
|
193 |
'added_on'
|
|
|
194 |
]);
|
|
|
195 |
|
|
|
196 |
if($blacklist_ids ) {
|
|
|
197 |
$select->where->notIn('a.id', $blacklist_ids );
|
|
|
198 |
}
|
|
|
199 |
$select->where->equalTo('b.interaction_type_id', $discoveryContactInteractionType ? $discoveryContactInteractionType->id : 0);
|
|
|
200 |
|
|
|
201 |
$select->where->equalTo('b.user_id', $user->id);
|
|
|
202 |
$select->where->between(new Expression('DATE(a.added_on)'), $startDate, $endDate);
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
$select->limit(100);
|
|
|
207 |
$select->order('added_on ASC');
|
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
$items = $queryMapper->fetchAll($select);
|
|
|
213 |
|
|
|
214 |
return new JsonModel([
|
|
|
215 |
'success' => true,
|
|
|
216 |
'data' => [
|
|
|
217 |
'items' => $items,
|
|
|
218 |
]
|
|
|
219 |
]);
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
} else {
|
|
|
223 |
$discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);
|
|
|
224 |
$user_ids = $discoveryContactInteractionMapper->fetchAllUserIdsForCompanyId($currentCompany->id);
|
|
|
225 |
|
|
|
226 |
$users = [];
|
|
|
227 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
228 |
$records = $userMapper->fetchAllByIds($user_ids);
|
|
|
229 |
foreach($records as $record)
|
|
|
230 |
{
|
|
|
231 |
$users[ $record->uuid ] = trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')';
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
$form = new ContactDownloadForm();
|
|
|
237 |
|
|
|
238 |
$this->layout()->setTemplate('layout/layout-backend.phtml');
|
|
|
239 |
$viewModel = new ViewModel();
|
|
|
240 |
$viewModel->setTemplate('leaders-linked/discovery-contact-report-by-user/index');
|
|
|
241 |
$viewModel->setVariables([
|
|
|
242 |
'form' => $form,
|
|
|
243 |
'users' => $users
|
|
|
244 |
]);
|
|
|
245 |
return $viewModel ;
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
return new JsonModel([
|
|
|
250 |
'success' => false,
|
|
|
251 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
252 |
]);;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public function downloadAction()
|
|
|
256 |
{
|
|
|
257 |
$request = $this->getRequest();
|
|
|
258 |
if($request->isGet()) {
|
|
|
259 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
260 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
261 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
262 |
|
|
|
263 |
$user_uuid = $this->params()->fromQuery('user');
|
|
|
264 |
if(!$user_uuid) {
|
|
|
265 |
|
|
|
266 |
return new JsonModel([
|
|
|
267 |
'success' => false,
|
|
|
268 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
269 |
]);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
273 |
$user = $userMapper->fetchOneByUuid($user_uuid);
|
|
|
274 |
|
|
|
275 |
if(!$user) {
|
|
|
276 |
return new JsonModel([
|
|
|
277 |
'success' => false,
|
|
|
278 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
279 |
]);
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
$startDate = $this->params()->fromQuery('startDate');
|
|
|
284 |
if (empty($startDate)) {
|
|
|
285 |
$startDate = date('Y-m-d');
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
$endDate = $this->params()->fromQuery('endDate');
|
|
|
289 |
if (empty($endDate)) {
|
|
|
290 |
$endDate = date('Y-m-d');
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
$dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
|
|
|
294 |
$dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
|
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
if (!$dtStartDate || !$dtEndDate) {
|
|
|
299 |
$startDate = date('Y-m-d');
|
|
|
300 |
$endDate = date('Y-m-d');
|
|
|
301 |
} else {
|
|
|
302 |
|
|
|
303 |
if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
|
|
|
304 |
$startDate = date('Y-m-d');
|
|
|
305 |
$endDate = date('Y-m-d');
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
$discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
|
|
|
311 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
|
|
|
312 |
if(!$discoveryContactInteractionType) {
|
|
|
313 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
$discoveryContactBlackListMapper = DiscoveryContactBlackListMapper::getInstance($this->adapter);
|
|
|
321 |
$blacklist_ids = $discoveryContactBlackListMapper->fetchAllContactIdsByCompanyId($currentCompany->id);
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
326 |
$select = $queryMapper->getSql()->select();
|
|
|
327 |
$select->from(['a' => DiscoveryContactMapper::_TABLE]);
|
|
|
328 |
$select->join(['b' => DiscoveryContactInteractionMapper::_TABLE], 'a.id = b.contact_id', []);
|
|
|
329 |
$select->columns([
|
|
|
330 |
'first_name',
|
|
|
331 |
'last_name',
|
|
|
332 |
'personal_email',
|
|
|
333 |
'corporate_email',
|
|
|
334 |
'company',
|
|
|
335 |
'position',
|
|
|
336 |
'country',
|
|
|
337 |
'state',
|
|
|
338 |
'city',
|
|
|
339 |
'phone',
|
|
|
340 |
'phone_extension',
|
|
|
341 |
'celular',
|
|
|
342 |
'whatsapp',
|
|
|
343 |
'linkedin',
|
|
|
344 |
'sector',
|
|
|
345 |
'scholarship',
|
|
|
346 |
'added_on'
|
|
|
347 |
]);
|
|
|
348 |
|
|
|
349 |
if($blacklist_ids ) {
|
|
|
350 |
$select->where->notIn('a.id', $blacklist_ids );
|
|
|
351 |
}
|
|
|
352 |
$select->where->equalTo('b.interaction_type_id', $discoveryContactInteractionType ? $discoveryContactInteractionType->id : 0);
|
|
|
353 |
|
|
|
354 |
$select->where->equalTo('b.user_id', $user->id);
|
|
|
355 |
$select->where->between(new Expression('DATE(a.added_on)'), $startDate, $endDate);
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
$fileName = 'reporte_relevamiento_de_contactos_por_usuario.csv';
|
|
|
360 |
$tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contactos_por_usuario_' . time(). '.csv');
|
|
|
361 |
|
|
|
362 |
$fp = fopen($tempFilename, 'w');
|
|
|
363 |
fputcsv($fp, [
|
|
|
364 |
$this->translator->translate('LABEL_FIRST_NAME'),
|
|
|
365 |
$this->translator->translate('LABEL_LAST_NAME'),
|
|
|
366 |
$this->translator->translate('LABEL_EMAIL'),
|
|
|
367 |
$this->translator->translate('LABEL_START'),
|
|
|
368 |
$this->translator->translate('LABEL_END'),
|
|
|
369 |
]);
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
|
373 |
fputcsv($fp, [
|
|
|
374 |
$user->first_name,
|
|
|
375 |
$user->last_name,
|
|
|
376 |
$user->email,
|
|
|
377 |
$startDate,
|
|
|
378 |
$endDate,
|
|
|
379 |
]);
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
fputcsv($fp, [
|
|
|
383 |
$this->translator->translate('LABEL_FIRST_NAME'),
|
|
|
384 |
$this->translator->translate('LABEL_LAST_NAME'),
|
|
|
385 |
$this->translator->translate('LABEL_PERSONAL_EMAIL'),
|
|
|
386 |
$this->translator->translate('LABEL_CORPORATE_EMAIL'),
|
|
|
387 |
$this->translator->translate('LABEL_COMPANY'),
|
|
|
388 |
$this->translator->translate('LABEL_POSITION'),
|
|
|
389 |
$this->translator->translate('LABEL_COUNTRY'),
|
|
|
390 |
$this->translator->translate('LABEL_STATE'),
|
|
|
391 |
$this->translator->translate('LABEL_CITY'),
|
|
|
392 |
$this->translator->translate('LABEL_PHONE'),
|
|
|
393 |
$this->translator->translate('LABEL_PHONE_EXTENSION'),
|
|
|
394 |
$this->translator->translate('LABEL_CELULAR'),
|
|
|
395 |
$this->translator->translate('LABEL_WHATSAPP'),
|
|
|
396 |
$this->translator->translate('LABEL_LINKEDIN'),
|
|
|
397 |
$this->translator->translate('LABEL_SECTOR'),
|
|
|
398 |
$this->translator->translate('LABEL_SCHOLARSHIP'),
|
|
|
399 |
$this->translator->translate('LABEL_ADDED_DATE')
|
|
|
400 |
]);
|
|
|
401 |
|
|
|
402 |
|
|
|
403 |
$items = $queryMapper->fetchAll($select);
|
|
|
404 |
|
|
|
405 |
foreach ( $items as $item )
|
|
|
406 |
{
|
|
|
407 |
fputcsv($fp,$item);
|
|
|
408 |
}
|
|
|
409 |
fclose($fp);
|
16975 |
efrain |
410 |
|
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
16974 |
efrain |
414 |
$items = null;
|
|
|
415 |
|
|
|
416 |
$zipName = 'reporte_relevamiento_de_contactos_por_usuario.zip';
|
|
|
417 |
$zipFilename = tempnam(sys_get_temp_dir(), $zipName);
|
|
|
418 |
|
16975 |
efrain |
419 |
|
16974 |
efrain |
420 |
|
|
|
421 |
|
16975 |
efrain |
422 |
$zip = new \ZipArchive;
|
|
|
423 |
if ($zip->open($zipFilename,\ZipArchive::CREATE|\ZipArchive::OVERWRITE) === TRUE) {
|
16974 |
efrain |
424 |
$zip->addFile($tempFilename, $fileName);
|
|
|
425 |
$zip->close();
|
|
|
426 |
|
|
|
427 |
|
|
|
428 |
$content = file_get_contents($zipFilename);
|
|
|
429 |
|
|
|
430 |
@unlink($tempFilename);
|
|
|
431 |
@unlink($zipFilename);
|
|
|
432 |
|
|
|
433 |
|
|
|
434 |
return new JsonModel([
|
|
|
435 |
'success' => true,
|
|
|
436 |
'data' => [
|
|
|
437 |
'content' => base64_encode($content),
|
|
|
438 |
'basename' => $zipName
|
|
|
439 |
|
|
|
440 |
]
|
|
|
441 |
]);
|
|
|
442 |
|
|
|
443 |
|
|
|
444 |
|
|
|
445 |
|
|
|
446 |
} else {
|
|
|
447 |
|
|
|
448 |
@unlink($tempFilename);
|
|
|
449 |
|
|
|
450 |
return new JsonModel([
|
|
|
451 |
'success' => true,
|
|
|
452 |
'data' => 'ERROR_ZIP_CREATE_FILE',
|
|
|
453 |
]);
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
|
|
|
457 |
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
return new JsonModel([
|
|
|
461 |
'success' => false,
|
|
|
462 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
463 |
]);;
|
|
|
464 |
}
|
|
|
465 |
}
|