]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge branch '0.8.x' into userdesign
[quix0rs-gnu-social.git] / classes / User_group.php
1 <?php
2 /**
3  * Table Definition for user_group
4  */
5
6 class User_group extends Memcached_DataObject
7 {
8     ###START_AUTOCODE
9     /* the code below is auto generated do not remove the above tag */
10
11     public $__table = 'user_group';                      // table name
12     public $id;                              // int(4)  primary_key not_null
13     public $nickname;                        // varchar(64)  unique_key
14     public $fullname;                        // varchar(255)
15     public $homepage;                        // varchar(255)
16     public $description;                     // varchar(140)
17     public $location;                        // varchar(255)
18     public $original_logo;                   // varchar(255)
19     public $homepage_logo;                   // varchar(255)
20     public $stream_logo;                     // varchar(255)
21     public $mini_logo;                       // varchar(255)
22     public $created;                         // datetime()   not_null
23     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
24
25     /* Static get */
26     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_group',$k,$v); }
27
28     /* the code above is auto generated do not remove the tag below */
29     ###END_AUTOCODE
30
31     function defaultLogo($size)
32     {
33         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
34                                   AVATAR_STREAM_SIZE => 'stream',
35                                   AVATAR_MINI_SIZE => 'mini');
36         return theme_path('default-avatar-'.$sizenames[$size].'.png');
37     }
38
39     function homeUrl()
40     {
41         return common_local_url('showgroup',
42                                 array('nickname' => $this->nickname));
43     }
44
45     function permalink()
46     {
47         return common_local_url('groupbyid',
48                                 array('id' => $this->id));
49     }
50
51     function getNotices($offset, $limit)
52     {
53         $ids = Notice::stream(array($this, '_streamDirect'),
54                               array(),
55                               'user_group:notice_ids:' . $this->id,
56                               $offset, $limit);
57
58         return Notice::getStreamByIds($ids);
59     }
60
61     function _streamDirect($offset, $limit, $since_id, $max_id, $since)
62     {
63         $inbox = new Group_inbox();
64
65         $inbox->group_id = $this->id;
66
67         $inbox->selectAdd();
68         $inbox->selectAdd('notice_id');
69
70         if ($since_id != 0) {
71             $inbox->whereAdd('notice_id > ' . $since_id);
72         }
73
74         if ($max_id != 0) {
75             $inbox->whereAdd('notice_id <= ' . $max_id);
76         }
77
78         if (!is_null($since)) {
79             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
80         }
81
82         $inbox->orderBy('notice_id DESC');
83
84         if (!is_null($offset)) {
85             $inbox->limit($offset, $limit);
86         }
87
88         $ids = array();
89
90         if ($inbox->find()) {
91             while ($inbox->fetch()) {
92                 $ids[] = $inbox->notice_id;
93             }
94         }
95
96         return $ids;
97     }
98
99     function allowedNickname($nickname)
100     {
101         static $blacklist = array('new');
102         return !in_array($nickname, $blacklist);
103     }
104
105     function getMembers($offset=0, $limit=null)
106     {
107         $qry =
108           'SELECT profile.* ' .
109           'FROM profile JOIN group_member '.
110           'ON profile.id = group_member.profile_id ' .
111           'WHERE group_member.group_id = %d ' .
112           'ORDER BY group_member.created DESC ';
113
114         if ($limit != null) {
115             if (common_config('db','type') == 'pgsql') {
116                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
117             } else {
118                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
119             }
120         }
121
122         $members = new Profile();
123
124         $members->query(sprintf($qry, $this->id));
125         return $members;
126     }
127
128     function getBlocked($offset=0, $limit=null)
129     {
130         $qry =
131           'SELECT profile.* ' .
132           'FROM profile JOIN group_block '.
133           'ON profile.id = group_block.blocked ' .
134           'WHERE group_block.group_id = %d ' .
135           'ORDER BY group_block.modified DESC ';
136
137         if ($limit != null) {
138             if (common_config('db','type') == 'pgsql') {
139                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
140             } else {
141                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
142             }
143         }
144
145         $blocked = new Profile();
146
147         $blocked->query(sprintf($qry, $this->id));
148         return $blocked;
149     }
150
151     function setOriginal($filename)
152     {
153         $imagefile = new ImageFile($this->id, Avatar::path($filename));
154
155         $orig = clone($this);
156         $this->original_logo = Avatar::url($filename);
157         $this->homepage_logo = Avatar::url($imagefile->resize(AVATAR_PROFILE_SIZE));
158         $this->stream_logo = Avatar::url($imagefile->resize(AVATAR_STREAM_SIZE));
159         $this->mini_logo = Avatar::url($imagefile->resize(AVATAR_MINI_SIZE));
160         common_debug(common_log_objstring($this));
161         return $this->update($orig);
162     }
163
164     function getBestName()
165     {
166         return ($this->fullname) ? $this->fullname : $this->nickname;
167     }
168
169     function getAliases()
170     {
171         $aliases = array();
172
173         // XXX: cache this
174
175         $alias = new Group_alias();
176
177         $alias->group_id = $this->id;
178
179         if ($alias->find()) {
180             while ($alias->fetch()) {
181                 $aliases[] = $alias->alias;
182             }
183         }
184
185         $alias->free();
186
187         return $aliases;
188     }
189
190     function setAliases($newaliases) {
191
192         $newaliases = array_unique($newaliases);
193
194         $oldaliases = $this->getAliases();
195
196         # Delete stuff that's old that not in new
197
198         $to_delete = array_diff($oldaliases, $newaliases);
199
200         # Insert stuff that's in new and not in old
201
202         $to_insert = array_diff($newaliases, $oldaliases);
203
204         $alias = new Group_alias();
205
206         $alias->group_id = $this->id;
207
208         foreach ($to_delete as $delalias) {
209             $alias->alias = $delalias;
210             $result = $alias->delete();
211             if (!$result) {
212                 common_log_db_error($alias, 'DELETE', __FILE__);
213                 return false;
214             }
215         }
216
217         foreach ($to_insert as $insalias) {
218             $alias->alias = $insalias;
219             $result = $alias->insert();
220             if (!$result) {
221                 common_log_db_error($alias, 'INSERT', __FILE__);
222                 return false;
223             }
224         }
225
226         return true;
227     }
228
229     static function getForNickname($nickname)
230     {
231         $nickname = common_canonical_nickname($nickname);
232         $group = User_group::staticGet('nickname', $nickname);
233         if (!empty($group)) {
234             return $group;
235         }
236         $alias = Group_alias::staticGet('alias', $nickname);
237         if (!empty($alias)) {
238             return User_group::staticGet('id', $alias->group_id);
239         }
240         return null;
241     }
242 }