<?php
/**
* @Author: stephan <m6ahenina@gmail.com>
* @Date: 2019-11-21 12:57:41
* @Last Modified by: stephan <m6ahenina@gmail.com>
* @Last Modified time: 2020-02-07 14:09:26
*/
namespace App\Twig;
use App\Entity\{Company,User};
use App\Utils\Resolver;
use IntlDateFormatter;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Vich\UploaderBundle\Storage\StorageInterface;
class AppExtension extends AbstractExtension
{
public function __construct(StorageInterface $storage, Security $security, Packages $packages, RequestStack $requestStack, $defaultLocale)
{
$this->storage = $storage;
$this->security = $security;
$this->packages = $packages;
$this->requestStack = $requestStack;
$this->defaultLocale = $defaultLocale;
}
public function getFilters(): array
{
return [
new TwigFilter('file_size', [$this, 'fileSize']),
new TwigFilter('intl_date', [$this, 'intlDate']),
];
}
public function getFunctions(): array
{
return [
new TwigFunction('user_avatar', [$this, 'userAvatar']),
new TwigFunction('company_logo', [$this, 'companyLogo']),
new TwigFunction('user_cover', [$this, 'userCover']),
new TwigFunction('resolve', [$this, 'resolve']),
new TwigFunction('intl_date', [$this, 'intlDate']),
new TwigFunction('assoc_array', [$this, 'assocArray']),
];
}
public function resolve($path, $defaultValue = null)
{
return Resolver::resolve($path, $defaultValue);
}
public function fileSize($size)
{
if (!$size || !is_numeric($size)) return $size;
$size = (int) $size;
$exts = ['B', 'KB', 'MB', 'GB'];
$dept = 0;
for ($i = 0; $i < count($exts); $i++) {
$dept = $i;
if ($size >= 1024*1.5) {
$size = $size / 1024;
} else {
break;
}
}
return number_format($size, $dept > 1 ? 2 : 0, ',', '.') . ' ' . $exts[$dept];
}
/**
* Return the given user photo(avatar|cover)'s uri
* If no user is provided, return the current user's photo'url
*
* @param User $user
* @return string User photo url
*/
public function getUserPhoto(?User $user = null, $prop='', $uploadableField='', $defaultValue='', $packageName = 'images')
{
if ($user) {
if (Resolver::resolve([$user, $prop], null)) {
return $this->storage->resolveUri($user, $uploadableField, User::class);
}
} else if ($user == $this->security->getUser()) {
if (Resolver::resolve([$user, $prop], null)) {
return $this->storage->resolveUri($user, $uploadableField, User::class);
}
}
return $this->packages->getUrl($defaultValue, $packageName);
}
public function getCompanyLogo(?Company $company = null, $prop = null, $uploadableField = null, $defaultValue = null, $packageName = 'images'): string
{
if ($company) {
if (Resolver::resolve([$company, $prop], null)) {
return $this->storage->resolveUri($company, $uploadableField, Company::class);
}
} /*else if ($company = $this->security->getUser()->getCompany()) {
if (Resolver::resolve([$company, $prop], null)) {
return $this->storage->resolveUri($company, $uploadableField, Company::class);
}
}*/
return '';
}
/**
* Return the given user avatar's url
* If no user is provided, return the current user's avatar'url
*
* @param User $user
* @return string User avatar url
*/
public function userAvatar(?User $user = null): string
{
$userPhoto = $this->getUserPhoto($user, 'avatar', 'avatarFile', 'user.png', 'images');
if(!empty($user)){
if(!empty($user->getAvatar())){
return "/images/avatar/".$user->getAvatar();
}
}
if(empty($userPhoto)){
return "/images/user.png";
}
return $this->getUserPhoto($user, 'avatar', 'avatarFile', 'user.png', 'images');
}
/**
* Return the given company logo's url
* If no company is provided, return the current company's logo'url
*
* @param Company $company
* @return string Company logo url
*/
public function companyLogo(?Company $company = null): string
{
return $this->getCompanyLogo($company, 'logo', 'logoFile', 'user.png', 'images');
}
public function userCover(?User $user = null): string
{
return $this->getUserPhoto($user, 'cover', 'coverFile', 'user_cover.png', 'images');
}
public function intlDate(\DateTime $date = null, $pattern = "d/m/Y", $locale = null)
{
if (is_null($date)) {
return '';
}
if (is_null($locale)) {
$request = $this->requestStack->getCurrentRequest();
$locale = Resolver::resolve([$request, 'locale'], $this->defaultLocale);
}
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$formatter->setPattern($pattern);
return ucwords($formatter->format($date));
}
public function assocArray(string $key, $value, array $source = []): ?array
{
$dest = $source;
$dest[$key] = $value;
return $dest;
}
}