BackendTwigExtension.php 2.8KB

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