3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
23 * Table Definition for profile
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 class Profile extends Memcached_DataObject
30 /* the code below is auto generated do not remove the above tag */
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
44 function staticGet($k,$v=null)
45 { return Memcached_DataObject::staticGet('Profile',$k,$v); }
47 /* the code above is auto generated do not remove the tag below */
50 function getAvatar($width, $height=null)
52 if (is_null($height)) {
55 return Avatar::pkeyGet(array('profile_id' => $this->id,
57 'height' => $height));
60 function getOriginalAvatar()
62 $avatar = DB_DataObject::factory('avatar');
63 $avatar->profile_id = $this->id;
64 $avatar->original = true;
65 if ($avatar->find(true)) {
72 function setOriginal($filename)
74 $imagefile = new ImageFile($this->id, Avatar::path($filename));
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 = Avatar::url($filename);
84 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
86 # XXX: start a transaction here
88 if (!$this->delete_avatars() || !$avatar->insert()) {
89 @unlink(Avatar::path($filename));
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)) {
97 $scaled_filename = $imagefile->resize($size);
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 = Avatar::url($scaled_filename);
108 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
110 if (!$scaled->insert()) {
119 function delete_avatars($original=true)
121 $avatar = new Avatar();
122 $avatar->profile_id = $this->id;
124 while ($avatar->fetch()) {
125 if ($avatar->original) {
126 if ($original == false) {
135 function getBestName()
137 return ($this->fullname) ? $this->fullname : $this->nickname;
140 # Get latest notice on or before date; default now
141 function getCurrentNotice($dt=null)
143 $notice = new Notice();
144 $notice->profile_id = $this->id;
146 $notice->whereAdd('created < "' . $dt . '"');
148 $notice->orderBy('created DESC, notice.id DESC');
150 if ($notice->find(true)) {
156 function getTaggedNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null, $tag=null)
158 // XXX: I'm not sure this is going to be any faster. It probably isn't.
159 $ids = Notice::stream(array($this, '_streamTaggedDirect'),
161 'profile:notice_ids:' . $this->id,
162 $offset, $limit, $since_id, $before_id, $since, $tag);
163 common_debug(print_r($ids, true));
164 return Notice::getStreamByIds($ids);
167 function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
169 // XXX: I'm not sure this is going to be any faster. It probably isn't.
170 $ids = Notice::stream(array($this, '_streamDirect'),
172 'profile:notice_ids:' . $this->id,
173 $offset, $limit, $since_id, $before_id, $since);
175 return Notice::getStreamByIds($ids);
178 function _streamTaggedDirect($offset, $limit, $since_id, $before_id, $since=null, $tag=null)
180 common_debug('_streamTaggedDirect()');
181 $notice = new Notice();
182 $notice->profile_id = $this->id;
183 $query = "select id from notice join notice_tag on id=notice_id where tag='" . $notice->escape($tag) . "' and profile_id=" . $notice->escape($notice->profile_id);
184 if ($since_id != 0) {
185 $query .= " and id > $since_id";
188 if ($before_id != 0) {
189 $query .= " and id < $before_id";
192 if (!is_null($since)) {
193 $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
196 $query .= ' order by id DESC';
198 if (!is_null($offset)) {
199 $query .= " limit $offset, $limit";
201 $notice->query($query);
204 while ($notice->fetch()) {
205 common_debug(print_r($notice, true));
206 $ids[] = $notice->id;
215 function _streamDirect($offset, $limit, $since_id, $before_id, $since = null)
217 $notice = new Notice();
219 $notice->profile_id = $this->id;
221 $notice->selectAdd();
222 $notice->selectAdd('id');
224 if ($since_id != 0) {
225 $notice->whereAdd('id > ' . $since_id);
228 if ($before_id != 0) {
229 $notice->whereAdd('id < ' . $before_id);
232 if (!is_null($since)) {
233 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
236 $notice->orderBy('id DESC');
238 if (!is_null($offset)) {
239 $notice->limit($offset, $limit);
244 if ($notice->find()) {
245 while ($notice->fetch()) {
246 $ids[] = $notice->id;
253 function isMember($group)
255 $mem = new Group_member();
257 $mem->group_id = $group->id;
258 $mem->profile_id = $this->id;
267 function isAdmin($group)
269 $mem = new Group_member();
271 $mem->group_id = $group->id;
272 $mem->profile_id = $this->id;
282 function avatarUrl($size=AVATAR_PROFILE_SIZE)
284 $avatar = $this->getAvatar($size);
286 return $avatar->displayUrl();
288 return Avatar::defaultImage($size);