Extended profile - show and edit experience

This commit is contained in:
Zach Copley 2011-03-14 15:29:11 -07:00
parent f11b788b5a
commit 27c75dd4bb
4 changed files with 222 additions and 46 deletions

View File

@ -129,6 +129,29 @@ class ExtendedProfile
return $pArrays;
}
function getExperiences()
{
$companies = (isset($this->fields['companies'])) ? $this->fields['company'] : null;
$start = (isset($this->fields['start'])) ? $this->fields['start'] : null;
$end = (isset($this->fields['end'])) ? $this->fields['end'] : null;
$cArrays = array();
if (empty($experiences)) {
$eArrays[] = array(
'label' => _m('Employer'),
'type' => 'experience',
'company' => "Bozotronix",
'start' => '1/5/10',
'end' => '2/3/11',
'current' => true,
'index' => 0
);
}
return $eArrays;
}
/**
* Return all the sections of the extended profile
*
@ -206,10 +229,7 @@ class ExtendedProfile
'experience' => array(
'label' => _m('Work experience'),
'fields' => array(
'experience' => array(
'type' => 'experience',
'label' => _m('Employer'),
),
'experience' => $this->getExperiences(),
),
),
'education' => array(

View File

@ -103,9 +103,13 @@ class ExtendedProfileWidget extends Form
$this->out->elementStart('table', array('class' => 'extended-profile'));
foreach ($section['fields'] as $fieldName => $field) {
if ($fieldName == 'phone') {
$this->showPhones($fieldName, $field);
} else {
switch($fieldName) {
case 'phone':
case 'experience':
$this->showMultiple($fieldName, $field);
break;
default:
$this->showExtendedProfileField($fieldName, $field);
}
}
@ -135,37 +139,9 @@ class ExtendedProfileWidget extends Form
$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)
{
$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;
case 'phone':
$this->showPhone($name, $field);
break;
default:
$this->out->text("TYPE: $type");
}
}
protected function showPhones($name, $field) {
foreach ($field as $phone) {
$this->showExtendedProfileField($name, $phone);
protected function showMultiple($name, $fields) {
foreach ($fields as $field) {
$this->showExtendedProfileField($name, $field);
}
}
@ -210,6 +186,74 @@ class ExtendedProfileWidget extends Form
isset($field['rel']) ? $field['rel'] : null
);
$this->showMultiControls();
$this->out->elementEnd('div');
}
protected function showExperience($name, $field)
{
$this->out->elementStart('div', array('class' => 'experience-display'));
$this->out->text($field['company']);
$this->out->elementStart('dl', 'experience-start-and-end');
$this->out->element('dt', null, _m('Start'));
$this->out->element('dd', null, $field['start']);
$this->out->element('dt', null, _m('End'));
if ($field['current']) {
$this->out->element('dd', null, '(' . _m('Current') . ')');
} else {
$this->out->element('dd', null, $field['end']);
}
$this->out->elementEnd('dl');
$this->out->elementEnd('div');
}
protected function showEditableExperience($name, $field)
{
$index = isset($field['index']) ? $field['index'] : 0;
$id = "extprofile-$name-$index";
$this->out->elementStart(
'div', array(
'id' => $id . '-edit',
'class' => 'experience-edit'
)
);
$this->out->input(
$id,
null,
isset($field['company']) ? $field['company'] : null
);
$this->out->elementStart('ul', 'experience-start-and-end');
$this->out->elementStart('li');
$this->out->input(
$id . '-start',
_m('Start'),
isset($field['start']) ? $field['start'] : null
);
$this->out->elementEnd('li');
$this->out->elementStart('li');
$this->out->input(
$id . '-end',
_m('End'),
isset($field['end']) ? $field['end'] : null
);
$this->out->elementEnd('li');
$this->out->elementStart('li');
$this->out->checkbox(
$id . '-current',
_m('Current'),
$field['current']
);
$this->out->elementEnd('li');
$this->out->elementEnd('ul');
$this->showMultiControls();
$this->out->elementEnd('div');
}
function showMultiControls()
{
$this->out->element(
'a',
array(
@ -229,8 +273,37 @@ class ExtendedProfileWidget extends Form
),
'-'
);
}
$this->out->elementEnd('div');
/**
* 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)
{
$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;
case 'phone':
$this->showPhone($name, $field);
break;
case 'experience':
$this->showExperience($name, $field);
break;
default:
$this->out->text("TYPE: $type");
}
}
/**
@ -262,6 +335,9 @@ class ExtendedProfileWidget extends Form
case 'phone':
$this->showEditablePhone($name, $field);
break;
case 'experience':
$this->showEditableExperience($name, $field);
break;
default:
$out->input($id, null, "TYPE: $type");
}

View File

@ -82,6 +82,7 @@ var removeRow = function() {
var init = function() {
reorder('phone-edit');
reorder('experience-edit');
}
$(document).ready(

View File

@ -113,6 +113,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
}
$this->savePhoneNumbers($user);
$this->saveExperiences($user);
$this->showForm(_('Details saved.'), true);
@ -141,17 +142,12 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$phoneParams = $this->findMultiParams('phone');
ksort($phoneParams); // this sorts them into pairs
$phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2);
$phoneTuples = array();
foreach ($phones as $phone) {
$firstkey = current(array_keys($phone));
$index = substr($firstkey, strrpos($firstkey, '-') + 1);
list($number, $rel) = array_values($phone);
$phoneTuples[] = array(
'value' => $number,
'index' => $index,
'rel' => $rel
);
}
@ -159,6 +155,86 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return $phoneTuples;
}
function findExperiences() {
// Form vals look like this:
// 'extprofile-experience-0' => 'Bozotronix',
// 'extprofile-experience-0-current' => 'true'
// 'extprofile-experience-0-start' => '1/5/10',
// 'extprofile-experience-0-end' => '2/3/11',
$experiences = array();
$expParams = $this->findMultiParams('experience');
ksort($expParams);
$experiences = $this->arraySplit($expParams, sizeof($expParams) / 4);
$expArray = array();
foreach ($experiences as $exp) {
list($company, $current, $start, $end) = array_values($exp);
$expArray[] = array(
'company' => $company,
'start' => $start,
'end' => $end,
'current' => $current,
);
}
return $expArray;
}
function saveExperiences($user) {
common_debug('save experiences');
$experiences = $this->findExperiences();
$this->removeAll($user, 'company');
$this->removeAll($user, 'start');
$this->removeAll($user, 'end'); // also stores 'current'
$i = 0;
foreach($experiences as $experience) {
if (!empty($experience['company'])) {
++$i;
$this->saveField(
$user,
'company',
$experience['company'],
null,
$i
);
/*
$this->saveField(
$user,
'start',
null,
null,
$i,
$experience['start']
);
// Save "current" employer indicator in rel
if ($experience['current']) {
$this->saveField(
$user,
'end',
null,
'current', // rel
$i
);
} else {
$this->saveField(
$user,
'end',
null,
null,
$i,
$experience['end']
);
}
*/
}
}
}
function arraySplit($array, $pieces)
{
if ($pieces < 2) {
@ -191,8 +267,9 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
* @param string $value field value
* @param string $rel field rel (type)
* @param int $index index (fields can have multiple values)
* @param date $date related date
*/
function saveField($user, $name, $value, $rel = null, $index = null)
function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
{
$profile = $user->getProfile();
$detail = new Profile_detail();
@ -207,6 +284,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$detial->value_index = $index;
$detail->rel = $rel;
$detail->field_value = $value;
$detail->date = $date;
$detail->created = common_sql_now();
$result = $detail->insert();
if (empty($result)) {
@ -218,6 +296,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$detail->field_value = $value;
$detail->rel = $rel;
$detail->date = $date;
$result = $detail->update($orig);
if (empty($result)) {