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