Group_block.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Table Definition for group_block
  18. *
  19. * @package GNUsocial
  20. * @author Evan Prodromou <evan@status.net>
  21. * @copyright 2008, 2009 StatusNet, Inc.
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. class Group_block extends Managed_DataObject
  26. {
  27. ###START_AUTOCODE
  28. /* the code below is auto generated do not remove the above tag */
  29. public $__table = 'group_block'; // table name
  30. public $group_id; // int(4) primary_key not_null
  31. public $blocked; // int(4) primary_key not_null
  32. public $blocker; // int(4) not_null
  33. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  34. /* the code above is auto generated do not remove the tag below */
  35. ###END_AUTOCODE
  36. public static function schemaDef()
  37. {
  38. return array(
  39. 'fields' => array(
  40. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group profile is blocked from'),
  41. 'blocked' => array('type' => 'int', 'not null' => true, 'description' => 'profile that is blocked'),
  42. 'blocker' => array('type' => 'int', 'not null' => true, 'description' => 'user making the block'),
  43. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date of blocking'),
  44. ),
  45. 'primary key' => array('group_id', 'blocked'),
  46. 'foreign keys' => array(
  47. 'group_block_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  48. 'group_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
  49. 'group_block_blocker_fkey' => array('user', array('blocker' => 'id')),
  50. ),
  51. );
  52. }
  53. public static function isBlocked($group, $profile)
  54. {
  55. $block = Group_block::pkeyGet([
  56. 'group_id' => $group->id,
  57. 'blocked' => $profile->id,
  58. ]);
  59. return !empty($block);
  60. }
  61. public static function blockProfile($group, $profile, $blocker)
  62. {
  63. // Insert the block
  64. $block = new Group_block();
  65. $block->query('START TRANSACTION');
  66. $block->group_id = $group->id;
  67. $block->blocked = $profile->id;
  68. $block->blocker = $blocker->id;
  69. $result = $block->insert();
  70. if ($result === false) {
  71. common_log_db_error($block, 'INSERT', __FILE__);
  72. return null;
  73. }
  74. // Delete membership if any
  75. $member = new Group_member();
  76. $member->group_id = $group->id;
  77. $member->profile_id = $profile->id;
  78. if ($member->find(true)) {
  79. $result = $member->delete();
  80. if ($result === false) {
  81. common_log_db_error($member, 'DELETE', __FILE__);
  82. return null;
  83. }
  84. }
  85. // Commit, since both have been done
  86. $block->query('COMMIT');
  87. return $block;
  88. }
  89. public static function unblockProfile($group, $profile)
  90. {
  91. $block = Group_block::pkeyGet(array('group_id' => $group->id,
  92. 'blocked' => $profile->id));
  93. if (empty($block)) {
  94. return null;
  95. }
  96. $result = $block->delete();
  97. if (!$result) {
  98. common_log_db_error($block, 'DELETE', __FILE__);
  99. return null;
  100. }
  101. return true;
  102. }
  103. }