src/AppBundle/Listener/UploadListener.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\AppBundle\Listener;
  3. use App\AppBundle\Entity\Document;
  4. use App\AppBundle\Entity\UserLoc;
  5. use App\AppBundle\Entity\Notification;
  6. use App\AppBundle\Entity\UserManagementDocument;
  7. use Oneup\UploaderBundle\Event\PostPersistEvent;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use \Mailjet\Resources;
  10. /**
  11.  * Listener des évènements d'upload de documents avec la dropzone
  12.  *
  13.  * @package AppBundle\Listener
  14.  */
  15. class UploadListener
  16. {
  17.     private $em;
  18.     private $documentService;
  19.     /**
  20.      * Construire l'objet
  21.      *
  22.      * @param $em
  23.      * @param $documentService
  24.      */
  25.     public function __construct($em$documentService)
  26.     {
  27.         $this->em $em;
  28.         $this->documentService $documentService;
  29.     }
  30.     /**
  31.      *
  32.      * Envoyer des pushs
  33.      *
  34.      * @param $userTokens
  35.      * @param $title
  36.      * @param $message
  37.      * @return mixed
  38.      */
  39.     public function sendPush2($userTokens$title$message)
  40.     {
  41.         $fields json_encode([
  42.             'app_id' => "4ad6bceb-2300-41c1-9eb7-9870506830d2",
  43.             'include_player_ids' => $userTokens,
  44.             'data' => [],
  45.             'contents' => [
  46.                 'fr' => $message,
  47.                 'en' => $message,
  48.             ],
  49.             'headings' => [
  50.                 'fr' => $title,
  51.                 'en' => $title,
  52.             ],
  53.         ]);
  54.         $ch curl_init();
  55.         curl_setopt($chCURLOPT_URL"https://onesignal.com/api/v1/notifications");
  56.         curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
  57.             'Authorization: Basic NWZjZDNkNTMtOTZkMC00M2ZjLWEwNmMtNzU5YTI4N2RjNDI1'));
  58.         curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
  59.         curl_setopt($chCURLOPT_HEADERFALSE);
  60.         curl_setopt($chCURLOPT_POSTTRUE);
  61.         curl_setopt($chCURLOPT_POSTFIELDS$fields);
  62.         curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
  63.         $response curl_exec($ch);
  64.         curl_close($ch);
  65.         return $response;
  66.     }
  67.     /**
  68.      * Lancer des traitements après upload de documents
  69.      * @param PostPersistEvent $event
  70.      */
  71.     public function onUpload(PostPersistEvent $event)
  72.     {
  73.         $file $event->getFile();
  74.         $entityId $event->getRequest()->get('id');
  75.         $category $event->getRequest()->get('category');
  76.         $userloc $event->getRequest()->get('userloc');
  77.         switch ($category) {
  78.             case 'identity_card':
  79.                 $parentCategory 'identity';
  80.                 break;
  81.             case 'proof_residency':
  82.                 $parentCategory 'identity';
  83.                 break;
  84.             case 'tax_renter':
  85.                 $parentCategory 'proof';
  86.                 break;
  87.             case 'pay_slip_renter':
  88.                 $parentCategory 'proof';
  89.                 break;
  90.             case 'profil_guarantor':
  91.                 $parentCategory 'guarantors';
  92.                 break;
  93.             case 'tax_guarantor':
  94.                 $parentCategory 'guarantors';
  95.                 break;
  96.             case 'pay_slip_guarantor':
  97.                 $parentCategory 'guarantors';
  98.                 break;
  99.             case 'other':
  100.                 $parentCategory 'others';
  101.                 break;
  102.             case 'rib_owner':
  103.                 $parentCategory 'rib';
  104.                 break;
  105.             default:
  106.                 $parentCategory $category;
  107.                 break;
  108.         }
  109.         $document = new Document();
  110.         $document->setParentCategory($parentCategory);
  111.         $document->setFile($file);
  112.         $document->setTitle($event->getRequest()->files->get('file')->getClientOriginalName());
  113.         $document->setCategory($category);
  114.         $this->em->persist($document);
  115.         switch ($event->getType()) {
  116.             case 'user_management':
  117.                 $userManagementDocument = new UserManagementDocument();
  118.                 $userManagementDocument->setDocument($document);
  119.                 $userManagementDocument->setOriginalName($document->getTitle());
  120.                 $this->em->persist($userManagementDocument);
  121.                 $user $this->em->getRepository('AppBundle:User')->find($entityId);
  122.                 $user->addManagementDocument($userManagementDocument);
  123.                 $description 'Un document ('.$document->getTitle().') est disponible dans votre onglet loyers. ';
  124.                 /**
  125.                  * envoie du mail doc gestion
  126.                  */
  127.                 $mailto $user->getemail();
  128.                 $emailtoname $user->getFirstname();
  129.                 $mj = new \Mailjet\Client('94cad0ae03c89d27d428b9549de10c48''c868648747688a02902d23babd895c51'true, ['version' => 'v3.1']);
  130.                 $body = [
  131.                     'Messages' => [
  132.                         [
  133.                             'From' => [
  134.                                 'Email' => "laura@wizi.eu",
  135.                                 'Name' => "Laura de Wizi"
  136.                             ],
  137.                             'To' => [
  138.                                 [
  139.                                     'Email' => $mailto,
  140.                                     'Name' => $emailtoname
  141.                                 ]
  142.                             ],
  143.                             'TemplateID' => 301457,
  144.                             'TemplateLanguage' => true,
  145.                             'Subject' => "Un nouveau document dans votre onglet loyers.",
  146.                             'Variables' => json_decode('{"firstname": "' $emailtoname '","doctitle": "' $document->getTitle() . '"}'true)
  147.                         ]
  148.                     ]
  149.                 ];
  150.                 $desc "Un nouveau document dans votre onglet loyers.";
  151.                 $this->sendPush2([$user->getDeviceToken()], 'Wizi'$desc);
  152.                 $myresponse $mj->post(Resources::$Email, ['body' => $body]);
  153.                 $myresponse->success() && $myresponse->getData();
  154.                 $notification = new Notification();
  155.                 $notification->setEmplacement('loyer');
  156.                 $notification->setUser($user);
  157.                 $notification->setDescription($description);
  158.                 $notification->setColor('#3498db');
  159.                 $notification->setIcon('far fa-file-invoice icon');
  160.                 $notification->setLink('/#/app/paiements-owner');
  161.                 $this->em->persist($notification);
  162.                 break;
  163.             case 'user':
  164.                 //$user = $this->em->getRepository('AppBundle:User')->find($entityId);
  165.                 if(isset($userloc) && $userloc == 1){
  166.                     $user $this->em->getRepository('AppBundle:UserLoc')->find($entityId);
  167.                 }else{
  168.                     $user $this->em->getRepository('AppBundle:User')->find($entityId);
  169.                 }
  170.                 $user->addDocument($document);
  171.                 $this->em->persist($user);
  172.                 $this->em->flush();
  173.                 $filePath $document->getId() . '.' $document->getExtension();
  174.                 /*
  175.                  *  ici gestion du filigrane
  176.                  */
  177.                 if($document->getExtension() != 'pdf') {
  178.                     $imagetoFil $document->getAbsolutePath();
  179.                     $cmdstr "composite -dissolve 70% -gravity south /home/suser/olivier/filigrane.png " $imagetoFil " " $imagetoFil;
  180.                     \shell_exec($cmdstr);
  181.                 }
  182.                 $this->documentService->compressPicture($document->getAbsolutePath(), $filePath);
  183.                 $file = new File($filePath);
  184.                 $lightDocument = new Document();
  185.                 $lightDocument->setFile($file);
  186.                 $lightDocument->setCategory('light_' $category);
  187.                 $document->setLightDocument($lightDocument);
  188.                 $this->em->persist($lightDocument);
  189.                 $this->em->flush();
  190.                 break;
  191.             case 'flat':
  192.                 $flat $this->em->getRepository('AppBundle:Flat')->find($entityId);
  193.                 $flat->addDocument($document);
  194.                 $this->em->flush();
  195.                 $filePath $document->getId().'.'.$document->getExtension();
  196.                 $this->documentService->compressPicture($document->getAbsolutePath(), $filePath);
  197.                 $file = new File($filePath);
  198.                 $lightDocument = new Document();
  199.                 $lightDocument->setFile($file);
  200.                 $lightDocument->setCategory('light_picture');
  201.                 $flat->addDocument($lightDocument);
  202.                 $document->setLightDocument($lightDocument);
  203.                 break;
  204.             case 'contract':
  205.                 $contract $this->em->getRepository('AppBundle:Contract')->find($entityId);
  206.                 $contract->addDocument($document);
  207.                 break;
  208.             case 'state_of_play_element':
  209.                 $stateOfPlayElement $this->em->getRepository('AppBundle:StateOfPlayElement')->find($entityId);
  210.                 $stateOfPlayElement->addPicture($document);
  211.                 break;
  212.         }
  213.         $this->em->flush();
  214.     }
  215. }