]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
Merge commit 'refs/merge-requests/166' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / classes / Avatar.php
1 <?php
2 /**
3  * Table Definition for avatar
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Avatar extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'avatar';                          // table name
13     public $profile_id;                      // int(4)  primary_key not_null
14     public $original;                        // tinyint(1)
15     public $width;                           // int(4)  primary_key not_null
16     public $height;                          // int(4)  primary_key not_null
17     public $mediatype;                       // varchar(32)   not_null
18     public $filename;                        // varchar(255)
19     public $url;                             // varchar(255)  unique_key
20     public $created;                         // datetime()   not_null
21     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
22
23     /* Static get */
24     function staticGet($k,$v=null)
25     { return Memcached_DataObject::staticGet('Avatar',$k,$v); }
26
27     /* the code above is auto generated do not remove the tag below */
28     ###END_AUTOCODE
29
30         static function pivotGet($keyCol, $keyVals, $otherCols)
31         {
32             return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
33         }
34         
35     public static function schemaDef()
36     {
37         return array(
38             'fields' => array(
39                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
40                 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
41                 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
42                 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
43                 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
44                 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
45                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
46                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
47                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
48             ),
49             'primary key' => array('profile_id', 'width', 'height'),
50             'unique keys' => array(
51                 'avatar_url_key' => array('url'),
52             ),
53             'foreign keys' => array(
54                 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
55             ),
56             'indexes' => array(
57                 'avatar_profile_id_idx' => array('profile_id'),
58             ),
59         );
60     }
61     // We clean up the file, too
62
63     function delete()
64     {
65         $filename = $this->filename;
66         if (parent::delete()) {
67             @unlink(Avatar::path($filename));
68         }
69     }
70
71     function pkeyGet($kv)
72     {
73         return Memcached_DataObject::pkeyGet('Avatar', $kv);
74     }
75
76     /**
77      * Where should the avatar go for this user?
78      */
79     static function filename($id, $extension, $size=null, $extra=null)
80     {
81         if ($size) {
82             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
83         } else {
84             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
85         }
86     }
87
88     static function path($filename)
89     {
90         $dir = common_config('avatar', 'dir');
91
92         if ($dir[strlen($dir)-1] != '/') {
93             $dir .= '/';
94         }
95
96         return $dir . $filename;
97     }
98
99     static function url($filename)
100     {
101         $path = common_config('avatar', 'path');
102
103         if ($path[strlen($path)-1] != '/') {
104             $path .= '/';
105         }
106
107         if ($path[0] != '/') {
108             $path = '/'.$path;
109         }
110
111         $server = common_config('avatar', 'server');
112
113         if (empty($server)) {
114             $server = common_config('site', 'server');
115         }
116
117         $ssl = common_config('avatar', 'ssl');
118
119         if (is_null($ssl)) { // null -> guess
120             if (common_config('site', 'ssl') == 'always' &&
121                 !common_config('avatar', 'server')) {
122                 $ssl = true;
123             } else {
124                 $ssl = false;
125             }
126         }
127
128         $protocol = ($ssl) ? 'https' : 'http';
129
130         return $protocol.'://'.$server.$path.$filename;
131     }
132
133     function displayUrl()
134     {
135         $server = common_config('avatar', 'server');
136         if ($server && !empty($this->filename)) {
137             return Avatar::url($this->filename);
138         } else {
139             return $this->url;
140         }
141     }
142
143     static function defaultImage($size)
144     {
145         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
146                                   AVATAR_STREAM_SIZE => 'stream',
147                                   AVATAR_MINI_SIZE => 'mini');
148         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
149     }
150 }