GNUsocialProfileExtensionResponse.php 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 GNUsocialProfileExtensionResponse extends Managed_DataObject
  28. {
  29. public $__table = 'gnusocialprofileextensionresponse';
  30. public $id; // int(11)
  31. public $extension_id; // int(11)
  32. public $profile_id; // int(11)
  33. public $value; // text
  34. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  35. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  36. public static function schemaDef(): array
  37. {
  38. return [
  39. 'fields' => [
  40. 'id' => ['type' => 'serial', 'not null' => true, 'description' => 'Unique ID for extension response'],
  41. 'extension_id' => ['type' => 'int', 'not null' => true, 'description' => 'The extension field ID'],
  42. 'profile_id' => ['type' => 'int', 'not null' => true, 'description' => 'Profile id that made the response'],
  43. 'value' => ['type' => 'text', 'not null' => true, 'description' => 'response entry'],
  44. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'],
  45. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  46. ],
  47. 'primary key' => ['id'],
  48. // Syntax: foreign_key_name => [remote_table, local_key => remote_key]]
  49. 'foreign keys' => [
  50. 'gnusocialprofileextensionresponse_profile_id_fkey' => ['profile', ['profile_id' => 'id']],
  51. 'gnusocialprofileextensionresponse_extension_id_fkey' => ['gnusocialprofileextensionfield', ['extension_id' => 'id']],
  52. ],
  53. 'indexes' => [
  54. 'gnusocialprofileextensionresponse_extension_id_idx' => ['extension_id'],
  55. ],
  56. ];
  57. }
  58. public static function newResponse($extension_id, $profile_id, $value): GNUsocialProfileExtensionResponse
  59. {
  60. $response = new GNUsocialProfileExtensionResponse();
  61. $response->extension_id = $extension_id;
  62. $response->profile_id = $profile_id;
  63. $response->value = $value;
  64. $response->id = $response->insert();
  65. if (!$response->id) {
  66. common_log_db_error($response, 'INSERT', __FILE__);
  67. throw new ServerException(_m('Error creating new response.'));
  68. }
  69. return $response;
  70. }
  71. public static function findResponsesByProfile($id): array
  72. {
  73. $extf = 'gnusocialprofileextensionfield';
  74. $extr = 'gnusocialprofileextensionresponse';
  75. $sql = "SELECT $extr.*, $extf.title, $extf.description, $extf.type, $extf.systemname FROM $extr JOIN $extf ON $extr.extension_id=$extf.id WHERE $extr.profile_id = $id";
  76. $response = new GNUsocialProfileExtensionResponse();
  77. $response->query($sql);
  78. $responses = [];
  79. while ($response->fetch()) {
  80. $responses[] = clone($response);
  81. }
  82. return $responses;
  83. }
  84. }