Group_block.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 'indexes' => array(
  52. 'group_block_blocked_idx' => array('blocked'),
  53. 'group_block_blocker_idx' => array('blocker'),
  54. ),
  55. );
  56. }
  57. public static function isBlocked($group, $profile)
  58. {
  59. $block = Group_block::pkeyGet([
  60. 'group_id' => $group->id,
  61. 'blocked' => $profile->id,
  62. ]);
  63. return !empty($block);
  64. }
  65. public static function blockProfile($group, $profile, $blocker)
  66. {
  67. // Insert the block
  68. $block = new Group_block();
  69. $block->query('START TRANSACTION');
  70. $block->group_id = $group->id;
  71. $block->blocked = $profile->id;
  72. $block->blocker = $blocker->id;
  73. $result = $block->insert();
  74. if ($result === false) {
  75. common_log_db_error($block, 'INSERT', __FILE__);
  76. return null;
  77. }
  78. // Delete membership if any
  79. $member = new Group_member();
  80. $member->group_id = $group->id;
  81. $member->profile_id = $profile->id;
  82. if ($member->find(true)) {
  83. $result = $member->delete();
  84. if ($result === false) {
  85. common_log_db_error($member, 'DELETE', __FILE__);
  86. return null;
  87. }
  88. }
  89. // Commit, since both have been done
  90. $block->query('COMMIT');
  91. return $block;
  92. }
  93. public static function unblockProfile($group, $profile)
  94. {
  95. $block = Group_block::pkeyGet(array('group_id' => $group->id,
  96. 'blocked' => $profile->id));
  97. if (empty($block)) {
  98. return null;
  99. }
  100. $result = $block->delete();
  101. if (!$result) {
  102. common_log_db_error($block, 'DELETE', __FILE__);
  103. return null;
  104. }
  105. return true;
  106. }
  107. }