]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
PHPCS Notice_bookmark
authorEvan Prodromou <evan@status.net>
Tue, 21 Dec 2010 15:32:35 +0000 (10:32 -0500)
committerEvan Prodromou <evan@status.net>
Tue, 21 Dec 2010 15:32:35 +0000 (10:32 -0500)
plugins/Bookmark/Notice_bookmark.php

index e816c45596cc5e8adc3c37d32506a82a235e6b0d..622c641ede29a6168ba5b4518d218ffcfb70aba7 100644 (file)
@@ -47,8 +47,8 @@ class Notice_bookmark extends Memcached_DataObject
 {
     public $__table = 'notice_bookmark'; // table name
     public $notice_id;                   // int(4)  primary_key not_null
-       public $title;                       // varchar(255)
-       public $description;                 // text
+    public $title;                       // varchar(255)
+    public $description;                 // text
 
     /**
      * Get an instance by key
@@ -80,7 +80,7 @@ class Notice_bookmark extends Memcached_DataObject
     {
         return array('notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
                      'title' => DB_DATAOBJECT_STR,
-                                        'description' => DB_DATAOBJECT_STR);
+                     'description' => DB_DATAOBJECT_STR);
     }
 
     /**
@@ -116,108 +116,133 @@ class Notice_bookmark extends Memcached_DataObject
         return array(false, false, false);
     }
 
-       static function getByURL($user, $url)
-       {
-               $file = File::staticGet('url', $url);
-               if (!empty($file)) {
-                       $f2p = new File_to_post();
-                       $f2p->file_id = $file->id;
-                       if ($f2p->find()) {
-                               while ($f2p->fetch()) {
-                                       $n = Notice::staticGet('id', $f2p->post_id);
-                                       if (!empty($n)) {
-                                               if ($n->profile_id == $user->id) {
-                                                       $nb = Notice_bookmark::staticGet('notice_id', $n->id);
-                                                       if (!empty($nb)) {
-                                                               return $nb;
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
-               return null;
-       }
-
-       static function saveNew($user, $title, $url, $rawtags, $description, $options=null)
-       {
-               $nb = self::getByURL($user, $url);
-
-               if (!empty($nb)) {
-                       throw new ClientException(_('Bookmark already exists.'));
-               }
-
-               if (empty($options)) {
-                       $options = array();
-               }
-
-               if (is_string($rawtags)) {
-                       $rawtags = preg_split('/[\s,]+/', $rawtags);
-               }
-
-               $tags = array();
-               $replies = array();
-
-               // filter "for:nickname" tags
-
-               foreach ($rawtags as $tag) {
-                       if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
-                               $nickname = mb_substr($tag, 4);
-                               $other = common_relative_profile($user->getProfile(),
-                                                                                                $nickname);
-                               if (!empty($other)) {
-                                       $replies[] = $other->getUri();
-                               }
-                       } else {
-                               $tags[] = common_canonical_tag($tag);
-                       }
-               }
-
-               $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);
-               }
-
-               $content = sprintf(_('"%s" %s %s %s'),
-                                                  $title,
-                                                  File_redirection::makeShort($url, $user),
-                                                  $description,
-                                                  implode(' ', $hashtags));
-
-               $rendered = sprintf(_('<span class="xfolkentry">'.
-                                                         '<a class="taggedlink" href="%s">%s</a> '.
-                                                         '<span class="description">%s</span> '.
-                                                         '<span class="meta">%s</span>'.
-                                                         '</span>'),
-                                                       htmlspecialchars($url),
-                                                       htmlspecialchars($title),
-                                                       htmlspecialchars($description),
-                                                       implode(' ', $taglinks));
-
-               $options = array_merge($options, array('urls' => array($url),
-                                                                                          'rendered' => $rendered,
-                                                                                          'tags' => $tags,
-                                                                                          'replies' => $replies));
-
-               $saved = Notice::saveNew($user->id,
-                                                                $content,
-                                                                'web',
-                                                                $options);
-
-               if (!empty($saved)) {
-                       $nb = new Notice_bookmark();
-                       $nb->notice_id   = $saved->id;
-                       $nb->title       = $title;
-                       $nb->description = $description;
-                       $nb->insert();
-               }
-
-               return $saved;
-       }
+    /**
+     * Get the bookmark that a user made for an URL
+     *
+     * @param User   $user User to check for
+     * @param string $url  URL to check for
+     *
+     * @return Notice_bookmark bookmark found or null
+     */
+     
+    static function getByURL($user, $url)
+    {
+        $file = File::staticGet('url', $url);
+        if (!empty($file)) {
+            $f2p = new File_to_post();
+
+            $f2p->file_id = $file->id;
+            if ($f2p->find()) {
+                while ($f2p->fetch()) {
+                    $n = Notice::staticGet('id', $f2p->post_id);
+                    if (!empty($n)) {
+                        if ($n->profile_id == $user->id) {
+                            $nb = Notice_bookmark::staticGet('notice_id', $n->id);
+                            if (!empty($nb)) {
+                                return $nb;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Save a new notice bookmark
+     *
+     * @param User   $user        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
+     */
+
+    static function saveNew($user, $title, $url, $rawtags, $description,
+                            $options=null)
+    {
+        $nb = self::getByURL($user, $url);
+
+        if (!empty($nb)) {
+            throw new ClientException(_('Bookmark already exists.'));
+        }
+
+        if (empty($options)) {
+            $options = array();
+        }
+
+        if (is_string($rawtags)) {
+            $rawtags = preg_split('/[\s,]+/', $rawtags);
+        }
+
+        $tags    = array();
+        $replies = array();
+
+        // filter "for:nickname" tags
+
+        foreach ($rawtags as $tag) {
+            if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
+                $nickname = mb_substr($tag, 4);
+                $other    = common_relative_profile($user->getProfile(),
+                                                    $nickname);
+                if (!empty($other)) {
+                    $replies[] = $other->getUri();
+                }
+            } else {
+                $tags[] = common_canonical_tag($tag);
+            }
+        }
+
+        $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);
+        }
+
+        $content = sprintf(_('"%s" %s %s %s'),
+                           $title,
+                           File_redirection::makeShort($url, $user),
+                           $description,
+                           implode(' ', $hashtags));
+
+        $rendered = sprintf(_('<span class="xfolkentry">'.
+                              '<a class="taggedlink" href="%s">%s</a> '.
+                              '<span class="description">%s</span> '.
+                              '<span class="meta">%s</span>'.
+                              '</span>'),
+                            htmlspecialchars($url),
+                            htmlspecialchars($title),
+                            htmlspecialchars($description),
+                            implode(' ', $taglinks));
+
+        $options = array_merge($options, array('urls' => array($url),
+                                               'rendered' => $rendered,
+                                               'tags' => $tags,
+                                               'replies' => $replies));
+
+        $saved = Notice::saveNew($user->id,
+                                 $content,
+                                 'web',
+                                 $options);
+
+        if (!empty($saved)) {
+            $nb = new Notice_bookmark();
+
+            $nb->notice_id   = $saved->id;
+            $nb->title       = $title;
+            $nb->description = $description;
+            $nb->insert();
+        }
+
+        return $saved;
+    }
 }