profilefieldsadminpanel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Allows administrators to define additional profile fields for the users of a GNU social installation.
  18. *
  19. * @category Widget
  20. * @package GNU social
  21. * @author Max Shinn <trombonechamp@gmail.com>
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2011-2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. class ProfilefieldsAdminPanelAction extends AdminPanelAction
  28. {
  29. /**
  30. * Title of the page
  31. *
  32. * @return string Title of the page
  33. */
  34. public function title(): string
  35. {
  36. return _m('Profile fields');
  37. }
  38. /**
  39. * Instructions for use
  40. *
  41. * @return string instructions for use
  42. */
  43. public function getInstructions(): string
  44. {
  45. return _m('GNU Social custom profile fields');
  46. }
  47. public function showForm(): void
  48. {
  49. $form = new ProfilefieldsAdminForm($this);
  50. $form->show();
  51. }
  52. protected function saveField(): void
  53. {
  54. $field = GNUsocialProfileExtensionField::getKV('id', $this->trimmed('id'));
  55. if (!$field) {
  56. $field = new GNUsocialProfileExtensionField();
  57. $field->systemname = $this->trimmed('systemname');
  58. if (!gnusocial_field_systemname_validate($field->systemname)) {
  59. $this->clientError(_m('Internal system name must be unique and consist of only alphanumeric characters!'));
  60. }
  61. }
  62. $field->title = $this->trimmed('title');
  63. $field->description = $this->trimmed('description');
  64. $field->type = $this->trimmed('type');
  65. if ($field->id) {
  66. if ($field->validate()) {
  67. $field->update();
  68. } else {
  69. $this->clientError(_m('There was an error with the field data.'));
  70. }
  71. } else {
  72. $field->insert();
  73. }
  74. }
  75. protected function removeField(): void
  76. {
  77. // Grab field
  78. $field = GNUsocialProfileExtensionField::getKV('id', $this->trimmed('id'));
  79. if (!$field) {
  80. $this->clientError(_m('Field not found.'));
  81. }
  82. // Delete responses to this field
  83. $responses = new GNUsocialProfileExtensionResponse();
  84. $responses->extension_id = $field->id;
  85. $responses->find();
  86. $responses = $responses->fetchAll();
  87. foreach ($responses as $response) {
  88. $response->delete();
  89. }
  90. // Delete field
  91. $field->delete();
  92. }
  93. public function saveSettings()
  94. {
  95. if ($this->arg('save')) {
  96. return $this->saveField();
  97. } elseif ($this->arg('remove')) {
  98. return $this->removeField();
  99. }
  100. // TRANS: Message given submitting a form with an unknown action in e-mail settings.
  101. throw new ClientException(_('Unexpected form submission.'));
  102. }
  103. }
  104. class ProfilefieldsAdminForm extends AdminForm
  105. {
  106. public function id(): string
  107. {
  108. return 'form_profilefields_admin_panel';
  109. }
  110. public function formClass(): string
  111. {
  112. return 'form_settings';
  113. }
  114. public function action(): string
  115. {
  116. return common_local_url('profilefieldsAdminPanel');
  117. }
  118. public function formData(): void
  119. {
  120. $title = null;
  121. $description = null;
  122. $type = null;
  123. $systemname = null;
  124. $id = null;
  125. $fieldsettitle = _m("New Profile Field");
  126. // Edit a field
  127. if ($this->out->trimmed('edit')) {
  128. $field = GNUsocialProfileExtensionField::getKV('id', $this->out->trimmed('edit'));
  129. $title = $field->title;
  130. $description = $field->description;
  131. $type = $field->type;
  132. $systemname = $field->systemname;
  133. $this->out->hidden('id', $field->id, 'id');
  134. $fieldsettitle = _m("Edit Profile Field");
  135. } // Don't show the list of all fields when editing one
  136. else {
  137. $this->out->elementStart('fieldset');
  138. $this->out->element('legend', null, _m('Existing Custom Profile Fields'));
  139. $this->out->elementStart('ul', 'form_data');
  140. $fields = GNUsocialProfileExtensionField::allFields();
  141. foreach ($fields as $field) {
  142. $this->li();
  143. $this->out->elementStart('div');
  144. $this->out->element(
  145. 'a',
  146. ['href' => common_local_url('profilefieldsAdminPanel').'?edit=' . $field->id],
  147. $field->title
  148. );
  149. $this->out->text(' (' . $field->type . '): ' . $field->description);
  150. $this->out->elementEnd('div');
  151. $this->unli();
  152. }
  153. $this->out->elementEnd('ul');
  154. $this->out->elementEnd('fieldset');
  155. }
  156. // New fields
  157. $this->out->elementStart('fieldset');
  158. $this->out->element('legend', null, $fieldsettitle);
  159. $this->out->elementStart('ul', 'form_data');
  160. $this->li();
  161. $this->out->input(
  162. 'title',
  163. _m('Title'),
  164. $title,
  165. _m('The title of the field')
  166. );
  167. $this->unli();
  168. $this->li();
  169. $this->out->input(
  170. 'systemname',
  171. _m('Internal name'),
  172. $systemname,
  173. _m('The alphanumeric name used internally for this field. Also the key used for federation (e.g. ActivityPub and OStatus) user info.')
  174. );
  175. $this->unli();
  176. $this->li();
  177. $this->out->input(
  178. 'description',
  179. _m('Description'),
  180. $description,
  181. _m('An optional more detailed description of the field')
  182. );
  183. $this->unli();
  184. $this->li();
  185. $this->out->dropdown(
  186. 'type',
  187. _m('Type'),
  188. array('text' => _m("Text"),
  189. 'str' => _m("String")),
  190. _m('The type of the datafield'),
  191. false,
  192. $type
  193. );
  194. $this->unli();
  195. $this->out->elementEnd('ul');
  196. $this->out->elementEnd('fieldset');
  197. }
  198. /**
  199. * Action elements
  200. *
  201. * @return void
  202. * @throws Exception
  203. */
  204. public function formActions(): void
  205. {
  206. $this->out->submit('save', _m('BUTTON', 'Save'), 'submit', null, _m('Save field'));
  207. if ($this->out->trimmed('edit')) {
  208. $this->out->submit('remove', _m('BUTTON', 'Remove'), 'submit', null, _m('Remove field'));
  209. }
  210. }
  211. }