]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
5be632f87c4cc29d59b6712bb3c7d79e0db0dc3c
[quix0rs-gnu-social.git] / classes / Profile.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for profile
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26
27 class Profile extends Memcached_DataObject
28 {
29     ###START_AUTOCODE
30     /* the code below is auto generated do not remove the above tag */
31
32     public $__table = 'profile';                         // table name
33     public $id;                              // int(4)  primary_key not_null
34     public $nickname;                        // varchar(64)  multiple_key not_null
35     public $fullname;                        // varchar(255)  multiple_key
36     public $profileurl;                      // varchar(255)
37     public $homepage;                        // varchar(255)  multiple_key
38     public $bio;                             // varchar(140)  multiple_key
39     public $location;                        // varchar(255)  multiple_key
40     public $created;                         // datetime()   not_null
41     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
42
43     /* Static get */
44     function staticGet($k,$v=null)
45     { return Memcached_DataObject::staticGet('Profile',$k,$v); }
46
47     /* the code above is auto generated do not remove the tag below */
48     ###END_AUTOCODE
49
50     function getAvatar($width, $height=null)
51     {
52         if (is_null($height)) {
53             $height = $width;
54         }
55         return Avatar::pkeyGet(array('profile_id' => $this->id,
56                                      'width' => $width,
57                                      'height' => $height));
58     }
59
60     function getOriginalAvatar()
61     {
62         $avatar = DB_DataObject::factory('avatar');
63         $avatar->profile_id = $this->id;
64         $avatar->original = true;
65         if ($avatar->find(true)) {
66             return $avatar;
67         } else {
68             return null;
69         }
70     }
71
72     function setOriginal($filename)
73     {
74         $imagefile = new ImageFile($this->id, common_avatar_path($filename));
75
76         $avatar = new Avatar();
77         $avatar->profile_id = $this->id;
78         $avatar->width = $imagefile->width;
79         $avatar->height = $imagefile->height;
80         $avatar->mediatype = image_type_to_mime_type($imagefile->type);
81         $avatar->filename = $filename;
82         $avatar->original = true;
83         $avatar->url = common_avatar_url($filename);
84         $avatar->created = DB_DataObject_Cast::dateTime(); # current time
85
86         # XXX: start a transaction here
87
88         if (!$this->delete_avatars() || !$avatar->insert()) {
89             @unlink(common_avatar_path($filename));
90             return null;
91         }
92
93         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
94             # We don't do a scaled one if original is our scaled size
95             if (!($avatar->width == $size && $avatar->height == $size)) {
96                 
97                 $scaled_filename = $imagefile->resize($size);
98                 
99                 //$scaled = DB_DataObject::factory('avatar');
100                 $scaled = new Avatar();
101                 $scaled->profile_id = $this->id;
102                 $scaled->width = $size;
103                 $scaled->height = $size;
104                 $scaled->original = false;
105                 $scaled->mediatype = image_type_to_mime_type($imagefile->type);
106                 $scaled->filename = $scaled_filename;
107                 $scaled->url = common_avatar_url($scaled_filename);
108                 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
109
110                 if (!$scaled->insert()) {
111                     return null;
112                 }
113             }
114         }
115
116         return $avatar;
117     }
118
119     function delete_avatars($original=true)
120     {
121         $avatar = new Avatar();
122         $avatar->profile_id = $this->id;
123         $avatar->find();
124         while ($avatar->fetch()) {
125             if ($avatar->original) {
126                 if ($original == false) {
127                     continue;
128                 }
129             }
130             $avatar->delete();
131         }
132         return true;
133     }
134
135     function getBestName()
136     {
137         return ($this->fullname) ? $this->fullname : $this->nickname;
138     }
139
140     # Get latest notice on or before date; default now
141     function getCurrentNotice($dt=null)
142     {
143         $notice = new Notice();
144         $notice->profile_id = $this->id;
145         if ($dt) {
146             $notice->whereAdd('created < "' . $dt . '"');
147         }
148         $notice->orderBy('created DESC, notice.id DESC');
149         $notice->limit(1);
150         if ($notice->find(true)) {
151             return $notice;
152         }
153         return null;
154     }
155
156     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
157     {
158         $qry =
159           'SELECT * ' .
160           'FROM notice ' .
161           'WHERE profile_id = %d ';
162
163         return Notice::getStream(sprintf($qry, $this->id),
164                                  'profile:notices:'.$this->id,
165                                  $offset, $limit, $since_id, $before_id);
166     }
167
168     function isMember($group)
169     {
170         $mem = new Group_member();
171
172         $mem->group_id = $group->id;
173         $mem->profile_id = $this->id;
174
175         if ($mem->find()) {
176             return true;
177         } else {
178             return false;
179         }
180     }
181
182     function isAdmin($group)
183     {
184         $mem = new Group_member();
185
186         $mem->group_id = $group->id;
187         $mem->profile_id = $this->id;
188         $mem->is_admin = 1;
189
190         if ($mem->find()) {
191             return true;
192         } else {
193             return false;
194         }
195     }
196
197 }