]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile.php
Some fixups of patches not already migrated to trunk to bring inline with PEAR coding...
[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($source)
73     {
74
75         $info = @getimagesize($source);
76
77         if (!$info) {
78             return null;
79         }
80
81         $filename = common_avatar_filename($this->id,
82                                            image_type_to_extension($info[2]),
83                                            null, common_timestamp());
84         $filepath = common_avatar_path($filename);
85
86         copy($source, $filepath);
87
88         $avatar = new Avatar();
89
90         $avatar->profile_id = $this->id;
91         $avatar->width = $info[0];
92         $avatar->height = $info[1];
93         $avatar->mediatype = image_type_to_mime_type($info[2]);
94         $avatar->filename = $filename;
95         $avatar->original = true;
96         $avatar->url = common_avatar_url($filename);
97         $avatar->created = DB_DataObject_Cast::dateTime(); # current time
98
99         # XXX: start a transaction here
100
101         if (!$this->delete_avatars()) {
102             @unlink($filepath);
103             return null;
104         }
105
106         if (!$avatar->insert()) {
107             @unlink($filepath);
108             return null;
109         }
110
111         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
112             # We don't do a scaled one if original is our scaled size
113             if (!($avatar->width == $size && $avatar->height == $size)) {
114                 $s = $avatar->scale($size);
115                 if (!$s) {
116                     return null;
117                 }
118             }
119         }
120
121         return $avatar;
122     }
123
124     function crop_avatars($x, $y, $w, $h) 
125     {
126
127         $avatar = $this->getOriginalAvatar();
128         $this->delete_avatars(false); # don't delete original
129
130         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
131             # We don't do a scaled one if original is our scaled size
132             if (!($avatar->width == $size && $avatar->height == $size)) {
133                 $s = $avatar->scale_and_crop($size, $x, $y, $w, $h);
134                 if (!$s) {
135                     return NULL;
136                 }
137             }
138         }
139         return true;
140     }
141
142     function delete_avatars($original=true) 
143     {
144         $avatar = new Avatar();
145         $avatar->profile_id = $this->id;
146         $avatar->find();
147         while ($avatar->fetch()) {
148             if ($avatar->original) {
149                 if ($original == false) {
150                     continue;
151                 }
152             }
153             $avatar->delete();
154         }
155         return true;
156     }
157
158     function getBestName()
159     {
160         return ($this->fullname) ? $this->fullname : $this->nickname;
161     }
162
163     # Get latest notice on or before date; default now
164     function getCurrentNotice($dt=null)
165     {
166         $notice = new Notice();
167         $notice->profile_id = $this->id;
168         if ($dt) {
169             $notice->whereAdd('created < "' . $dt . '"');
170         }
171         $notice->orderBy('created DESC, notice.id DESC');
172         $notice->limit(1);
173         if ($notice->find(true)) {
174             return $notice;
175         }
176         return null;
177     }
178
179     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
180     {
181         $qry =
182           'SELECT * ' .
183           'FROM notice ' .
184           'WHERE profile_id = %d ';
185
186         return Notice::getStream(sprintf($qry, $this->id),
187                                  'profile:notices:'.$this->id,
188                                  $offset, $limit, $since_id, $before_id);
189     }
190 }