]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User_group.php
Merge branch 'master' of evan@dev.controlyourself.ca:/var/www/trunk
[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         $qry =
54           'SELECT notice.* ' .
55           'FROM notice JOIN group_inbox ON notice.id = group_inbox.notice_id ' .
56           'WHERE group_inbox.group_id = %d ';
57         return Notice::getStream(sprintf($qry, $this->id),
58                                  'group:notices:'.$this->id,
59                                  $offset, $limit);
60     }
61
62     function allowedNickname($nickname)
63     {
64         static $blacklist = array('new');
65         return !in_array($nickname, $blacklist);
66     }
67
68     function getMembers($offset=0, $limit=null)
69     {
70         $qry =
71           'SELECT profile.* ' .
72           'FROM profile JOIN group_member '.
73           'ON profile.id = group_member.profile_id ' .
74           'WHERE group_member.group_id = %d ' .
75           'ORDER BY group_member.created DESC ';
76
77         if (common_config('db','type') == 'pgsql') {
78             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
79         } else {
80             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
81         }
82
83         $members = new Profile();
84
85         $cnt = $members->query(sprintf($qry, $this->id));
86
87         return $members;
88     }
89
90     function setOriginal($filename, $type)
91     {
92         $orig = clone($this);
93         $this->original_logo = common_avatar_url($filename);
94         $this->homepage_logo = common_avatar_url($this->scale($filename,
95                                                                       AVATAR_PROFILE_SIZE,
96                                                                       $type));
97         $this->stream_logo = common_avatar_url($this->scale($filename,
98                                                                     AVATAR_STREAM_SIZE,
99                                                                       $type));
100         $this->mini_logo = common_avatar_url($this->scale($filename,
101                                                                   AVATAR_MINI_SIZE,
102                                                                   $type));
103         common_debug(common_log_objstring($this));
104         return $this->update($orig);
105     }
106
107     function scale($filename, $size, $type)
108     {
109         $filepath = common_avatar_path($filename);
110
111         if (!file_exists($filepath)) {
112             $this->serverError(_('Lost our file.'));
113             return;
114         }
115
116         $info = @getimagesize($filepath);
117
118         switch ($type) {
119         case IMAGETYPE_GIF:
120             $image_src = imagecreatefromgif($filepath);
121             break;
122         case IMAGETYPE_JPEG:
123             $image_src = imagecreatefromjpeg($filepath);
124             break;
125         case IMAGETYPE_PNG:
126             $image_src = imagecreatefrompng($filepath);
127             break;
128          default:
129             $this->serverError(_('Unknown file type'));
130             return;
131         }
132
133         $image_dest = imagecreatetruecolor($size, $size);
134
135         $background = imagecolorallocate($image_dest, 0, 0, 0);
136         ImageColorTransparent($image_dest, $background);
137         imagealphablending($image_dest, false);
138
139         imagecopyresized($image_dest, $image_src, 0, 0, $x, $y, $size, $size, $info[0], $info[1]);
140
141         $cur = common_current_user();
142
143         $outname = common_avatar_filename($cur->id,
144                                           image_type_to_extension($type),
145                                           null,
146                                           common_timestamp());
147
148         $outpath = common_avatar_path($outname);
149
150         switch ($type) {
151         case IMAGETYPE_GIF:
152             imagegif($image_dest, $outpath);
153             break;
154         case IMAGETYPE_JPEG:
155             imagejpeg($image_dest, $outpath);
156             break;
157         case IMAGETYPE_PNG:
158             imagepng($image_dest, $outpath);
159             break;
160          default:
161             $this->serverError(_('Unknown file type'));
162             return;
163         }
164
165         return $outname;
166     }
167 }