ExtendedProfile now works better as extended ProfilesettingsAction

This commit is contained in:
Mikael Nordfeldth 2015-07-17 12:46:09 +02:00
parent 9f82da07f1
commit 9045575e62
2 changed files with 85 additions and 149 deletions

View File

@ -17,9 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
class ProfileDetailSettingsAction extends ProfileSettingsAction class ProfileDetailSettingsAction extends ProfileSettingsAction
{ {
@ -29,18 +27,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return _m('Extended profile settings'); return _m('Extended profile settings');
} }
/**
* Instructions for use
*
* @return instructions for use
*/
function getInstructions()
{
// TRANS: Usage instructions for profile settings.
return _m('You can update your personal profile info here '.
'so people know more about you.');
}
function showStylesheets() { function showStylesheets() {
parent::showStylesheets(); parent::showStylesheets();
$this->cssLink('plugins/ExtendedProfile/css/profiledetail.css'); $this->cssLink('plugins/ExtendedProfile/css/profiledetail.css');
@ -53,36 +39,21 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return true; return true;
} }
function handlePost() protected function doPost()
{ {
// CSRF protection if ($this->arg('save')) {
$token = $this->trimmed('token'); return $this->saveDetails();
if (!$token || $token != common_session_token()) {
$this->showForm(
// TRANS: Client error displayed when the session token does not match or is not given.
_m('There was a problem with your session token. '
. 'Try again, please.'
)
);
return;
} }
if ($this->arg('save')) { // TRANS: Message given submitting a form with an unknown action.
$this->saveDetails(); throw new ClientException(_m('Unexpected form submission.'));
} else {
// TRANS: Message given submitting a form with an unknown action.
$this->showForm(_m('Unexpected form submission.'));
}
} }
function showContent() function showContent()
{ {
$cur = common_current_user();
$profile = $cur->getProfile();
$widget = new ExtendedProfileWidget( $widget = new ExtendedProfileWidget(
$this, $this,
$profile, $this->scoped,
ExtendedProfileWidget::EDITABLE ExtendedProfileWidget::EDITABLE
); );
$widget->show(); $widget->show();
@ -92,49 +63,38 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
{ {
common_debug(var_export($_POST, true)); common_debug(var_export($_POST, true));
$user = common_current_user(); $this->saveStandardProfileDetails();
try { $simpleFieldNames = array('title', 'spouse', 'kids', 'manager');
$this->saveStandardProfileDetails($user); $dateFieldNames = array('birthday');
$profile = $user->getProfile(); foreach ($simpleFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name);
$simpleFieldNames = array('title', 'spouse', 'kids', 'manager'); if (!empty($value)) {
$dateFieldNames = array('birthday'); $this->saveField($name, $value);
foreach ($simpleFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name);
if (!empty($value)) {
$this->saveField($user, $name, $value);
}
} }
foreach ($dateFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name);
$dateVal = $this->parseDate($name, $value);
$this->saveField(
$user,
$name,
null,
null,
null,
$dateVal
);
}
$this->savePhoneNumbers($user);
$this->saveIms($user);
$this->saveWebsites($user);
$this->saveExperiences($user);
$this->saveEducations($user);
} catch (Exception $e) {
$this->showForm($e->getMessage(), false);
return;
} }
foreach ($dateFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name);
$dateVal = $this->parseDate($name, $value);
$this->saveField(
$name,
null,
null,
null,
$dateVal
);
}
$this->savePhoneNumbers();
$this->saveIms();
$this->saveWebsites();
$this->saveExperiences();
$this->saveEducations();
// TRANS: Success message after saving extended profile details. // TRANS: Success message after saving extended profile details.
$this->showForm(_m('Details saved.'), true); return _m('Details saved.');
} }
@ -168,15 +128,14 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return null; return null;
} }
function savePhoneNumbers($user) { function savePhoneNumbers() {
$phones = $this->findPhoneNumbers(); $phones = $this->findPhoneNumbers();
$this->removeAll($user, 'phone'); $this->removeAll('phone');
$i = 0; $i = 0;
foreach($phones as $phone) { foreach($phones as $phone) {
if (!empty($phone['value'])) { if (!empty($phone['value'])) {
++$i; ++$i;
$this->saveField( $this->saveField(
$user,
'phone', 'phone',
$phone['value'], $phone['value'],
$phone['rel'], $phone['rel'],
@ -226,15 +185,14 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return $imArray; return $imArray;
} }
function saveIms($user) { function saveIms() {
$ims = $this->findIms(); $ims = $this->findIms();
$this->removeAll($user, 'im'); $this->removeAll('im');
$i = 0; $i = 0;
foreach($ims as $im) { foreach($ims as $im) {
if (!empty($im['value'])) { if (!empty($im['value'])) {
++$i; ++$i;
$this->saveField( $this->saveField(
$user,
'im', 'im',
$im['value'], $im['value'],
$im['rel'], $im['rel'],
@ -262,9 +220,9 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return $wsArray; return $wsArray;
} }
function saveWebsites($user) { function saveWebsites() {
$sites = $this->findWebsites(); $sites = $this->findWebsites();
$this->removeAll($user, 'website'); $this->removeAll('website');
$i = 0; $i = 0;
foreach($sites as $site) { foreach($sites as $site) {
if (!empty($site['value']) && !common_valid_http_url($site['value'])) { if (!empty($site['value']) && !common_valid_http_url($site['value'])) {
@ -276,7 +234,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
if (!empty($site['value'])) { if (!empty($site['value'])) {
++$i; ++$i;
$this->saveField( $this->saveField(
$user,
'website', 'website',
$site['value'], $site['value'],
$site['rel'], $site['rel'],
@ -317,20 +274,19 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return $expArray; return $expArray;
} }
function saveExperiences($user) { function saveExperiences() {
common_debug('save experiences'); common_debug('save experiences');
$experiences = $this->findExperiences(); $experiences = $this->findExperiences();
$this->removeAll($user, 'company'); $this->removeAll('company');
$this->removeAll($user, 'start'); $this->removeAll('start');
$this->removeAll($user, 'end'); // also stores 'current' $this->removeAll('end'); // also stores 'current'
$i = 0; $i = 0;
foreach($experiences as $experience) { foreach($experiences as $experience) {
if (!empty($experience['company'])) { if (!empty($experience['company'])) {
++$i; ++$i;
$this->saveField( $this->saveField(
$user,
'company', 'company',
$experience['company'], $experience['company'],
null, null,
@ -338,7 +294,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
); );
$this->saveField( $this->saveField(
$user,
'start', 'start',
null, null,
null, null,
@ -349,7 +304,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
// Save "current" employer indicator in rel // Save "current" employer indicator in rel
if ($experience['current']) { if ($experience['current']) {
$this->saveField( $this->saveField(
$user,
'end', 'end',
null, null,
'current', // rel 'current', // rel
@ -357,7 +311,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
); );
} else { } else {
$this->saveField( $this->saveField(
$user,
'end', 'end',
null, null,
null, null,
@ -399,44 +352,40 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
} }
function saveEducations($user) { function saveEducations() {
common_debug('save education'); common_debug('save education');
$edus = $this->findEducations(); $edus = $this->findEducations();
common_debug(var_export($edus, true)); common_debug(var_export($edus, true));
$this->removeAll($user, 'school'); $this->removeAll('school');
$this->removeAll($user, 'degree'); $this->removeAll('degree');
$this->removeAll($user, 'degree_descr'); $this->removeAll('degree_descr');
$this->removeAll($user, 'school_start'); $this->removeAll('school_start');
$this->removeAll($user, 'school_end'); $this->removeAll('school_end');
$i = 0; $i = 0;
foreach($edus as $edu) { foreach($edus as $edu) {
if (!empty($edu['school'])) { if (!empty($edu['school'])) {
++$i; ++$i;
$this->saveField( $this->saveField(
$user,
'school', 'school',
$edu['school'], $edu['school'],
null, null,
$i $i
); );
$this->saveField( $this->saveField(
$user,
'degree', 'degree',
$edu['degree'], $edu['degree'],
null, null,
$i $i
); );
$this->saveField( $this->saveField(
$user,
'degree_descr', 'degree_descr',
$edu['description'], $edu['description'],
null, null,
$i $i
); );
$this->saveField( $this->saveField(
$user,
'school_start', 'school_start',
null, null,
null, null,
@ -445,7 +394,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
); );
$this->saveField( $this->saveField(
$user,
'school_end', 'school_end',
null, null,
null, null,
@ -491,35 +439,33 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
/** /**
* Save an extended profile field as a Profile_detail * Save an extended profile field as a Profile_detail
* *
* @param User $user the current user
* @param string $name field name * @param string $name field name
* @param string $value field value * @param string $value field value
* @param string $rel field rel (type) * @param string $rel field rel (type)
* @param int $index index (fields can have multiple values) * @param int $index index (fields can have multiple values)
* @param date $date related date * @param date $date related date
*/ */
function saveField($user, $name, $value, $rel = null, $index = null, $date = null) function saveField($name, $value, $rel = null, $index = null, $date = null)
{ {
$profile = $user->getProfile();
$detail = new Profile_detail(); $detail = new Profile_detail();
$detail->profile_id = $profile->id; $detail->profile_id = $this->scoped->getID();
$detail->field_name = $name; $detail->field_name = $name;
$detail->value_index = $index; $detail->value_index = $index;
$result = $detail->find(true); $result = $detail->find(true);
if (empty($result)) { if (!$result instanceof Profile_detail) {
$detial->value_index = $index; $detail->value_index = $index;
$detail->rel = $rel; $detail->rel = $rel;
$detail->field_value = $value; $detail->field_value = $value;
$detail->date = $date; $detail->date = $date;
$detail->created = common_sql_now(); $detail->created = common_sql_now();
$result = $detail->insert(); $result = $detail->insert();
if (empty($result)) { if ($result === false) {
common_log_db_error($detail, 'INSERT', __FILE__); common_log_db_error($detail, 'INSERT', __FILE__);
// TRANS: Server error displayed when a field could not be saved in the database. // TRANS: Server error displayed when a field could not be saved in the database.
$this->serverError(_m('Could not save profile details.')); throw new ServerException(_m('Could not save profile details.'));
} }
} else { } else {
$orig = clone($detail); $orig = clone($detail);
@ -529,21 +475,20 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$detail->date = $date; $detail->date = $date;
$result = $detail->update($orig); $result = $detail->update($orig);
if (empty($result)) { if ($result === false) {
common_log_db_error($detail, 'UPDATE', __FILE__); common_log_db_error($detail, 'UPDATE', __FILE__);
// TRANS: Server error displayed when a field could not be saved in the database. // TRANS: Server error displayed when a field could not be saved in the database.
$this->serverError(_m('Could not save profile details.')); throw new ServerException(_m('Could not save profile details.'));
} }
} }
$detail->free(); $detail->free();
} }
function removeAll($user, $name) function removeAll($name)
{ {
$profile = $user->getProfile();
$detail = new Profile_detail(); $detail = new Profile_detail();
$detail->profile_id = $profile->id; $detail->profile_id = $this->scoped->getID();
$detail->field_name = $name; $detail->field_name = $name;
$detail->delete(); $detail->delete();
$detail->free(); $detail->free();
@ -554,10 +499,8 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
* *
* XXX: There's a lot of dupe code here from ProfileSettingsAction. * XXX: There's a lot of dupe code here from ProfileSettingsAction.
* Do not want. * Do not want.
*
* @param User $user the current user
*/ */
function saveStandardProfileDetails($user) function saveStandardProfileDetails()
{ {
$fullname = $this->trimmed('extprofile-fullname'); $fullname = $this->trimmed('extprofile-fullname');
$location = $this->trimmed('extprofile-location'); $location = $this->trimmed('extprofile-location');
@ -581,54 +524,47 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
} }
} }
$profile = $user->getProfile(); $oldTags = Profile_tag::getSelfTagsArray($this->scoped);
$oldTags = $user->getSelfTags();
$newTags = array_diff($tags, $oldTags); $newTags = array_diff($tags, $oldTags);
if ($fullname != $profile->fullname if ($fullname != $this->scoped->getFullname()
|| $location != $profile->location || $location != $this->scoped->location
|| !empty($newTags) || !empty($newTags)
|| $bio != $profile->bio) { || $bio != $this->scoped->getDescription()) {
$orig = clone($profile); $orig = clone($this->scoped);
$profile->nickname = $user->nickname; // Skipping nickname change here until we add logic for when the site allows it or not
$profile->fullname = $fullname; // old Profilesettings will still let us do that.
$profile->bio = $bio;
$profile->location = $location; $this->scoped->fullname = $fullname;
$this->scoped->bio = $bio;
$this->scoped->location = $location;
$loc = Location::fromName($location); $loc = Location::fromName($location);
if (empty($loc)) { if (empty($loc)) {
$profile->lat = null; $this->scoped->lat = null;
$profile->lon = null; $this->scoped->lon = null;
$profile->location_id = null; $this->scoped->location_id = null;
$profile->location_ns = null; $this->scoped->location_ns = null;
} else { } else {
$profile->lat = $loc->lat; $this->scoped->lat = $loc->lat;
$profile->lon = $loc->lon; $this->scoped->lon = $loc->lon;
$profile->location_id = $loc->location_id; $this->scoped->location_id = $loc->location_id;
$profile->location_ns = $loc->location_ns; $this->scoped->location_ns = $loc->location_ns;
} }
$profile->profileurl = common_profile_url($user->nickname); $result = $this->scoped->update($orig);
$result = $profile->update($orig);
if ($result === false) { if ($result === false) {
common_log_db_error($profile, 'UPDATE', __FILE__); common_log_db_error($this->scoped, 'UPDATE', __FILE__);
// TRANS: Server error thrown when user profile settings could not be saved. // TRANS: Server error thrown when user profile settings could not be saved.
$this->serverError(_m('Could not save profile.')); throw new ServerException(_m('Could not save profile.'));
} }
// Set the user tags // Set the user tags
$result = $user->setSelfTags($tags); $result = Profile_tag::setSelfTags($this->scoped, $tags);
if (!$result) {
// TRANS: Server error thrown when user profile settings tags could not be saved.
$this->serverError(_m('Could not save tags.'));
}
Event::handle('EndProfileSaveForm', array($this)); Event::handle('EndProfileSaveForm', array($this));
} }

View File

@ -52,7 +52,7 @@ class ExtendedProfile
function loadFields() function loadFields()
{ {
$detail = new Profile_detail(); $detail = new Profile_detail();
$detail->profile_id = $this->profile->id; $detail->profile_id = $this->profile->getID();
$detail->find(); $detail->find();
$fields = array(); $fields = array();
@ -71,7 +71,7 @@ class ExtendedProfile
*/ */
function getTags() function getTags()
{ {
return implode(' ', $this->user->getSelfTags()); return implode(' ', Profile_tag::getSelfTagsArray($this->profile));
} }
/** /**