MuzichTwigSocialBar.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Muzich\CoreBundle\Twig\Extensions;
  3. class MuzichTwigSocialBar extends \Twig_Extension{
  4. protected $container;
  5. protected $translator;
  6. /**
  7. * Constructor.
  8. *
  9. * @param ContainerInterface $container
  10. */
  11. public function __construct($container, $translator)
  12. {
  13. $this->container = $container;
  14. $this->translator = $translator;
  15. }
  16. public function getName()
  17. {
  18. return 'muzich_social_bar';
  19. }
  20. public function getFunctions()
  21. {
  22. return array(
  23. 'socialButtons' => new \Twig_Function_Method($this, 'getSocialButtons' ,array('is_safe' => array('html'))),
  24. 'facebookButton' => new \Twig_Function_Method($this, 'getFacebookLikeButton' ,array('is_safe' => array('html'))),
  25. 'twitterButton' => new \Twig_Function_Method($this, 'getTwitterButton' ,array('is_safe' => array('html'))),
  26. 'googlePlusButton' => new \Twig_Function_Method($this, 'getGooglePlusButton' ,array('is_safe' => array('html'))),
  27. );
  28. }
  29. public function getSocialButtons($parameters = array())
  30. {
  31. // no parameters were defined, keeps default values
  32. if (!array_key_exists('facebook', $parameters)){
  33. $render_parameters['facebook'] = array();
  34. // parameters are defined, overrides default values
  35. }else if(is_array($parameters['facebook'])){
  36. $render_parameters['facebook'] = $parameters['facebook'];
  37. // the button is not displayed
  38. }else{
  39. $render_parameters['facebook'] = false;
  40. }
  41. if (!array_key_exists('twitter', $parameters)){
  42. $render_parameters['twitter'] = array();
  43. }else if(is_array($parameters['twitter'])){
  44. $render_parameters['twitter'] = $parameters['twitter'];
  45. }else{
  46. $render_parameters['twitter'] = false;
  47. }
  48. if (!array_key_exists('googleplus', $parameters)){
  49. $render_parameters['googleplus'] = array();
  50. }else if(is_array($parameters['googleplus'])){
  51. $render_parameters['googleplus'] = $parameters['googleplus'];
  52. }else{
  53. $render_parameters['googleplus'] = false;
  54. }
  55. // get the helper service and display the template
  56. return $this->container->get('muzich.socialBarHelper')->socialButtons($render_parameters);
  57. }
  58. // https://developers.facebook.com/docs/reference/plugins/like/
  59. public function getFacebookLikeButton($parameters = array())
  60. {
  61. // default values, you can override the values by setting them
  62. $parameters = $parameters + array(
  63. 'url' => null,
  64. 'locale' => 'fr_FR',
  65. 'send' => false,
  66. 'width' => 300,
  67. 'showFaces' => false,
  68. 'layout' => 'button_count'
  69. );
  70. return $this->container->get('muzich.socialBarHelper')->facebookButton($parameters);
  71. }
  72. public function getTwitterButton($parameters = array())
  73. {
  74. $parameters = $parameters + array(
  75. 'url' => null,
  76. 'locale' => 'fr',
  77. 'message' => $this->translator->trans('element.share.twitter.text', array(), 'userui'),
  78. 'text' => 'Tweet',
  79. 'via' => 'Muzich_Official',
  80. 'tag' => 'music'
  81. );
  82. return $this->container->get('muzich.socialBarHelper')->twitterButton($parameters);
  83. }
  84. public function getGooglePlusButton($parameters = array())
  85. {
  86. $parameters = $parameters + array(
  87. 'url' => null,
  88. 'locale' => 'en',
  89. 'size' => 'medium',
  90. 'annotation' => 'bubble',
  91. 'width' => '300'
  92. );
  93. return $this->container->get('muzich.socialBarHelper')->googlePlusButton($parameters);
  94. }
  95. }