You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 satır
2.3KB

  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. if(strpos($field, '.')===false) {
  38. $tradKey = $type . '.' . $entityName . '.' . $field;
  39. $tradDefaultKey = $type . '.default.' . $field;
  40. }else{
  41. $tradKey = $type . $field;
  42. $tradDefaultKey = $type .'.default'. substr($field,strpos($field, '.'));
  43. }
  44. $trad = $this->trans->trans($tradKey, array(), 'lcshop');
  45. if ($trad == $tradKey) {
  46. $trad = $this->trans->trans($tradDefaultKey, array(), 'lcshop');
  47. }
  48. return $trad;
  49. }
  50. public function getListReductionCodes()
  51. {
  52. $reductionCartRepo = $this->em->getRepository(ReductionCartInterface::class);
  53. $codesToReturn = array();
  54. foreach ($reductionCartRepo->getValuesOfFieldCode() as $codes) {
  55. foreach ($codes['codes'] as $code) {
  56. $codesToReturn[] = $code;
  57. }
  58. }
  59. ;
  60. return $codesToReturn;
  61. }
  62. }