[ExtendedProfile] Fix updates and allow to delete values

This commit is contained in:
Diogo Cordeiro 2019-11-03 16:49:10 +00:00 committed by Diogo Peralta Cordeiro
parent 53af27a303
commit 3d805026a2
4 changed files with 72 additions and 89 deletions

View File

@ -522,14 +522,18 @@ abstract class Managed_DataObject extends Memcached_DataObject
return $aliases; return $aliases;
} }
// 'update' won't write key columns, so we have to do it ourselves.
// This also automatically calls "update" _before_ it sets the keys.
// FIXME: This only works with single-column primary keys so far! Beware!
/** /**
* @param DB_DataObject &$orig Must be "instanceof" $this * update() won't write key columns, so we have to do it ourselves.
* This also automatically calls "update" _before_ it sets the keys.
* FIXME: This only works with single-column primary keys so far! Beware!
*
* @param Managed_DataObject $orig Must be "instanceof" $this
* @param string $pid Primary ID column (no escaping is done on column name!) * @param string $pid Primary ID column (no escaping is done on column name!)
* @return bool|void
* @throws MethodNotImplementedException
* @throws ServerException
*/ */
public function updateWithKeys(Managed_DataObject $orig, $pid=null) public function updateWithKeys(Managed_DataObject $orig, ?string $pid = null)
{ {
if (!$orig instanceof $this) { if (!$orig instanceof $this) {
throw new ServerException('Tried updating a DataObject with a different class than itself.'); throw new ServerException('Tried updating a DataObject with a different class than itself.');

View File

@ -33,7 +33,7 @@ include_once __DIR__ . '/lib/profiletools.php';
class ExtendedProfilePlugin extends Plugin class ExtendedProfilePlugin extends Plugin
{ {
const PLUGIN_VERSION = '3.0.1'; const PLUGIN_VERSION = '3.0.2';
public function onPluginVersion(array &$versions): bool public function onPluginVersion(array &$versions): bool
{ {

View File

@ -35,20 +35,6 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
return _m('Extended profile settings'); return _m('Extended profile settings');
} }
public function showStylesheets()
{
parent::showStylesheets();
$this->cssLink('plugins/ExtendedProfile/css/profiledetail.css');
return true;
}
public function showScripts()
{
parent::showScripts();
$this->script('plugins/ExtendedProfile/js/profiledetail.js');
return true;
}
protected function doPost() protected function doPost()
{ {
if ($this->arg('save')) { if ($this->arg('save')) {
@ -75,26 +61,18 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$this->saveStandardProfileDetails(); $this->saveStandardProfileDetails();
$simpleFieldNames = array('title', 'spouse', 'kids', 'manager'); $simpleFieldNames = ['title', 'spouse', 'kids', 'manager'];
$dateFieldNames = array('birthday'); $dateFieldNames = ['birthday'];
foreach ($simpleFieldNames as $name) { foreach ($simpleFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name); $value = $this->trimmed('extprofile-' . $name);
if (!empty($value)) {
$this->saveField($name, $value); $this->saveField($name, $value);
} }
}
foreach ($dateFieldNames as $name) { foreach ($dateFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name); $value = $this->trimmed('extprofile-' . $name);
$dateVal = $this->parseDate($name, $value); $date_val = $this->parseDate($name, $value);
$this->saveField( $this->saveField($name, null, null, null, $date_val);
$name,
null,
null,
null,
$dateVal
);
} }
$this->savePhoneNumbers(); $this->savePhoneNumbers();
@ -140,6 +118,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
public function savePhoneNumbers() public function savePhoneNumbers()
{ {
common_debug('save phone numbers');
$phones = $this->findPhoneNumbers(); $phones = $this->findPhoneNumbers();
$this->removeAll('phone'); $this->removeAll('phone');
$i = 0; $i = 0;
@ -164,17 +143,17 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
// 'extprofile-phone-1-rel' => 'mobile', // 'extprofile-phone-1-rel' => 'mobile',
$phones = $this->sliceParams('phone', 2); $phones = $this->sliceParams('phone', 2);
$phoneArray = array(); $phone_array = [];
foreach ($phones as $phone) { foreach ($phones as $phone) {
list($number, $rel) = array_values($phone); list($number, $rel) = array_values($phone);
$phoneArray[] = array( $phone_array[] = [
'value' => $number, 'value' => $number,
'rel' => $rel 'rel' => $rel
); ];
} }
return $phoneArray; return $phone_array;
} }
public function findIms() public function findIms()
@ -189,10 +168,10 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
foreach ($ims as $im) { foreach ($ims as $im) {
list($id, $rel) = array_values($im); list($id, $rel) = array_values($im);
$imArray[] = array( $imArray[] = [
'value' => $id, 'value' => $id,
'rel' => $rel 'rel' => $rel
); ];
} }
return $imArray; return $imArray;
@ -200,6 +179,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
public function saveIms() public function saveIms()
{ {
common_debug('save ims');
$ims = $this->findIms(); $ims = $this->findIms();
$this->removeAll('im'); $this->removeAll('im');
$i = 0; $i = 0;
@ -237,6 +217,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
public function saveWebsites() public function saveWebsites()
{ {
common_debug('save websites');
$sites = $this->findWebsites(); $sites = $this->findWebsites();
$this->removeAll('website'); $this->removeAll('website');
$i = 0; $i = 0;
@ -272,11 +253,11 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$expArray = array(); $expArray = array();
foreach ($experiences as $exp) { foreach ($experiences as $exp) {
if (sizeof($experiences) == 4) { if (count($exp) === 4) {
list($company, $current, $end, $start) = array_values($exp); [$company, $current, $end, $start] = array_values($exp);
} else { } else {
$end = null; $end = null;
list($company, $current, $start) = array_values($exp); [$company, $current, $start] = array_values($exp);
} }
if (!empty($company)) { if (!empty($company)) {
$expArray[] = array( $expArray[] = array(
@ -372,7 +353,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
public function saveEducations() public function saveEducations()
{ {
common_debug('save education'); common_debug('save educations');
$edus = $this->findEducations(); $edus = $this->findEducations();
common_debug(var_export($edus, true)); common_debug(var_export($edus, true));
@ -469,39 +450,38 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
*/ */
public function saveField(string $name, ?string $value, ?string $rel = null, ?int $index = null, ?string $date = null) public function saveField(string $name, ?string $value, ?string $rel = null, ?int $index = null, ?string $date = null)
{ {
common_debug('save ' . $name);
$detail = new Profile_detail(); $detail = new Profile_detail();
$detail->profile_id = $this->scoped->getID(); $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); if ($detail->find(true)) {
// Found an existing Profile Detail, let's update it!
$orig = clone($detail);
if (!$result instanceof Profile_detail) { $detail->field_value = $value ?? $detail->sqlValue('NULL');
$detail->value_index = $index; $detail->rel = $rel ?? $detail->sqlValue('NULL');
$detail->date = $date ?? $detail->sqlValue('NULL');
if (!$detail->update($orig)) {
common_log_db_error($detail, 'UPDATE', __FILE__);
// TRANS: Server error displayed when a field could not be saved in the database.
throw new ServerException(_m('Could not save profile details.'));
}
} elseif (!empty($rel) || !empty($value) || !empty($date)) {
// It's the first time, let's insert, if there is anything to insert.
$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();
if ($result === false) { if (!$detail->insert()) {
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.
throw new ServerException(_m('Could not save profile details.')); throw new ServerException(_m('Could not save profile details.'));
} }
} else {
$orig = clone($detail);
$detail->field_value = $value;
$detail->rel = $rel;
$detail->date = $date;
$result = $detail->update($orig);
if ($result === false) {
common_log_db_error($detail, 'UPDATE', __FILE__);
// TRANS: Server error displayed when a field could not be saved in the database.
throw new ServerException(_m('Could not save profile details.'));
}
} }
$detail->free(); $detail->free();
@ -593,6 +573,7 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
private function saveCustomFields($action) private function saveCustomFields($action)
{ {
common_debug('save custom fields');
$fields = GNUsocialProfileExtensionField::allFields(); $fields = GNUsocialProfileExtensionField::allFields();
$user = common_current_user(); $user = common_current_user();
$profile = $user->getProfile(); $profile = $user->getProfile();

View File

@ -71,37 +71,35 @@ class Profile_detail extends Managed_DataObject
public static function schemaDef() public static function schemaDef()
{ {
return array( return [
// No need for i18n. Table properties. // No need for i18n. Table properties.
'description' 'description' => 'Additional profile details for the ExtendedProfile plugin',
=> 'Additional profile details for the ExtendedProfile plugin', 'fields' => [
'fields' => array( 'id' => ['type' => 'serial', 'not null' => true],
'id' => array('type' => 'serial', 'not null' => true), 'profile_id' => ['type' => 'int', 'not null' => true],
'profile_id' => array('type' => 'int', 'not null' => true), 'field_name' => [
'field_name' => array(
'type' => 'varchar', 'type' => 'varchar',
'length' => 16, 'length' => 16,
'not null' => true 'not null' => true,
), ],
'value_index' => array('type' => 'int'), 'value_index' => ['type' => 'int'],
'field_value' => array('type' => 'text'), 'field_value' => ['type' => 'text'],
'date' => array('type' => 'datetime'), 'date' => ['type' => 'datetime'],
'rel' => array('type' => 'varchar', 'length' => 16), 'rel' => ['type' => 'varchar', 'length' => 16],
'rel_profile' => array('type' => 'int'), 'rel_profile' => ['type' => 'int'],
'created' => array( 'created' => [
'type' => 'datetime', 'type' => 'datetime',
'not null' => true 'not null' => true,
), ],
'modified' => array( 'modified' => [
'type' => 'timestamp', 'type' => 'timestamp',
'not null' => true 'not null' => true,
), ],
), ],
'primary key' => array('id'), 'primary key' => ['id'],
'unique keys' => array( 'unique keys' => [
'profile_detail_profile_id_field_name_value_index' 'profile_detail_profile_id_field_name_value_index' => ['profile_id', 'field_name', 'value_index']
=> array('profile_id', 'field_name', 'value_index'), ],
) ];
);
} }
} }