GNUsocialProfileExtensionField.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 GNUsocialProfileExtensionField extends Managed_DataObject
  28. {
  29. public $__table = 'gnusocialprofileextensionfield';
  30. public $id; // int(11)
  31. public $systemname; // varchar(64)
  32. public $title; // varchar(191) not 255 because utf8mb4 takes more space
  33. public $description; // text
  34. public $type; // varchar(191) not 255 because utf8mb4 takes more space
  35. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  36. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  37. public static function schemaDef(): array
  38. {
  39. return [
  40. 'fields' => [
  41. 'id' => ['type' => 'serial', 'not null' => true, 'description' => 'Unique ID for extension field'],
  42. 'systemname' => ['type' => 'varchar', 'not null' => true, 'length' => 64, 'description' => 'field systemname'],
  43. 'title' => ['type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'field title'],
  44. 'description' => ['type' => 'text', 'not null' => true, 'description' => 'field description'],
  45. 'type' => ['type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'field type'],
  46. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'],
  47. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  48. ],
  49. 'primary key' => ['id'],
  50. 'indexes' => [
  51. 'gnusocialprofileextensionfield_title_idx' => ['title'],
  52. ],
  53. ];
  54. }
  55. public static function newField($title, $description = null, $type = 'str', $system_name = null): GNUsocialProfileExtensionField
  56. {
  57. $field = new GNUsocialProfileExtensionField();
  58. $field->title = $title;
  59. $field->description = $description;
  60. $field->type = $type;
  61. if (empty($system_name)) {
  62. $field->systemname = 'field' . $field->id;
  63. } else {
  64. $field->systemname = $system_name;
  65. }
  66. $field->id = $field->insert();
  67. if (!$field->id) {
  68. common_log_db_error($field, 'INSERT', __FILE__);
  69. throw new ServerException(_m('Error creating new field.'));
  70. }
  71. return $field;
  72. }
  73. public static function allFields(): array
  74. {
  75. $field = new GNUsocialProfileExtensionField();
  76. $fields = [];
  77. if ($field->find()) {
  78. while ($field->fetch()) {
  79. $fields[] = clone($field);
  80. }
  81. }
  82. return $fields;
  83. }
  84. }