]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/NoticeTitle/NoticeTitlePlugin.php
Merge branch 'swat0' into 0.9.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
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',
73                                                  'integer',
74                                                  null,
75                                                  true,
76                                                  'PRI'),
77                                    new ColumnDef('title',
78                                                  'varchar',
79                                                  Notice_title::MAXCHARS,
80                                                  false)));
81
82         return true;
83     }
84
85     /**
86      * Load related modules when needed
87      *
88      * @param string $cls Name of the class to be loaded
89      *
90      * @return boolean hook value; true means continue processing, false means stop.
91      */
92
93     function onAutoload($cls)
94     {
95         $dir = dirname(__FILE__);
96
97         switch ($cls)
98         {
99         case 'Notice_title':
100             include_once $dir . '/'.$cls.'.php';
101             return false;
102         default:
103             return true;
104         }
105     }
106
107     /**
108      * Provide plugin version information.
109      *
110      * This data is used when showing the version page.
111      *
112      * @param array &$versions array of version data arrays; see EVENTS.txt
113      *
114      * @return boolean hook value
115      */
116
117     function onPluginVersion(&$versions)
118     {
119         $url = 'http://status.net/wiki/Plugin:NoticeTitle';
120
121         $versions[] = array('name' => 'NoticeTitle',
122                             'version' => NOTICE_TITLE_PLUGIN_VERSION,
123                             'author' => 'Evan Prodromou',
124                             'homepage' => $url,
125                             'rawdescription' =>
126                             _m('Adds optional titles to notices'));
127         return true;
128     }
129
130     /**
131      * Show title entry when showing notice form
132      *
133      * @param Form $form Form being shown
134      *
135      * @return boolean hook value
136      */
137
138     function onStartShowNoticeFormData($form)
139     {
140         $form->out->element('style',
141                             null,
142                             'label#notice_data-text-label { display: none }');
143         $form->out->element('input', array('type' => 'text',
144                                            'id' => 'notice_title',
145                                            'name' => 'notice_title',
146                                            'size' => 40,
147                                            'maxlength' => Notice_title::MAXCHARS));
148         return true;
149     }
150
151     /**
152      * Validate notice title before saving
153      *
154      * @param Action  $action    NewNoticeAction being executed
155      * @param integer &$authorId Author ID
156      * @param string  &$text     Text of the notice
157      * @param array   &$options  Options array
158      *
159      * @return boolean hook value
160      */
161
162     function onStartNoticeSaveWeb($action, &$authorId, &$text, &$options)
163     {
164         $title = $action->trimmed('notice_title');
165         if (!empty($title)) {
166             if (mb_strlen($title) > Notice_title::MAXCHARS) {
167                 throw new Exception(sprintf(_m("Notice title too long (max %d)",
168                                                Notice_title::MAXCHARS)));
169             }
170         }
171         return true;
172     }
173
174     /**
175      * Save notice title after notice is saved
176      *
177      * @param Action $action NewNoticeAction being executed
178      * @param Notice $notice Notice that was saved
179      *
180      * @return boolean hook value
181      */
182
183     function onEndNoticeSaveWeb($action, $notice)
184     {
185         if (!empty($notice)) {
186
187             $title = $action->trimmed('notice_title');
188
189             if (!empty($title)) {
190
191                 $nt = new Notice_title();
192
193                 $nt->notice_id = $notice->id;
194                 $nt->title     = $title;
195
196                 $nt->insert();
197             }
198         }
199
200         return true;
201     }
202
203     /**
204      * Show the notice title in lists
205      *
206      * @param NoticeListItem $nli NoticeListItem being shown
207      *
208      * @return boolean hook value
209      */
210
211     function onStartShowNoticeItem($nli)
212     {
213         $title = Notice_title::fromNotice($nli->notice);
214
215         if (!empty($title)) {
216             $nli->out->element('h4', array('class' => 'notice_title'), $title);
217         }
218
219         return true;
220     }
221
222     /**
223      * Show the notice title in RSS output
224      *
225      * @param Notice $notice Notice being shown
226      * @param array  &$entry array of values used for RSS output
227      *
228      * @return boolean hook value
229      */
230
231     function onEndRssEntryArray($notice, &$entry)
232     {
233         $title = Notice_title::fromNotice($notice);
234
235         if (!empty($title)) {
236             $entry['title'] = $title;
237         }
238
239         return true;
240     }
241
242     /**
243      * Show the notice title in Atom output
244      *
245      * @param Notice      &$notice Notice being shown
246      * @param XMLStringer &$xs     output context
247      * @param string      &$output string to be output as title
248      *
249      * @return boolean hook value
250      */
251
252     function onStartActivityTitle(&$notice, &$xs, &$output)
253     {
254         $title = Notice_title::fromNotice($notice);
255
256         if (!empty($title)) {
257             $output = $title;
258         }
259
260         return true;
261     }
262
263     /**
264      * Remove title when the notice is deleted
265      *
266      * @param Notice $notice Notice being deleted
267      *
268      * @return boolean hook value
269      */
270
271     function onNoticeDeleteRelated($notice)
272     {
273         $nt = Notice_title::staticGet('notice_id', $notice->id);
274
275         if (!empty($nt)) {
276             $nt->delete();
277         }
278
279         return true;
280     }
281 }
282