Soundcloudcom.php 4.8KB

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