]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialProfileExtensions/GNUsocialProfileExtensionsPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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 include_once $dir . '/lib/profiletools.php';
34
35 class GNUsocialProfileExtensionsPlugin extends Plugin
36 {
37
38     function onCheckSchema()
39     {
40         $schema = Schema::get();
41         $schema->ensureTable('GNUsocialProfileExtensionField', GNUsocialProfileExtensionField::schemaDef());
42         $schema->ensureTable('GNUsocialProfileExtensionResponse', GNUsocialProfileExtensionResponse::schemaDef());
43     }
44
45     function onRouterInitialized(URLMapper $m)
46     {
47         $m->connect(':nickname/bio', array('action' => 'bio'));
48         $m->connect('admin/profilefields', array('action' => 'profilefieldsAdminPanel'));
49         $m->connect('notice/respond', array('action' => 'newresponse'));
50         return true;
51     }
52
53     function onEndProfileFormData($action)
54     {
55         $fields = GNUsocialProfileExtensionField::allFields();
56         $user = common_current_user();
57         $profile = $user->getProfile();
58         gnusocial_profile_merge($profile);
59         foreach ($fields as $field) {
60             $action->elementStart('li');
61             $fieldname = $field->systemname;
62             if ($field->type == 'str') {
63                 $action->input($fieldname, $field->title, 
64                                ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname, 
65                                $field->description);
66             }
67             else if ($field->type == 'text') {
68                 $action->textarea($fieldname, $field->title,
69                                   ($action->arg($fieldname)) ? $action->arg($fieldname) : $profile->$fieldname,
70                                   $field->description);
71             }
72             $action->elementEnd('li');
73         }
74     }
75
76     function onEndProfileSaveForm($action)
77     {
78         $fields = GNUsocialProfileExtensionField::allFields();
79         $user = common_current_user();
80         $profile = $user->getProfile();
81         foreach ($fields as $field) {
82             $val = $action->trimmed($field->systemname);
83
84             $response = new GNUsocialProfileExtensionResponse();
85             $response->profile_id = $profile->id;
86             $response->extension_id = $field->id;
87             
88             if ($response->find()) {
89                 $response->fetch();
90                 $response->value = $val;
91                 if ($response->validate()) {
92                     if (empty($val))
93                         $response->delete();
94                     else
95                         $response->update();
96                 }
97             }
98             else {
99                 $response->value = $val;
100                 $response->insert();
101             }
102         }
103     }
104     
105     function onEndShowStyles(Action $action)
106     {
107         $action->cssLink('/plugins/GNUsocialProfileExtensions/res/style.css');
108     }
109
110     function onEndShowScripts(Action $action)
111     {
112         $action->script('plugins/GNUsocialProfileExtensions/js/profile.js');
113     }
114
115     function onEndAdminPanelNav(Menu $nav)
116     {
117         if (AdminPanelAction::canAdmin('profilefields')) {
118
119             $action_name = $nav->action->trimmed('action');
120
121             $nav->out->menuItem(
122                 '/admin/profilefields',
123                 _m('Profile Fields'),
124                 _m('Custom profile fields'),
125                 $action_name == 'profilefieldsadminpanel',
126                 'nav_profilefields_admin_panel'
127             );
128         }
129
130         return true;
131     }
132
133     function onStartPersonalGroupNav(Menu $nav, Profile $target, Profile $scoped=null)
134     { 
135         $nav->out->menuItem(common_local_url('bio',
136                            array('nickname' => $nav->action->trimmed('nickname'))), _('Bio'), 
137                            _('The user\'s extended profile'), $nav->action->trimmed('action') == 'bio', 'nav_bio');
138     }
139 }