|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
-
- namespace Lc\ShopBundle\Twig;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\ReductionCartInterface;
- use Lc\ShopBundle\Context\TicketInterface;
- use Lc\ShopBundle\Repository\ProductRepository;
- use Symfony\Contracts\Translation\TranslatorInterface;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
-
- class BackendTwigExtension extends AbstractExtension
- {
- public $em;
- public $trans;
-
- public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
- {
- $this->em = $em;
- $this->trans = $translator;
-
- }
-
-
- public function getFunctions()
- {
- return array(
- new TwigFunction('list_reduction_codes', [$this, 'getListReductionCodes']),
- new TwigFunction('count_menu_items', [$this, 'countMenuItems']),
-
- );
- }
-
- public function getFilters()
- {
- return [
- new TwigFilter('lc_trad', [$this, 'lcTrad']),
- new TwigFilter('uc_first', [$this, 'ucFirst'])
-
- ];
- }
-
- public function ucFirst($string)
- {
- return ucfirst($string);
-
- }
-
- public function lcTrad($field, $entityName, $type = "field")
- {
- if($entityName == 'OrderCart') $entityName ='OrderShop';
- if(strpos($field, '.')===false) {
- $tradKey = $type . '.' . $entityName . '.' . $field;
- $tradDefaultKey = $type . '.default.' . $field;
- }else{
- $tradKey = $type . $field;
- $tradDefaultKey = $type .'.default'. substr($field,strpos($field, '.'));
- }
- $trad = $this->trans->trans($tradKey, array(), 'lcshop');
- if ($trad == $tradKey) {
- $trad = $this->trans->trans($tradDefaultKey, array(), 'lcshop');
- }
- return $trad;
- }
-
- public function getListReductionCodes()
- {
- $reductionCartRepo = $this->em->getRepository(ReductionCartInterface::class);
- $codesToReturn = array();
- foreach ($reductionCartRepo->getValuesOfFieldCode() as $codes) {
- foreach ($codes['codes'] as $code) {
- $codesToReturn[] = $code;
- }
- }
- ;
- return $codesToReturn;
- }
-
- public function countMenuItems($entityName){
-
- switch($entityName){
- case 'ticket' :
- $ticketRepo = $this->em->getRepository(TicketInterface::class);
- return $ticketRepo->countAllOpen();
- case 'productAvailabilitiesNegative' :
- $productRepo = $this->em->getRepository(ProductInterface::class);
- return count($productRepo->findProductByAvailabilitiesNegative());
-
- }
- }
-
- }
|