3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A plugin to add titles to notices
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.
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.
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/>.
23 * @category NoticeTitle
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
37 define('NOTICE_TITLE_PLUGIN_VERSION', '0.1');
40 * NoticeTitle plugin to add an optional title to notices.
42 * Stores notice titles in a secondary table, notice_title.
44 * @category NoticeTitle
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/
52 class NoticeTitlePlugin extends Plugin
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;
61 * Database schema setup
63 * Add the notice_title helper table
68 * @return boolean hook value; true means continue processing, false means stop.
71 function onCheckSchema()
73 $schema = Schema::get();
75 // For storing titles for notices
77 $schema->ensureTable('notice_title',
78 array(new ColumnDef('notice_id',
83 new ColumnDef('title',
85 Notice_title::MAXCHARS,
92 * Load related modules when needed
94 * @param string $cls Name of the class to be loaded
96 * @return boolean hook value; true means continue processing, false means stop.
99 function onAutoload($cls)
101 $dir = dirname(__FILE__);
106 include_once $dir . '/'.$cls.'.php';
114 * Provide plugin version information.
116 * This data is used when showing the version page.
118 * @param array &$versions array of version data arrays; see EVENTS.txt
120 * @return boolean hook value
123 function onPluginVersion(&$versions)
125 $url = 'http://status.net/wiki/Plugin:NoticeTitle';
127 $versions[] = array('name' => 'NoticeTitle',
128 'version' => NOTICE_TITLE_PLUGIN_VERSION,
129 'author' => 'Evan Prodromou',
132 _m('Adds optional titles to notices.'));
137 * Show title entry when showing notice form
139 * @param Form $form Form being shown
141 * @return boolean hook value
144 function onStartShowNoticeFormData($form)
146 if ($this->isAllowedRichEdit()) {
147 $form->out->element('style',
149 'label#notice_data-text-label { display: none }');
150 $form->out->element('input', array('type' => 'text',
151 'id' => 'notice_title',
152 'name' => 'notice_title',
154 'maxlength' => Notice_title::MAXCHARS));
160 * Validate notice title before saving
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
167 * @return boolean hook value
170 function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options)
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)));
183 * Save notice title after notice is saved
185 * @param Action $action NewNoticeAction being executed
186 * @param Notice $notice Notice that was saved
188 * @return boolean hook value
191 function onEndNoticeSaveWeb($action, $notice)
193 if (!empty($notice)) {
195 $title = $action->trimmed('notice_title');
197 if (!empty($title) && $this->isAllowedRichEdit()) {
199 $nt = new Notice_title();
201 $nt->notice_id = $notice->id;
212 * Show the notice title in lists
214 * @param NoticeListItem $nli NoticeListItem being shown
216 * @return boolean hook value
219 function onStartShowNoticeItem($nli)
221 $title = Notice_title::fromNotice($nli->notice);
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');
233 * Show the notice title in RSS output
235 * @param Notice $notice Notice being shown
236 * @param array &$entry array of values used for RSS output
238 * @return boolean hook value
241 function onEndRssEntryArray($notice, &$entry)
243 $title = Notice_title::fromNotice($notice);
245 if (!empty($title)) {
246 $entry['title'] = $title;
253 * Show the notice title in Atom output
255 * @param Notice &$notice Notice being shown
256 * @param XMLStringer &$xs output context
257 * @param string &$output string to be output as title
259 * @return boolean hook value
262 function onEndNoticeAsActivity($notice, &$activity)
264 $title = Notice_title::fromNotice($notice);
266 if (!empty($title)) {
267 foreach ($activity->objects as $obj) {
268 if ($obj->id == $notice->uri) {
269 $obj->title = $title;
279 * Remove title when the notice is deleted
281 * @param Notice $notice Notice being deleted
283 * @return boolean hook value
286 function onNoticeDeleteRelated($notice)
288 $nt = Notice_title::staticGet('notice_id', $notice->id);
298 * If a notice has a title, show it in the <title> element
300 * @param Action $action Action being executed
302 * @return boolean hook value
305 function onStartShowHeadTitle($action)
307 $actionName = $action->trimmed('action');
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"),
316 common_config('site', 'name')));
324 * If a notice has a title, show it in the <h1> element
326 * @param Action $action Action being executed
328 * @return boolean hook value
331 function onStartShowPageTitle($action)
333 $actionName = $action->trimmed('action');
335 if ($actionName == 'shownotice') {
336 $title = Notice_title::fromNotice($action->notice);
337 if (!empty($title)) {
338 $action->element('h1', null, $title);
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.
351 * @fixme make that more sanely configurable :)
355 private function isAllowedRichEdit()
357 if ($this->restricted) {
358 $user = common_current_user();
359 return !empty($user) && $user->hasRole('richedit');