Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16759 | Rev 16766 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16758 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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 LeadersLinked\Mapper\UserMapper;
14
use PhpOffice\PhpSpreadsheet\IOFactory;
15
use PhpOffice\PhpSpreadsheet\Spreadsheet;
16
use LeadersLinked\Form\DiscoveryContact\ContactDownloadForm;
17
use LeadersLinked\Mapper\DiscoveryContactMapper;
18
use Laminas\Mvc\I18n\Translator;
19
 
20
 
21
 
22
class DiscoveryContactReportController extends AbstractActionController
23
{
24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    private $adapter;
29
 
30
 
31
    /**
32
     *
33
     * @var AbstractAdapter
34
     */
35
    private $cache;
36
 
37
    /**
38
     *
39
     * @var  LoggerInterface
40
     */
41
    private $logger;
42
 
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $config;
48
 
49
    /**
50
     *
51
     * @var Translator
52
     */
53
    private $translator;
54
 
55
    /**
56
     *
57
     * @param AdapterInterface $adapter
58
     * @param AbstractAdapter $cache
59
     * @param LoggerInterface $logger
60
     * @param array $config
61
     * @param Translator $translator;
62
     */
63
    public function __construct($adapter, $cache, $logger, $config, $translator)
64
    {
65
        $this->adapter      = $adapter;
66
        $this->cache        = $cache;
67
        $this->logger       = $logger;
68
        $this->config       = $config;
69
        $this->translator   = $translator;
70
    }
71
 
72
 
73
    public function indexAction()
74
    {
75
 
76
        $request = $this->getRequest();
77
        if($request->isGet()) {
78
            $currentUserPlugin = $this->plugin('currentUserPlugin');
79
            $currentUser = $currentUserPlugin->getUser();
80
            $currentCompany = $currentUserPlugin->getCompany();
81
 
82
            $headers  = $request->getHeaders();
83
 
84
            $isJson = false;
85
            if($headers->has('Accept')) {
86
                $accept = $headers->get('Accept');
87
 
88
                $prioritized = $accept->getPrioritized();
89
 
90
                foreach($prioritized as $key => $value) {
91
                    $raw = trim($value->getRaw());
92
 
93
                    if(!$isJson) {
94
                        $isJson = strpos($raw, 'json');
95
                    }
96
 
97
                }
98
            }
99
 
100
            if($isJson) {
101
 
102
                $first_name      = filter_var($this->params()->fromQuery('first_name', ''), FILTER_SANITIZE_STRING);
103
                $last_name	     = filter_var($this->params()->fromQuery('last_name', ''), FILTER_SANITIZE_STRING);
104
                $corporate_email = filter_var($this->params()->fromQuery('corporate_email', ''), FILTER_SANITIZE_STRING);
105
                $company         = filter_var($this->params()->fromQuery('company', ''), FILTER_SANITIZE_STRING);
106
                $position        = filter_var($this->params()->fromQuery('position', ''), FILTER_SANITIZE_STRING);
107
                $country	     = filter_var($this->params()->fromQuery('country', ''), FILTER_SANITIZE_STRING);
108
                $state	         = filter_var($this->params()->fromQuery('state', ''), FILTER_SANITIZE_STRING);
109
                $city	         = filter_var($this->params()->fromQuery('city', ''), FILTER_SANITIZE_STRING);
110
                $personal_email  = filter_var($this->params()->fromQuery('personal_email', ''), FILTER_SANITIZE_STRING);
111
                $phone	         = filter_var($this->params()->fromQuery('phone', ''), FILTER_SANITIZE_STRING);
112
                $phone_extension = filter_var($this->params()->fromQuery('phone_extension', ''), FILTER_SANITIZE_STRING);
113
                $celular	     = filter_var($this->params()->fromQuery('celular', ''), FILTER_SANITIZE_STRING);
114
                $whatsapp        = filter_var($this->params()->fromQuery('whatsapp', ''), FILTER_SANITIZE_STRING);
115
                $linkedin	     = filter_var($this->params()->fromQuery('linkedin', ''), FILTER_SANITIZE_STRING);
116
 
117
                $queryMapper = QueryMapper::getInstance($this->adapter);
118
                $select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
119
 
120
                if($first_name) {
121
                    $select->where->like('first_name', '%'.$first_name.'%');
122
                }
123
                if($last_name) {
124
                    $select->where->like('last_name', '%'.$last_name.'%');
125
                }
126
                if($corporate_email) {
127
                    $select->where->like('corporate_email', '%'.$corporate_email.'%');
128
                }
129
                if($company) {
130
                    $select->where->like('company', '%'.$company.'%');
131
                }
132
                if($position) {
133
                    $select->where->like('position', '%'.$position.'%');
134
                }
135
                if($country) {
136
                    $select->where->like('country', '%'.$country.'%');
137
                }
138
                if($state) {
139
                    $select->where->like('state', '%'.$state.'%');
140
                }
141
                if($city) {
142
                    $select->where->like('city', '%'.$city.'%');
143
                }
144
                if($personal_email) {
145
                    $select->where->like('personal_email', '%'.$personal_email.'%');
146
                }
147
                if($phone) {
148
                    $select->where->like('phone', '%'.$phone.'%');
149
                }
150
                if($phone_extension) {
151
                    $select->where->like('phone_extension', '%'.$phone_extension.'%');
152
                }
153
                if($celular) {
154
                    $select->where->like('celular', '%'.$celular.'%');
155
                }
156
                if($whatsapp) {
157
                    $select->where->like('whatsapp', '%'.$whatsapp.'%');
158
                }
159
                if($linkedin) {
160
                    $select->where->like('linkedin', '%'.$linkedin.'%');
161
                }
162
                $select->limit(100);
163
                $select->order('added_on ASC');
164
 
165
                $items = $queryMapper->fetchAll($select);
166
 
167
                return new JsonModel([
168
                    'success' => true,
169
                    'data' => [
170
                        'items' => $items,
171
                    ]
172
                ]);
173
 
174
 
175
            } else {
176
                $form = new ContactDownloadForm();
177
 
178
                $this->layout()->setTemplate('layout/layout-backend.phtml');
179
                $viewModel = new ViewModel();
180
                $viewModel->setTemplate('leaders-linked/discovery-contact-report/index');
181
                $viewModel->setVariables([
182
                   'form' => $form
183
                ]);
184
                return $viewModel ;
185
            }
186
        }
187
 
188
        return new JsonModel([
189
            'success' => false,
190
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
191
        ]);;
192
    }
193
 
194
    public function downloadAction()
195
    {
196
        $request = $this->getRequest();
197
        if($request->isGet()) {
198
            $currentUserPlugin = $this->plugin('currentUserPlugin');
199
            $currentUser = $currentUserPlugin->getUser();
200
            $currentCompany = $currentUserPlugin->getCompany();
201
 
202
            $first_name      = filter_var($this->params()->fromQuery('first_name', ''), FILTER_SANITIZE_STRING);
203
            $last_name	     = filter_var($this->params()->fromQuery('last_name', ''), FILTER_SANITIZE_STRING);
204
            $corporate_email = filter_var($this->params()->fromQuery('corporate_email', ''), FILTER_SANITIZE_STRING);
205
            $company         = filter_var($this->params()->fromQuery('company', ''), FILTER_SANITIZE_STRING);
206
            $position        = filter_var($this->params()->fromQuery('position', ''), FILTER_SANITIZE_STRING);
207
            $country	     = filter_var($this->params()->fromQuery('country', ''), FILTER_SANITIZE_STRING);
208
            $state	         = filter_var($this->params()->fromQuery('state', ''), FILTER_SANITIZE_STRING);
209
            $city	         = filter_var($this->params()->fromQuery('city', ''), FILTER_SANITIZE_STRING);
210
            $personal_email  = filter_var($this->params()->fromQuery('personal_email', ''), FILTER_SANITIZE_STRING);
211
            $phone	         = filter_var($this->params()->fromQuery('phone', ''), FILTER_SANITIZE_STRING);
212
            $phone_extension = filter_var($this->params()->fromQuery('phone_extension', ''), FILTER_SANITIZE_STRING);
213
            $celular	     = filter_var($this->params()->fromQuery('celular', ''), FILTER_SANITIZE_STRING);
214
            $whatsapp        = filter_var($this->params()->fromQuery('whatsapp', ''), FILTER_SANITIZE_STRING);
215
            $linkedin	     = filter_var($this->params()->fromQuery('linkedin', ''), FILTER_SANITIZE_STRING);
216
 
217
            $queryMapper = QueryMapper::getInstance($this->adapter);
218
            $select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
219
            $select->columns([
220
                'first_name',
221
                'last_name',
222
                'personal_email',
223
                'corporate_email',
224
                'company',
225
                'position',
226
                'country',
227
                'state',
228
                'city',
229
                'phone',
230
                'phone_extension',
231
                'celular',
232
                'whatsapp',
233
                'linkedin',
234
                'sector',
16759 efrain 235
                'added_on'
16758 efrain 236
            ]);
237
 
238
            if($first_name) {
239
                $select->where->like('first_name', '%'.$first_name.'%');
240
            }
241
            if($last_name) {
242
                $select->where->like('last_name', '%'.$last_name.'%');
243
            }
244
            if($corporate_email) {
245
                $select->where->like('corporate_email', '%'.$corporate_email.'%');
246
            }
247
            if($company) {
248
                $select->where->like('company', '%'.$company.'%');
249
            }
250
            if($position) {
251
                $select->where->like('position', '%'.$position.'%');
252
            }
253
            if($country) {
254
                $select->where->like('country', '%'.$country.'%');
255
            }
256
            if($state) {
257
                $select->where->like('state', '%'.$state.'%');
258
            }
259
            if($city) {
260
                $select->where->like('city', '%'.$city.'%');
261
            }
262
            if($personal_email) {
263
                $select->where->like('personal_email', '%'.$personal_email.'%');
264
            }
265
            if($phone) {
266
                $select->where->like('phone', '%'.$phone.'%');
267
            }
268
            if($phone_extension) {
269
                $select->where->like('phone_extension', '%'.$phone_extension.'%');
270
            }
271
            if($celular) {
272
                $select->where->like('celular', '%'.$celular.'%');
273
            }
274
            if($whatsapp) {
275
                $select->where->like('whatsapp', '%'.$whatsapp.'%');
276
            }
277
            if($linkedin) {
278
                $select->where->like('linkedin', '%'.$linkedin.'%');
279
            }
280
 
281
            $spreadsheet = new Spreadsheet();
282
            $spreadsheet->getProperties()->setTitle('Relevamiento de Contactos');
283
 
284
            $sheetIndex = 0;
285
            $workSheet  =  $spreadsheet->setActiveSheetIndex($sheetIndex);
286
 
287
            $max_row = 100000;
288
            $col = 1;
289
            $row = 1;
290
            $items = $queryMapper->fetchAll($select);
16760 efrain 291
 
292
 
293
            $j = 0;
16758 efrain 294
            foreach ( $items as $item )
295
            {
16760 efrain 296
                $j++;
16758 efrain 297
 
298
                if($row == 1) {
299
                    $col = 1;
300
                    $labels = [
301
                        $this->translator->translate('LABEL_FIRST_NAME'),
302
                        $this->translator->translate('LABEL_LAST_NAME'),
303
                        $this->translator->translate('LABEL_PERSONAL_EMAIL'),
304
                        $this->translator->translate('LABEL_CORPORATE_EMAIL'),
305
                        $this->translator->translate('LABEL_COMPANY'),
306
                        $this->translator->translate('LABEL_POSITION'),
307
                        $this->translator->translate('LABEL_COUNTRY'),
308
                        $this->translator->translate('LABEL_STATE'),
309
                        $this->translator->translate('LABEL_CITY'),
310
                        $this->translator->translate('LABEL_PHONE'),
311
                        $this->translator->translate('LABEL_PHONE_EXTENSION'),
312
                        $this->translator->translate('LABEL_CELULAR'),
313
                        $this->translator->translate('LABEL_WHATSAPP'),
314
                        $this->translator->translate('LABEL_LINKEDIN'),
16759 efrain 315
                        $this->translator->translate('LABEL_SECTOR'),
316
                        $this->translator->translate('LABEL_ADDED_DATE')
16758 efrain 317
                    ];
318
 
319
 
320
                    foreach ($labels as $label)
321
                    {
322
                        $workSheet->setCellValue([$col, $row],$label );
323
                        $col++;
324
                    }
325
                    $row++;
326
                }
327
 
328
 
329
 
330
                $col = 1;
331
                foreach($item as $value)
332
                {
333
                    $workSheet->setCellValue([$col, $row], $value);
334
                    $col++;
335
                }
336
 
16759 efrain 337
 
16760 efrain 338
                $row++;
16758 efrain 339
                if($row > $max_row) {
340
                    $row = 1;
341
                    $workSheet = $spreadsheet->createSheet();
342
 
343
                }
344
 
345
            }
346
 
16760 efrain 347
 
348
 
16758 efrain 349
            $fileName = 'reporte_relevamiento_de_contactos_'  . date('d-m-Y-h-i-a', time()) . '.xls';
350
            $tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contactos_' . time());
351
 
352
            $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
353
            $writer->save($tempFilename);
354
 
355
            $content = file_get_contents($tempFilename);
356
            @unlink($tempFilename);
357
 
358
            return new JsonModel([
359
                'success' => true,
360
                'data' => [
361
                    'content' => base64_encode($content),
362
                    'basename' => $fileName
363
 
364
                ]
365
            ]);
366
 
367
        }
368
 
369
        return new JsonModel([
370
            'success' => false,
371
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
372
        ]);;
373
    }
374
 
375
 
376
 
377
 
378
}