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