<?php
namespace App\Controller;
use App\Entity\RequestPayload;
use App\Entity\PromozioniPortale;
use App\Entity\SegnalazioniPortale;
use App\Entity\SegnalazioniPortaleList;
use App\Security\Users;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as SecurityGranted;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
/**
* Richiediamo il ROLE_USER per ogni metodo presente in questo controller.
*
* @SecurityGranted("is_granted('ROLE_USER')")
***/
class HomePageController extends AbstractController
{
private $webservice;
private $id_contatto;
private Users $user;
private $urlGenerator;
private $feeds = [];
private $feedsPA = [];
/*
* @string stringa che contiene la lista degli ambiti utente concatenati per recuperare le segnalazioni
*/
private string $strAmbitiUtente;
public function __construct(string $kpm_ws_endpoint, string $kpm_ws_endpoint_user, string $kpm_ws_endpoint_password,UrlGeneratorInterface $urlGenerator)
{
$this->webservice = new WebServiceController($kpm_ws_endpoint, $kpm_ws_endpoint_user, $kpm_ws_endpoint_password);
$this->urlGenerator = $urlGenerator;
}
/**
* @Route("/", name="home_page")
*/
public function index(Security $security, Request $request): Response
{
/**
* @var $this- >user Users
*/
$this->user = $security->getUser();
$error = $request->get('error');
if ($error == '') {
$session = $request->getSession();
$error = $session->get('error');
if ($error != '')
$session->set('error', '');
}
$recordfound = false;
//print_r($this->user->getId());
$tickets = [];
$servizi = [];
if ($this->user) {
/*
* gestione cards Formazione e Promozioni
*/
$contactid = $this->user->getId();
$rec_formazione = [];
$rec_promozione = [];
$rec_news = [];
$rec_eventi = [];
# $rec_formazione = $this->getRecords('Formazione', 4);
$rec_formazione = $this->getRecordsProfiled('Formazione',$contactid);
$this->shuffle_with_keys($rec_formazione);
//ADD PROFILAZIONE
$rec_formazione = array_slice($rec_formazione, 0, 6);
# $rec_promozione = $this->getRecords('Promozione', 12);
$rec_promozione = $this->getRecordsProfiled('Promozione',$contactid);
$this->shuffle_with_keys($rec_promozione);
$rec_promozione = array_slice($rec_promozione, 0, 6);
$str_feeds = $session->get('feeds');
$this->feeds = new \SimpleXMLElement($str_feeds);
$rec_news = $this->getNewsFromFeed(); //$this->getRecords('News',3);
$str_feedsPA = $session->get('feedsPA');
$this->feedsPA = new \SimpleXMLElement($str_feedsPA);
$rec_newslinkpa = $this->getNewsLinkPA();
$rec_webinar = $this->getNewsFromFeed(true);
$recordfound = true;
$account = $this->user->getAccount();
#TODO gestire $csrfToken
#$csrfToken = $request->request->get('_csrf_token');
#RECUPERIAMO I TICKET
$body_params = new \stdClass;
$body_params->mine = 'true';
$body_params->macrotipo = 'SW';
$body_params->contactid = $this->user->getId();
$body_params->accountid = $account->getId();
$body_params->countstatus = true;
$payload = new RequestPayload();
$payload->setClass("HelpDesk");
$payload->setMethod("getTickets");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
$return_gettickets = $ws_response->getData();
if ($ws_response->isSuccess()) {
$tickets = $return_gettickets;
}
$segnalazioni = [];
$segnalazioniPortlaleList = $this->getSegnalazioniWS();
foreach ($segnalazioniPortlaleList->getAll() as $segnalazione) {
$segnalazioni[] = $segnalazione;
}
#RECUPERIAMO I SERVIZI
$body_params->macrotipo = 'servizi';
$payload = new RequestPayload();
$payload->setClass("HelpDesk");
$payload->setMethod("getTickets");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
$return_gettickets = $ws_response->getData();
if ($ws_response->isSuccess()) {
$servizi = $return_gettickets;
}
}
return $this->render('home_page/home.html.twig', [
'controller_name' => 'HomePageController', 'recordfound' => $recordfound, 'tickets' => $tickets,
'servizi' => $servizi, 'segnalazioni' => $segnalazioni,
'rec_formazione' => $rec_formazione,
'rec_promozione' => $rec_promozione,
'rec_news' => $rec_news,
'rec_eventi' => $rec_webinar,
'rec_newslinkpa' => $rec_newslinkpa,
'error' => $error,
]);
}
/*
* funzione che prende i record di formazione da WS
*/
private function getRecords($tipo, $limit): array
{
$body_params = new \stdClass;
$body_params->tipo_promozione = $tipo;
$body_params->limit = $limit;
$payload = new RequestPayload();
$payload->setClass("Promozioni");
$payload->setMethod("getPromozioniList");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
$promozioni = [];
$email = "";
//dobbiamo recuperare la email del contatto utilizzato e completare l'url ,altrimenti rimane vuota
$email = $this->user->getEmail();
if ($ws_response->isSuccess() == 1) {
foreach ($ws_response->getData() as $promorec) {
$promo = new PromozioniPortale();
$promo->promozionecreatedtime = $promorec['promozionecreatedtime'];
$promo->promozionemodifiedtime = $promorec['promozionemodifiedtime'];
$promo->promozione_no = $promorec['promozione_no'];
$promo->bu_mc = $promorec['bu_mc'];
$promo->nome_promozione = $promorec['nome_promozione'];
$promo->descrizione = $promorec['descrizione'];
$promo->tag = $promorec['tag'];
$promo->datafine = $promorec['datafine'];
$promo->datainizio = $promorec['datainizio'];
$promo->modalita = $promorec['modalita'];
$promo->promozioniportaleid = $promorec['promozioniportaleid'];
$promo->tipo_promozione = $promorec['tipo_promozione'];
($promo->modalita == 'Webinar') ? $promo->url_riferimento = $promorec['url_riferimento'] . $email : $promo->url_riferimento = $promorec['url_riferimento'];
$promo->area = $promorec['area'];
$promozioni[] = $promo;
}
}
return $promozioni;
}
/*
* funzione che prende i record di formazione da WS
*/
private function getRecordsProfiled($tipo,$contactid): array
{
$body_params = new \stdClass;
$body_params->tipo_promozione = $tipo;
$body_params->contactid = $contactid;
$payload = new RequestPayload();
$payload->setClass("Promozioni");
$payload->setMethod("getPromozioniListProfiled");
$payload->setParameters($body_params);
#die(var_dump($payload));
$ws_response = $this->webservice->call($payload);
$promozioni = [];
$email = "";
//dobbiamo recuperare la email del contatto utilizzato e completare l'url ,altrimenti rimane vuota
$email = $this->user->getEmail();
if ($ws_response->isSuccess() == 1) {
foreach ($ws_response->getData() as $promorec) {
$promo = new PromozioniPortale();
$promo->promozionecreatedtime = $promorec['promozionecreatedtime'];
$promo->promozionemodifiedtime = $promorec['promozionemodifiedtime'];
$promo->promozione_no = $promorec['promozione_no'];
$promo->bu_mc = $promorec['bu_mc'];
$promo->nome_promozione = $promorec['nome_promozione'];
$promo->descrizione = $promorec['descrizione'];
$promo->tag = $promorec['tag'];
$promo->datafine = $promorec['datafine'];
$promo->datainizio = $promorec['datainizio'];
$promo->modalita = $promorec['modalita'];
$promo->promozioniportaleid = $promorec['promozioniportaleid'];
$promo->tipo_promozione = $promorec['tipo_promozione'];
($promo->modalita == 'Webinar') ? $promo->url_riferimento = $promorec['url_riferimento'] . $email : $promo->url_riferimento = $promorec['url_riferimento'];
$promo->area = $promorec['area'];
$promo->algoritmo = $promorec['algoritmo'];
$promozioni[] = $promo;
}
}
return $promozioni;
}
private function getNewsFromFeed($onlyWebinar = false): array
{
$news = [];
$url = "https://www.gruppokib.it/feed/";
$conta = 0;
if (count($this->feeds) == 0) {
if (@simplexml_load_file($url)) {
$this->feeds = simplexml_load_file($url);
}
}
foreach ($this->feeds->channel->item as $feed) {
if ($conta >= 3)
continue;
$strCategorie = "";
foreach ($feed->category as $category) {
$strCategorie .= "," . $category;
}
if ($onlyWebinar) {
if (strpos(strtolower($strCategorie), "webinar") === false)
continue;
} else {
if (strpos(strtolower($strCategorie), "webinar"))
continue;
}
$promo = new PromozioniPortale();
$data = date('d-m-Y', strtotime($feed->pubDate));
$promo->promozionecreatedtime = $data;
$promo->promozionemodifiedtime = $feed->pubDate;
$promo->nome_promozione = (string)($feed->title);
$promo->datafine = $data;
$promo->datainizio = $data;
$promo->url_riferimento = $feed->guid;
$news[] = $promo;
$conta++;
}
return $news;
}
private function getNewsLinkPA(): array
{
$news = [];
$url = "https://linkpa.it/feed/";
$conta = 0;
if (count($this->feedsPA) == 0) {
if (@simplexml_load_file($url)) {
$this->feeds = simplexml_load_file($url);
}
}
foreach ($this->feedsPA->channel->item as $feed) {
//print_r($feed);
if ($conta >= 4)
continue;
$strCategorie = "";
foreach ($feed->category as $category) {
$strCategorie .= "," . $category;
}
/*if ($onlyWebinar) {
if (strpos(strtolower($strCategorie), "webinar") === false)
continue;
} else {
if (strpos(strtolower($strCategorie), "webinar"))
continue;
}*/
$promo = new PromozioniPortale();
$data = date('d-m-Y', strtotime($feed->pubDate));
$promo->promozionecreatedtime = $data;
$promo->promozionemodifiedtime = $feed->pubDate;
$promo->nome_promozione = (string)($feed->title);
$promo->datafine = $data;
$promo->datainizio = $data;
$promo->url_riferimento = $feed->guid;
$news[] = $promo;
$conta++;
}
return $news;
}
private function getSegnalazioniWS(): SegnalazioniPortaleList
{
//dobbiamo recuperare gli ambiti dell'utente per passarli al WS che ritornerà solo le segnalazioni che riguardano l'utente
$ambiti = $this->user->getAmbitiSw();
$ambiti1 = $this->user->getAmbitiServizi();
$ambiti = $ambiti . ' |##| ' . $ambiti1;
$segnalazioni = new SegnalazioniPortaleList();
$body_params = new \stdClass;
$body_params->ambiti = $ambiti;
$payload = new RequestPayload();
$payload->setClass("Segnalazioni");
$payload->setMethod("getSegnalazioniList");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
foreach ($ws_response->getData() as $segnalaz) {
$segnalazione = new SegnalazioniPortale();
$segnalazione->setSegnalazioneNo($segnalaz['segnalazione_no']);
$segnalazione->setId($segnalaz['segnalazioniportaleid']);
$segnalazione->setNomeSegnalazione($segnalaz['nome_segnalazione']);
$segnalazione->setDescrizione($segnalaz['descrizione']);
$segnalazione->setDataInizio($segnalaz['data_inizio']);
$segnalazione->setOraInizio($segnalaz['ora_inizio']);
$segnalazione->setDataFine($segnalaz['data_fine']);
$segnalazione->setOraFine($segnalaz['ora_fine']);
$segnalazione->setStatoUrgenza($segnalaz['stato_urgenza']);
$segnalazione->setAmbiti($segnalaz['category']);
$segnalazioni->add($segnalazione);
}
}
return $segnalazioni;
}
/**
* @Route("/consenso-privacy", name="app_set_consenso")
*/
public function setConsensoPrivacy(Request $request,Security $security): response
{
$consenso = $request->get('consenso');
$user = $security->getUser();
$userid = $security->getUser()->getId();
$body_params = new \stdClass;
$body_params->contactid = $userid;
$body_params->consenso_utilizzo_dati = $consenso;
$payload = new RequestPayload();
$payload->setClass("Authenticate");
$payload->setMethod("setConsensoPrivacy");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
} else {
}
$user->setConsenso_utilizzo_dati($consenso);
$referal = 'home_page';
$referal_url = $this->urlGenerator->generate($referal);
return new RedirectResponse($referal_url);
}
/**
* @Route("/nascondi-Wizard", name="app_nascondiWizard")
*/
public function nascondiWizard(Request $request, Security $security): response
{
$user = $security->getUser();
$userid = $security->getUser()->getId();
$body_params = new \stdClass;
$body_params->contactid = $userid;
$payload = new RequestPayload();
$payload->setClass("Authenticate");
$payload->setMethod("nascondiWizard");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
} else {
}
$user->setNascondiWizard(true);
$referal = 'home_page';
$referal_url = $this->urlGenerator->generate($referal);
return new RedirectResponse($referal_url);
}
/**
* @Route("/register-click/{id_enc}/{posizione}/{setype}/{algoritmo}/{referer}/", name="app_registerClick" )
*/
public function registerClick(Request $request,Security $security, $id_enc, $posizione, $setype, $algoritmo,$referer) :response
{
$user = $security->getUser();
//MM 13-07-2023 dobbiamo gestire le chiamate pre-login quindi se non abbiamo l'user
//loggato dobbiamo valorizzare $userid a 0
if(!$user == null) {
$userid = $security->getUser()->getId();
}
else
{
$userid = 0;
}
$caller_ip = $request->getClientIp();
$promozione = new PromozioniPortale();
$id = $promozione->fromFollowLink($id_enc);
$focus = $this->getEntityWS($id,$setype);
foreach ($focus as $mod_field => $val){
$promozione->$mod_field = $val;
}
$ambiti_sw_arr = array();
$ambiti_servizi_arr = array();
//MM 13-07-2023 facciamo solo se siamo loggati
$ambiti_str = "";
if(!$user == null) {
if (!empty($user->getAmbitiSw())) {
$ambiti_sw_arr = explode(' |##| ', $user->getAmbitiSw());
}
if (!empty($user->getAmbitiServizi())) {
$ambiti_servizi_arr = explode(' |##| ', $user->getAmbitiServizi());
}
$ambiti_arr = array_merge($ambiti_servizi_arr,$ambiti_sw_arr);
$ambiti_str = implode(' |##| ',$ambiti_arr);
}
$body_params = new \stdClass;
$body_params->contactid = $userid;
$body_params->crmid = $id;
$body_params->posizione = $posizione;
$body_params->url = $promozione->url_riferimento;
$body_params->setype = $setype;
$body_params->portaltype = $promozione->tipo_promozione;
$body_params->tags = $promozione->tag;
$body_params->algoritmo = $algoritmo;
$body_params->ambiti = $ambiti_str;
$body_params->referer = $referer;
$body_params->caller_ip = $caller_ip;
$payload = new RequestPayload();
$payload->setClass("Utils");
$payload->setMethod("setClick");
$payload->setParameters($body_params);
if($user->getConsenso_utilizzo_dati()=='Sì') {
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
} else {
}
}
return new RedirectResponse($promozione->url_riferimento);
}
/**
* @Route("/home_redirect-Generic/{url}/{setype}/{algoritmo}/{referer}/", name="app_home_registerClickGeneric" )
*/
public function registerClickGeneric(Request $request,Security $security, string $url, string $setype, string $algoritmo, string $referer) :response
{
$user = $security->getUser();
//MM 13-07-2023 dobbiamo gestire le chiamate pre-login quindi se non abbiamo l'user
//loggato dobbiamo valorizzare $userid a 0
if(!$user == null) {
$userid = $security->getUser()->getId();
}
else
{
$userid = 0;
}
$url = strtr($url, '_', '/');
//MM 13-07-2023 facciamo solo se siamo loggati
$ambiti_str = "";
if(!$user == null) {
if (!empty($user->getAmbitiSw())) {
$ambiti_sw_arr = explode(' |##| ', $user->getAmbitiSw());
}
if (!empty($user->getAmbitiServizi())) {
$ambiti_servizi_arr = explode(' |##| ', $user->getAmbitiServizi());
}
$ambiti_arr = array_merge($ambiti_servizi_arr,$ambiti_sw_arr);
$ambiti_str = implode(' |##| ',$ambiti_arr);
}
$caller_ip = $request->getClientIp();
$body_params = new \stdClass;
$body_params->contactid = $userid;
$body_params->crmid = 0;
$body_params->posizione = 0;
$body_params->url = $url;
$body_params->setype = $setype;
$body_params->portaltype = '';
$body_params->tags = "";
$body_params->algoritmo = $algoritmo;
$body_params->ambiti = $ambiti_str;
$body_params->referer = $referer;
$body_params->caller_ip = $caller_ip;
$payload = new RequestPayload();
$payload->setClass("Utils");
$payload->setMethod("setClick");
$payload->setParameters($body_params);
if($userid == 0)
{
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
} else {
}
}else
{
if($user->getConsenso_utilizzo_dati()=='Sì') {
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
} else {
}
}
}
return new RedirectResponse($url);
}
function shuffle_with_keys(&$array)
{
/* Auxiliary array to hold the new order */
$aux = array();
/* We work with an array of the keys */
$keys = array_keys($array);
/* We shuffle the keys */
shuffle($keys);
/* We iterate thru' the new order of the keys */
foreach ($keys as $key) {
/* We insert the key, value pair in its new order */
$aux[$key] = $array[$key];
/* We remove the element from the old array to save memory */
unset($array[$key]);
}
$array = $aux;
}
private function getEntityWS($id,$module){
$body_params = new \stdClass;
$body_params->id = $id;
$body_params->module = $module;
$payload = new RequestPayload();
$payload->setClass("Utils");
$payload->setMethod("retrieveEntityModelWS");
$payload->setParameters($body_params);
$ws_response = $this->webservice->call($payload);
if ($ws_response->isSuccess() == 1) {
$focus = $ws_response->getData();
}
return $focus;
}
}