]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ExtendedProfile/profiledetailsettingsaction.php
ee450118c3d7f6eba3dbfd50181da19b6ed14649
[quix0rs-gnu-social.git] / plugins / ExtendedProfile / profiledetailsettingsaction.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 class ProfileDetailSettingsAction extends ProfileSettingsAction
25 {
26
27     function title()
28     {
29         return _m('Extended profile settings');
30     }
31
32     /**
33      * Instructions for use
34      *
35      * @return instructions for use
36      */
37     function getInstructions()
38     {
39         // TRANS: Usage instructions for profile settings.
40         return _('You can update your personal profile info here '.
41                  'so people know more about you.');
42     }
43
44     function showStylesheets() {
45         parent::showStylesheets();
46         $this->cssLink('plugins/ExtendedProfile/css/profiledetail.css');
47         return true;
48     }
49
50     function  showScripts() {
51         parent::showScripts();
52         $this->script('plugins/ExtendedProfile/js/profiledetail.js');
53         return true;
54     }
55
56     function handlePost()
57     {
58         // CSRF protection
59         $token = $this->trimmed('token');
60         if (!$token || $token != common_session_token()) {
61             $this->show_form(
62                 _m(
63                     'There was a problem with your session token. '
64                     .   'Try again, please.'
65                   )
66             );
67             return;
68         }
69
70         if ($this->arg('save')) {
71             $this->saveDetails();
72         } else {
73             // TRANS: Message given submitting a form with an unknown action
74             $this->showForm(_m('Unexpected form submission.'));
75         }
76     }
77
78     function showContent()
79     {
80         $cur = common_current_user();
81         $profile = $cur->getProfile();
82
83         $widget = new ExtendedProfileWidget(
84             $this,
85             $profile,
86             ExtendedProfileWidget::EDITABLE
87         );
88         $widget->show();
89     }
90
91     function saveDetails()
92     {
93         common_debug(var_export($_POST, true));
94
95         $user = common_current_user();
96
97         try {
98            $this->saveStandardProfileDetails($user);
99         } catch (Exception $e) {
100             $this->showForm($e->getMessage(), false);
101             return;
102         }
103
104         $profile = $user->getProfile();
105
106         $simpleFieldNames = array('title', 'spouse', 'kids');
107
108         foreach ($simpleFieldNames as $name) {
109             $value = $this->trimmed('extprofile-' . $name);
110             $this->saveField($user, $name, $value);
111         }
112
113         $this->savePhoneNumbers($user);
114
115         $this->showForm(_('Details saved.'), true);
116
117     }
118
119     function savePhoneNumbers($user) {
120         $phones = $this->findPhoneNumbers();
121
122         foreach ($phones as $phone) {
123             $this->saveField(
124                 $user,
125                 'phone',
126                 $phone['value'],
127                 $phone['rel'],
128                 $phone['index']
129             );
130         }
131     }
132
133     function findPhoneNumbers() {
134         $phones      = array();
135         $phoneParams = $this->findMultiParams('phone');
136         ksort($phoneParams); // this sorts them into pairs
137         $phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2);
138
139         $phoneTuples = array();
140
141         foreach($phones as $phone) {
142             $firstkey           = current(array_keys($phone));
143             $index              = substr($firstkey, strrpos($firstkey, '-') + 1);
144             list($number, $rel) = array_values($phone);
145
146             $phoneTuples[] = array(
147                 'value' => $number,
148                 'index' => $index,
149                 'rel'   => $rel
150             );
151         }
152
153         return $phoneTuples;
154     }
155
156     function arraySplit($array, $pieces)
157     {
158         if ($pieces < 2) {
159             return array($array);
160         }
161
162         $newCount = ceil(count($array) / $pieces);
163         $a = array_slice($array, 0, $newCount);
164         $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
165
166         return array_merge(array($a), $b);
167     }
168
169     function findMultiParams($type) {
170         $formVals = array();
171         $target   = $type;
172         foreach ($_POST as $key => $val) {
173             if (strrpos('extprofile-' . $key, $target) !== false) {
174                 $formVals[$key] = $val;
175             }
176         }
177         return $formVals;
178     }
179
180     /**
181      * Save an extended profile field as a Profile_detail
182      *
183      * @param User   $user    the current user
184      * @param string $name    field name
185      * @param string $value   field value
186      * @param string $rel     field rel (type)
187      * @param int    $index   index (fields can have multiple values)
188      */
189     function saveField($user, $name, $value, $rel = null, $index = null)
190     {
191         $profile = $user->getProfile();
192         $detail  = new Profile_detail();
193
194         $detail->profile_id  = $profile->id;
195         $detail->field_name  = $name;
196         $detail->value_index = $index;
197
198         $result = $detail->find(true);
199
200         if (empty($result)) {
201             $detial->value_index = $index;
202             $detail->rel         = $rel;
203             $detail->field_value = $value;
204             $detail->created     = common_sql_now();
205             $result = $detail->insert();
206             if (empty($result)) {
207                 common_log_db_error($detail, 'INSERT', __FILE__);
208                 $this->serverError(_m('Could not save profile details.'));
209             }
210         } else {
211             $orig = clone($detail);
212
213             $detail->field_value = $value;
214             $detail->rel         = $rel;
215
216             $result = $detail->update($orig);
217             if (empty($result)) {
218                 common_log_db_error($detail, 'UPDATE', __FILE__);
219                 $this->serverError(_m('Could not save profile details.'));
220             }
221         }
222
223         $detail->free();
224     }
225
226     /**
227      * Save fields that should be stored in the main profile object
228      *
229      * XXX: There's a lot of dupe code here from ProfileSettingsAction.
230      *      Do not want.
231      *
232      * @param User $user the current user
233      */
234     function saveStandardProfileDetails($user)
235     {
236         $fullname  = $this->trimmed('extprofile-fullname');
237         $location  = $this->trimmed('extprofile-location');
238         $tagstring = $this->trimmed('extprofile-tags');
239         $bio       = $this->trimmed('extprofile-bio');
240
241         if ($tagstring) {
242             $tags = array_map(
243                 'common_canonical_tag',
244                 preg_split('/[\s,]+/', $tagstring)
245             );
246         } else {
247             $tags = array();
248         }
249
250         foreach ($tags as $tag) {
251             if (!common_valid_profile_tag($tag)) {
252                 // TRANS: Validation error in form for profile settings.
253                 // TRANS: %s is an invalid tag.
254                 throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
255             }
256         }
257
258         $profile = $user->getProfile();
259
260         $oldTags = $user->getSelfTags();
261         $newTags = array_diff($tags, $oldTags);
262
263         if ($fullname    != $profile->fullname
264             || $location != $profile->location
265             || !empty($newTags)
266             || $bio      != $profile->bio) {
267
268             $orig = clone($profile);
269
270             $profile->nickname = $user->nickname;
271             $profile->fullname = $fullname;
272             $profile->bio      = $bio;
273             $profile->location = $location;
274
275             $loc = Location::fromName($location);
276
277             if (empty($loc)) {
278                 $profile->lat         = null;
279                 $profile->lon         = null;
280                 $profile->location_id = null;
281                 $profile->location_ns = null;
282             } else {
283                 $profile->lat         = $loc->lat;
284                 $profile->lon         = $loc->lon;
285                 $profile->location_id = $loc->location_id;
286                 $profile->location_ns = $loc->location_ns;
287             }
288
289             $profile->profileurl = common_profile_url($user->nickname);
290
291             $result = $profile->update($orig);
292
293             if ($result === false) {
294                 common_log_db_error($profile, 'UPDATE', __FILE__);
295                 // TRANS: Server error thrown when user profile settings could not be saved.
296                 $this->serverError(_('Could not save profile.'));
297                 return;
298             }
299
300             // Set the user tags
301             $result = $user->setSelfTags($tags);
302
303             if (!$result) {
304                 // TRANS: Server error thrown when user profile settings tags could not be saved.
305                 $this->serverError(_('Could not save tags.'));
306                 return;
307             }
308
309             Event::handle('EndProfileSaveForm', array($this));
310             common_broadcast_profile($profile);
311         }
312     }
313
314 }