Save basic profile data to the right place

This commit is contained in:
Zach Copley 2011-03-08 19:20:43 -08:00
parent 3bbe481695
commit 794cb5609b
4 changed files with 338 additions and 19 deletions

View File

@ -21,15 +21,29 @@ if (!defined('STATUSNET')) {
exit(1);
}
/**
* Class to represent extended profile data
*/
class ExtendedProfile
{
/**
* Constructor
*
* @param Profile $profile
*/
function __construct(Profile $profile)
{
$this->profile = $profile;
$this->profile = $profile;
$this->user = $profile->getUser();
$this->sections = $this->getSections();
$this->fields = $this->loadFields();
$this->fields = $this->loadFields();
}
/**
* Load extended profile fields
*
* @return array $fields the list of fields
*/
function loadFields()
{
$detail = new Profile_detail();
@ -41,9 +55,48 @@ class ExtendedProfile
while ($detail->fetch()) {
$fields[$detail->field][] = clone($detail);
}
return $fields;
}
/**
* Get a the self-tags associated with this profile
*
* @return string the concatenated string of tags
*/
function getTags()
{
return implode(' ', $this->user->getSelfTags());
}
/**
* Return a simple string value. Checks for fields that should
* be stored in the regular profile and returns values from it
* if appropriate.
*
* @param string $name name of the detail field to get the
* value from
*
* @return string the value
*/
function getTextValue($name)
{
$profileFields = array('fullname', 'location', 'bio');
if (in_array(strtolower($name), $profileFields)) {
return $this->profile->$name;
} else if (in_array($name, $this->fields)) {
return $this->fields[$name]->value;
} else {
return null;
}
}
/**
* Return all the sections of the extended profile
*
* @return array the big list of sections and fields
*/
function getSections()
{
return array(

View File

@ -21,13 +21,35 @@ if (!defined('STATUSNET')) {
exit(1);
}
class ExtendedProfileWidget extends Widget
/**
* Class for outputting a widget to display or edit
* extended profiles
*/
class ExtendedProfileWidget extends Form
{
const EDITABLE=true;
const EDITABLE = true;
/**
* The parent profile
*
* @var Profile
*/
protected $profile;
/**
* The extended profile
*
* @var Extended_profile
*/
protected $ext;
/**
* Constructor
*
* @param XMLOutputter $out
* @param Profile $profile
* @param boolean $editable
*/
public function __construct(XMLOutputter $out=null, Profile $profile=null, $editable=false)
{
parent::__construct($out);
@ -38,7 +60,30 @@ class ExtendedProfileWidget extends Widget
$this->editable = $editable;
}
/**
* Show the extended profile, or the edit form
*/
public function show()
{
if ($this->editable) {
parent::show();
} else {
$this->showSections();
}
}
/**
* Show form data
*/
public function formData()
{
$this->showSections();
}
/**
* Show each section of the extended profile
*/
public function showSections()
{
$sections = $this->ext->getSections();
foreach ($sections as $name => $section) {
@ -46,6 +91,12 @@ class ExtendedProfileWidget extends Widget
}
}
/**
* Show an extended profile section
*
* @param string $name name of the section
* @param array $section array of fields for the section
*/
protected function showExtendedProfileSection($name, $section)
{
$this->out->element('h3', null, $section['label']);
@ -56,6 +107,12 @@ class ExtendedProfileWidget extends Widget
$this->out->elementEnd('table');
}
/**
* Show an extended profile field
*
* @param string $name name of the field
* @param array $field set of key/value pairs for the field
*/
protected function showExtendedProfileField($name, $field)
{
$this->out->elementStart('tr');
@ -73,30 +130,110 @@ class ExtendedProfileWidget extends Widget
$this->out->elementEnd('tr');
}
/**
* Outputs the value of a field
*
* @param string $name name of the field
* @param array $field set of key/value pairs for the field
*/
protected function showFieldValue($name, $field)
{
$this->out->text($name);
$type = strval(@$field['type']);
switch($type)
{
case '':
case 'text':
case 'textarea':
$this->out->text($this->ext->getTextValue($name));
break;
case 'tags':
$this->out->text($this->ext->getTags());
break;
default:
$this->out->text("TYPE: $type");
}
}
/**
* Show an editable version of the field
*
* @param string $name name fo the field
* @param array $field array of key/value pairs for the field
*/
protected function showEditableField($name, $field)
{
$out = $this->out;
//$out = new HTMLOutputter();
// @fixme
$type = strval(@$field['type']);
$id = "extprofile-" . $name;
$value = 'placeholder';
switch ($type) {
case '':
case 'text':
$out->input($id, null, $value);
break;
case 'textarea':
$out->textarea($id, null, $value);
break;
default:
$out->input($id, null, "TYPE: $type");
case '':
case 'text':
$out->input($id, null, $this->ext->getTextValue($name));
break;
case 'textarea':
$out->textarea($id, null, $this->ext->getTextValue($name));
break;
case 'tags':
$out->input($id, null, $this->ext->getTags());
break;
default:
$out->input($id, null, "TYPE: $type");
}
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
$this->out->submit(
'save',
_m('BUTTON','Save'),
'submit form_action-secondary',
'save',
_('Save details')
);
}
/**
* ID of the form
*
* @return string ID of the form
*/
function id()
{
return 'profile-details-' . $this->profile->id;
}
/**
* class of the form
*
* @return string of the form class
*/
function formClass()
{
return 'form_profile_details';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('profiledetailsettings');
}
}

View File

@ -23,6 +23,7 @@ if (!defined('STATUSNET')) {
class ProfileDetailAction extends ProfileAction
{
function isReadOnly($args)
{
return true;

View File

@ -21,7 +21,7 @@ if (!defined('STATUSNET')) {
exit(1);
}
class ProfileDetailSettingsAction extends AccountSettingsAction
class ProfileDetailSettingsAction extends SettingsAction
{
function title()
@ -47,9 +47,26 @@ class ProfileDetailSettingsAction extends AccountSettingsAction
return true;
}
function handle($args)
function handlePost()
{
$this->showPage();
// CSRF protection
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) {
$this->show_form(
_m(
'There was a problem with your session token. '
. 'Try again, please.'
)
);
return;
}
if ($this->arg('save')) {
$this->saveDetails();
} else {
// TRANS: Message given submitting a form with an unknown action
$this->showForm(_m('Unexpected form submission.'));
}
}
function showContent()
@ -60,4 +77,115 @@ class ProfileDetailSettingsAction extends AccountSettingsAction
$widget = new ExtendedProfileWidget($this, $profile, ExtendedProfileWidget::EDITABLE);
$widget->show();
}
function saveDetails()
{
common_debug(var_export($_POST, true));
$user = common_current_user();
try {
$this->saveStandardProfileDetails($user);
} catch (Exception $e) {
$this->showForm($e->getMessage(), false);
return;
}
$profile = $user->getProfile();
$simple_fields = array('title', 'manager', 'tags', 'spouse');
$this->showForm(_('Details saved.'), true);
}
/**
* Save fields that should be stored in the main profile object
*
* XXX: There's a lot of dupe code here from ProfileSettingsAction.
* Do not want.
*
* @param User $user the current user
*/
function saveStandardProfileDetails($user)
{
$user->query('BEGIN');
$fullname = $this->trimmed('extprofile-fullname');
$location = $this->trimmed('extprofile-location');
$tagstring = $this->trimmed('extprofile-tags');
$bio = $this->trimmed('extprofile-bio');
if ($tagstring) {
$tags = array_map(
'common_canonical_tag',
preg_split('/[\s,]+/', $tagstring)
);
} else {
$tags = array();
}
foreach ($tags as $tag) {
if (!common_valid_profile_tag($tag)) {
// TRANS: Validation error in form for profile settings.
// TRANS: %s is an invalid tag.
throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
}
}
$profile = $user->getProfile();
if ($fullname != $profile->fullname
|| $location != $profile->location
|| $tags != $profile->tags
|| $bio != $profile->bio) {
$orig = clone($profile);
$profile->nickname = $user->nickname;
$profile->fullname = $fullname;
$profile->bio = $bio;
$profile->location = $location;
$loc = Location::fromName($location);
if (empty($loc)) {
$profile->lat = null;
$profile->lon = null;
$profile->location_id = null;
$profile->location_ns = null;
} else {
$profile->lat = $loc->lat;
$profile->lon = $loc->lon;
$profile->location_id = $loc->location_id;
$profile->location_ns = $loc->location_ns;
}
$profile->profileurl = common_profile_url($user->nickname);
$result = $profile->update($orig);
if ($result === false) {
common_log_db_error($profile, 'UPDATE', __FILE__);
// TRANS: Server error thrown when user profile settings could not be saved.
$this->serverError(_('Could not save profile.'));
return;
}
// Set the user tags
$result = $user->setSelfTags($tags);
if (!$result) {
// TRANS: Server error thrown when user profile settings tags could not be saved.
$this->serverError(_('Could not save tags.'));
return;
}
$user->query('COMMIT');
Event::handle('EndProfileSaveForm', array($this));
common_broadcast_profile($profile);
}
}
}