<?php
/**
* Created by PhpStorm.
* User: lisavandaele
* Date: 5/02/15
* Time: 09:35
*/
namespace Notaris\FrontendBundle\Twig;
use Notaris\AdminBundle\Entity\Notary;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class SvenBuildExtension extends AbstractExtension{
protected $doctrine;
public function __construct(Registry $doctrine, Container $container) {
$this->doctrine = $doctrine;
$this->container = $container;
}
public function getFunctions() {
return array(
'getSvenBuild' => new TwigFunction( 'getSvenBuild', array($this, 'getSvenBuild')),
);
}
public function getSvenBuild(Notary $notary, $locale, $addressSlug = null) {
$manager = $this->doctrine->getManager();
$repo_build = $manager->getRepository('Notaris\AdminBundle\Entity\SvenBuild');
/** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressInfoRepository */
$svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
$address_info = $svenAddress_repo->getAddressFromNotary($notary, $addressSlug, $locale);
$build = $repo_build->getSvenBuildMenu($notary);
$builditemJobs = $repo_build->findOneBy(
array(
'notary'=>$notary,
'type'=>'jobs'
),
);
foreach ($build as $key=>$b):
if($b->getType() == 'jobs'):
unset($build[$key]);
endif;
if($b->getType() == 'people' and $address_info):
$repo_people_info = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryPeopleInfo');
$people_infos = $repo_people_info->getEmployeeFromNotary($notary, $locale, $address_info->getAddress());
if(!$people_infos):
unset($build[$key]);
endif;
endif;
if($b->getType() == 'downloads' and $address_info):
$repo_download = $manager->getRepository('Notaris\AdminBundle\Entity\SvenDownload');
$downloads = $repo_download->getDownloadsFromNotary($notary, $locale, $address_info->getAddress());
if(!$downloads):
unset($build[$key]);
endif;
endif;
endforeach;
if(!empty($builditemJobs)):
/** @var $repo_notary_job \Notaris\AdminBundle\Entity\NotaryJob */
$repo_notary_job = $this->em->getRepository('Notaris\AdminBundle\Entity\NotaryJob');
$jobs = $repo_notary_job->findByNotary($notary);
if($jobs):
$build[] = $builditemJobs;
endif;
endif;
usort($build, function($a, $b) {
return $a->getPosition() <=> $b->getPosition();
});
return $build;
}
public function getName()
{
return 'sven_build_extension';
}
}