]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TagSub/TagSubPlugin.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[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      * Load related modules when needed
65      *
66      * @param string $cls Name of the class to be loaded
67      *
68      * @return boolean hook value; true means continue processing, false means stop.
69      */
70     function onAutoload($cls)
71     {
72         $dir = dirname(__FILE__);
73
74         switch ($cls)
75         {
76         case 'TagSub':
77             include_once $dir.'/'.$cls.'.php';
78             return false;
79         case 'TagsubAction':
80         case 'TagunsubAction':
81         case 'TagsubsAction':
82         case 'TagSubForm':
83         case 'TagUnsubForm':
84             include_once $dir.'/'.strtolower($cls).'.php';
85             return false;
86         default:
87             return true;
88         }
89     }
90
91     /**
92      * Map URLs to actions
93      *
94      * @param Net_URL_Mapper $m path-to-action mapper
95      *
96      * @return boolean hook value; true means continue processing, false means stop.
97      */
98     function onRouterInitialized($m)
99     {
100         $m->connect('tag/:tag/subscribe',
101                     array('action' => 'tagsub'),
102                     array('tag' => Router::REGEX_TAG));
103         $m->connect('tag/:tag/unsubscribe',
104                     array('action' => 'tagunsub'),
105                     array('tag' => Router::REGEX_TAG));
106
107         $m->connect(':nickname/tag-subscriptions',
108                     array('action' => 'tagsubs'),
109                     array('nickname' => Nickname::DISPLAY_FMT));
110         return true;
111     }
112
113     /**
114      * Plugin version data
115      *
116      * @param array &$versions array of version data
117      *
118      * @return value
119      */
120     function onPluginVersion(&$versions)
121     {
122         $versions[] = array('name' => 'TagSub',
123                             'version' => self::VERSION,
124                             'author' => 'Brion Vibber',
125                             'homepage' => 'http://status.net/wiki/Plugin:TagSub',
126                             'rawdescription' =>
127                             // TRANS: Plugin description.
128                             _m('Plugin to allow following all messages with a given tag.'));
129         return true;
130     }
131
132     /**
133      * Hook inbox delivery setup so tag subscribers receive all
134      * notices with that tag in their inbox.
135      *
136      * Currently makes no distinction between local messages and
137      * remote ones which happen to come in to the system. Remote
138      * notices that don't come in at all won't ever reach this.
139      *
140      * @param Notice $notice
141      * @param array $ni in/out map of profile IDs to inbox constants
142      * @return boolean hook result
143      */
144     function onStartNoticeWhoGets(Notice $notice, array &$ni)
145     {
146         foreach ($notice->getTags() as $tag) {
147             $tagsub = new TagSub();
148             $tagsub->tag = $tag;
149             $tagsub->find();
150
151             while ($tagsub->fetch()) {
152                 // These constants are currently not actually used, iirc
153                 $ni[$tagsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
154             }
155         }
156         return true;
157     }
158
159     /**
160      *
161      * @param TagAction $action
162      * @return boolean hook result
163      */
164     function onStartTagShowContent(TagAction $action)
165     {
166         $user = common_current_user();
167         if ($user) {
168             $tag = $action->trimmed('tag');
169             $tagsub = TagSub::pkeyGet(array('tag' => $tag,
170                                             'profile_id' => $user->id));
171             if ($tagsub) {
172                 $form = new TagUnsubForm($action, $tag);
173             } else {
174                 $form = new TagSubForm($action, $tag);
175             }
176             $action->elementStart('div', 'entity_actions');
177             $action->elementStart('ul');
178             $action->elementStart('li', 'entity_subscribe');
179             $form->show();
180             $action->elementEnd('li');
181             $action->elementEnd('ul');
182             $action->elementEnd('div');
183         }
184         return true;
185     }
186
187     /**
188      * Menu item for personal subscriptions/groups area
189      *
190      * @param Widget $widget Widget being executed
191      *
192      * @return boolean hook return
193      */
194
195     function onEndSubGroupNav($widget)
196     {
197         $action = $widget->out;
198         $action_name = $action->trimmed('action');
199
200         $action->menuItem(common_local_url('tagsubs', array('nickname' => $action->user->nickname)),
201                           // TRANS: SubMirror plugin menu item on user settings page.
202                           _m('MENU', 'Tags'),
203                           // TRANS: SubMirror plugin tooltip for user settings menu item.
204                           _m('Configure tag subscriptions'),
205                           $action_name == 'tagsubs' && $action->arg('nickname') == $action->user->nickname);
206
207         return true;
208     }
209
210     /**
211      * Add a count of mirrored feeds into a user's profile sidebar stats.
212      *
213      * @param Profile $profile
214      * @param array $stats
215      * @return boolean hook return value
216      */
217     function onProfileStats($profile, &$stats)
218     {
219         $cur = common_current_user();
220         if (!empty($cur) && $cur->id == $profile->id) {
221             $tagsub = new TagSub();
222             $tagsub->profile_id = $profile->id;
223             $entry = array(
224                 'id' => 'tagsubs',
225                 'label' => _m('Tag subscriptions'),
226                 'link' => common_local_url('tagsubs', array('nickname' => $profile->nickname)),
227                 'value' => $tagsub->count(),
228             );
229
230             $insertAt = count($stats);
231             foreach ($stats as $i => $row) {
232                 if ($row['id'] == 'groups') {
233                     // Slip us in after them.
234                     $insertAt = $i + 1;
235                     break;
236                 }
237             }
238             array_splice($stats, $insertAt, 0, array($entry));
239         }
240         return true;
241     }
242 }