<?php
namespace App\Controller\Frontend;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Media\ShareCart;
use App\Form\Media\ShareCartType;
use App\Entity\Media\Document;
use App\Form\Media\DownloadMultipleType;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use App\Entity\History\History;
use App\Entity\Media\Share;
use App\Entity\Media\Quality;
use App\Service\Site\SiteManager;
use App\Service\Media\DownloadManager;
class ShareController extends AbstractController
{
public function listAction(Request $request, SiteManager $site)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$accessSite = $site->userAccessSite($user);
$shares = $this->getDoctrine()->getManager()->getRepository(ShareCart::class)->findByAccessSite($accessSite);
return $this->render('Frontend/Views/Share/list.html.twig', array(
'shares' => $shares
));
}
public function editAction(Request $request, ShareCart $shareCart)
{
$form = $this->createForm(ShareCartType::class, $shareCart, ['qualities' => $shareCart->getAccessSite()->getQualities()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($shareCart);
$em->flush();
return $this->redirectToRoute('frontend_share_list');
}
return $this->render('Frontend/Views/Share/edit.html.twig', array(
'form' => $form->createView(),
'deleteForm' => $this->createDeleteForm($shareCart)->createView(),
'shareCart' => $shareCart
));
}
public function viewAction(Request $request, $token = null, DownloadManager $downloadManager)
{
$documents = array();
$form = $this->createForm(DownloadMultipleType::class, null, array());
$em = $this->getDoctrine()->getManager();
$share = $this->getDoctrine()->getManager()->getRepository(ShareCart::class)->findOneByToken($token);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$documentsIds = $form['documents']->getData();
foreach($documentsIds as $downloadId)
array_push($documents, $em->getRepository(Document::class)->find($downloadId));
if(!empty($documents))
{
$response = $downloadManager->downloadPictures($documents, $share->getQuality());
return $response;
}
}
return $this->render('Frontend/Views/Share/view.html.twig', array(
'share' => $share,
'form' => $form->createView()
));
}
public function deleteAction(Request $request, ShareCart $shareCart)
{
$em = $this->getDoctrine()->getManager();
$form = $this->createDeleteForm($shareCart);
$form->handleRequest($request);
if($request->isMethod('DELETE'))
{
if ($form->isValid())
{
$em->remove($shareCart);
$em->flush();
return $this->redirectToRoute('frontend_share_list');
}
}
}
private function createDeleteForm(ShareCart $shareCart)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('frontend_share_delete', array('id' => $shareCart->getId())))
->setMethod('DELETE')
->getForm();
}
public function documentViewAction(Request $request, Document $document)
{
$proofValidation = false;
if(sizeof($document->getSkippers()) > 0 || sizeof($document->getBoats()) > 0){
$referents = $this->getDoctrine()->getManager()->getRepository(\App\Entity\Federation\Participation::class)->getReferents($document->getRace(), $document->getBoats(), $document->getSkippers());
if(!empty($referents))
{
foreach ($referents as $key => $referent)
if(!empty($referent['id']))
$referents[$key] = $this->getDoctrine()->getManager()->getRepository(\App\Entity\User\User::class)->find($referent['id']);
}
}
return $this->render('Frontend/Views/Document/view-'.$document->getDocumentType()->getDirectoryName().'.html.twig', array(
'document' => $document,
'proofValidation' => $proofValidation
));
}
public function downloadAction(ShareCart $shareCart, Quality $quality, DownloadManager $downloadManager)
{
$response = $downloadManager->downloadPictures($shareCart->getDocuments(), $quality);
return $response;
}
public function shareToFlickerAction(ShareCart $shareCart, Document $parentDocument)
{
$flickerPhotoId = $document = null;
$fm = $this->get('flickr.sdkmanager');
foreach($parentDocument->getDocumentsChild() as $child)
if($child->getQuality()->getCode() == 'BD')
$document = $child;
if(!empty($document))
{
$copyright = '';
if(!empty($document->getCopyright()))
$copyright = "©".str_replace("©", "", $document->getCopyright())."<br>";
$description = $copyright."\n".$document->getLegend()."\n".$document->getDescription();
//1 - Upload Photo
$path = $this->get('media.fileManager')->getRootFileManager().$document->getRace()->getDirectoryName()."/".$document->getFile()['Key'];
$result = $fm->upload($path, $document->getTitle(), $description);
if(isset($result['photoid']))
$flickerPhotoId = $result['photoid'];
if($flickerPhotoId > 0)
{
if(empty($shareCart->getFlickerShare()))
{
//1 - Upload first image and Create Album
$album = $fm->createAlbum($shareCart->getName(), $shareCart->getComment(), [ $flickerPhotoId ]);
$shareCart->setFlickerAlbum($album);
$shareCart->setFlickerShare(1);
$this->getDoctrine()->getManager()->flush();
} else {
//2 - Upload photo and assign album
$album = $shareCart->getFlickerAlbum();
$fm->addPhotoToAlbum($album['id'], $flickerPhotoId);
}
}
}
return $this->json(['size' => $document->getFile()['Size']]);
}
}