Soundcloudcom.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Muzich\CoreBundle\Factory\Elements;
  3. use Muzich\CoreBundle\Factory\ElementFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Muzich\CoreBundle\Util\TagLike;
  6. use Muzich\CoreBundle\lib\Api\Response as ApiResponse;
  7. use Muzich\CoreBundle\Factory\UrlMatchs;
  8. use Symfony\Component\DependencyInjection\Container;
  9. use Doctrine\ORM\EntityManager;
  10. class Soundcloudcom extends ElementFactory
  11. {
  12. public function __construct(Element $element, Container $container, EntityManager $entity_manager)
  13. {
  14. $this->url_matchs = UrlMatchs::$soundcloud;
  15. parent::__construct($element, $container, $entity_manager);
  16. }
  17. public function proceedDatas()
  18. {
  19. $this->setElementDatasWithApi();
  20. // TODO: Embed code ne devrais plus être necessaire (on créer les lecteurs avec JS)
  21. $this->proceedEmbedCode();
  22. $this->proceedThumbnailUrl();
  23. }
  24. protected function setElementDatasWithApi()
  25. {
  26. if (($response = $this->getApiDatasResponse()))
  27. {
  28. if ($response->get('sharing') == 'public')
  29. {
  30. $this->setElementSharingData($response);
  31. $this->setTagsData($response);
  32. }
  33. if ($response->get('embeddable_by') == 'all')
  34. {
  35. $this->setElementEmbeddableData($response);
  36. }
  37. }
  38. }
  39. protected function getApiDatasResponse()
  40. {
  41. if (($response = $this->getApiConnector()->getResponseForUrl('http://api.soundcloud.com/resolve.json?url='.$this->element->getUrl().'&client_id=39946ea18e3d78d64c0ac95a025794e1')))
  42. {
  43. if ($response->haveNot('errors') && $response->have('location'))
  44. {
  45. return $this->getApiConnector()->getResponseForUrl($response->get('location'));
  46. }
  47. }
  48. return null;
  49. }
  50. protected function setElementSharingData(ApiResponse $response)
  51. {
  52. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  53. Element::DATA_NORMALIZED_URL => 'uri',
  54. Element::DATA_TYPE => 'kind',
  55. Element::DATA_DOWNLOAD => 'downloadable',
  56. Element::DATA_DOWNLOAD_URL => 'download_url',
  57. Element::DATA_TITLE => 'title',
  58. Element::DATA_ARTIST => array('user' => 'username')
  59. ));
  60. $this->setThumbnailData($response);
  61. }
  62. protected function setThumbnailData(ApiResponse $response)
  63. {
  64. if ($response->have('artwork_url'))
  65. {
  66. $this->element->setData(Element::DATA_THUMB_URL, $response->get('artwork_url'));
  67. }
  68. elseif ($response->have(array('user' => 'avatar_url')))
  69. {
  70. $this->element->setData(Element::DATA_THUMB_URL, $response->get(array('user' => 'avatar_url')));
  71. }
  72. }
  73. protected function setTagsData(ApiResponse $response)
  74. {
  75. $tags_string = $response->get('genre').' '.$response->get('tag_list').' '.str_replace(' ', '-', $response->get('genre'));
  76. $tags_like = array();
  77. if (strlen($tags_string))
  78. {
  79. $tag_like = new TagLike($this->entity_manager);
  80. foreach (explode(' ', $tags_string) as $word)
  81. {
  82. $similar_tags = $tag_like->getSimilarTags($word, ($this->element->getOwner())?$this->element->getOwner()->getId():null);
  83. if (count($similar_tags))
  84. {
  85. if ($similar_tags['same_found'])
  86. {
  87. $tags_like[] = $similar_tags['tags'][0]['name'];
  88. }
  89. }
  90. }
  91. $tags_like[] = $response->get('genre');
  92. if (count($tags_like))
  93. {
  94. $this->element->setData(Element::DATA_TAGS, array_unique($tags_like));
  95. }
  96. }
  97. }
  98. protected function setElementEmbeddableData($response)
  99. {
  100. $this->getApiConnector()->setElementDatasWithResponse($response, array(
  101. Element::DATA_REF_ID => 'id'
  102. ));
  103. }
  104. public function proceedEmbedCode()
  105. {
  106. if (($ref_id = $this->element->getData(Element::DATA_REF_ID))
  107. && ($this->element->getData(Element::DATA_TYPE) == 'track' || $this->element->getData(Element::DATA_TYPE) == 'playlist' ))
  108. {
  109. $ref_id = $this->element->getUrl();
  110. $embed_id = md5($ref_id);
  111. $height = $this->container->getParameter('soundcloud_player_height');
  112. $this->element->setEmbed(
  113. '<object height="'.$height.'" width="100%" id="embed_'.$embed_id.'" '
  114. .'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
  115. <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;'
  116. .'enable_api=true&amp;object_id=embed_'.$embed_id.'"></param>
  117. <param name="allowscriptaccess" value="always"></param>
  118. <embed allowscriptaccess="always" height="'.$height.'" '
  119. .'src="http://player.soundcloud.com/player.swf?url='.$ref_id.'&amp;enable_api=true'
  120. .'&amp;object_id=embed_'.$embed_id.'" type="application/x-shockwave-flash" '
  121. .'width="100%" name="embed_'.$embed_id.'"></embed>
  122. </object>'
  123. );
  124. }
  125. }
  126. public function proceedThumbnailUrl()
  127. {
  128. if (($thumb = $this->element->getData(Element::DATA_THUMB_URL)))
  129. {
  130. $this->element->setThumbnailUrl($thumb);
  131. }
  132. }
  133. }