Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BackendTwigExtension.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(strpos($field, '.')===false) {
  40. $tradKey = $type . '.' . $entityName . '.' . $field;
  41. $tradDefaultKey = $type . '.default.' . $field;
  42. }else{
  43. $tradKey = $type . $field;
  44. $tradDefaultKey = $type .'.default'. substr($field,strpos($field, '.'));
  45. }
  46. $trad = $this->trans->trans($tradKey, array(), 'lcshop');
  47. if ($trad == $tradKey) {
  48. $trad = $this->trans->trans($tradDefaultKey, array(), 'lcshop');
  49. }
  50. return $trad;
  51. }
  52. public function getListReductionCodes()
  53. {
  54. $reductionCartRepo = $this->em->getRepository(ReductionCartInterface::class);
  55. $codesToReturn = array();
  56. foreach ($reductionCartRepo->getValuesOfFieldCode() as $codes) {
  57. foreach ($codes['codes'] as $code) {
  58. $codesToReturn[] = $code;
  59. }
  60. }
  61. ;
  62. return $codesToReturn;
  63. }
  64. public function countMenuItems($entityName){
  65. switch($entityName){
  66. case 'ticket' :
  67. $ticketRepo = $this->em->getRepository(TicketInterface::class);
  68. return $ticketRepo->countByTicketStatus('open');
  69. }
  70. }
  71. }