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