]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialProfileExtensions/GNUsocialProfileExtensionsPlugin.php
New plugin: GNUsocialProfileExtensions! Profiles can be extended with administrator...
[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 2010 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             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
44             break;
45         case 'ProfilefieldsAdminPanelAction':
46             include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -16)) . '.php';
47             break;
48         default:
49             break;
50         }
51         include_once $dir . '/classes/GNUsocialProfileExtensionField.php';
52         include_once $dir . '/classes/GNUsocialProfileExtensionResponse.php';
53         include_once $dir . '/lib/profiletools.php';
54         return true;
55     }
56
57     function onCheckSchema()
58     {
59         $schema = Schema::get();
60         $schema->ensureTable('GNUsocialProfileExtensionField',
61                                 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
62                                       new ColumnDef('systemname', 'varchar(64)', null, false),
63                                       new ColumnDef('title', 'varchar(256)', null, false),
64                                       new ColumnDef('description', 'text', null, false),
65                                       new ColumnDef('type', 'varchar(256)', null, false)));
66         $schema->ensureTable('GNUsocialProfileExtensionResponse',
67                                 array(new ColumnDef('id', 'int(11)', null, false, 'PRI', null, null, true),
68                                       new ColumnDef('extension_id', 'int(11)', null, false),
69                                       new ColumnDef('profile_id', 'int(11)', null, false),
70                                       new ColumnDef('value', 'text', null, false)));
71                                           
72     }
73
74     function onRouterInitialized($m)
75     {
76         $m->connect(':nickname/bio', array('action' => 'bio'));
77         $m->connect('admin/profilefields', array('action' => 'profilefieldsAdminPanel'));
78         return true;
79     }
80
81     function onEndProfileFormData($action)
82     {
83         $fields = GNUsocialProfileExtensionField::allFields();
84         $user = common_current_user();
85         $profile = $user->getProfile();
86         gnusocial_profile_merge($profile);
87         foreach ($fields as $field) {
88             $action->elementStart('li');
89             $fieldname = $field->systemname;
90             if ($field->type == 'str') {
91                 $action->input($fieldname, $field->title, 
92                                ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname, 
93                                $field->description);
94             }
95             else if ($field->type == 'text') {
96                 $action->textarea($fieldname, $field->title,
97                                   ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname,
98                                   $field->description);
99             }
100             $action->elementEnd('li');
101         }
102     }
103
104     function onEndProfileSaveForm($action)
105     {
106         $fields = GNUsocialProfileExtensionField::allFields();
107         $user = common_current_user();
108         $profile = $user->getProfile();
109         foreach ($fields as $field) {
110             $val = $action->trimmed($field->systemname);
111
112             $response = new GNUsocialProfileExtensionResponse();
113             $response->profile_id = $profile->id;
114             $response->extension_id = $field->id;
115             
116             if ($response->find()) {
117                 $response->fetch();
118                 $response->value = $val;
119                 if ($response->validate()) {
120                     if (empty($val))
121                         $response->delete();
122                     else
123                         $response->update();
124                 }
125             }
126             else {
127                 $response->value = $val;
128                 $response->insert();
129             }
130         }
131     }
132     
133     function onEndShowStyles($action)
134     {
135         $action->cssLink('/plugins/GNUsocialProfileExtensions/res/style.css');
136     }
137
138     function onEndAdminPanelNav($nav)
139     {
140         if (AdminPanelAction::canAdmin('profilefields')) {
141
142             $action_name = $nav->action->trimmed('action');
143
144             $nav->out->menuItem(
145                 '/admin/profilefields',
146                 _m('Profile Fields'),
147                 _m('Custom profile fields'),
148                 $action_name == 'profilefieldsadminpanel',
149                 'nav_profilefields_admin_panel'
150             );
151         }
152
153         return true;
154     }
155
156     function onStartPersonalGroupNav($nav)
157     { 
158         $nav->out->menuItem(common_local_url('bio',
159                            array('nickname' => $nav->action->trimmed('nickname'))), _('Bio'), 
160                            _('The user\'s extended profile'), $nav->action->trimmed('action') == 'bio', 'nav_bio');
161     }
162
163 }
164