Jamendocom.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\Factory\UrlMatchs;
  6. use Symfony\Component\DependencyInjection\Container;
  7. use Doctrine\ORM\EntityManager;
  8. class Jamendocom extends ElementFactory
  9. {
  10. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  11. {
  12. $this->url_matchs = UrlMatchs::$jamendo;
  13. parent::__construct($element, $container, $entity_manager);
  14. }
  15. public function getStreamData()
  16. {
  17. $ref_id = $this->element->getData(Element::DATA_REF_ID);
  18. $api_url = "http://api.jamendo.com/get2/name+stream/track/json/?". $this->element->getData(Element::DATA_TYPE)."_id=".$ref_id;
  19. if ($ref_id)
  20. {
  21. $response = $this->getApiConnector()->getResponseForUrl($api_url);
  22. if (count($response->getContent()))
  23. {
  24. $data_return = array();
  25. foreach ($response->getContent() as $song)
  26. {
  27. $data_return[] = array(
  28. 'name' => $song['name'],
  29. 'url' => $song['stream'],
  30. );
  31. }
  32. return $data_return;
  33. }
  34. }
  35. }
  36. public function proceedDatas()
  37. {
  38. $this->retrieveDatas();
  39. $this->proceedThumbnailUrl();
  40. $this->proceedEmbedCode();
  41. }
  42. protected function getApiUrl()
  43. {
  44. switch ($this->url_analyzer->getType())
  45. {
  46. case Element::TYPE_ALBUM:
  47. return "http://api.jamendo.com/get2/id+name+url+image+artist_name+artist_url/album/json/?album_id="
  48. .$this->url_analyzer->getRefId();
  49. break;
  50. case Element::TYPE_TRACK:
  51. return "http://api.jamendo.com/get2/id+name+url+image+artist_name+artist_url+track_name/album/json/?track_id="
  52. .$this->url_analyzer->getRefId();
  53. break;
  54. }
  55. }
  56. protected function getApiTagUrl()
  57. {
  58. return "http://api.jamendo.com/get2/name+weight/tag/json/".$this->url_analyzer->getType()."_tag/?".$this->url_analyzer->getType()."_id="
  59. .$this->url_analyzer->getRefId();
  60. }
  61. public function retrieveDatas()
  62. {
  63. if (($response = $this->getApiConnector()->getResponseForUrl($this->getApiUrl())))
  64. {
  65. // Check si tout se passe bien si pas de retour de l'api
  66. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  67. Element::DATA_THUMB_URL => array(0 => 'image'),
  68. Element::DATA_ARTIST => array(0 => 'artist_name'),
  69. ));
  70. if ($this->url_analyzer->getType() == Element::TYPE_ALBUM)
  71. {
  72. $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'name')));
  73. }
  74. if ($this->url_analyzer->getType() == Element::TYPE_TRACK)
  75. {
  76. $this->element->setData(Element::DATA_TITLE, $response->get(array(0 => 'track_name')));
  77. }
  78. $tags = array();
  79. $response = $this->getApiConnector()->getResponseForUrl($this->getApiTagUrl());
  80. if (count($response->getContent()))
  81. {
  82. foreach ($response->getContent() as $tag)
  83. {
  84. $tags[] = $tag['name'];
  85. }
  86. }
  87. $this->element->setData(Element::DATA_TAGS, $tags);
  88. }
  89. // Un contenu jamendo est toujours téléchargeable
  90. $this->element->setData(Element::DATA_DOWNLOAD, true);
  91. }
  92. public function proceedThumbnailUrl()
  93. {
  94. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  95. {
  96. $this->element->setThumbnailUrl($thumb);
  97. }
  98. }
  99. public function proceedEmbedCode()
  100. {
  101. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  102. && ($type = $this->element->getData(Element::DATA_TYPE)))
  103. {
  104. $height = $this->container->getParameter('jamendo_player_height');
  105. $width = $this->container->getParameter('jamendo_player_width');
  106. $embed_url = "http://widgets.jamendo.com/fr/$type/?".$type."_id=$ref_id&playertype=2008";
  107. $this->element->setEmbed(
  108. '<object width="'.$width.'" height="'.$height.'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
  109. .' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" align="middle">
  110. <param name="allowScriptAccess" value="always" />
  111. <param name="wmode" value="transparent" />
  112. <param name="movie" value="'.$embed_url.'" />
  113. <param name="quality" value="high" />
  114. <param name="bgcolor" value="#FFFFFF" />
  115. <embed src="'.$embed_url.'" quality="high" wmode="transparent" bgcolor="#FFFFFF"'
  116. .' width="'.$width.'" height="'.$height.'" align="middle" allowScriptAccess="always"'
  117. .' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
  118. &nbsp;
  119. </embed>
  120. &nbsp;
  121. </object>'
  122. );
  123. }
  124. }
  125. }