src/Helper/NewsHelper.php line 230

Open in your IDE?
  1. <?php
  2. namespace App\Helper;
  3. use App\Constants\NewsConstants;
  4. use App\Constants\TimeOffConstants;
  5. use App\DTO\NewsDTO;
  6. use App\Entity\Configuration;
  7. use App\Entity\Document;
  8. use App\Entity\News;
  9. use App\Entity\NewsDocument;
  10. use App\Entity\SettingsCompany;
  11. use App\Entity\User;
  12. use App\Service\FileUploader;
  13. use Doctrine\Common\Persistence\ObjectManager;
  14. use Exception;
  15. use App\MDS\WebInOutTravelBundle\Constants\ApiNewsWebConstants;
  16. use App\MDS\WebInOutTravelBundle\Service\ApiNewsWebService;
  17. use PhpOffice\Common\File;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Response;
  20. class NewsHelper
  21. {
  22.     private $manager;
  23.     private $baseURL;
  24.     /**
  25.     * @param ObjectManager    $em
  26.     */
  27.     public function __construct($em)
  28.     {
  29.         $this->manager $em;
  30.     }
  31.     /**
  32.      * @param News $news
  33.      * @param $files
  34.      */
  35.     public function storeFiles($news$files) {
  36.         $fileUploader = new FileUploader();
  37.         $fileUploader->setBaseURL($this->baseURL);
  38.         $fileUploader->setTargetDirectory(NewsConstants::NEWS_DOCUMENT_DIR);
  39.         if (!is_null($files["document1"])) {
  40.             $doc1 $files["document1"];
  41.             $news->setDocument1($this->getBaseURL() . $fileUploader->upload($doc1));
  42.         } else {
  43.             $news->setDocument1($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR NewsConstants::NEWS_PLACEHOLDER_PICTURE);
  44.         }
  45.         if (!is_null($files["document2"])) {
  46.             $doc2 $files["document2"];
  47.             $news->setDocument2($this->getBaseURL() . $fileUploader->upload($doc2));
  48.         }
  49.         if (!is_null($files["document3"])) {
  50.             $doc3 $files["document3"];
  51.             $newFilenamePath $this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR $fileUploader->upload($doc3);
  52.             $doc3->move($fileUploader->getTargetDirectory(), $newFilenamePath);
  53.             $news->setDocument3($newFilenamePath);
  54.         }
  55.     }
  56.     /**
  57.      * @param News $news
  58.      * @param $files
  59.      */
  60.     public function updateFiles($news$files) {
  61.         $fileUploader = new FileUploader();
  62.         $fileUploader->setBaseURL($this->baseURL);
  63.         $fileUploader->setTargetDirectory(NewsConstants::NEWS_DOCUMENT_DIR);
  64.         $doc1 $files["document1"];
  65.         if (!is_null($doc1)) {
  66.             if (!is_null($news->getDocument1())) {
  67.                 $this->deleteFileByNewsID($news->getId(), NewsConstants::FIRST_PICTURE);
  68.             }
  69.             $news->setDocument1($this->getBaseURL() .$fileUploader->upload($doc1));
  70.         }
  71.         $doc2 $files["document2"];
  72.         if (!is_null($doc2)) {
  73.             if (!is_null($news->getDocument2())) {
  74.                 $this->deleteFileByNewsID($news->getId(), NewsConstants::SECOND_PICTURE);
  75.             }
  76.             $news->setDocument2($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR .$fileUploader->upload($doc2));
  77.         }
  78.         $doc3 $files["document3"];
  79.         if (!is_null($files["document3"])) {
  80.             if (!is_null($news->getDocument3())) {
  81.                 $this->deleteFileByNewsID($news->getId(), NewsConstants::PDF_FILE);
  82.             }
  83.             $newPDF $fileUploader->upload($doc3);
  84.             $doc3->move($fileUploader->getTargetDirectory(), $newPDF);
  85.             $news->setDocument3($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR .$newPDF);
  86.         }
  87.     }
  88.     /**
  89.      * @param $newsID
  90.      * @param $fileID
  91.      */
  92.     public function deleteFileByNewsID($newsID$fileID) {
  93.         $news $this->manager->getRepository(News::class)->find($newsID);
  94.         switch($fileID) {
  95.             case NewsConstants::FIRST_PICTURE:
  96.                 $filePath $news->getDocument1();
  97.                 $news->setDocument1(null);
  98.                 break;
  99.             case NewsConstants::SECOND_PICTURE:
  100.                 $filePath $news->getDocument2();
  101.                 $news->setDocument2(null);
  102.                 break;
  103.             case NewsConstants::PDF_FILE:
  104.                 $filePath $news->getDocument3();
  105.                 $news->setDocument3(null);
  106.                 break;
  107.             default:
  108.                 $filePath "";
  109.                 break;
  110.         }
  111.         $filePath parse_url($filePathPHP_URL_PATH);
  112.         // Finalmente lo borraré del disco
  113.         $this->deleteFile("./" $filePath);
  114.         try {
  115.             $this->manager->persist($news);
  116.             $this->manager->flush();
  117.         } catch(Exception $exception) {
  118.         }
  119.     }
  120.     /**
  121.      * @param $settingCompanyID
  122.      * @param $statusEnablingValue
  123.      * @param string $defaultLanguage "es" by default; Another value: "en"
  124.      * @return array
  125.      */
  126.     public function getListNewsBySettingCompany($settingCompanyID$statusEnablingValue$defaultLanguage="es") {
  127.         $newsDTOList = array();
  128.         if (NewsConstants::ALL_SETTINGS_COMPANY === intval($settingCompanyID) &&
  129.                 NewsConstants::ANY_ENABLED_DISABLED === intval($statusEnablingValue)) {
  130.             $newsList $this->manager->getRepository(News::class)
  131.                 ->findAll(
  132.                     array('publicationDate' => 'DESC')
  133.                 )
  134.             ;
  135.         }elseif (NewsConstants::ALL_SETTINGS_COMPANY === intval($settingCompanyID) &&
  136.                 NewsConstants::ANY_ENABLED_DISABLED !== intval($statusEnablingValue)) {
  137.             $newsList $this->manager->getRepository(News::class)
  138.                 ->findNewsByAnySettingCompanyEnablingStatus(intval($statusEnablingValue));
  139.         } elseif (NewsConstants::ALL_SETTINGS_COMPANY !== intval($settingCompanyID) &&
  140.                 NewsConstants::ANY_ENABLED_DISABLED === intval($statusEnablingValue)) {
  141.             $newsList $this->manager->getRepository(News::class)
  142.                 ->findNewsBySettingsCompany(intval($settingCompanyID));
  143.         } else {
  144.             $newsList $this->manager->getRepository(News::class)
  145.                 ->findBy(
  146.                     array(
  147.                         'company' => intval($settingCompanyID),
  148.                         'enabled' => intval($statusEnablingValue)
  149.                     ),
  150.                     array('publicationDate' => 'DESC')
  151.                 )
  152.             ;
  153.         }
  154.         if (!is_null($newsList)) {
  155.             foreach ($newsList as $news) {
  156.                 /**
  157.                  * @var News $news
  158.                  */
  159.                 $newsDTO = new NewsDTO();
  160.                 $newsDTO->setNewsID($news->getId());
  161.                 $newsDTO->setToken($news->getToken());
  162.                 $newsDTO->setNewsHeader($news->getHeader());
  163.                 $newsDTO->setNewsHeadline($news->getHeadline());
  164.                 $newsDTO->setObservations($news->getObservations());
  165.                 $newsDTO->setCategory($news->getCategory());
  166.                 $newsDTO->setNewsDateCreation($news->getPublicationDate());
  167.                 if (is_null($news->getDocument1())) {
  168.                     $doc1 $this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR NewsConstants::NEWS_PLACEHOLDER_PICTURE;
  169.                 } else {
  170.                     $doc1 $news->getDocument1();
  171.                 }
  172.                 $newsDTO->setDocument1($doc1);
  173.                 $newsDTO->setDocument2($news->getDocument2());
  174.                 $newsDTO->setDocument3($news->getDocument3());
  175.                 $language NewsConstants::SPANISH_LANGUAGE;
  176.                 if (NewsConstants::ENGLISH_ISO_CODE === $news->getLanguage()) {
  177.                     $language NewsConstants::ENGLISH_LANGUAGE;
  178.                 }
  179.                 $newsDTO->setLanguage($language);
  180.                 $newsDTO->setEnabled($news->isEnabled());
  181.                 $newsDTO->setPrice($news->getPrice());
  182.                 $newsCompanyID $news->getCompany();
  183.                 if (!empty($newsCompanyID) && !is_null($newsCompanyID)) {
  184.                     $companyNews $this->manager->getRepository(SettingsCompany::class)
  185.                         ->find($newsCompanyID);
  186.                     if (!empty($companyNews) && !is_null($companyNews)) {
  187.                         $newsDTO->setUrlPublishing($this->getPublishingURLByToken($companyNews->getToken()));
  188.                     }
  189.                 }
  190.                 $newsDTO->setSlug($news->getSlug());
  191.                 $newsDTO->setMetaDescription($news->getMetaDescription());
  192.                 $newsDTO->setMetaTitle($news->getMetaTitle());
  193.                 /**
  194.                  * @var SettingsCompany $settingsCompany
  195.                  */
  196.                 $settingsCompany $this->manager->getRepository(SettingsCompany::class)
  197.                     ->find($news->getCompany());
  198.                 if(empty($settingsCompany)){
  199.                     $settingsCompany = new SettingsCompany();
  200.                 }
  201.                 $newsDTO->setNewsSettingsCompany($settingsCompany->getCompany());
  202.                 /**
  203.                  * @var User $user
  204.                  */
  205.                 $user $this->manager->getRepository(User::class)
  206.                     ->find($news->getCreatedBy()) ?? new User();
  207.                 $newsDTO->setNewsAuthor($user->getFullName());
  208.                 $newsDTOList[] = $newsDTO;
  209.             }
  210.         }
  211.         return $newsDTOList;
  212.     }
  213.     public function deleteFile($filename) {
  214.         unlink($filename);
  215.     }
  216.     public function setBaseURL($baseURL) {
  217.         $this->baseURL $baseURL;
  218.     }
  219.     public function getBaseURL() {
  220.         return $this->baseURL;
  221.     }
  222.     public static function getPublishingURLByToken ($token) {
  223.         $publishingURL NewsConstants::$newsWebsites[$token];
  224.         return $publishingURL;
  225.     }
  226.     public function getNews($websiteReferer$categoryList$tokenList$language) {
  227.         $newsList = array();
  228.         // Compruebo:
  229.         // 1. que la página web que solicita este recurso está entre las websites permitidas
  230.         $onlyHost parse_url($websiteRefererPHP_URL_HOST);
  231.         $isAllowedWeb ApiNewsWebService::isAllowedWebsiteReferer($onlyHost);
  232.         // 2. que la lista de categorías NO esté vacía, que SÍ la hayan enviado y que tenga categorías permitidas.
  233.         $isAllowedCategory ApiNewsWebService::isAllowedCategory($categoryList);
  234.         // 3. que la lista de tokens NO esté vacía y que SÍ la hayan enviado.
  235.         $isTokenValid ApiNewsWebService::isTokenValid($tokenList);
  236.         if ($isAllowedWeb && $isAllowedCategory && $isTokenValid) {
  237.             $tokens $tokenList;
  238.             if (is_array($tokenList)) {
  239.                 $tokens explode(","$tokenList);
  240.             }
  241.             $categories $categoryList;
  242.             if (is_array($categoryList)) {
  243.                 $categories explode(","$categoryList);
  244.             }
  245.             $settingCompany $this->manager->getRepository(Configuration::class)->findOneBy(
  246.                 array(
  247.                     'token' => $tokens
  248.                 )
  249.             );
  250.             if(!empty($settingCompany) && !is_null($settingCompany)) {
  251.                 // Generamos el listado
  252.                 $newsDTOList $this->getListNewsBySettingCompany($settingCompany->getId(), NewsConstants::ENABLED$language);
  253.                 if (!is_null($newsDTOList)) {
  254.                     foreach($newsDTOList as $newsDTO) {
  255.                         /**
  256.                          * @var NewsDTO $newsDTO
  257.                          */
  258.                         $newsDTOCategory $newsDTO->getCategory();
  259.                         $myCategories $categoryList;
  260.                         if (!is_array($categoryList)) {
  261.                             $myCategories str_split($categoryListstrlen($categoryList) + 1);
  262.                         }
  263.                         if (in_array($newsDTOCategory$myCategories)){
  264.                             $newsList[] = $newsDTO;
  265.                         }
  266.                     }
  267.                 }
  268.             }
  269.         } else {
  270.             $newsList[] = [
  271.                 "onlyHost" => $onlyHost,
  272.                 "categoryList" => $categoryList,
  273.                 "tokenList" => $tokenList,
  274.                 "isAllowedWeb" => $isAllowedWeb,
  275.                 "isAllowedCategory" => $isAllowedCategory,
  276.                 "isTokenValid" => $isTokenValid,
  277.             ]
  278.             ;
  279.         }
  280.         $response_base = array(
  281.             'data' => $newsList,
  282.         );
  283.         return $response_base;
  284.     }
  285. }