]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Bookmark/classes/Bookmark.php
First step of making Bookmark saveActivity-compatible
[quix0rs-gnu-social.git] / plugins / Bookmark / classes / Bookmark.php
index a3a608ce5e4ece02b4a2c9445bac7a6202b284aa..95518631a3468cfb7536f646ec17d4cf44675335 100644 (file)
@@ -105,11 +105,11 @@ class Bookmark extends Managed_DataObject
      *
      * @return Bookmark bookmark found or null
      */
-    static function getByURL($profile, $url)
+    static function getByURL(Profile $profile, $url)
     {
         $nb = new Bookmark();
 
-        $nb->profile_id = $profile->id;
+        $nb->profile_id = $profile->getID();
         $nb->url        = $url;
 
         if (!$nb->find(true)) {
@@ -119,6 +119,61 @@ class Bookmark extends Managed_DataObject
         return $nb;
     }
 
+    /**
+     * Store a Bookmark object
+     *
+     * @param Profile $profile     To save the bookmark for
+     * @param string  $title       Title of the bookmark
+     * @param string  $url         URL of the bookmark
+     * @param string  $description Description of the bookmark
+     *
+     * @return Bookmark the Bookmark object
+     */
+    static function addNew(Notice $stored, $title, $url, $description)
+    {
+        if ($title === '' or is_null($title)) {
+            throw new ClientException(_m('You must provide a non-empty title.'));
+        }
+        if (!common_valid_http_url($url)) {
+            throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
+        }
+
+        try {
+            $object = self::getByURL($stored->getProfile(), $url);
+            throw new ClientException(_m('You have already bookmarked this URL.'));
+        } catch (NoResultException $e) {
+            // Alright, so then we have to create it.
+        }
+
+        $nb = new Bookmark();
+
+        $nb->id          = UUID::gen();
+        $nb->uri         = $stored->uri;
+        $nb->profile_id  = $stored->getProfile()->getID();
+        $nb->title       = $title;
+        $nb->url         = $url;
+        $nb->description = $description;
+        $nb->created     = $stored->created;
+
+        $result = $nb->insert();
+        if ($result === false) {
+            throw new ServerException('Could not insert Bookmark into database!');
+        }
+
+        /*$hashtags = array();
+        $taglinks = array();
+
+        foreach ($tags as $tag) {
+            $hashtags[] = '#'.$tag;
+            $attrs      = array('href' => Notice_tag::url($tag),
+                                'rel'  => $tag,
+                                'class' => 'tag');
+            $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
+        }*/
+
+        return $nb;
+    }
+
     /**
      * Save a new notice bookmark
      *