]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialProfileExtensions/GNUsocialProfileExtensionsPlugin.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[quix0rs-gnu-social.git] / plugins / GNUsocialProfileExtensions / GNUsocialProfileExtensionsPlugin.php
1 <?php
2 /**
3  * GNU Social
4  * Copyright (C) 2010, Free Software Foundation, Inc.
5  *
6  * PHP version 5
7  *
8  * LICENCE:
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Widget
23  * @package   GNU Social
24  * @author    Max Shinn <trombonechamp@gmail.com>
25  * @copyright 2011 Free Software Foundation, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 class GNUsocialProfileExtensionsPlugin extends Plugin
34 {
35
36     function onAutoload($cls)
37     {
38         $dir = dirname(__FILE__);
39
40         switch ($cls)
41         {
42         case 'BioAction':
43         case 'NewresponseAction':
44             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
45             break;
46         case 'ProfilefieldsAdminPanelAction':
47             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -16)) . '.php';
48             break;
49         default:
50             break;
51         }
52         include_once $dir . '/classes/GNUsocialProfileExtensionField.php';
53         include_once $dir . '/classes/GNUsocialProfileExtensionResponse.php';
54         include_once $dir . '/lib/profiletools.php';
55         include_once $dir . '/lib/noticetree.php';
56         return true;
57     }
58
59     function onCheckSchema()
60     {
61         $schema = Schema::get();
62         $schema->ensureTable('GNUsocialProfileExtensionField',
63                                 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
64                                       new ColumnDef('systemname', 'varchar(64)', null, false),
65                                       new ColumnDef('title', 'varchar(256)', null, false),
66                                       new ColumnDef('description', 'text', null, false),
67                                       new ColumnDef('type', 'varchar(256)', null, false)));
68         $schema->ensureTable('GNUsocialProfileExtensionResponse',
69                                 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
70                                       new ColumnDef('extension_id', 'int(11)', null, false),
71                                       new ColumnDef('profile_id', 'int(11)', null, false),
72                                       new ColumnDef('value', 'text', null, false)));
73                                           
74     }
75
76     function onRouterInitialized($m)
77     {
78         $m->connect(':nickname/bio', array('action' => 'bio'));
79         $m->connect('admin/profilefields', array('action' => 'profilefieldsAdminPanel'));
80         $m->connect('notice/respond', array('action' => 'newresponse'));
81         return true;
82     }
83
84     function onEndProfileFormData($action)
85     {
86         $fields = GNUsocialProfileExtensionField::allFields();
87         $user = common_current_user();
88         $profile = $user->getProfile();
89         gnusocial_profile_merge($profile);
90         foreach ($fields as $field) {
91             $action->elementStart('li');
92             $fieldname = $field->systemname;
93             if ($field->type == 'str') {
94                 $action->input($fieldname, $field->title, 
95                                ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname, 
96                                $field->description);
97             }
98             else if ($field->type == 'text') {
99                 $action->textarea($fieldname, $field->title,
100                                   ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname,
101                                   $field->description);
102             }
103             $action->elementEnd('li');
104         }
105     }
106
107     function onEndProfileSaveForm($action)
108     {
109         $fields = GNUsocialProfileExtensionField::allFields();
110         $user = common_current_user();
111         $profile = $user->getProfile();
112         foreach ($fields as $field) {
113             $val = $action->trimmed($field->systemname);
114
115             $response = new GNUsocialProfileExtensionResponse();
116             $response->profile_id = $profile->id;
117             $response->extension_id = $field->id;
118             
119             if ($response->find()) {
120                 $response->fetch();
121                 $response->value = $val;
122                 if ($response->validate()) {
123                     if (empty($val))
124                         $response->delete();
125                     else
126                         $response->update();
127                 }
128             }
129             else {
130                 $response->value = $val;
131                 $response->insert();
132             }
133         }
134     }
135     
136     function onEndShowStyles($action)
137     {
138         $action->cssLink('/plugins/GNUsocialProfileExtensions/res/style.css');
139     }
140
141     function onEndShowScripts($action)
142     {
143         $action->script('plugins/GNUsocialProfileExtensions/js/profile.js');
144     }
145
146     function onEndAdminPanelNav($nav)
147     {
148         if (AdminPanelAction::canAdmin('profilefields')) {
149
150             $action_name = $nav->action->trimmed('action');
151
152             $nav->out->menuItem(
153                 '/admin/profilefields',
154                 _m('Profile Fields'),
155                 _m('Custom profile fields'),
156                 $action_name == 'profilefieldsadminpanel',
157                 'nav_profilefields_admin_panel'
158             );
159         }
160
161         return true;
162     }
163
164     function onStartPersonalGroupNav($nav)
165     { 
166         $nav->out->menuItem(common_local_url('bio',
167                            array('nickname' => $nav->action->trimmed('nickname'))), _('Bio'), 
168                            _('The user\'s extended profile'), $nav->action->trimmed('action') == 'bio', 'nav_bio');
169     }
170
171     //Why the heck is this shoved into this plugin!?!?  It deserves its own!
172     function onShowStreamNoticeList($notice, $action, &$pnl)
173     {
174         //TODO: This function is called after the notices in $notice are superfluously retrieved in showstream.php
175         $newnotice = new Notice();
176         $newnotice->profile_id = $action->user->id;
177         $newnotice->orderBy('modified DESC');
178         $newnotice->whereAdd('reply_to IS NULL');
179         $newnotice->limit(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
180         $newnotice->find();
181
182         $pnl = new NoticeTree($newnotice, $action);
183         return false;
184     }
185
186 }
187