]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
sphinx search for notices
[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) { return Memcached_DataObject::staticGet('Profile',$k,$v); }
45
46     /* the code above is auto generated do not remove the tag below */
47     ###END_AUTOCODE
48
49         function getAvatar($width, $height=NULL) {
50                 if (is_null($height)) {
51                         $height = $width;
52                 }
53                 return Avatar::pkeyGet(array('profile_id' => $this->id,
54                                                                          'width' => $width,
55                                                                          'height' => $height));
56         }
57
58         function getOriginalAvatar() {
59                 $avatar = DB_DataObject::factory('avatar');
60                 $avatar->profile_id = $this->id;
61                 $avatar->original = true;
62                 if ($avatar->find(true)) {
63                         return $avatar;
64                 } else {
65                         return NULL;
66                 }
67         }
68
69         function setOriginal($source) {
70
71                 $info = @getimagesize($source);
72
73                 if (!$info) {
74                         return NULL;
75                 }
76
77                 $filename = common_avatar_filename($this->id,
78                                                                                    image_type_to_extension($info[2]),
79                                                                                    NULL, common_timestamp());
80                 $filepath = common_avatar_path($filename);
81
82                 copy($source, $filepath);
83
84                 $avatar = new Avatar();
85
86                 $avatar->profile_id = $this->id;
87                 $avatar->width = $info[0];
88                 $avatar->height = $info[1];
89                 $avatar->mediatype = image_type_to_mime_type($info[2]);
90                 $avatar->filename = $filename;
91                 $avatar->original = true;
92                 $avatar->url = common_avatar_url($filename);
93                 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
94
95                 # XXX: start a transaction here
96
97                 if (!$this->delete_avatars()) {
98                         @unlink($filepath);
99                         return NULL;
100                 }
101
102                 if (!$avatar->insert()) {
103                         @unlink($filepath);
104                         return NULL;
105                 }
106
107                 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
108                         # We don't do a scaled one if original is our scaled size
109                         if (!($avatar->width == $size && $avatar->height == $size)) {
110                                 $s = $avatar->scale($size);
111                                 if (!$s) {
112                                         return NULL;
113                                 }
114                         }
115                 }
116
117                 return $avatar;
118         }
119
120         function delete_avatars() {
121                 $avatar = new Avatar();
122                 $avatar->profile_id = $this->id;
123                 $avatar->find();
124                 while ($avatar->fetch()) {
125                         $avatar->delete();
126                 }
127                 return true;
128         }
129
130         function getBestName() {
131                 return ($this->fullname) ? $this->fullname : $this->nickname;
132         }
133
134     # Get latest notice on or before date; default now
135         function getCurrentNotice($dt=NULL) {
136                 $notice = new Notice();
137                 $notice->profile_id = $this->id;
138                 if ($dt) {
139                         $notice->whereAdd('created < "' . $dt . '"');
140                 }
141                 $notice->orderBy('created DESC, notice.id DESC');
142                 $notice->limit(1);
143                 if ($notice->find(true)) {
144                         return $notice;
145                 }
146                 return NULL;
147         }
148 }