Group_block.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Table Definition for group_block
  4. *
  5. * StatusNet - the distributed open-source microblogging tool
  6. * Copyright (C) 2008, 2009, StatusNet, Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  22. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  23. class Group_block extends Managed_DataObject
  24. {
  25. ###START_AUTOCODE
  26. /* the code below is auto generated do not remove the above tag */
  27. public $__table = 'group_block'; // table name
  28. public $group_id; // int(4) primary_key not_null
  29. public $blocked; // int(4) primary_key not_null
  30. public $blocker; // int(4) not_null
  31. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  32. /* the code above is auto generated do not remove the tag below */
  33. ###END_AUTOCODE
  34. public static function schemaDef()
  35. {
  36. return array(
  37. 'fields' => array(
  38. 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group profile is blocked from'),
  39. 'blocked' => array('type' => 'int', 'not null' => true, 'description' => 'profile that is blocked'),
  40. 'blocker' => array('type' => 'int', 'not null' => true, 'description' => 'user making the block'),
  41. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date of blocking'),
  42. ),
  43. 'primary key' => array('group_id', 'blocked'),
  44. 'foreign keys' => array(
  45. 'group_block_group_id_fkey' => array('user_group', array('group_id' => 'id')),
  46. 'group_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
  47. 'group_block_blocker_fkey' => array('user', array('blocker' => 'id')),
  48. ),
  49. );
  50. }
  51. static function isBlocked($group, $profile)
  52. {
  53. $block = Group_block::pkeyGet(array('group_id' => $group->id,
  54. 'blocked' => $profile->id));
  55. return !empty($block);
  56. }
  57. static function blockProfile($group, $profile, $blocker)
  58. {
  59. // Insert the block
  60. $block = new Group_block();
  61. $block->query('BEGIN');
  62. $block->group_id = $group->id;
  63. $block->blocked = $profile->id;
  64. $block->blocker = $blocker->id;
  65. $result = $block->insert();
  66. if ($result === false) {
  67. common_log_db_error($block, 'INSERT', __FILE__);
  68. return null;
  69. }
  70. // Delete membership if any
  71. $member = new Group_member();
  72. $member->group_id = $group->id;
  73. $member->profile_id = $profile->id;
  74. if ($member->find(true)) {
  75. $result = $member->delete();
  76. if ($result === false) {
  77. common_log_db_error($member, 'DELETE', __FILE__);
  78. return null;
  79. }
  80. }
  81. // Commit, since both have been done
  82. $block->query('COMMIT');
  83. return $block;
  84. }
  85. static function unblockProfile($group, $profile)
  86. {
  87. $block = Group_block::pkeyGet(array('group_id' => $group->id,
  88. 'blocked' => $profile->id));
  89. if (empty($block)) {
  90. return null;
  91. }
  92. $result = $block->delete();
  93. if (!$result) {
  94. common_log_db_error($block, 'DELETE', __FILE__);
  95. return null;
  96. }
  97. return true;
  98. }
  99. }