StatusYoutube.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: logicp
  5. * Date: 6/14/17
  6. * Time: 12:08 AM
  7. */
  8. namespace Drupal\statusmessage;
  9. use Drupal\heartbeat\Entity\Heartbeat;
  10. use Drupal\taxonomy\Entity\Term;
  11. use Drupal\node\Entity\Node;
  12. use Drupal\file\Entity\File;
  13. class StatusYoutube {
  14. protected $url;
  15. protected $message;
  16. protected $generator;
  17. public function __construct($url, $message = null) {
  18. $this->url = $url;
  19. $this->message = $message;
  20. $this->generator = new MarkupGenerator();
  21. }
  22. public function generateEmbed() {
  23. return '<iframe class="heartbeat-youtube" width="auto" height="auto" src="' . $this->parameter . '" frameborder="0"></iframe>';
  24. }
  25. public function generateNode() {
  26. if ($this->generateRequest()) {
  27. $node = $this->setNodeData();
  28. $this->processTerms();
  29. if ($node->save()) {
  30. return $node->id();
  31. }
  32. }
  33. return null;
  34. }
  35. public function setNodeData() {
  36. $provider_manager = \Drupal::service('video.provider_manager');
  37. $enabled_providers = $provider_manager->loadDefinitionsFromOptionList(array('youtube' => 'youtube'));
  38. if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $this->url)) {
  39. $definition = $provider_matches['definition'];
  40. $matches = $provider_matches['matches'];
  41. $uri = $definition['stream_wrapper'] . '://' . $matches['id'];
  42. $storage = \Drupal::entityManager()->getStorage('file');
  43. $results = $storage->getQuery()
  44. ->condition('uri', $uri)
  45. ->execute();
  46. if (!(count($results) > 0)) {
  47. $user = \Drupal::currentUser();
  48. $file = File::Create([
  49. 'uri' => $uri,
  50. 'filemime' => $definition['mimetype'],
  51. 'filesize' => 1,
  52. 'uid' => $user->id()
  53. ]);
  54. $file->save();
  55. $fid = $file->id();
  56. } else {
  57. $fid = array_values($results)[0];
  58. }
  59. $node = Node::create([
  60. 'type' => 'youtube_video',
  61. 'title' => $this->generator->getTitle(),
  62. 'status' => 1,
  63. 'uid' => \Drupal::currentUser()->id(),
  64. 'field_video_embed' => $fid,
  65. ]);
  66. if ($this->message) {
  67. $node->set('body', [
  68. 'value' => '<div class="status-youtube"> ' . $this->message . ' </div>',
  69. 'format' => 'full_html'
  70. ]);
  71. }
  72. return $node;
  73. }
  74. return null;
  75. }
  76. public static function parseHashtags($message) {
  77. $tids = array();
  78. $i = 0;
  79. $tagsArray = explode('#', $message);
  80. unset($tagsArray[0]);
  81. $num = count($tagsArray);
  82. foreach ($tagsArray as $hashtag) {
  83. $hashtag = strpos($hashtag, ' ') ? substr($hashtag, 0, strpos($hashtag, ' ')) : $hashtag;
  84. if ($i === $num - 1) {
  85. $lastTagArray = explode(' ', $hashtag);
  86. if (strlen($lastTagArray[1]) > 1) {
  87. $hashtag = trim($lastTagArray[0]);
  88. }
  89. }
  90. $tid = \Drupal::entityQuery("taxonomy_term")
  91. ->condition("name", trim($hashtag))
  92. ->condition('vid', [
  93. 'twitter',
  94. 'tags',
  95. ], 'IN')
  96. ->execute();
  97. if (count($tid) > 0) {
  98. if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
  99. Heartbeat::updateTermUsage(array_values($tid)[0], 'tags');
  100. }
  101. $tids[] = array_values($tid)[0];
  102. } else {
  103. $term = Term::create([
  104. 'name' => trim($hashtag),
  105. 'vid' => 'tags',
  106. 'field_count' => 1
  107. ]);
  108. if ($term->save()) {
  109. $tids[] = $term->id();
  110. if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
  111. Heartbeat::newTermUsage($term->id());
  112. }
  113. }
  114. }
  115. $i++;
  116. }
  117. return $tids;
  118. }
  119. private function processTerms() {
  120. foreach ($this->generator->getTags() as $tag) {
  121. $newTag = $tag;
  122. }
  123. if ($this->message) {
  124. $this->tags = self::parseHashtags($this->message);
  125. }
  126. return $this->generator->getTags();
  127. }
  128. private function generateRequest() {
  129. return $this->generator->parseMarkup($this->url);
  130. }
  131. }