]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/Notice_bookmark.php
don't reinsert existing bookmark
[quix0rs-gnu-social.git] / plugins / Bookmark / Notice_bookmark.php
1 <?php
2 /**
3  * Data class to mark notices as bookmarks
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the fact that a notice is a bookmark
36  *
37  * @category Bookmark
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45
46 class Notice_bookmark extends Memcached_DataObject
47 {
48     public $__table = 'notice_bookmark'; // table name
49     public $notice_id;                   // int(4)  primary_key not_null
50         public $title;                       // varchar(255)
51         public $description;                 // text
52
53     /**
54      * Get an instance by key
55      *
56      * This is a utility method to get a single instance with a given key value.
57      *
58      * @param string $k Key to use to lookup (usually 'user_id' for this class)
59      * @param mixed  $v Value to lookup
60      *
61      * @return User_greeting_count object found, or null for no hits
62      *
63      */
64
65     function staticGet($k, $v=null)
66     {
67         return Memcached_DataObject::staticGet('Notice_bookmark', $k, $v);
68     }
69
70     /**
71      * return table definition for DB_DataObject
72      *
73      * DB_DataObject needs to know something about the table to manipulate
74      * instances. This method provides all the DB_DataObject needs to know.
75      *
76      * @return array array of column definitions
77      */
78
79     function table()
80     {
81         return array('notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
82                      'title' => DB_DATAOBJECT_STR,
83                                          'description' => DB_DATAOBJECT_STR);
84     }
85
86     /**
87      * return key definitions for DB_DataObject
88      *
89      * @return array list of key field names
90      */
91
92     function keys()
93     {
94         return array_keys($this->keyTypes());
95     }
96
97     /**
98      * return key definitions for Memcached_DataObject
99      *
100      * @return array associative array of key definitions
101      */
102
103     function keyTypes()
104     {
105         return array('notice_id' => 'K');
106     }
107
108     /**
109      * Magic formula for non-autoincrementing integer primary keys
110      *
111      * @return array magic three-false array that stops auto-incrementing.
112      */
113
114     function sequenceKey()
115     {
116         return array(false, false, false);
117     }
118
119         static function getByURL($user, $url)
120         {
121                 $file = File::staticGet('url', $url);
122                 if (!empty($file)) {
123                         $f2p = new File_to_post();
124                         $f2p->file_id = $file->id;
125                         if ($f2p->find()) {
126                                 while ($f2p->fetch()) {
127                                         $n = Notice::staticGet('id', $f2p->post_id);
128                                         if (!empty($n)) {
129                                                 if ($n->profile_id == $user->id) {
130                                                         $nb = Notice_bookmark::staticGet('notice_id', $n->id);
131                                                         if (!empty($nb)) {
132                                                                 return $nb;
133                                                         }
134                                                 }
135                                         }
136                                 }
137                         }
138                 }
139                 return null;
140         }
141
142         static function saveNew($user, $title, $url, $rawtags, $description, $options=null)
143         {
144                 $nb = self::getByURL($user, $url);
145
146                 if (!empty($nb)) {
147                         throw new ClientException(_('Bookmark already exists.'));
148                 }
149
150                 if (empty($options)) {
151                         $options = array();
152                 }
153
154                 if (is_string($rawtags)) {
155                         $rawtags = preg_split('/[\s,]+/', $rawtags);
156                 }
157
158                 $tags = array();
159                 $replies = array();
160
161                 // filter "for:nickname" tags
162
163                 foreach ($rawtags as $tag) {
164                         if (strtolower(mb_substr($tag, 0, 4)) == 'for:') {
165                                 $nickname = mb_substr($tag, 4);
166                                 $other = common_relative_profile($user->getProfile(),
167                                                                                                  $nickname);
168                                 if (!empty($other)) {
169                                         $replies[] = $other->getUri();
170                                 }
171                         } else {
172                                 $tags[] = common_canonical_tag($tag);
173                         }
174                 }
175
176                 $hashtags = array();
177                 $taglinks = array();
178
179                 foreach ($tags as $tag) {
180                         $hashtags[] = '#'.$tag;
181                         $attrs = array('href' => Notice_tag::url($tag),
182                                                    'rel'  => $tag,
183                                                    'class' => 'tag');
184                         $taglinks[] = XMLStringer::estring('a', $attrs, $tag);
185                 }
186
187                 $content = sprintf(_('"%s" %s %s %s'),
188                                                    $title,
189                                                    File_redirection::makeShort($url, $user),
190                                                    $description,
191                                                    implode(' ', $hashtags));
192
193                 $rendered = sprintf(_('<span class="xfolkentry">'.
194                                                           '<a class="taggedlink" href="%s">%s</a> '.
195                                                           '<span class="description">%s</span> '.
196                                                           '<span class="meta">%s</span>'.
197                                                           '</span>'),
198                                                         htmlspecialchars($url),
199                                                         htmlspecialchars($title),
200                                                         htmlspecialchars($description),
201                                                         implode(' ', $taglinks));
202
203                 $options = array_merge($options, array('urls' => array($url),
204                                                                                            'rendered' => $rendered,
205                                                                                            'tags' => $tags,
206                                                                                            'replies' => $replies));
207
208                 $saved = Notice::saveNew($user->id,
209                                                                  $content,
210                                                                  'web',
211                                                                  $options);
212
213                 if (!empty($saved)) {
214                         $nb = new Notice_bookmark();
215                         $nb->notice_id   = $saved->id;
216                         $nb->title       = $title;
217                         $nb->description = $description;
218                         $nb->insert();
219                 }
220
221                 return $saved;
222         }
223 }