]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TagSub/TagSubPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / TagSub / TagSubPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A plugin to enable local tab subscription
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  TagSubPlugin
24  * @package   StatusNet
25  * @author    Brion Vibber <brion@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * TagSub plugin main class
37  *
38  * @category  TagSubPlugin
39  * @package   StatusNet
40  * @author    Brion Vibber <brionv@status.net>
41  * @copyright 2011 StatusNet, Inc.
42  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43  * @link      http://status.net/
44  */
45 class TagSubPlugin extends Plugin
46 {
47     const VERSION         = '0.1';
48
49     /**
50      * Database schema setup
51      *
52      * @see Schema
53      *
54      * @return boolean hook value; true means continue processing, false means stop.
55      */
56     function onCheckSchema()
57     {
58         $schema = Schema::get();
59         $schema->ensureTable('tagsub', TagSub::schemaDef());
60         return true;
61     }
62
63     /**
64      * Map URLs to actions
65      *
66      * @param URLMapper $m path-to-action mapper
67      *
68      * @return boolean hook value; true means continue processing, false means stop.
69      */
70     public function onRouterInitialized(URLMapper $m)
71     {
72         $m->connect('tag/:tag/subscribe',
73                     array('action' => 'tagsub'),
74                     array('tag' => Router::REGEX_TAG));
75         $m->connect('tag/:tag/unsubscribe',
76                     array('action' => 'tagunsub'),
77                     array('tag' => Router::REGEX_TAG));
78
79         $m->connect(':nickname/tag-subscriptions',
80                     array('action' => 'tagsubs'),
81                     array('nickname' => Nickname::DISPLAY_FMT));
82         return true;
83     }
84
85     /**
86      * Plugin version data
87      *
88      * @param array &$versions array of version data
89      *
90      * @return value
91      */
92     function onPluginVersion(array &$versions)
93     {
94         $versions[] = array('name' => 'TagSub',
95                             'version' => self::VERSION,
96                             'author' => 'Brion Vibber',
97                             'homepage' => 'http://status.net/wiki/Plugin:TagSub',
98                             'rawdescription' =>
99                             // TRANS: Plugin description.
100                             _m('Plugin to allow following all messages with a given tag.'));
101         return true;
102     }
103
104     /**
105      * Hook inbox delivery setup so tag subscribers receive all
106      * notices with that tag in their inbox.
107      *
108      * Currently makes no distinction between local messages and
109      * remote ones which happen to come in to the system. Remote
110      * notices that don't come in at all won't ever reach this.
111      *
112      * @param Notice $notice
113      * @param array $ni in/out map of profile IDs to inbox constants
114      * @return boolean hook result
115      */
116     function onStartNoticeWhoGets(Notice $notice, array &$ni)
117     {
118         foreach ($notice->getTags() as $tag) {
119             $tagsub = new TagSub();
120             $tagsub->tag = $tag;
121             $tagsub->find();
122
123             while ($tagsub->fetch()) {
124                 // These constants are currently not actually used, iirc
125                 $ni[$tagsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
126             }
127         }
128         return true;
129     }
130
131     /**
132      *
133      * @param TagAction $action
134      * @return boolean hook result
135      */
136     function onStartTagShowContent(TagAction $action)
137     {
138         $user = common_current_user();
139
140         if ($user instanceof User) {
141             $tag = $action->trimmed('tag');
142             $tagsub = TagSub::pkeyGet(array('tag' => $tag,
143                                             'profile_id' => $user->id));
144             if ($tagsub) {
145                 $form = new TagUnsubForm($action, $tag);
146             } else {
147                 $form = new TagSubForm($action, $tag);
148             }
149             $action->elementStart('div', 'entity_actions');
150             $action->elementStart('ul');
151             $action->elementStart('li', 'entity_subscribe');
152             $form->show();
153             $action->elementEnd('li');
154             $action->elementEnd('ul');
155             $action->elementEnd('div');
156         }
157
158         return true;
159     }
160
161     /**
162      * Menu item for personal subscriptions/groups area
163      *
164      * @param Widget $widget Widget being executed
165      *
166      * @return boolean hook return
167      */
168     function onEndSubGroupNav($widget)
169     {
170         $action = $widget->out;
171         $action_name = $action->trimmed('action');
172
173         $action->menuItem(common_local_url('tagsubs', array('nickname' => $action->user->nickname)),
174                           // TRANS: SubMirror plugin menu item on user settings page.
175                           _m('MENU', 'Tags'),
176                           // TRANS: SubMirror plugin tooltip for user settings menu item.
177                           _m('Configure tag subscriptions'),
178                           $action_name == 'tagsubs' && $action->arg('nickname') == $action->user->nickname);
179
180         return true;
181     }
182
183     function onEndDefaultLocalNav($menu, $user)
184     {
185         $user = common_current_user();
186
187         if (!empty($user)) {
188
189             $tags = TagSub::forProfile($user->getProfile());
190
191             if (!empty($tags) && count($tags) > 0) {
192                 $tagSubMenu = new TagSubMenu($menu->out, $user, $tags);
193                 // TRANS: Menu item text for tags submenu.
194                 $menu->submenu(_m('Tags'), $tagSubMenu);
195             }
196         }
197
198         return true;
199     }
200 }