GNUsocialProfileExtensionResponse.php 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 GNUsocial
  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()
  35. public $modified; // timestamp() 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', 'description' => 'date this record was created'],
  45. 'modified' => ['type' => 'timestamp', 'not null' => true, '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_profile_id_idx' => ['profile_id'],
  55. 'gnusocialprofileextensionresponse_extension_id_idx' => ['extension_id'],
  56. ],
  57. ];
  58. }
  59. public static function newResponse($extension_id, $profile_id, $value): GNUsocialProfileExtensionResponse
  60. {
  61. $response = new GNUsocialProfileExtensionResponse();
  62. $response->extension_id = $extension_id;
  63. $response->profile_id = $profile_id;
  64. $response->value = $value;
  65. $response->id = $response->insert();
  66. if (!$response->id) {
  67. common_log_db_error($response, 'INSERT', __FILE__);
  68. throw new ServerException(_m('Error creating new response.'));
  69. }
  70. return $response;
  71. }
  72. public static function findResponsesByProfile($id): array
  73. {
  74. $extf = 'gnusocialprofileextensionfield';
  75. $extr = 'gnusocialprofileextensionresponse';
  76. $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";
  77. $response = new GNUsocialProfileExtensionResponse();
  78. $response->query($sql);
  79. $responses = [];
  80. while ($response->fetch()) {
  81. $responses[] = clone($response);
  82. }
  83. return $responses;
  84. }
  85. }