]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/NoticeTitle/NoticeTitlePlugin.php
Merge branch '0.9.x' into 1.0.x
[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 onStartActivityTitle(&$notice, &$xs, &$output)
263     {
264         $title = Notice_title::fromNotice($notice);
265
266         if (!empty($title)) {
267             $output = $title;
268         }
269
270         return true;
271     }
272
273     /**
274      * Remove title when the notice is deleted
275      *
276      * @param Notice $notice Notice being deleted
277      *
278      * @return boolean hook value
279      */
280
281     function onNoticeDeleteRelated($notice)
282     {
283         $nt = Notice_title::staticGet('notice_id', $notice->id);
284
285         if (!empty($nt)) {
286             $nt->delete();
287         }
288
289         return true;
290     }
291
292     /**
293      * If a notice has a title, show it in the <title> element
294      *
295      * @param Action $action Action being executed
296      *
297      * @return boolean hook value
298      */
299
300     function onStartShowHeadTitle($action)
301     {
302         $actionName = $action->trimmed('action');
303
304         if ($actionName == 'shownotice') {
305             $title = Notice_title::fromNotice($action->notice);
306             if (!empty($title)) {
307                 $action->element('title', null,
308                                  // TRANS: Page title. %1$s is the title, %2$s is the site name.
309                                  sprintf(_m("%1\$s - %2\$s"),
310                                          $title,
311                                          common_config('site', 'name')));
312             }
313         }
314
315         return true;
316     }
317
318     /**
319      * If a notice has a title, show it in the <h1> element
320      *
321      * @param Action $action Action being executed
322      *
323      * @return boolean hook value
324      */
325
326     function onStartShowPageTitle($action)
327     {
328         $actionName = $action->trimmed('action');
329
330         if ($actionName == 'shownotice') {
331             $title = Notice_title::fromNotice($action->notice);
332             if (!empty($title)) {
333                 $action->element('h1', null, $title);
334                 return false;
335             }
336         }
337
338         return true;
339     }
340
341     /**
342      * Does the current user have permission to use the notice-title widget?
343      * Always true unless the plugin's "restricted" setting is on, in which
344      * case it's limited to users with the "richedit" role.
345      *
346      * @fixme make that more sanely configurable :)
347      *
348      * @return boolean
349      */
350     private function isAllowedRichEdit()
351     {
352         if ($this->restricted) {
353             $user = common_current_user();
354             return !empty($user) && $user->hasRole('richedit');
355         } else {
356             return true;
357         }
358     }
359
360 }