]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/microappplugin.php
Moved functions into ActivityHandlerPlugin from MicroAppPlugin
[quix0rs-gnu-social.git] / lib / microappplugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Superclass for microapp plugin
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  Microapp
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  * Superclass for microapp plugins
39  *
40  * This class lets you define micro-applications with different kinds of activities.
41  *
42  * The applications work more-or-less like other
43  *
44  * @category  Microapp
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2011 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51 abstract class MicroAppPlugin extends ActivityHandlerPlugin
52 {
53     /**
54      * Returns a localized string which represents this micro-app,
55      * to be shown to users selecting what type of post to make.
56      * This is paired with the key string in $this->tag().
57      *
58      * All micro-app classes must override this method.
59      *
60      * @return string
61      */
62     abstract function appTitle();
63
64     /**
65      * Returns a key string which represents this micro-app in HTML
66      * ids etc, as when offering selection of what type of post to make.
67      * This is paired with the user-visible localizable $this->appTitle().
68      *
69      * All micro-app classes must override this method.
70      */
71     abstract function tag();
72
73     /**
74      * When building the primary notice form, we'll fetch also some
75      * alternate forms for specialized types -- that's you!
76      *
77      * Return a custom Widget or Form object for the given output
78      * object, and it'll be included in the HTML output. Beware that
79      * your form may be initially hidden.
80      *
81      * All micro-app classes must override this method.
82      *
83      * @param HTMLOutputter $out
84      * @return Widget
85      */
86     abstract function entryForm($out);
87
88     /**
89      *
90      */
91     public function newFormAction() {
92         // such as 'newbookmark' or 'newevent' route
93         return 'new'.$this->tag();
94     }
95
96     /**
97      * Output the HTML for this kind of object in a list
98      *
99      * @param NoticeListItem $nli The list item being shown.
100      *
101      * @return boolean hook value
102      *
103      * @fixme WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
104      */
105     function onStartShowNoticeItem($nli)
106     {
107         if (!$this->isMyNotice($nli->notice)) {
108             return true;
109         }
110
111         $adapter = $this->adaptNoticeListItem($nli);
112
113         if (!empty($adapter)) {
114             $adapter->showNotice();
115             $adapter->showNoticeAttachments();
116             $adapter->showNoticeInfo();
117             $adapter->showNoticeOptions();
118         } else {
119             $this->oldShowNotice($nli);
120         }
121
122         return false;
123     }
124
125     /**
126      * Given a notice list item, returns an adapter specific
127      * to this plugin.
128      *
129      * @param NoticeListItem $nli item to adapt
130      *
131      * @return NoticeListItemAdapter adapter or null
132      */
133     function adaptNoticeListItem($nli)
134     {
135       return null;
136     }
137
138     function oldShowNotice($nli)
139     {
140         $out = $nli->out;
141         $notice = $nli->notice;
142
143         try {
144             $this->showNotice($notice, $out);
145         } catch (Exception $e) {
146             common_log(LOG_ERR, $e->getMessage());
147             // try to fall back
148             $out->elementStart('div');
149             $nli->showAuthor();
150             $nli->showContent();
151         }
152
153         $nli->showNoticeLink();
154         $nli->showNoticeSource();
155         $nli->showNoticeLocation();
156         $nli->showContext();
157         $nli->showRepeat();
158
159         $out->elementEnd('div');
160
161         $nli->showNoticeOptions();
162     }
163
164     function onStartShowEntryForms(&$tabs)
165     {
166         $tabs[$this->tag()] = array('title' => $this->appTitle(),
167                                     'href'  => common_local_url($this->newFormAction()),
168                                    );
169         return true;
170     }
171
172     function onStartMakeEntryForm($tag, $out, &$form)
173     {
174         if ($tag == $this->tag()) {
175             $form = $this->entryForm($out);
176             return false;
177         }
178
179         return true;
180     }
181
182     function showNotice($notice, $out)
183     {
184         // TRANS: Server exception thrown when a micro app plugin developer has not done his job too well.
185         throw new ServerException(_('You must implement either adaptNoticeListItem() or showNotice().'));
186     }
187 }