Explorar el Código

Work on StatusYoutube and generator

logicp hace 7 años
padre
commit
217d5c4311
Se han modificado 4 ficheros con 118 adiciones y 19 borrados
  1. 9 2
      js/statusmessage.js
  2. 3 3
      src/Form/StatusForm.php
  3. 2 2
      src/StatusTwitter.php
  4. 104 12
      src/StatusYoutube.php

+ 9 - 2
js/statusmessage.js

@@ -24,7 +24,7 @@
                 let markup = document.createElement('div');
                 markup.innerHTML = response.data;
 
-                let statusBlock = document.getElementById('block-statusblock');
+                let statusTextBox = document.getElementById('edit-message');
                 let oldPreviewIframe = document.querySelector('.statusmessage-preview-iframe');
 
                 if (oldPreviewIframe !== null) {
@@ -33,7 +33,7 @@
                 }
                 previewIframe = document.createElement('iframe');
                 previewIframe.classList.add('statusmessage-preview-iframe');
-                statusBlock.appendChild(previewIframe);
+                statusTextBox.parentNode.appendChild(previewIframe);
                 previewIframe.contentWindow.document.open();
                 previewIframe.contentWindow.document.appendChild(markup);
                 previewIframe.contentWindow.document.close();
@@ -58,6 +58,13 @@
         }
       }
 
+
+      let statusPostButton = document.getElementById('edit-post');
+
+      statusPostButton.addEventListener('click', function() {
+        let textBox = document.getElementById('edit-message');
+        textBox.value = "";
+      })
     }
   };
 

+ 3 - 3
src/Form/StatusForm.php

@@ -82,7 +82,7 @@ class StatusForm extends FormBase {
         'event' => 'change, paste, keyup',
         'callback' => '::generatePreview',
         'progress' => array(
-          'type' => 'throbber',
+          'type' => 'none',
           'message' => t('Generating preview'),
         ),
       ],
@@ -234,10 +234,10 @@ $stophere = null;
 
         } else if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
 
-          $statusYoutube = new StatusYoutube($url);
+          $statusYoutube = new StatusYoutube($url, $message);
           $nid = $statusYoutube->generateNode();
 
-        } else {
+        } else if ($url !== null) {
           $statusHeartPost = new StatusHeartPost($url, $message);
           $nid = $statusHeartPost->sendRequest();
 

+ 2 - 2
src/StatusTwitter.php

@@ -165,8 +165,8 @@ class StatusTwitter {
     $terms = $this->processTerms($data);
 
     if (!empty($data->entities->urls)) {
-      foreach ($data->entities->urls as $url)  {
-        $links[] = $url->display_url;
+      foreach ($data->entities->urls as $url) {
+        $links[] = !strpos($url->display_url, 'http') ? 'http://' . $url->display_url : $url->display_url;
       }
     }
     //Check for attached media and create a directory for saving

+ 104 - 12
src/StatusYoutube.php

@@ -8,6 +8,7 @@
 
 namespace Drupal\statusmessage;
 
+use Drupal\heartbeat\Entity\Heartbeat;
 use Drupal\taxonomy\Entity\Term;
 use Drupal\node\Entity\Node;
 use Drupal\file\Entity\File;
@@ -15,29 +16,45 @@ use Drupal\file\Entity\File;
 
 class StatusYoutube {
 
-  protected $parameter;
+  protected $url;
 
+  protected $message;
 
-  public function __construct($parameter) {
-    $this->parameter = $parameter;
-  }
+  protected $generator;
 
 
-  private function parseUrl ($text) {
-    return explode('status/', $text)[1];
+  public function __construct($url, $message = null) {
+    $this->url = $url;
+    $this->message = $message;
+    $this->generator = new MarkupGenerator();
   }
 
+
   public function generateEmbed() {
     return '<iframe class="heartbeat-youtube" width="auto" height="auto" src="' . $this->parameter . '" frameborder="0"></iframe>';
   }
 
-  public function generateNode()
-  {
+  public function generateNode() {
+
+    if ($this->generateRequest()) {
+
+      $node = $this->setNodeData();
+
+      $this->processTerms();
+
+      if ($node->save()) {
+        return $node->id();
+      }
+    }
+    return null;
+  }
+
+  public function setNodeData() {
 
     $provider_manager = \Drupal::service('video.provider_manager');
     $enabled_providers = $provider_manager->loadDefinitionsFromOptionList(array('youtube' => 'youtube'));
 
-    if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $this->parameter)) {
+    if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $this->url)) {
 
       $definition = $provider_matches['definition'];
       $matches = $provider_matches['matches'];
@@ -61,16 +78,91 @@ class StatusYoutube {
       }
       $node = Node::create([
         'type' => 'youtube_video',
-        'title' => 'dev_title',
+        'title' => $this->generator->getTitle(),
+        'status' => 1,
         'uid' => \Drupal::currentUser()->id(),
         'field_video_embed' => $fid,
       ]);
 
-      $node->save();
+      if ($this->message) {
+        $node->set('body', [
+          'value' => '<div class="status-youtube"> ' . $this->message . ' </div>',
+          'format' => 'full_html'
+        ]);
+      }
+
+      return $node;
+
+    }
+
+    return null;
+  }
+
+  public static function parseHashtags($message) {
 
-      return $node->id();
+    $tids = array();
+    $i = 0;
+    $tagsArray = explode('#', $message);
+    $num = count($tagsArray);
 
+    unset($tagsArray[0]);
+
+    foreach ($tagsArray as $hashtag) {
+      if ($i === $num - 1) {
+        $lastTagArray = explode(' ', $hashtag);
+        if (strlen($lastTagArray[1]) > 1) {
+          $hashtag = trim($lastTagArray[0]);
+        }
+      }
+      $tid = \Drupal::entityQuery("taxonomy_term")
+        ->condition("name", trim($hashtag))
+        ->condition('vid', [
+          'twitter',
+          'tags',
+          'kekistan'
+        ], 'IN')
+        ->execute();
+
+      if (count($tid) > 0) {
+        if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
+          Heartbeat::updateTermUsage(array_values($tid)[0], 'tags');
+        }
+        $tids[] = array_values($tid)[0];
+      } else {
+        $term = Term::create([
+          'name' => trim($hashtag),
+          'vid' => 'tags',
+          'field_count' => 1
+        ]);
+        if ($term->save()) {
+          $tids[] = $term->id();
+          if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
+            Heartbeat::newTermUsage($term->id());
+          }
+        }
+      }
+      $i++;
+    }
+    return $tids;
+  }
+
+  private function processTerms() {
+
+    foreach ($this->generator->getTags() as $tag) {
+      $newTag = $tag;
     }
+
+    if ($this->message) {
+      $this->tags = self::parseHashtags($this->message);
+    }
+
+    return $this->generator->getTags();
   }
+
+
+  private function generateRequest() {
+    return $this->generator->parseMarkup($this->url);
+  }
+
 }