Rev 16766 | Rev 16769 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Mapper\QueryMapper;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use LeadersLinked\Form\DiscoveryContact\ContactDownloadForm;
use LeadersLinked\Mapper\DiscoveryContactMapper;
use Laminas\Mvc\I18n\Translator;
use LeadersLinked\Library\Functions;
class DiscoveryContactReportController extends AbstractActionController
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var Translator
*/
private $translator;
/**
*
* @param AdapterInterface $adapter
* @param LoggerInterface $logger
* @param array $config
* @param Translator $translator;
*/
public function __construct($adapter, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
}
public function indexAction()
{
$request = $this->getRequest();
if($request->isGet()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$headers = $request->getHeaders();
$isJson = false;
if($headers->has('Accept')) {
$accept = $headers->get('Accept');
$prioritized = $accept->getPrioritized();
foreach($prioritized as $key => $value) {
$raw = trim($value->getRaw());
if(!$isJson) {
$isJson = strpos($raw, 'json');
}
}
}
if($isJson) {
$first_name = Functions::sanitizeFilterString($this->params()->fromQuery('first_name', ''));
$last_name = Functions::sanitizeFilterString($this->params()->fromQuery('last_name', ''));
$corporate_email = Functions::sanitizeFilterString($this->params()->fromQuery('corporate_email', ''));
$company = Functions::sanitizeFilterString($this->params()->fromQuery('company', ''));
$position = Functions::sanitizeFilterString($this->params()->fromQuery('position', ''));
$country = Functions::sanitizeFilterString($this->params()->fromQuery('country', ''));
$state = Functions::sanitizeFilterString($this->params()->fromQuery('state', ''));
$city = Functions::sanitizeFilterString($this->params()->fromQuery('city', ''));
$personal_email = Functions::sanitizeFilterString($this->params()->fromQuery('personal_email', ''));
$phone = Functions::sanitizeFilterString($this->params()->fromQuery('phone', ''));
$phone_extension = Functions::sanitizeFilterString($this->params()->fromQuery('phone_extension', ''));
$celular = Functions::sanitizeFilterString($this->params()->fromQuery('celular', ''));
$whatsapp = Functions::sanitizeFilterString($this->params()->fromQuery('whatsapp', ''));
$linkedin = Functions::sanitizeFilterString($this->params()->fromQuery('linkedin', ''));
$queryMapper = QueryMapper::getInstance($this->adapter);
$select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
if($first_name) {
$select->where->like('first_name', '%'.$first_name.'%');
}
if($last_name) {
$select->where->like('last_name', '%'.$last_name.'%');
}
if($corporate_email) {
$select->where->like('corporate_email', '%'.$corporate_email.'%');
}
if($company) {
$select->where->like('company', '%'.$company.'%');
}
if($position) {
$select->where->like('position', '%'.$position.'%');
}
if($country) {
$select->where->like('country', '%'.$country.'%');
}
if($state) {
$select->where->like('state', '%'.$state.'%');
}
if($city) {
$select->where->like('city', '%'.$city.'%');
}
if($personal_email) {
$select->where->like('personal_email', '%'.$personal_email.'%');
}
if($phone) {
$select->where->like('phone', '%'.$phone.'%');
}
if($phone_extension) {
$select->where->like('phone_extension', '%'.$phone_extension.'%');
}
if($celular) {
$select->where->like('celular', '%'.$celular.'%');
}
if($whatsapp) {
$select->where->like('whatsapp', '%'.$whatsapp.'%');
}
if($linkedin) {
$select->where->like('linkedin', '%'.$linkedin.'%');
}
$select->limit(100);
$select->order('added_on ASC');
$items = $queryMapper->fetchAll($select);
return new JsonModel([
'success' => true,
'data' => [
'items' => $items,
]
]);
} else {
$form = new ContactDownloadForm();
$this->layout()->setTemplate('layout/layout-backend.phtml');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/discovery-contact-report/index');
$viewModel->setVariables([
'form' => $form
]);
return $viewModel ;
}
}
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);;
}
public function downloadAction()
{
$request = $this->getRequest();
if($request->isGet()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$first_name = Functions::sanitizeFilterString($this->params()->fromQuery('first_name', ''));
$last_name = Functions::sanitizeFilterString($this->params()->fromQuery('last_name', ''));
$corporate_email = Functions::sanitizeFilterString($this->params()->fromQuery('corporate_email', ''));
$company = Functions::sanitizeFilterString($this->params()->fromQuery('company', ''));
$position = Functions::sanitizeFilterString($this->params()->fromQuery('position', ''));
$country = Functions::sanitizeFilterString($this->params()->fromQuery('country', ''));
$state = Functions::sanitizeFilterString($this->params()->fromQuery('state', ''));
$city = Functions::sanitizeFilterString($this->params()->fromQuery('city', ''));
$personal_email = Functions::sanitizeFilterString($this->params()->fromQuery('personal_email', ''));
$phone = Functions::sanitizeFilterString($this->params()->fromQuery('phone', ''));
$phone_extension = Functions::sanitizeFilterString($this->params()->fromQuery('phone_extension', ''));
$celular = Functions::sanitizeFilterString($this->params()->fromQuery('celular', ''));
$whatsapp = Functions::sanitizeFilterString($this->params()->fromQuery('whatsapp', ''));
$linkedin = Functions::sanitizeFilterString($this->params()->fromQuery('linkedin', ''));
$scholarship = Functions::sanitizeFilterString($this->params()->fromQuery('scholarship', ''));
$queryMapper = QueryMapper::getInstance($this->adapter);
$select = $queryMapper->getSql()->select(DiscoveryContactMapper::_TABLE);
$select->columns([
'first_name',
'last_name',
'personal_email',
'corporate_email',
'company',
'position',
'country',
'state',
'city',
'phone',
'phone_extension',
'celular',
'whatsapp',
'linkedin',
'sector',
'scholarship',
'added_on'
]);
if($first_name) {
$select->where->like('first_name', '%'.$first_name.'%');
}
if($last_name) {
$select->where->like('last_name', '%'.$last_name.'%');
}
if($corporate_email) {
$select->where->like('corporate_email', '%'.$corporate_email.'%');
}
if($company) {
$select->where->like('company', '%'.$company.'%');
}
if($position) {
$select->where->like('position', '%'.$position.'%');
}
if($country) {
$select->where->like('country', '%'.$country.'%');
}
if($state) {
$select->where->like('state', '%'.$state.'%');
}
if($city) {
$select->where->like('city', '%'.$city.'%');
}
if($personal_email) {
$select->where->like('personal_email', '%'.$personal_email.'%');
}
if($phone) {
$select->where->like('phone', '%'.$phone.'%');
}
if($phone_extension) {
$select->where->like('phone_extension', '%'.$phone_extension.'%');
}
if($celular) {
$select->where->like('celular', '%'.$celular.'%');
}
if($whatsapp) {
$select->where->like('whatsapp', '%'.$whatsapp.'%');
}
if($linkedin) {
$select->where->like('linkedin', '%'.$linkedin.'%');
}
if($scholarship) {
// scholarship
}
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setTitle('Relevamiento de Contactos');
$sheetIndex = 0;
$workSheet = $spreadsheet->setActiveSheetIndex($sheetIndex);
$max_row = 100000;
$col = 1;
$row = 1;
$items = $queryMapper->fetchAll($select);
$j = 0;
foreach ( $items as $item )
{
$j++;
if($row == 1) {
$col = 1;
$labels = [
$this->translator->translate('LABEL_FIRST_NAME'),
$this->translator->translate('LABEL_LAST_NAME'),
$this->translator->translate('LABEL_PERSONAL_EMAIL'),
$this->translator->translate('LABEL_CORPORATE_EMAIL'),
$this->translator->translate('LABEL_COMPANY'),
$this->translator->translate('LABEL_POSITION'),
$this->translator->translate('LABEL_COUNTRY'),
$this->translator->translate('LABEL_STATE'),
$this->translator->translate('LABEL_CITY'),
$this->translator->translate('LABEL_PHONE'),
$this->translator->translate('LABEL_PHONE_EXTENSION'),
$this->translator->translate('LABEL_CELULAR'),
$this->translator->translate('LABEL_WHATSAPP'),
$this->translator->translate('LABEL_LINKEDIN'),
$this->translator->translate('LABEL_SECTOR'),
$this->translator->translate('LABEL_SCHOLARSHIP'),
$this->translator->translate('LABEL_ADDED_DATE')
];
foreach ($labels as $label)
{
$workSheet->setCellValue([$col, $row],$label );
$col++;
}
$row++;
}
$col = 1;
foreach($item as $value)
{
$workSheet->setCellValue([$col, $row], $value);
$col++;
}
$row++;
if($row > $max_row) {
$row = 1;
$workSheet = $spreadsheet->createSheet();
}
}
$fileName = 'reporte_relevamiento_de_contactos_' . date('d-m-Y-h-i-a', time()) . '.xls';
$tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contactos_' . time());
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($tempFilename);
$content = file_get_contents($tempFilename);
@unlink($tempFilename);
return new JsonModel([
'success' => true,
'data' => [
'content' => base64_encode($content),
'basename' => $fileName
]
]);
}
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);;
}
}