BackendTwigExtension.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Lc\ShopBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\ProductInterface;
  5. use Lc\ShopBundle\Context\ReductionCartInterface;
  6. use Lc\ShopBundle\Context\TicketInterface;
  7. use Lc\ShopBundle\Repository\ProductRepository;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFilter;
  11. use Twig\TwigFunction;
  12. class BackendTwigExtension extends AbstractExtension
  13. {
  14. public $em;
  15. public $trans;
  16. public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
  17. {
  18. $this->em = $em;
  19. $this->trans = $translator;
  20. }
  21. public function getFunctions()
  22. {
  23. return array(
  24. new TwigFunction('list_reduction_codes', [$this, 'getListReductionCodes']),
  25. new TwigFunction('count_menu_items', [$this, 'countMenuItems']),
  26. );
  27. }
  28. public function getFilters()
  29. {
  30. return [
  31. new TwigFilter('lc_trad', [$this, 'lcTrad']),
  32. new TwigFilter('uc_first', [$this, 'ucFirst'])
  33. ];
  34. }
  35. public function ucFirst($string)
  36. {
  37. return ucfirst($string);
  38. }
  39. public function lcTrad($field, $entityName, $type = "field")
  40. {
  41. if($entityName == 'OrderCart') $entityName ='OrderShop';
  42. if(strpos($field, '.')===false) {
  43. $tradKey = $type . '.' . $entityName . '.' . $field;
  44. $tradDefaultKey = $type . '.default.' . $field;
  45. }else{
  46. $tradKey = $type . $field;
  47. $tradDefaultKey = $type .'.default'. substr($field,strpos($field, '.'));
  48. }
  49. $trad = $this->trans->trans($tradKey, array(), 'lcshop');
  50. if ($trad == $tradKey) {
  51. $trad = $this->trans->trans($tradDefaultKey, array(), 'lcshop');
  52. }
  53. return $trad;
  54. }
  55. public function getListReductionCodes()
  56. {
  57. $reductionCartRepo = $this->em->getRepository(ReductionCartInterface::class);
  58. $codesToReturn = array();
  59. foreach ($reductionCartRepo->getValuesOfFieldCode() as $codes) {
  60. foreach ($codes['codes'] as $code) {
  61. $codesToReturn[] = $code;
  62. }
  63. }
  64. ;
  65. return $codesToReturn;
  66. }
  67. public function countMenuItems($entityName){
  68. switch($entityName){
  69. case 'ticket' :
  70. $ticketRepo = $this->em->getRepository(TicketInterface::class);
  71. return $ticketRepo->countAllOpen();
  72. case 'productAvailabilitiesNegative' :
  73. $productRepo = $this->em->getRepository(ProductInterface::class);
  74. return count($productRepo->findProductByAvailabilitiesNegative());
  75. }
  76. }
  77. }