Proyectos de Subversion LeadersLinked - Backend

Rev

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