]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/NoticeTitle/NoticeTitlePlugin.php
show notice title for notices in a notice list
[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      * Database schema setup
56      *
57      * Add the notice_title helper table
58      *
59      * @see Schema
60      * @see ColumnDef
61      *
62      * @return boolean hook value; true means continue processing, false means stop.
63      */
64
65     function onCheckSchema()
66     {
67         $schema = Schema::get();
68
69         // For storing titles for notices
70
71         $schema->ensureTable('notice_title',
72                              array(new ColumnDef('notice_id', 'integer', null, true, 'PRI'),
73                                    new ColumnDef('title', 'varchar', Notice_title::MAXCHARS, false)));
74
75         return true;
76     }
77
78     /**
79      * Load related modules when needed
80      *
81      * @param string $cls Name of the class to be loaded
82      *
83      * @return boolean hook value; true means continue processing, false means stop.
84      */
85
86     function onAutoload($cls)
87     {
88         $dir = dirname(__FILE__);
89
90         switch ($cls)
91         {
92         case 'Notice_title':
93             include_once $dir . '/'.$cls.'.php';
94             return false;
95         default:
96             return true;
97         }
98     }
99
100     function onPluginVersion(&$versions)
101     {
102         $versions[] = array('name' => 'NoticeTitle',
103                             'version' => NOTICE_TITLE_PLUGIN_VERSION,
104                             'author' => 'Evan Prodromou',
105                             'homepage' => 'http://status.net/wiki/Plugin:NoticeTitle',
106                             'rawdescription' =>
107                             _m('Adds optional titles to notices'));
108         return true;
109     }
110
111     function onStartShowNoticeFormData($form)
112     {
113         $form->out->element('input', array('type' => 'text',
114                                            'id' => 'notice_title',
115                                            'name' => 'notice_title',
116                                            'size' => 40,
117                                            'maxlength' => Notice_title::MAXCHARS,
118                                            'value' => _m('Title'),
119                                            'style' => 'color: 333333',
120                                            'onFocus' => 'this.value = ""; this.style = \'color: black\';'));
121         return true;
122     }
123
124     function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options)
125     {
126         $title = $action->trimmed('notice_title');
127         if (!empty($title)) {
128             if (mb_strlen($title) > Notice_title::MAXCHARS) {
129                 throw new Exception(sprintf(_m("Notice title too long (max %d chars)", Notice_title::MAXCHARS)));
130             }
131         }
132         return true;
133     }
134
135     function onEndNoticeSaveWeb($action, $notice)
136     {
137         if (!empty($notice)) {
138
139             $title = $action->trimmed('notice_title');
140
141             if (!empty($title)) {
142
143                 $nt = new Notice_title();
144
145                 $nt->notice_id = $notice->id;
146                 $nt->title     = $title;
147
148                 $nt->insert();
149             }
150         }
151
152         return true;
153     }
154
155     function onStartShowNoticeItem($nli)
156     {
157         $title = Notice_title::fromNotice($nli->notice);
158
159         if (!empty($title)) {
160             $nli->out->element('h4', array('class' => 'notice_title'), $title);
161         }
162
163         return true;
164     }
165 }
166