]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Group_block.php
Merge branch 'lookup_url_fix' into 'nightly'
[quix0rs-gnu-social.git] / classes / Group_block.php
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
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23
24 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
25
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
31     public $__table = 'group_block';                     // table name
32     public $group_id;                        // int(4)  primary_key not_null
33     public $blocked;                         // int(4)  primary_key not_null
34     public $blocker;                         // int(4)   not_null
35     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
36
37     /* the code above is auto generated do not remove the tag below */
38     ###END_AUTOCODE
39
40     public static function schemaDef()
41     {
42         return array(
43             'fields' => array(
44                 'group_id' => array('type' => 'int', 'not null' => true, 'description' => 'group profile is blocked from'),
45                 'blocked' => array('type' => 'int', 'not null' => true, 'description' => 'profile that is blocked'),
46                 'blocker' => array('type' => 'int', 'not null' => true, 'description' => 'user making the block'),
47                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date of blocking'),
48             ),
49             'primary key' => array('group_id', 'blocked'),
50             'foreign keys' => array(
51                 'group_block_group_id_fkey' => array('user_group', array('group_id' => 'id')),
52                 'group_block_blocked_fkey' => array('profile', array('blocked' => 'id')),
53                 'group_block_blocker_fkey' => array('user', array('blocker' => 'id')),
54             ),
55         );
56     }
57
58     static function isBlocked($group, $profile)
59     {
60         $block = Group_block::pkeyGet(array('group_id' => $group->id,
61                                             'blocked' => $profile->id));
62         return !empty($block);
63     }
64
65     static function blockProfile($group, $profile, $blocker)
66     {
67         // Insert the block
68
69         $block = new Group_block();
70
71         $block->query('BEGIN');
72
73         $block->group_id = $group->id;
74         $block->blocked  = $profile->id;
75         $block->blocker  = $blocker->id;
76
77         $result = $block->insert();
78
79         if ($result === false) {
80             common_log_db_error($block, 'INSERT', __FILE__);
81             return null;
82         }
83
84         // Delete membership if any
85
86         $member = new Group_member();
87
88         $member->group_id   = $group->id;
89         $member->profile_id = $profile->id;
90
91         if ($member->find(true)) {
92             $result = $member->delete();
93             if ($result === false) {
94                 common_log_db_error($member, 'DELETE', __FILE__);
95                 return null;
96             }
97         }
98
99         // Commit, since both have been done
100
101         $block->query('COMMIT');
102
103         return $block;
104     }
105
106     static function unblockProfile($group, $profile)
107     {
108         $block = Group_block::pkeyGet(array('group_id' => $group->id,
109                                             'blocked' => $profile->id));
110
111         if (empty($block)) {
112             return null;
113         }
114
115         $result = $block->delete();
116
117         if (!$result) {
118             common_log_db_error($block, 'DELETE', __FILE__);
119             return null;
120         }
121
122         return true;
123     }
124 }