Created
October 2, 2014 12:56
-
-
Save kolyadin/46da309a0433f337dceb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace fifa\app\controllers\site; | |
use fifa\app\controllers\ControllerInterface; | |
use fifa\app\controllers\GenericController; | |
use fifa\lib\Config; | |
use fifa\lib\TwigHelper; | |
use fifa\model\content\Cheerleader; | |
use fifa\model\content\CheerleaderFactory; | |
use fifa\model\content\Comment; | |
use fifa\model\content\Image; | |
use fifa\model\content\ImageFactory; | |
use fifa\model\content\MediaFile; | |
use fifa\model\content\MediaFileFactory; | |
use fifa\model\dataMaps\CommentDataMap; | |
use fifa\model\system\users\GuestUser; | |
use fifa\model\system\users\User; | |
use fifa\model\system\users\UserFactory; | |
class MediaController extends GenericController implements ControllerInterface { | |
public function getRoutes() { | |
$this | |
->getApp() | |
->get('/:country/media(/page:page)', function ($country, $page = null) { | |
$this->mediaPage($country, $page); | |
}) | |
->conditions([ | |
':country' => '(' . implode('|', GenericController::$langs) . ')' | |
]); | |
$this | |
->getApp() | |
->map('/:country/media/:imageId', function ($country, $imageId) { | |
switch ($this->getApp()->request->getMethod()) { | |
default: | |
$this->showFile($country, $imageId); | |
break; | |
case 'POST': | |
$this->postComment($country); | |
break; | |
} | |
}) | |
->via('GET', 'POST') | |
->conditions([ | |
':country' => '(' . implode('|', GenericController::$langs) . ')', | |
':imageId' => '[1-9][0-9]*' | |
]); | |
} | |
public function postComment($country) { | |
$request = $this->getApp()->request; | |
$fileId = $request->post('mediaFileId'); | |
$content = $request->post('comment'); | |
$file = MediaFileFactory::getMediaFile($fileId); | |
if (!($file instanceof MediaFile)) { | |
$this->getApp()->notFound(); | |
} | |
if (UserFactory::getCurrentUser() instanceof GuestUser) { | |
$this->getApp()->notFound(); | |
} | |
$comment = new Comment(); | |
$comment->setUserId(UserFactory::getCurrentUser()->getId()); | |
$comment->setCreatedAt(time()); | |
$comment->setMediaFileId($file->getId()); | |
$comment->setType(Comment::TYPE_MEDIA); | |
$comment->setContent($content); | |
(new CommentDataMap())->save($comment); | |
if ($comment->getId() > 0) { | |
$this->getApp()->redirect(sprintf('/%s/media/%u#comments', $country, $file->getId())); | |
} | |
} | |
public function showFile($country, $fileId) { | |
$file = MediaFileFactory::proxy()->findById($fileId); | |
if (!($file instanceof MediaFile)) { | |
$this->getApp()->notFound(); | |
} | |
if ($file->getType() == MediaFile::TYPE_IMAGE) { | |
$this->showImage($country, $file->getId()); | |
} elseif ($file->getType() == MediaFile::TYPE_VIDEO) { | |
$this->showVideo($country, $file->getId()); | |
} | |
} | |
public function showVideo($country, $fileId) { | |
$file = MediaFileFactory::proxy()->findById($fileId); | |
$next = MediaFileFactory::proxy()->findNext($file); | |
$prev = MediaFileFactory::proxy()->findPrev($file); | |
//Комменты | |
$comments = (new CommentDataMap())->findByCheerleader($file, [], 0, 10, $totalFound); | |
foreach ($comments as &$comment) { | |
if ($comment->getUser() instanceof User && $comment->getUser()->getAvatar() instanceof Image) { | |
if ($comment->getUser()->getAvatar()->getId() > 0) { | |
$thumb = $comment->getUser()->getAvatar()->magick(function ($file, $size = [29, 29]) { | |
$im = new \Imagick($file); | |
$im->thumbnailImage($size[0], $size[1]); | |
return $im; | |
}); | |
$comment->getUser()->getAvatar()->setThumb('comment', $thumb); | |
} | |
} | |
} | |
$this | |
->getApp() | |
->render("/locale/$country/SingleMedia.twig", [ | |
'file' => $file, | |
'next' => $next, | |
'prev' => $prev, | |
'comments' => $comments, | |
'totalFound' => $totalFound | |
]); | |
} | |
public function showImage($country, $fileId) { | |
$file = MediaFileFactory::proxy()->findById($fileId); | |
$next = MediaFileFactory::proxy()->findNext($file); | |
$prev = MediaFileFactory::proxy()->findPrev($file); | |
$image = $file->getParentId(); | |
$thumb = $image->magick(function ($file, $size = [940, 530]) use($image) { | |
$im = new \Imagick($file); | |
$im->thumbnailImage($size[0], $size[1]); | |
$need = [940, 530]; | |
$newHeight = ceil($image->getHeight() * $need[0] / $image->getWidth()); | |
$im->thumbnailImage($need[0], $newHeight); | |
$im->cropImage($need[0], $need[1], 0, 0); | |
return $im; | |
}); | |
$image->setThumb('small', $thumb); | |
//Комменты | |
$comments = (new CommentDataMap())->findByMediaFile($file, [], 0, 10, $totalFound); | |
foreach ($comments as &$comment) { | |
if ($comment->getUser() instanceof User && $comment->getUser()->getAvatar() instanceof Image) { | |
if ($comment->getUser()->getAvatar()->getId() > 0) { | |
$thumb = $comment->getUser()->getAvatar()->magick(function ($file, $size = [29, 29]) { | |
$im = new \Imagick($file); | |
$im->thumbnailImage($size[0], $size[1]); | |
return $im; | |
}); | |
$comment->getUser()->getAvatar()->setThumb('comment', $thumb); | |
} | |
} | |
} | |
$this | |
->getApp() | |
->render("/locale/$country/SingleMedia.twig", [ | |
'file' => $file, | |
'next' => $next, | |
'prev' => $prev, | |
'comments' => $comments, | |
'totalFound' => $totalFound | |
]); | |
} | |
public function mediaPage($country, $page) { | |
if (is_null($page)) { | |
$page = 1; | |
} | |
$city = $this->getApp()->request->get('city'); | |
$onlyPhoto = $this->getApp()->request->get('onlyPhoto'); | |
$onlyVideo = $this->getApp()->request->get('onlyVideo'); | |
$onPage = 45; | |
$options = []; | |
$paginatorHelper = []; | |
if ($city > 0) { | |
$options = array_merge($options, [ | |
'city' => $city | |
]); | |
$paginatorHelper['city'] = $city; | |
} | |
if ($onlyPhoto) { | |
$options = array_merge($options, [ | |
'type' => 'image' | |
]); | |
$paginatorHelper['onlyPhoto'] = 1; | |
} | |
if ($onlyVideo) { | |
$options = array_merge($options, [ | |
'type' => 'video' | |
]); | |
$paginatorHelper['onlyVideo'] = 1; | |
} | |
$paginatorUrl = []; | |
foreach ($paginatorHelper as $part => $value) { | |
$paginatorUrl[] = "$part=$value"; | |
} | |
if (isset($paginatorUrl[0])){ | |
$paginatorUrl[0] = '?'.$paginatorUrl[0]; | |
} | |
$mediaFiles = MediaFileFactory::proxy()->findAll($options, ($page - 1) * $onPage, $onPage, $totalFound); | |
foreach ($mediaFiles as $file) { | |
if ($file->getType() == MediaFile::TYPE_IMAGE) { | |
/** @var \fifa\model\content\Image $image */ | |
$image = $file->getParentId(); | |
$thumb = $image->magick(function ($file, $size = [0, 200]) use($image) { | |
$im = new \Imagick($file); | |
// $im->thumbnailImage($size[0], $size[1]); | |
$need = [940, 530]; | |
$newHeight = ceil($image->getHeight() * $need[0] / $image->getWidth()); | |
$im->thumbnailImage($need[0], $newHeight); | |
$im->cropImage($need[0], $need[1], 0, 0); | |
$im->thumbnailImage(0, 200); | |
return $im; | |
}); | |
$image->setThumb('small', $thumb); | |
} | |
} | |
$this | |
->getApp() | |
->render("/locale/$country/Media.twig", [ | |
'mediaFiles' => $mediaFiles, | |
'commentsCount' => $totalFound, | |
'paginator' => [ | |
'pages' => ceil($totalFound / $onPage), | |
'active' => $page, | |
'url' => implode('&',$paginatorUrl) | |
] | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment