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