]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/AnonymousFavePlugin.php
- Increment/decrement notice fave tally
[quix0rs-gnu-social.git] / plugins / AnonymousFave / AnonymousFavePlugin.php
1 <?php
2
3 /**
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2010, StatusNet, Inc.
6  *
7  * A plugin to allow anonymous users to favorite notices
8  *
9  * PHP version 5
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Affero General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Affero General Public License for more details.
20  *
21  * You should have received a copy of the GNU Affero General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  * @category  Plugin
25  * @package   StatusNet
26  * @author    Zach Copley <zach@status.net>
27  * @copyright 2010 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     // This check helps protect against security problems;
34     // your code file can't be executed directly from the web.
35     exit(1);
36 }
37
38 define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1');
39
40 /**
41  * Anonymous Fave plugin to allow anonymous (not logged in) users
42  * to favorite notices
43  *
44  * @category  Plugin
45  * @package   StatusNet
46  * @author    Zach Copley <zach@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51 class AnonymousFavePlugin extends Plugin {
52
53     function onArgsInitialize() {
54         // We always want a session because we're tracking anon users
55         common_ensure_session();
56     }
57
58     /**
59      * Hook for ensuring our tables are created
60      *
61      * Ensures the fave_tally table is there and has the right columns
62      *
63      * @return boolean hook return
64      */
65
66     function onCheckSchema()
67     {
68         $schema = Schema::get();
69
70         // For storing total number of times a notice has been faved
71
72         $schema->ensureTable('fave_tally',
73             array(
74                 new ColumnDef('notice_id', 'integer', null,  false, 'PRI'),
75                 new ColumnDef('count', 'integer', null, false),
76                 new ColumnDef(
77                     'modified',
78                     'timestamp',
79                     null,
80                     false,
81                     null,
82                     'CURRENT_TIMESTAMP',
83                     'on update CURRENT_TIMESTAMP'
84                 )
85             )
86         );
87
88         return true;
89     }
90
91     function onEndShowHTML($action)
92     {
93         if (!common_logged_in()) {
94             // Set a place to return to when submitting forms
95             common_set_returnto($action->selfUrl());
96         }
97     }
98
99     function onEndShowScripts($action)
100     {
101         // Setup ajax calls for favoriting. Usually this is only done when
102         // a user is logged in.
103         $action->inlineScript('SN.U.NoticeFavor();');
104     }
105
106     function onAutoload($cls)
107     {
108         $dir = dirname(__FILE__);
109
110         switch ($cls) {
111             case 'Fave_tally':
112                 include_once $dir . '/' . $cls . '.php';
113                 return false;
114             case 'AnonFavorAction':
115                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
116                 return false;
117             case 'AnonDisFavorAction':
118                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
119                 return false;
120             case 'AnonFavorForm':
121                 include_once $dir . '/anonfavorform.php';
122                 return false;
123             case 'AnonDisFavorForm':
124                 include_once $dir . '/anondisfavorform.php';
125                 return false;
126             default:
127                 return true;
128         }
129     }
130
131     function onStartInitializeRouter($m) {
132
133         $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
134         $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
135
136         return true;
137     }
138
139
140     function onEndShowNoticeInfo($item)
141     {
142         common_debug("XXXXXXXXXXX onEndShowNoticeInfo");
143
144         $tally = Fave_tally::ensureTally($item->notice->id);
145
146         if (!empty($tally)) {
147             $item->out->elementStart(
148                 'div',
149                 array(
150                     'id' => 'notice-' . $item->notice->id . '-tally',
151                     'class' => 'notice-tally'
152                 )
153             );
154             $item->out->raw(sprintf(_m("favored %d times"), $tally->count));
155             $item->out->elementEnd('div');
156         }
157         return true;
158     }
159
160     function onStartShowNoticeOptions($item) {
161
162         if (!common_logged_in()) {
163             $item->out->elementStart('div', 'notice-options');
164             $item->showFaveForm();
165             $item->out->elementEnd('div');
166         }
167
168         return true;
169     }
170
171     function onStartShowFaveForm($item) {
172
173         if (!common_logged_in()) {
174
175             $profile = $this->getAnonProfile();
176             if (!empty($profile)) {
177                 if ($profile->hasFave($item->notice)) {
178                     $disfavor = new AnonDisFavorForm($item->out, $item->notice);
179                     $disfavor->show();
180                 } else {
181                     $favor = new AnonFavorForm($item->out, $item->notice);
182                     $favor->show();
183                 }
184             }
185         }
186
187         return true;
188     }
189
190     function onEndFavorNotice($profile, $notice)
191     {
192         $tally = Fave_tally::increment($notice->id);
193     }
194
195     function onEndDisfavorNotice($profile, $notice)
196     {
197         $tally = Fave_tally::decrement($notice->id);
198     }
199
200     function createAnonProfile() {
201
202         // Get the anon user's IP, and turn it into a nickname
203         list($proxy, $ip) = common_client_ip();
204         // IP + time + random number should avoid collisions
205         $nickname = 'anonymous-' . $ip . '-' . time() . '-' . common_good_rand(5);
206
207         $profile = new Profile();
208         $profile->nickname = $nickname;
209         $id = $profile->insert();
210
211         if (!empty($id)) {
212             common_log(
213                     LOG_INFO,
214                     "AnonymousFavePlugin - created profile for anonymous user from IP: "
215                     . $ip
216                     . ', nickname = '
217                     . $nickname
218             );
219         }
220
221         return $profile;
222     }
223
224     function getAnonProfile() {
225
226         $anon = $_SESSION['anon_nickname'];
227
228         $profile = null;
229
230         if (!empty($anon)) {
231             $profile = Profile::staticGet('nickname', $anon);
232         } else {
233             $profile = $this->createAnonProfile();
234             $_SESSION['anon_nickname'] = $profile->nickname;
235         }
236
237         if (!empty($profile)) {
238             return $profile;
239         }
240     }
241
242     /**
243      * Provide plugin version information.
244      *
245      * This data is used when showing the version page.
246      *
247      * @param array &$versions array of version data arrays; see EVENTS.txt
248      *
249      * @return boolean hook value
250      */
251     function onPluginVersion(&$versions)
252     {
253         $url = 'http://status.net/wiki/Plugin:AnonymousFave';
254
255         $versions[] = array('name' => 'AnonymousFave',
256             'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
257             'author' => 'Zach Copley',
258             'homepage' => $url,
259             'rawdescription' =>
260             _m('Allow anonymous users to favorite notices.'));
261
262         return true;
263     }
264
265 }