Rev 16802 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(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;use Laminas\Http\Response;class DiscoveryContactReportController extends AbstractActionController{/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @var \LeadersLinked\Cache\CacheInterface*/private $cache;/**** @var \Laminas\Log\LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var \Laminas\Mvc\I18n\Translator*/private $translator;/**** @param \Laminas\Db\Adapter\AdapterInterface $adapter* @param \LeadersLinked\Cache\CacheInterface $cache* @param \Laminas\Log\LoggerInterface LoggerInterface $logger* @param array $config* @param \Laminas\Mvc\I18n\Translator $translator*/public function __construct($adapter, $cache, $logger, $config, $translator){$this->adapter = $adapter;$this->cache = $cache;$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}$fileName = 'reporte_relevamiento_de_contactos.csv';$tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contactos_' . time(). '.csv');$fp = fopen($tempFilename, 'w');fputcsv($fp, [$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')]);$items = $queryMapper->fetchAll($select);foreach ( $items as $item ){fputcsv($fp,$item);}fclose($fp);$items = null;$zipName = 'reporte_relevamiento_de_contactos.zip';$zipFilename = tempnam(sys_get_temp_dir(), $zipName);$zip = new \ZipArchive();if ($zip->open($zipFilename) === TRUE) {$zip->addFile($tempFilename, $fileName);$zip->close();$content = file_get_contents($zipFilename);@unlink($tempFilename);@unlink($zipFilename);return new JsonModel(['success' => true,'data' => ['content' => base64_encode($content),'basename' => $zipName]]);} else {@unlink($tempFilename);return new JsonModel(['success' => true,'data' => 'ERROR_ZIP_CREATE_FILE',]);}}return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);;}}