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