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