]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/NoticeTitle/NoticeTitlePlugin.php
d73da51d588848696ea4b7032ab8fe3c73e6b0de
[quix0rs-gnu-social.git] / plugins / NoticeTitle / NoticeTitlePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A plugin to add titles to notices
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  NoticeTitle
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 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     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 define('NOTICE_TITLE_PLUGIN_VERSION', '0.1');
38
39 /**
40  * NoticeTitle plugin to add an optional title to notices.
41  *
42  * Stores notice titles in a secondary table, notice_title.
43  *
44  * @category  NoticeTitle
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@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 NoticeTitlePlugin extends Plugin
52 {
53
54     // By default, notice-title widget will be available to all users.
55     // With restricted on, only users who have been granted the
56     // "richedit" role get it.
57     public $restricted = false;
58
59     /**
60      * Database schema setup
61      *
62      * Add the notice_title helper table
63      *
64      * @see Schema
65      * @see ColumnDef
66      *
67      * @return boolean hook value; true means continue processing, false means stop.
68      */
69     function onCheckSchema()
70     {
71         $schema = Schema::get();
72
73         // For storing titles for notices
74         $schema->ensureTable('notice_title', Notice_title::schemaDef());
75         return true;
76     }
77
78     /**
79      * Load related modules when needed
80      *
81      * @param string $cls Name of the class to be loaded
82      *
83      * @return boolean hook value; true means continue processing, false means stop.
84      */
85     function onAutoload($cls)
86     {
87         $dir = dirname(__FILE__);
88
89         switch ($cls)
90         {
91         case 'Notice_title':
92             include_once $dir . '/'.$cls.'.php';
93             return false;
94         default:
95             return true;
96         }
97     }
98
99     /**
100      * Provide plugin version information.
101      *
102      * This data is used when showing the version page.
103      *
104      * @param array &$versions array of version data arrays; see EVENTS.txt
105      *
106      * @return boolean hook value
107      */
108     function onPluginVersion(&$versions)
109     {
110         $url = 'http://status.net/wiki/Plugin:NoticeTitle';
111
112         $versions[] = array('name' => 'NoticeTitle',
113                             'version' => NOTICE_TITLE_PLUGIN_VERSION,
114                             'author' => 'Evan Prodromou',
115                             'homepage' => $url,
116                             'rawdescription' =>
117                             // TRANS: Plugin description.
118                             _m('Adds optional titles to notices.'));
119         return true;
120     }
121
122     /**
123      * Show title entry when showing notice form
124      *
125      * @param Form $form Form being shown
126      *
127      * @return boolean hook value
128      */
129     function onStartShowNoticeFormData($form)
130     {
131         if ($this->isAllowedRichEdit()) {
132             $form->out->element('style',
133                                 null,
134                                 'label#notice_data-text-label { display: none }');
135             $form->out->element('input', array('type' => 'text',
136                                                'id' => 'notice_title',
137                                                'name' => 'notice_title',
138                                                'size' => 40,
139                                                'maxlength' => Notice_title::MAXCHARS));
140         }
141         return true;
142     }
143
144     /**
145      * Validate notice title before saving
146      *
147      * @param Action  $action    NewNoticeAction being executed
148      * @param integer &$authorId Author ID
149      * @param string  &$text     Text of the notice
150      * @param array   &$options  Options array
151      *
152      * @return boolean hook value
153      */
154     function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options)
155     {
156         $title = $action->trimmed('notice_title');
157         if (!empty($title) && $this->isAllowedRichEdit()) {
158             if (mb_strlen($title) > Notice_title::MAXCHARS) {
159                 // TRANS: Exception thrown when a notice title is too long.
160                 // TRANS: %d is the maximum number of characters allowed in a title (used for plural).
161                 throw new Exception(sprintf(_m('The notice title is too long (maximum %d character).',
162                                                'The notice title is too long (maximum %d characters).',
163                                                Notice_title::MAXCHARS),
164                                             Notice_title::MAXCHARS));
165             }
166         }
167         return true;
168     }
169
170     /**
171      * Save notice title after notice is saved
172      *
173      * @param Action $action NewNoticeAction being executed
174      * @param Notice $notice Notice that was saved
175      *
176      * @return boolean hook value
177      */
178     function onEndNoticeSaveWeb($action, $notice)
179     {
180         if (!empty($notice)) {
181
182             $title = $action->trimmed('notice_title');
183
184             if (!empty($title) && $this->isAllowedRichEdit()) {
185
186                 $nt = new Notice_title();
187
188                 $nt->notice_id = $notice->id;
189                 $nt->title     = $title;
190
191                 $nt->insert();
192             }
193         }
194
195         return true;
196     }
197
198     /**
199      * Show the notice title in lists
200      *
201      * @param NoticeListItem $nli NoticeListItem being shown
202      *
203      * @return boolean hook value
204      */
205     function onStartShowNoticeItem($nli)
206     {
207         $title = Notice_title::fromNotice($nli->notice);
208
209         if (!empty($title)) {
210             $nli->out->elementStart('h4', array('class' => 'notice_title'));
211             $nli->out->element('a', array('href' => $nli->notice->bestUrl()), $title);
212             $nli->out->elementEnd('h4');
213         }
214
215         return true;
216     }
217
218     /**
219      * Show the notice title in RSS output
220      *
221      * @param Notice $notice Notice being shown
222      * @param array  &$entry array of values used for RSS output
223      *
224      * @return boolean hook value
225      */
226     function onEndRssEntryArray($notice, &$entry)
227     {
228         $title = Notice_title::fromNotice($notice);
229
230         if (!empty($title)) {
231             $entry['title'] = $title;
232         }
233
234         return true;
235     }
236
237     /**
238      * Show the notice title in Atom output
239      *
240      * @param Notice      &$notice Notice being shown
241      * @param XMLStringer &$xs     output context
242      * @param string      &$output string to be output as title
243      *
244      * @return boolean hook value
245      */
246     function onEndNoticeAsActivity($notice, &$activity)
247     {
248         $title = Notice_title::fromNotice($notice);
249
250         if (!empty($title)) {
251             foreach ($activity->objects as $obj) {
252                 if ($obj->id == $notice->uri) {
253                     $obj->title = $title;
254                     break;
255                 }
256             }
257         }
258
259         return true;
260     }
261
262     /**
263      * Remove title when the notice is deleted
264      *
265      * @param Notice $notice Notice being deleted
266      *
267      * @return boolean hook value
268      */
269     function onNoticeDeleteRelated($notice)
270     {
271         $nt = Notice_title::getKV('notice_id', $notice->id);
272
273         if (!empty($nt)) {
274             $nt->delete();
275         }
276
277         return true;
278     }
279
280     /**
281      * If a notice has a title, show it in the <title> element
282      *
283      * @param Action $action Action being executed
284      *
285      * @return boolean hook value
286      */
287     function onStartShowHeadTitle($action)
288     {
289         $actionName = $action->trimmed('action');
290
291         if ($actionName == 'shownotice') {
292             $title = Notice_title::fromNotice($action->notice);
293             if (!empty($title)) {
294                 $action->element('title', null,
295                                  // TRANS: Page title. %1$s is the title, %2$s is the site name.
296                                  sprintf(_m("%1\$s - %2\$s"),
297                                          $title,
298                                          common_config('site', 'name')));
299             }
300         }
301
302         return true;
303     }
304
305     /**
306      * If a notice has a title, show it in the <h1> element
307      *
308      * @param Action $action Action being executed
309      *
310      * @return boolean hook value
311      */
312     function onStartShowPageTitle($action)
313     {
314         $actionName = $action->trimmed('action');
315
316         if ($actionName == 'shownotice') {
317             $title = Notice_title::fromNotice($action->notice);
318             if (!empty($title)) {
319                 $action->element('h1', null, $title);
320                 return false;
321             }
322         }
323
324         return true;
325     }
326
327     /**
328      * Does the current user have permission to use the notice-title widget?
329      * Always true unless the plugin's "restricted" setting is on, in which
330      * case it's limited to users with the "richedit" role.
331      *
332      * @todo FIXME: make that more sanely configurable :)
333      *
334      * @return boolean
335      */
336     private function isAllowedRichEdit()
337     {
338         if ($this->restricted) {
339             $user = common_current_user();
340             return !empty($user) && $user->hasRole('richedit');
341         } else {
342             return true;
343         }
344     }
345 }