src/Notaris/FrontendBundle/Twig/SvenBuildExtension.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: lisavandaele
  5.  * Date: 5/02/15
  6.  * Time: 09:35
  7.  */
  8. namespace Notaris\FrontendBundle\Twig;
  9. use Notaris\AdminBundle\Entity\Notary;
  10. use Doctrine\Bundle\DoctrineBundle\Registry;
  11. use Symfony\Component\DependencyInjection\ContainerInterface as Container;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFunction;
  14. class SvenBuildExtension extends AbstractExtension{
  15.     protected $doctrine;
  16.     public function __construct(Registry $doctrine, Container $container) {
  17.         $this->doctrine = $doctrine;
  18.         $this->container = $container;
  19.     }
  20.     public function getFunctions() {
  21.         return array(
  22.             'getSvenBuild' => new TwigFunction( 'getSvenBuild', array($this, 'getSvenBuild')),
  23.         );
  24.     }
  25.     public function getSvenBuild(Notary $notary, $locale, $addressSlug = null) {
  26.         $manager = $this->doctrine->getManager();
  27.         $repo_build = $manager->getRepository('Notaris\AdminBundle\Entity\SvenBuild');
  28.         /** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressInfoRepository */
  29.         $svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
  30.         $address_info = $svenAddress_repo->getAddressFromNotary($notary, $addressSlug, $locale);
  31.         $build = $repo_build->getSvenBuildMenu($notary);
  32.         $builditemJobs = $repo_build->findOneBy(
  33.             array(
  34.                 'notary'=>$notary,
  35.                 'type'=>'jobs'
  36.             ),
  37.         );
  38.         foreach ($build as $key=>$b):
  39.             if($b->getType() == 'jobs'):
  40.                 unset($build[$key]);
  41.             endif;
  42.             if($b->getType() == 'people' and $address_info):
  43.                 $repo_people_info = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryPeopleInfo');
  44.                 $people_infos = $repo_people_info->getEmployeeFromNotary($notary, $locale, $address_info->getAddress());
  45.                 if(!$people_infos):
  46.                     unset($build[$key]);
  47.                 endif;
  48.             endif;
  49.             if($b->getType() == 'downloads' and $address_info):
  50.                 $repo_download =  $manager->getRepository('Notaris\AdminBundle\Entity\SvenDownload');
  51.                 $downloads = $repo_download->getDownloadsFromNotary($notary, $locale, $address_info->getAddress());
  52.                 if(!$downloads):
  53.                     unset($build[$key]);
  54.                 endif;
  55.             endif;
  56.         endforeach;
  57.         if(!empty($builditemJobs)):
  58.             /** @var  $repo_notary_job \Notaris\AdminBundle\Entity\NotaryJob */
  59.             $repo_notary_job = $this->em->getRepository('Notaris\AdminBundle\Entity\NotaryJob');
  60.             $jobs = $repo_notary_job->findByNotary($notary);
  61.             if($jobs):
  62.                 $build[] = $builditemJobs;
  63.             endif;
  64.         endif;
  65.         usort($build, function($a, $b) {
  66.             return $a->getPosition() <=> $b->getPosition();
  67.         });
  68.         return $build;
  69.     }
  70.     public function getName()
  71.     {
  72.         return 'sven_build_extension';
  73.     }
  74. }