Explorar el Código

Adding StatusHeartPost class to autogenerate generic shared content from the web

logicp hace 7 años
padre
commit
741cc6e9be
Se han modificado 4 ficheros con 182 adiciones y 15 borrados
  1. 20 14
      src/Form/StatusForm.php
  2. 41 1
      src/MarkupGenerator.php
  3. 18 0
      src/SharedContentInterface.php
  4. 103 0
      src/StatusHeartPost.php

+ 20 - 14
src/Form/StatusForm.php

@@ -10,6 +10,7 @@ use Drupal\statusmessage\MarkupGenerator;
 use Drupal\statusmessage\StatusService;
 use Drupal\statusmessage\StatusTypeService;
 use Drupal\statusmessage\Ajax\ClientCommand;
+use Drupal\statusmessage\StatusHeartPost;
 use Drupal\statusmessage\StatusTwitter;
 use Drupal\statusmessage\StatusYoutube;
 use Drupal\Core\Ajax\AjaxResponse;
@@ -220,23 +221,28 @@ $stophere = null;
     $message = $form_state->getValue('message');
     if (strlen(trim($message)) > 1) {
       preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
-      if (strpos($message, 'twitter')) {
-        if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
-          $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
+
+      if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
+
+        $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
+
+        if (strpos($message, 'twitter')) {
+
+
           $statusTwitter = new StatusTwitter($url);
           $nid = $statusTwitter->sendRequest();
-//        return $nid;
-        }
-      }
-      else {
-        if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
-          if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
-            $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
-            $statusYoutube = new StatusYoutube($url);
-            $nid = $statusYoutube->generateNode();
-//        return $nid;
-          }
+
+        } else if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
+
+          $statusYoutube = new StatusYoutube($url);
+          $nid = $statusYoutube->generateNode();
+
+        } else {
+          $statusHeartPost = new StatusHeartPost($url);
+          $statusHeartPost->sendRequest();
+
         }
+
       }
 
       if ($nid === NULL && !empty($this->statusTypeService)) {

+ 41 - 1
src/MarkupGenerator.php

@@ -42,7 +42,8 @@ class MarkupGenerator implements Parser {
    * @return mixed
    */
   public function parseMarkup($url) {
-    $this->parsedMarkup = Embed::create('http://' . $url);
+    $url = strpos($url, 'http://') ? 'http://' . $url : $url;
+    $this->parsedMarkup = Embed::create($url);
     return true;
   }
 
@@ -65,4 +66,43 @@ class MarkupGenerator implements Parser {
     return $templateCreator->getPreview();
 
   }
+
+
+  /**
+   * @return mixed
+   */
+
+  public function getImages() {
+    return $this->parsedMarkup->images;
+  }
+
+  /**
+   * @return mixed
+   */
+  public function getImage() {
+    return $this->parsedMarkup->image;
+  }
+
+  /**
+   * @return mixed
+   */
+  public function getTitle() {
+    return $this->parsedMarkup->title;
+  }
+
+  /**
+   * @return mixed
+   */
+  public function getDescription() {
+    return $this->parsedMarkup->description;
+  }
+
+  /**
+   * @return mixed
+   */
+  public function getTags() {
+    return $this->parsedMarkup->tags;
+  }
+
+
 }

+ 18 - 0
src/SharedContentInterface.php

@@ -0,0 +1,18 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: logicp
+ * Date: 6/21/17
+ * Time: 7:42 PM
+ */
+
+namespace Drupal\statusmessage;
+
+
+interface SharedContentInterface {
+
+
+  public function sendRequest();
+
+
+}

+ 103 - 0
src/StatusHeartPost.php

@@ -0,0 +1,103 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: logicp
+ * Date: 6/21/17
+ * Time: 7:41 PM
+ */
+
+namespace Drupal\statusmessage;
+
+
+use Drupal\node\Entity\Node;
+
+/**
+ * @property \Drupal\statusmessage\MarkupGenerator generator
+ * @property  message
+ */
+class StatusHeartPost implements SharedContentInterface {
+
+  protected $url;
+
+  protected $message;
+
+  protected $generator;
+
+  /**
+   * StatusHeartPost constructor.
+   * @param $url
+   */
+  public function __construct($url, $message = null) {
+    $this->url = $url;
+    $this->message = $message;
+    $this->generator = new MarkupGenerator();
+  }
+
+  public function sendRequest() {
+
+    if ($this->generateRequest()) {
+
+      $node = $this->setNodeData();
+
+      $tags = $this->processTerms();
+
+      if ($fid = $this->getMedia()) {
+        $node->set('field_image', $fid);
+      }
+
+      if ($node->save()) {
+        return $node->id();
+      }
+    }
+
+  }
+
+  private function generateRequest() {
+
+    return $this->generator->parseMarkup($this->url);
+  }
+
+
+  private function processTerms() {
+
+    foreach ($this->generator->getTags() as $tag) {
+      $newTag = $tag;
+    }
+
+    return $this->generator->getTags();
+
+  }
+
+
+  private function setNodeData() {
+
+    $node = Node::create([
+      'type' => 'heartpost',
+      'title' => $this->generator->getTitle(),
+      'status' => 1,
+    ]);
+
+    if ($this->message) {
+      $node->set('body', ['value' => '<div class="status-heartpost"> ' . $this->message . '</div>']);
+    }
+    $node->set('field_description', ['value' => '<div class="status-heartpost-description"> ' . $this->generator->getDescription() . '</div>', 'format' =>'full_html']);
+
+    return $node;
+
+  }
+
+
+  private function getMedia() {
+
+    if ($this->generator->getImage()) {
+      $mainImage = file_get_contents($this->generator->getImage());
+      $file = file_save_data($mainImage, 'public://' . substr($this->generator->getImage(), strrpos($this->generator->getImage(), '/') + 1), FILE_EXISTS_REPLACE);
+
+      return $file->id();
+    }
+//    return $this->generator->getImages();
+
+  }
+
+
+}