Group_block.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  26. class Group_block extends Managed_DataObject
  27. {
  28. ###START_AUTOCODE
  29. /* the code below is auto generated do not remove the above tag */
  30. public $__table = 'group_block'; // table name
  31. public $group_id; // int(4) primary_key not_null
  32. public $blocked; // int(4) primary_key not_null
  33. public $blocker; // int(4) not_null
  34. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  35. /* the code above is auto generated do not remove the tag below */
  36. ###END_AUTOCODE
  37. public static function schemaDef()
  38. {
  39. return array(
  40. 'fields' => array(
  41. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group profile is blocked from'),
  42. 'blocked' => array('type' => 'int', 'not null' => true, 'description' => 'profile that is blocked'),
  43. 'blocker' => array('type' => 'int', 'not null' => true, 'description' => 'user making the block'),
  44. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date of blocking'),
  45. ),
  46. 'primary key' => array('group_id', 'blocked'),
  47. 'foreign keys' => array(
  48. 'group_block_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  49. 'group_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
  50. 'group_block_blocker_fkey' => array('user', array('blocker' => 'id')),
  51. ),
  52. );
  53. }
  54. public static function isBlocked($group, $profile)
  55. {
  56. $block = Group_block::pkeyGet([
  57. 'group_id' => $group->id,
  58. 'blocked' => $profile->id,
  59. ]);
  60. return !empty($block);
  61. }
  62. public static function blockProfile($group, $profile, $blocker)
  63. {
  64. // Insert the block
  65. $block = new Group_block();
  66. $block->query('START TRANSACTION');
  67. $block->group_id = $group->id;
  68. $block->blocked = $profile->id;
  69. $block->blocker = $blocker->id;
  70. $result = $block->insert();
  71. if ($result === false) {
  72. common_log_db_error($block, 'INSERT', __FILE__);
  73. return null;
  74. }
  75. // Delete membership if any
  76. $member = new Group_member();
  77. $member->group_id = $group->id;
  78. $member->profile_id = $profile->id;
  79. if ($member->find(true)) {
  80. $result = $member->delete();
  81. if ($result === false) {
  82. common_log_db_error($member, 'DELETE', __FILE__);
  83. return null;
  84. }
  85. }
  86. // Commit, since both have been done
  87. $block->query('COMMIT');
  88. return $block;
  89. }
  90. public static function unblockProfile($group, $profile)
  91. {
  92. $block = Group_block::pkeyGet(array('group_id' => $group->id,
  93. 'blocked' => $profile->id));
  94. if (empty($block)) {
  95. return null;
  96. }
  97. $result = $block->delete();
  98. if (!$result) {
  99. common_log_db_error($block, 'DELETE', __FILE__);
  100. return null;
  101. }
  102. return true;
  103. }
  104. }