<?php
namespace App\Helper;
use App\Constants\NewsConstants;
use App\Constants\TimeOffConstants;
use App\DTO\NewsDTO;
use App\Entity\Configuration;
use App\Entity\Document;
use App\Entity\News;
use App\Entity\NewsDocument;
use App\Entity\SettingsCompany;
use App\Entity\User;
use App\Service\FileUploader;
use Doctrine\Common\Persistence\ObjectManager;
use Exception;
use App\MDS\WebInOutTravelBundle\Constants\ApiNewsWebConstants;
use App\MDS\WebInOutTravelBundle\Service\ApiNewsWebService;
use PhpOffice\Common\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
class NewsHelper
{
private $manager;
private $baseURL;
/**
* @param ObjectManager $em
*/
public function __construct($em)
{
$this->manager = $em;
}
/**
* @param News $news
* @param $files
*/
public function storeFiles($news, $files) {
$fileUploader = new FileUploader();
$fileUploader->setBaseURL($this->baseURL);
$fileUploader->setTargetDirectory(NewsConstants::NEWS_DOCUMENT_DIR);
if (!is_null($files["document1"])) {
$doc1 = $files["document1"];
$news->setDocument1($this->getBaseURL() . $fileUploader->upload($doc1));
} else {
$news->setDocument1($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR . NewsConstants::NEWS_PLACEHOLDER_PICTURE);
}
if (!is_null($files["document2"])) {
$doc2 = $files["document2"];
$news->setDocument2($this->getBaseURL() . $fileUploader->upload($doc2));
}
if (!is_null($files["document3"])) {
$doc3 = $files["document3"];
$newFilenamePath = $this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR . $fileUploader->upload($doc3);
$doc3->move($fileUploader->getTargetDirectory(), $newFilenamePath);
$news->setDocument3($newFilenamePath);
}
}
/**
* @param News $news
* @param $files
*/
public function updateFiles($news, $files) {
$fileUploader = new FileUploader();
$fileUploader->setBaseURL($this->baseURL);
$fileUploader->setTargetDirectory(NewsConstants::NEWS_DOCUMENT_DIR);
$doc1 = $files["document1"];
if (!is_null($doc1)) {
if (!is_null($news->getDocument1())) {
$this->deleteFileByNewsID($news->getId(), NewsConstants::FIRST_PICTURE);
}
$news->setDocument1($this->getBaseURL() .$fileUploader->upload($doc1));
}
$doc2 = $files["document2"];
if (!is_null($doc2)) {
if (!is_null($news->getDocument2())) {
$this->deleteFileByNewsID($news->getId(), NewsConstants::SECOND_PICTURE);
}
$news->setDocument2($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR .$fileUploader->upload($doc2));
}
$doc3 = $files["document3"];
if (!is_null($files["document3"])) {
if (!is_null($news->getDocument3())) {
$this->deleteFileByNewsID($news->getId(), NewsConstants::PDF_FILE);
}
$newPDF = $fileUploader->upload($doc3);
$doc3->move($fileUploader->getTargetDirectory(), $newPDF);
$news->setDocument3($this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR .$newPDF);
}
}
/**
* @param $newsID
* @param $fileID
*/
public function deleteFileByNewsID($newsID, $fileID) {
$news = $this->manager->getRepository(News::class)->find($newsID);
switch($fileID) {
case NewsConstants::FIRST_PICTURE:
$filePath = $news->getDocument1();
$news->setDocument1(null);
break;
case NewsConstants::SECOND_PICTURE:
$filePath = $news->getDocument2();
$news->setDocument2(null);
break;
case NewsConstants::PDF_FILE:
$filePath = $news->getDocument3();
$news->setDocument3(null);
break;
default:
$filePath = "";
break;
}
$filePath = parse_url($filePath, PHP_URL_PATH);
// Finalmente lo borraré del disco
$this->deleteFile("./" . $filePath);
try {
$this->manager->persist($news);
$this->manager->flush();
} catch(Exception $exception) {
}
}
/**
* @param $settingCompanyID
* @param $statusEnablingValue
* @param string $defaultLanguage "es" by default; Another value: "en"
* @return array
*/
public function getListNewsBySettingCompany($settingCompanyID, $statusEnablingValue, $defaultLanguage="es") {
$newsDTOList = array();
if (NewsConstants::ALL_SETTINGS_COMPANY === intval($settingCompanyID) &&
NewsConstants::ANY_ENABLED_DISABLED === intval($statusEnablingValue)) {
$newsList = $this->manager->getRepository(News::class)
->findAll(
array('publicationDate' => 'DESC')
)
;
}elseif (NewsConstants::ALL_SETTINGS_COMPANY === intval($settingCompanyID) &&
NewsConstants::ANY_ENABLED_DISABLED !== intval($statusEnablingValue)) {
$newsList = $this->manager->getRepository(News::class)
->findNewsByAnySettingCompanyEnablingStatus(intval($statusEnablingValue));
} elseif (NewsConstants::ALL_SETTINGS_COMPANY !== intval($settingCompanyID) &&
NewsConstants::ANY_ENABLED_DISABLED === intval($statusEnablingValue)) {
$newsList = $this->manager->getRepository(News::class)
->findNewsBySettingsCompany(intval($settingCompanyID));
} else {
$newsList = $this->manager->getRepository(News::class)
->findBy(
array(
'company' => intval($settingCompanyID),
'enabled' => intval($statusEnablingValue)
),
array('publicationDate' => 'DESC')
)
;
}
if (!is_null($newsList)) {
foreach ($newsList as $news) {
/**
* @var News $news
*/
$newsDTO = new NewsDTO();
$newsDTO->setNewsID($news->getId());
$newsDTO->setToken($news->getToken());
$newsDTO->setNewsHeader($news->getHeader());
$newsDTO->setNewsHeadline($news->getHeadline());
$newsDTO->setObservations($news->getObservations());
$newsDTO->setCategory($news->getCategory());
$newsDTO->setNewsDateCreation($news->getPublicationDate());
if (is_null($news->getDocument1())) {
$doc1 = $this->getBaseURL() . NewsConstants::NEWS_DOCUMENT_DIR . NewsConstants::NEWS_PLACEHOLDER_PICTURE;
} else {
$doc1 = $news->getDocument1();
}
$newsDTO->setDocument1($doc1);
$newsDTO->setDocument2($news->getDocument2());
$newsDTO->setDocument3($news->getDocument3());
$language = NewsConstants::SPANISH_LANGUAGE;
if (NewsConstants::ENGLISH_ISO_CODE === $news->getLanguage()) {
$language = NewsConstants::ENGLISH_LANGUAGE;
}
$newsDTO->setLanguage($language);
$newsDTO->setEnabled($news->isEnabled());
$newsDTO->setPrice($news->getPrice());
$newsCompanyID = $news->getCompany();
if (!empty($newsCompanyID) && !is_null($newsCompanyID)) {
$companyNews = $this->manager->getRepository(SettingsCompany::class)
->find($newsCompanyID);
if (!empty($companyNews) && !is_null($companyNews)) {
$newsDTO->setUrlPublishing($this->getPublishingURLByToken($companyNews->getToken()));
}
}
$newsDTO->setSlug($news->getSlug());
$newsDTO->setMetaDescription($news->getMetaDescription());
$newsDTO->setMetaTitle($news->getMetaTitle());
/**
* @var SettingsCompany $settingsCompany
*/
$settingsCompany = $this->manager->getRepository(SettingsCompany::class)
->find($news->getCompany());
if(empty($settingsCompany)){
$settingsCompany = new SettingsCompany();
}
$newsDTO->setNewsSettingsCompany($settingsCompany->getCompany());
/**
* @var User $user
*/
$user = $this->manager->getRepository(User::class)
->find($news->getCreatedBy()) ?? new User();
$newsDTO->setNewsAuthor($user->getFullName());
$newsDTOList[] = $newsDTO;
}
}
return $newsDTOList;
}
public function deleteFile($filename) {
unlink($filename);
}
public function setBaseURL($baseURL) {
$this->baseURL = $baseURL;
}
public function getBaseURL() {
return $this->baseURL;
}
public static function getPublishingURLByToken ($token) {
$publishingURL = NewsConstants::$newsWebsites[$token];
return $publishingURL;
}
public function getNews($websiteReferer, $categoryList, $tokenList, $language) {
$newsList = array();
// Compruebo:
// 1. que la página web que solicita este recurso está entre las websites permitidas
$onlyHost = parse_url($websiteReferer, PHP_URL_HOST);
$isAllowedWeb = ApiNewsWebService::isAllowedWebsiteReferer($onlyHost);
// 2. que la lista de categorías NO esté vacía, que SÍ la hayan enviado y que tenga categorías permitidas.
$isAllowedCategory = ApiNewsWebService::isAllowedCategory($categoryList);
// 3. que la lista de tokens NO esté vacía y que SÍ la hayan enviado.
$isTokenValid = ApiNewsWebService::isTokenValid($tokenList);
if ($isAllowedWeb && $isAllowedCategory && $isTokenValid) {
$tokens = $tokenList;
if (is_array($tokenList)) {
$tokens = explode(",", $tokenList);
}
$categories = $categoryList;
if (is_array($categoryList)) {
$categories = explode(",", $categoryList);
}
$settingCompany = $this->manager->getRepository(Configuration::class)->findOneBy(
array(
'token' => $tokens
)
);
if(!empty($settingCompany) && !is_null($settingCompany)) {
// Generamos el listado
$newsDTOList = $this->getListNewsBySettingCompany($settingCompany->getId(), NewsConstants::ENABLED, $language);
if (!is_null($newsDTOList)) {
foreach($newsDTOList as $newsDTO) {
/**
* @var NewsDTO $newsDTO
*/
$newsDTOCategory = $newsDTO->getCategory();
$myCategories = $categoryList;
if (!is_array($categoryList)) {
$myCategories = str_split($categoryList, strlen($categoryList) + 1);
}
if (in_array($newsDTOCategory, $myCategories)){
$newsList[] = $newsDTO;
}
}
}
}
} else {
$newsList[] = [
"onlyHost" => $onlyHost,
"categoryList" => $categoryList,
"tokenList" => $tokenList,
"isAllowedWeb" => $isAllowedWeb,
"isAllowedCategory" => $isAllowedCategory,
"isTokenValid" => $isTokenValid,
]
;
}
$response_base = array(
'data' => $newsList,
);
return $response_base;
}
}