]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/UserFlagPlugin.php
78979ace232c7daa211f7b677764cff80e368938
[quix0rs-gnu-social.git] / plugins / UserFlag / UserFlagPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Allows users to flag content and accounts as offensive/spam/whatever
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Allows users to flag content and accounts as offensive/spam/whatever
36  *
37  * @category Plugin
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class UserFlagPlugin extends Plugin
45 {
46     const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
47     const CLEARFLAGS  = 'UserFlagPlugin::clearflags';
48
49     function onCheckSchema()
50     {
51         $schema = Schema::get();
52
53         // For storing user-submitted flags on profiles
54
55         $schema->ensureTable('user_flag_profile',
56                              array(new ColumnDef('profile_id', 'integer', null,
57                                                  false, 'PRI'),
58                                    new ColumnDef('user_id', 'integer', null,
59                                                  false, 'PRI'),
60                                    new ColumnDef('created', 'datetime', null,
61                                                  false, 'MUL'),
62                                    new ColumnDef('cleared', 'datetime', null,
63                                                  true, 'MUL')));
64
65         return true;
66     }
67
68     function onInitializePlugin()
69     {
70         // XXX: do something here?
71         return true;
72     }
73
74     function onRouterInitialized($m) {
75         $m->connect('main/flag/profile', array('action' => 'flagprofile'));
76         $m->connect('main/flag/clear', array('action' => 'clearflag'));
77         $m->connect('admin/profile/flag', array('action' => 'adminprofileflag'));
78         return true;
79     }
80
81    function onAutoload($cls)
82     {
83         switch ($cls)
84         {
85         case 'FlagprofileAction':
86         case 'AdminprofileflagAction':
87         case 'ClearflagAction':
88             require_once(INSTALLDIR.'/plugins/UserFlag/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
89             return false;
90         case 'FlagProfileForm':
91         case 'ClearFlagForm':
92             require_once(INSTALLDIR.'/plugins/UserFlag/' . strtolower($cls . '.php'));
93             return false;
94         case 'User_flag_profile':
95             require_once(INSTALLDIR.'/plugins/UserFlag/'.$cls.'.php');
96             return false;
97         default:
98             return true;
99         }
100     }
101
102     function onEndProfilePageActionsElements(&$action, $profile)
103     {
104         $user = common_current_user();
105
106         if (!empty($user) && ($user->id != $profile->id)) {
107
108             $action->elementStart('li', 'entity_flag');
109
110             if (User_flag_profile::exists($profile->id, $user->id)) {
111                 $action->element('p', 'flagged', _('Flagged'));
112             } else {
113                 $form = new FlagProfileForm($action, $profile,
114                                         array('action' => 'showstream',
115                                               'nickname' => $profile->nickname));
116                 $form->show();
117             }
118
119             $action->elementEnd('li');
120         }
121
122         return true;
123     }
124
125     function onEndProfileListItemActionElements($item)
126     {
127         $user = common_current_user();
128
129         if (!empty($user)) {
130
131             list($action, $args) = $item->action->returnToArgs();
132
133             $args['action'] = $action;
134
135             $form = new FlagProfileForm($item->action, $item->profile, $args);
136
137             $item->action->elementStart('li', 'entity_flag');
138             $form->show();
139             $item->action->elementEnd('li');
140         }
141
142         return true;
143     }
144
145     function onEndShowStatusNetStyles($action)
146     {
147         $action->cssLink(common_path('plugins/UserFlag/userflag.css'),
148                          null, 'screen, projection, tv');
149         return true;
150     }
151
152     function onEndShowScripts($action)
153     {
154         $action->inlineScript('if ($(".form_entity_flag").length > 0) { SN.U.FormXHR($(".form_entity_flag")); }');
155         return true;
156     }
157
158     function onUserRightsCheck($user, $right, &$result) {
159         switch ($right) {
160         case self::REVIEWFLAGS:
161         case self::CLEARFLAGS:
162             $result = $user->hasRole('moderator');
163             return false; // done processing!
164         }
165
166         return true; // unchanged!
167     }
168 }