summaryrefslogtreecommitdiff
path: root/plugins/SocialObject/SocialObjectPlugin.php
blob: 7c63c8a70c9b4f3e0a6eec28b31e378baf563cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2009, StatusNet, Inc.
 * Copyright (C) 2010, Free Software Foundation, Inc.
 *
 * Base class for all social object plugins
 *
 * PHP version 5
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  GNUSocial
 * @package   StatusNet
 * @author    Shashi Gowda <connect2shashi@gmail.com>
 * @copyright 2009, 2010, StatusNet, Inc.
 * @copyright 2010, Free Software Foundation, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
 * @link      http://daisycha.in
 */


if (!defined('STATUSNET')) {
    exit(1);
}

class SocialObjectPlugin extends Plugin
{
    # name to use in the UI
    public $name=null;
    # plural name for menus
    private $name_plural=null;
    # short name for use in urls
    private $slug=null;
    # plural slug
    private $slug_plural=null;
    # Database class
    private $dbclass=null;
    # path to the plugin directory
    private $plugin_path = null;

    function initialize()
    {
        # set $this->plugin_path here.
    }

    function onAutoload($cls)
    {
        $actions = array();
        foreach(array('Public', 'User', 'Group') as $a) {
            $actions[] = $a.$this->slug_plural.'Action';
        }
        $actions[] = 'New'.$this->slug.'Action';
        $actions[] = $this->dbclass;

        if(in_array($cls, $actions)) {
            require_once $this->plugin_path."/$cls.php";
            return false;
        }
        return true;
    }

    # setup DB tables etc.
    function onCheckSchema()
    {
        $schema = Schema::get();
        $classname = $this->dbclass
        $schema->ensureTable($classname::schemaDef());
        return true;
    }

    # Add some urls to be routed to respective actions
    function onRouterInitialized(&$m)
    {
        # timeline of all public social-objects
        $m->connect('public/'.$this->slug_plural,
                        array('action' => 'public'.$this->slug_plural));
        # timeline of this social-object for each user
        $m->connect(':user/'.$this->slug_plural,
                        array('action' => 'user'.$this->slug_plural,
                              'user' => '[a-zA-Z0-9]{1,64}'));
        # timeline of this social-object for groups
        $m->connect('group/:group/'.$this->slug_plural,
                        array('action' => 'group'.$this->slug_plural,
                              'group' => '[a-zA-Z0-9]{1,64}'));
        # create new
        $m->connect($this->slug.'/new',
                        array('action' => 'new'.$this->slug_plural));
        # more to come in future: popular & lists.
        return true;
    }

    # use this to blow our caches.
    # the alternative is to decide
    # which keys to blow, no thanks
    # Notice class already does that
    function onEndCacheDelete($key)
    {
        $keys = array('public:notice_ids' => 'public:%s:notice_ids',
                             'profile:notice_ids:' => 'profile:%s:notice_ids:',
                             'user_group:notice_ids:' => 'user_group:%s:notice_ids:');
        foreach($keys as $s => $r) {
            $len = strlen($s);
            # if key starts with $s,
            if(substr($key, 0, $len) === $s) {
                Memcached_DataObject::blow(sprintf($r, $this->slug)));
            }
        }
    }

    # Menu on public page
    function onEndPublicGroupNav($action)
    {
        $action->out->menuItem(common_local_url('public'.$this->slug_plural), $this->name_plural);
        return true;
    }

    # Menu on personal pages (/user, /user/all, /user/replies etc)
    function onEndPersonalGroupNav($action)
    {
        $action->out->menuItem(common_local_url('user'.$this->slug_plural,
                array('user' => $action->trimmed('nickname'))), $this->name_plural);
        return true;
    }

    # Menu on group pages
    function onEndGroupGroupNav($action)
    {
        $action->out->menuItem(common_local_url('user'.$this->slug_plural,
                array('group' => $action->trimmed('nickname'))), $this->name_plural);
        return true;
    }

    # This is the menu for cranking up the notice form
    function onShowSocialSwitcher($form)
    {
        $form->out->elementStart('li', "social-switch {$this->slug}-switch");
        $form->out->element('a',
                    array('href' => common_local_url('shownoticeform').'#social-'.$this->slug,
                          'title' => $this->name)
                    , $this->name);
        $form->out->elementEnd('li');
    }

    # if the notice being shown has metadata for
    # a social-object then render it differently
    function onStartShowNoticeItem($widget)
    {
        if($this->isThisObject($widget->notice)) {

            $this->showItem($widget);
            $widget->showNoticeInfo();
            $widget->showNoticeOptions();
            # don't want to break other plugins
            Event::handle('EndShowNoticeItem', array($widget));
            # return false, i.e stop rendering the notice
            return false;
        }
        # continue with showing notice
        return true;
    }

    # this hook needs to be patched to upstream
    function onStartSingleNoticeItem($widget)
    {
        return $this->showItem($widget);
    }

    # does the notice have metadata related to this social object?
    function isThisObject($notice)
    {
        $obj=call_user_func($this->dbclass, 'staticGet',
                array(array('id' => $notice->id)));
        return !empty($obj);
    }

    # show object in timeline
    function showItem($widget)
    {
        $classname = $this->slug.'Widget';
        $widget=new $classname();
        $widget->show();
    }
}