BackendTwigExtension.php 2.1KB

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