<?php
namespace App\AppBundle\Listener;
use App\AppBundle\Entity\Document;
use App\AppBundle\Entity\UserLoc;
use App\AppBundle\Entity\Notification;
use App\AppBundle\Entity\UserManagementDocument;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Symfony\Component\HttpFoundation\File\File;
use \Mailjet\Resources;
/**
* Listener des évènements d'upload de documents avec la dropzone
*
* @package AppBundle\Listener
*/
class UploadListener
{
private $em;
private $documentService;
/**
* Construire l'objet
*
* @param $em
* @param $documentService
*/
public function __construct($em, $documentService)
{
$this->em = $em;
$this->documentService = $documentService;
}
/**
*
* Envoyer des pushs
*
* @param $userTokens
* @param $title
* @param $message
* @return mixed
*/
public function sendPush2($userTokens, $title, $message)
{
$fields = json_encode([
'app_id' => "4ad6bceb-2300-41c1-9eb7-9870506830d2",
'include_player_ids' => $userTokens,
'data' => [],
'contents' => [
'fr' => $message,
'en' => $message,
],
'headings' => [
'fr' => $title,
'en' => $title,
],
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic NWZjZDNkNTMtOTZkMC00M2ZjLWEwNmMtNzU5YTI4N2RjNDI1'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Lancer des traitements après upload de documents
* @param PostPersistEvent $event
*/
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$entityId = $event->getRequest()->get('id');
$category = $event->getRequest()->get('category');
$userloc = $event->getRequest()->get('userloc');
switch ($category) {
case 'identity_card':
$parentCategory = 'identity';
break;
case 'proof_residency':
$parentCategory = 'identity';
break;
case 'tax_renter':
$parentCategory = 'proof';
break;
case 'pay_slip_renter':
$parentCategory = 'proof';
break;
case 'profil_guarantor':
$parentCategory = 'guarantors';
break;
case 'tax_guarantor':
$parentCategory = 'guarantors';
break;
case 'pay_slip_guarantor':
$parentCategory = 'guarantors';
break;
case 'other':
$parentCategory = 'others';
break;
case 'rib_owner':
$parentCategory = 'rib';
break;
default:
$parentCategory = $category;
break;
}
$document = new Document();
$document->setParentCategory($parentCategory);
$document->setFile($file);
$document->setTitle($event->getRequest()->files->get('file')->getClientOriginalName());
$document->setCategory($category);
$this->em->persist($document);
switch ($event->getType()) {
case 'user_management':
$userManagementDocument = new UserManagementDocument();
$userManagementDocument->setDocument($document);
$userManagementDocument->setOriginalName($document->getTitle());
$this->em->persist($userManagementDocument);
$user = $this->em->getRepository('AppBundle:User')->find($entityId);
$user->addManagementDocument($userManagementDocument);
$description = 'Un document ('.$document->getTitle().') est disponible dans votre onglet loyers. ';
/**
* envoie du mail doc gestion
*/
$mailto = $user->getemail();
$emailtoname = $user->getFirstname();
$mj = new \Mailjet\Client('94cad0ae03c89d27d428b9549de10c48', 'c868648747688a02902d23babd895c51', true, ['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => "laura@wizi.eu",
'Name' => "Laura de Wizi"
],
'To' => [
[
'Email' => $mailto,
'Name' => $emailtoname
]
],
'TemplateID' => 301457,
'TemplateLanguage' => true,
'Subject' => "Un nouveau document dans votre onglet loyers.",
'Variables' => json_decode('{"firstname": "' . $emailtoname . '","doctitle": "' . $document->getTitle() . '"}', true)
]
]
];
$desc = "Un nouveau document dans votre onglet loyers.";
$this->sendPush2([$user->getDeviceToken()], 'Wizi', $desc);
$myresponse = $mj->post(Resources::$Email, ['body' => $body]);
$myresponse->success() && $myresponse->getData();
$notification = new Notification();
$notification->setEmplacement('loyer');
$notification->setUser($user);
$notification->setDescription($description);
$notification->setColor('#3498db');
$notification->setIcon('far fa-file-invoice icon');
$notification->setLink('/#/app/paiements-owner');
$this->em->persist($notification);
break;
case 'user':
//$user = $this->em->getRepository('AppBundle:User')->find($entityId);
if(isset($userloc) && $userloc == 1){
$user = $this->em->getRepository('AppBundle:UserLoc')->find($entityId);
}else{
$user = $this->em->getRepository('AppBundle:User')->find($entityId);
}
$user->addDocument($document);
$this->em->persist($user);
$this->em->flush();
$filePath = $document->getId() . '.' . $document->getExtension();
/*
* ici gestion du filigrane
*/
if($document->getExtension() != 'pdf') {
$imagetoFil = $document->getAbsolutePath();
$cmdstr = "composite -dissolve 70% -gravity south /home/suser/olivier/filigrane.png " . $imagetoFil . " " . $imagetoFil;
\shell_exec($cmdstr);
}
$this->documentService->compressPicture($document->getAbsolutePath(), $filePath);
$file = new File($filePath);
$lightDocument = new Document();
$lightDocument->setFile($file);
$lightDocument->setCategory('light_' . $category);
$document->setLightDocument($lightDocument);
$this->em->persist($lightDocument);
$this->em->flush();
break;
case 'flat':
$flat = $this->em->getRepository('AppBundle:Flat')->find($entityId);
$flat->addDocument($document);
$this->em->flush();
$filePath = $document->getId().'.'.$document->getExtension();
$this->documentService->compressPicture($document->getAbsolutePath(), $filePath);
$file = new File($filePath);
$lightDocument = new Document();
$lightDocument->setFile($file);
$lightDocument->setCategory('light_picture');
$flat->addDocument($lightDocument);
$document->setLightDocument($lightDocument);
break;
case 'contract':
$contract = $this->em->getRepository('AppBundle:Contract')->find($entityId);
$contract->addDocument($document);
break;
case 'state_of_play_element':
$stateOfPlayElement = $this->em->getRepository('AppBundle:StateOfPlayElement')->find($entityId);
$stateOfPlayElement->addPicture($document);
break;
}
$this->em->flush();
}
}