]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blog/BlogPlugin.php
eb1f5833c49eaf754692b935e7fbaf0b5b58a4ec
[quix0rs-gnu-social.git] / plugins / Blog / BlogPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A microapp to implement lite blogging
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  Blog
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 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 /**
38  * Blog plugin
39  *
40  * Many social systems have a way to write and share long-form texts with
41  * your network. This microapp plugin lets users post blog entries.
42  *
43  * @category  Blog
44  * @package   StatusNet
45  * @author    Evan Prodromou <evan@status.net>
46  * @copyright 2011 StatusNet, Inc.
47  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48  * @link      http://status.net/
49  */
50
51 class BlogPlugin extends MicroAppPlugin
52 {
53     /**
54      * Database schema setup
55      *
56      * @see Schema
57      * @see ColumnDef
58      *
59      * @return boolean hook value; true means continue processing, false means stop.
60      */
61     function onCheckSchema()
62     {
63         $schema = Schema::get();
64
65         $schema->ensureTable('blog_entry', Blog_entry::schemaDef());
66
67         return true;
68     }
69
70     /**
71      * Load related modules when needed
72      *
73      * @param string $cls Name of the class to be loaded
74      *
75      * @return boolean hook value; true means continue processing, false means stop.
76      */
77     function onAutoload($cls)
78     {
79         $dir = dirname(__FILE__);
80
81         switch ($cls)
82         {
83         case 'NewblogentryAction':
84         case 'ShowblogentryAction':
85             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
86             return false;
87         case 'BlogEntryForm':
88         case 'BlogEntryListItem':
89             include_once $dir . '/'.strtolower($cls).'.php';
90             return false;
91         case 'Blog_entry':
92             include_once $dir . '/'.$cls.'.php';
93             return false;
94         default:
95             return true;
96         }
97     }
98
99     /**
100      * Map URLs to actions
101      *
102      * @param Net_URL_Mapper $m path-to-action mapper
103      *
104      * @return boolean hook value; true means continue processing, false means stop.
105      */
106     function onRouterInitialized($m)
107     {
108         $m->connect('blog/new',
109                     array('action' => 'newblogentry'));
110         $m->connect('blog/:id',
111                     array('action' => 'showblogentry'),
112                     array('id' => UUID::REGEX));
113         return true;
114     }
115
116     function onPluginVersion(&$versions)
117     {
118         $versions[] = array('name' => 'Blog',
119                             'version' => STATUSNET_VERSION,
120                             'author' => 'Evan Prodromou',
121                             'homepage' => 'http://status.net/wiki/Plugin:Blog',
122                             'rawdescription' =>
123                             // TRANS: Plugin description.
124                             _m('Let users write and share long-form texts.'));
125         return true;
126     }
127
128     function appTitle()
129     {
130         // TRANS: Blog application title.
131         return _m('TITLE','Blog');
132     }
133
134     function tag()
135     {
136         return 'blog';
137     }
138
139     function types()
140     {
141         return array(Blog_entry::TYPE);
142     }
143
144     function saveNoticeFromActivity($activity, $actor, $options=array())
145     {
146         if (count($activity->objects) != 1) {
147             // TRANS: Exception thrown when there are too many activity objects.
148             throw new ClientException(_m('Too many activity objects.'));
149         }
150
151         $entryObj = $activity->objects[0];
152
153         if ($entryObj->type != Blog_entry::TYPE) {
154             // TRANS: Exception thrown when blog plugin comes across a non-blog entry type object.
155             throw new ClientException(_m('Wrong type for object.'));
156         }
157
158         $notice = null;
159
160         switch ($activity->verb) {
161         case ActivityVerb::POST:
162             $notice = Blog_entry::saveNew($actor,
163                                          $entryObj->title,
164                                          $entryObj->content,
165                                          $options);
166             break;
167         default:
168             // TRANS: Exception thrown when blog plugin comes across a undefined verb.
169             throw new ClientException(_m('Unknown verb for blog entries.'));
170         }
171
172         return $notice;
173     }
174
175     function activityObjectFromNotice($notice)
176     {
177         $entry = Blog_entry::fromNotice($notice);
178
179         if (empty($entry)) {
180             // TRANS: Exception thrown when requesting a non-existing blog entry for notice.
181             throw new ClientException(sprintf(_m('No blog entry for notice %s.'),
182                         $notice->id));
183         }
184
185         return $entry->asActivityObject();
186     }
187
188     function entryForm($out)
189     {
190         return new BlogEntryForm($out);
191     }
192
193     function deleteRelated($notice)
194     {
195         if ($notice->object_type == Blog_entry::TYPE) {
196             $entry = Blog_entry::fromNotice($notice);
197             if (!empty($entry)) {
198                 $entry->delete();
199             }
200         }
201     }
202
203     function adaptNoticeListItem($nli)
204     {
205         $notice = $nli->notice;
206
207         if ($notice->object_type == Blog_entry::TYPE) {
208             return new BlogEntryListItem($nli);
209         }
210
211         return null;
212     }
213
214     function onEndShowScripts($action)
215     {
216         $action->script(common_path('plugins/TinyMCE/js/jquery.tinymce.js'));
217         $action->inlineScript('var _tinymce_path = "'.common_path('plugins/TinyMCE/js/tiny_mce.js').'";'."\n".
218                               'var _tinymce_placeholder = "'.common_path('plugins/TinyMCE/icons/placeholder.png').'";'."\n");
219         $action->script($this->path('blog.js'));
220         return true;
221     }
222 }