X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FNoticeTitle%2FNoticeTitlePlugin.php;h=054c3d3e48a590b8b632997a6bfb270805ae2537;hb=39cf2338c26d55fced83a0c411550d522f9badd8;hp=18ffed4be29457ecfe200991998eb885d47eef6f;hpb=9b7536351b1efa5bf222fad1fb92b6dc9e33156f;p=quix0rs-gnu-social.git diff --git a/plugins/NoticeTitle/NoticeTitlePlugin.php b/plugins/NoticeTitle/NoticeTitlePlugin.php index 18ffed4be2..054c3d3e48 100644 --- a/plugins/NoticeTitle/NoticeTitlePlugin.php +++ b/plugins/NoticeTitle/NoticeTitlePlugin.php @@ -51,6 +51,12 @@ define('NOTICE_TITLE_PLUGIN_VERSION', '0.1'); class NoticeTitlePlugin extends Plugin { + + // By default, notice-title widget will be available to all users. + // With restricted on, only users who have been granted the + // "richedit" role get it. + public $restricted = false; + /** * Database schema setup * @@ -123,7 +129,7 @@ class NoticeTitlePlugin extends Plugin 'author' => 'Evan Prodromou', 'homepage' => $url, 'rawdescription' => - _m('Adds optional titles to notices')); + _m('Adds optional titles to notices.')); return true; } @@ -137,12 +143,16 @@ class NoticeTitlePlugin extends Plugin function onStartShowNoticeFormData($form) { - $form->out->element('style', null, 'label#notice_data-text-label { display: none }'); - $form->out->element('input', array('type' => 'text', - 'id' => 'notice_title', - 'name' => 'notice_title', - 'size' => 40, - 'maxlength' => Notice_title::MAXCHARS)); + if ($this->isAllowedRichEdit()) { + $form->out->element('style', + null, + 'label#notice_data-text-label { display: none }'); + $form->out->element('input', array('type' => 'text', + 'id' => 'notice_title', + 'name' => 'notice_title', + 'size' => 40, + 'maxlength' => Notice_title::MAXCHARS)); + } return true; } @@ -160,9 +170,9 @@ class NoticeTitlePlugin extends Plugin function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options) { $title = $action->trimmed('notice_title'); - if (!empty($title)) { + if (!empty($title) && $this->isAllowedRichEdit()) { if (mb_strlen($title) > Notice_title::MAXCHARS) { - throw new Exception(sprintf(_m("Notice title too long (max %d)", + throw new Exception(sprintf(_m("The notice title is too long (max %d characters).", Notice_title::MAXCHARS))); } } @@ -184,7 +194,7 @@ class NoticeTitlePlugin extends Plugin $title = $action->trimmed('notice_title'); - if (!empty($title)) { + if (!empty($title) && $this->isAllowedRichEdit()) { $nt = new Notice_title(); @@ -211,7 +221,9 @@ class NoticeTitlePlugin extends Plugin $title = Notice_title::fromNotice($nli->notice); if (!empty($title)) { - $nli->out->element('h4', array('class' => 'notice_title'), $title); + $nli->out->elementStart('h4', array('class' => 'notice_title')); + $nli->out->element('a', array('href' => $nli->notice->bestUrl()), $title); + $nli->out->elementEnd('h4'); } return true; @@ -247,15 +259,107 @@ class NoticeTitlePlugin extends Plugin * @return boolean hook value */ - function onStartActivityTitle(&$notice, &$xs, &$output) + function onEndNoticeAsActivity($notice, &$activity) { $title = Notice_title::fromNotice($notice); if (!empty($title)) { - $output = $title; + foreach ($activity->objects as $obj) { + if ($obj->id == $notice->uri) { + $obj->title = $title; + break; + } + } + } + + return true; + } + + /** + * Remove title when the notice is deleted + * + * @param Notice $notice Notice being deleted + * + * @return boolean hook value + */ + + function onNoticeDeleteRelated($notice) + { + $nt = Notice_title::staticGet('notice_id', $notice->id); + + if (!empty($nt)) { + $nt->delete(); + } + + return true; + } + + /** + * If a notice has a title, show it in the element + * + * @param Action $action Action being executed + * + * @return boolean hook value + */ + + function onStartShowHeadTitle($action) + { + $actionName = $action->trimmed('action'); + + if ($actionName == 'shownotice') { + $title = Notice_title::fromNotice($action->notice); + if (!empty($title)) { + $action->element('title', null, + // TRANS: Page title. %1$s is the title, %2$s is the site name. + sprintf(_m("%1\$s - %2\$s"), + $title, + common_config('site', 'name'))); + } + } + + return true; + } + + /** + * If a notice has a title, show it in the <h1> element + * + * @param Action $action Action being executed + * + * @return boolean hook value + */ + + function onStartShowPageTitle($action) + { + $actionName = $action->trimmed('action'); + + if ($actionName == 'shownotice') { + $title = Notice_title::fromNotice($action->notice); + if (!empty($title)) { + $action->element('h1', null, $title); + return false; + } } return true; } -} + /** + * Does the current user have permission to use the notice-title widget? + * Always true unless the plugin's "restricted" setting is on, in which + * case it's limited to users with the "richedit" role. + * + * @fixme make that more sanely configurable :) + * + * @return boolean + */ + private function isAllowedRichEdit() + { + if ($this->restricted) { + $user = common_current_user(); + return !empty($user) && $user->hasRole('richedit'); + } else { + return true; + } + } + +}