]> 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 cdbdf526cae733953ccef6e7e835787c8ea262bf..95518631a3468cfb7536f646ec17d4cf44675335 100644 (file)
@@ -105,48 +105,99 @@ 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)) {
-            return $nb;
-        } else {
-            return null;
+        if (!$nb->find(true)) {
+            throw new NoResultException($nb);
         }
+
+        return $nb;
     }
 
     /**
-     * Save a new notice bookmark
+     * 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 mixed   $rawtags     array of tags or string
      * @param string  $description Description of the bookmark
-     * @param array   $options     Options for the Notice::saveNew()
      *
-     * @return Notice saved notice
+     * @return Bookmark the Bookmark object
      */
-    static function saveNew($profile, $title, $url, $rawtags, $description,
-                            $options=null)
+    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).'));
         }
 
-        $nb = self::getByURL($profile, $url);
+        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;
 
-        if (!empty($nb)) {
-            // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
-            throw new ClientException(_m('Bookmark already exists.'));
+        $result = $nb->insert();
+        if ($result === false) {
+            throw new ServerException('Could not insert Bookmark into database!');
         }
 
-        if (empty($options)) {
-            $options = array();
+        /*$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
+     *
+     * @param Profile $profile     To save the bookmark for
+     * @param string  $title       Title of the bookmark
+     * @param string  $url         URL of the bookmark
+     * @param array   $rawtags     array of tags
+     * @param string  $description Description of the bookmark
+     * @param array   $options     Options for the Notice::saveNew()
+     *
+     * @return Notice saved notice
+     */
+    static function saveNew(Profile $profile, $title, $url, $rawtags, $description,
+                            array $options=array())
+    {
+        if (!common_valid_http_url($url)) {
+            throw new ClientException(_m('Only web bookmarks can be posted (HTTP or HTTPS).'));
+        }
+
+        try {
+            $object = self::getByURL($profile, $url);
+            return $object;
+        } catch (NoResultException $e) {
+            // Alright, so then we have to create it.
         }
 
         if (array_key_exists('uri', $options)) {
@@ -157,14 +208,6 @@ class Bookmark extends Managed_DataObject
             }
         }
 
-        if (is_string($rawtags)) {
-            if (empty($rawtags)) {
-                $rawtags = array();
-            } else {
-                $rawtags = preg_split('/[\s,]+/', $rawtags);
-            }
-        }
-
         $nb = new Bookmark();
 
         $nb->id          = UUID::gen();