From e5831d6807f5ea153f791e388466d4380786182e Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Sat, 10 Aug 2019 13:40:47 +0100 Subject: [PATCH] [ExtendedProfile] Allow to delete custom profile field --- .../actions/profiledetailsettings.php | 6 +++ .../actions/profilefieldsadminpanel.php | 50 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/plugins/ExtendedProfile/actions/profiledetailsettings.php b/plugins/ExtendedProfile/actions/profiledetailsettings.php index 1a00c2e1be..d2fdbd6c6e 100644 --- a/plugins/ExtendedProfile/actions/profiledetailsettings.php +++ b/plugins/ExtendedProfile/actions/profiledetailsettings.php @@ -23,9 +23,15 @@ if (!defined('GNUSOCIAL')) { class ProfileDetailSettingsAction extends ProfileSettingsAction { + /** + * Title of the page + * + * @return string Title of the page + */ public function title() { // TRANS: Title for extended profile settings. + // TRANS: %%site.name%% is the name of the site. return _m('Extended profile settings'); } diff --git a/plugins/ExtendedProfile/actions/profilefieldsadminpanel.php b/plugins/ExtendedProfile/actions/profilefieldsadminpanel.php index fe3b2362b1..727c74198d 100644 --- a/plugins/ExtendedProfile/actions/profilefieldsadminpanel.php +++ b/plugins/ExtendedProfile/actions/profilefieldsadminpanel.php @@ -29,11 +29,21 @@ defined('GNUSOCIAL') || die(); class ProfilefieldsAdminPanelAction extends AdminPanelAction { + /** + * Title of the page + * + * @return string Title of the page + */ public function title(): string { return _m('Profile fields'); } + /** + * Instructions for use + * + * @return string instructions for use + */ public function getInstructions(): string { return _m('GNU Social custom profile fields'); @@ -45,7 +55,7 @@ class ProfilefieldsAdminPanelAction extends AdminPanelAction $form->show(); } - public function saveSettings(): void + protected function saveField(): void { $field = GNUsocialProfileExtensionField::getKV('id', $this->trimmed('id')); if (!$field) { @@ -68,6 +78,39 @@ class ProfilefieldsAdminPanelAction extends AdminPanelAction $field->insert(); } } + + protected function removeField(): void + { + // Grab field + $field = GNUsocialProfileExtensionField::getKV('id', $this->trimmed('id')); + if (!$field) { + $this->clientError(_m('Field not found.')); + } + + // Delete responses to this field + $responses = new GNUsocialProfileExtensionResponse(); + $responses->extension_id = $field->id; + $responses->find(); + $responses = $responses->fetchAll(); + foreach ($responses as $response) { + $response->delete(); + } + + // Delete field + $field->delete(); + } + + public function saveSettings() + { + if ($this->arg('save')) { + return $this->saveField(); + } else if ($this->arg('remove')) { + return $this->removeField(); + } + + // TRANS: Message given submitting a form with an unknown action in e-mail settings. + throw new ClientException(_('Unexpected form submission.')); + } } class ProfilefieldsAdminForm extends AdminForm @@ -178,6 +221,9 @@ class ProfilefieldsAdminForm extends AdminForm */ public function formActions(): void { - $this->out->submit('submit', _m('Save'), 'submit', null, _m('Save new field')); + $this->out->submit('save', _m('BUTTON','Save'), 'submit', null, _m('Save field')); + if ($this->out->trimmed('edit')) { + $this->out->submit('remove', _m('BUTTON', 'Remove'), 'submit', null, _m('Remove field')); + } } }