]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
pkeyGet is now static and more similar to getKV
[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     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25
26         static function pivotGet($keyCol, $keyVals, $otherCols)
27         {
28             return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
29         }
30         
31     public static function schemaDef()
32     {
33         return array(
34             'fields' => array(
35                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
36                 'original' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'uploaded by user or generated?'),
37                 'width' => array('type' => 'int', 'not null' => true, 'description' => 'image width'),
38                 'height' => array('type' => 'int', 'not null' => true, 'description' => 'image height'),
39                 'mediatype' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'file type'),
40                 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'local filename, if local'),
41                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'avatar location'),
42                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
43                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
44             ),
45             'primary key' => array('profile_id', 'width', 'height'),
46             'unique keys' => array(
47                 'avatar_url_key' => array('url'),
48             ),
49             'foreign keys' => array(
50                 'avatar_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
51             ),
52             'indexes' => array(
53                 'avatar_profile_id_idx' => array('profile_id'),
54             ),
55         );
56     }
57     // We clean up the file, too
58
59     function delete()
60     {
61         $filename = $this->filename;
62         if (parent::delete()) {
63             @unlink(Avatar::path($filename));
64         }
65     }
66
67     /**
68      * Where should the avatar go for this user?
69      */
70     static function filename($id, $extension, $size=null, $extra=null)
71     {
72         if ($size) {
73             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
74         } else {
75             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
76         }
77     }
78
79     static function path($filename)
80     {
81         $dir = common_config('avatar', 'dir');
82
83         if ($dir[strlen($dir)-1] != '/') {
84             $dir .= '/';
85         }
86
87         return $dir . $filename;
88     }
89
90     static function url($filename)
91     {
92         $path = common_config('avatar', 'path');
93
94         if ($path[strlen($path)-1] != '/') {
95             $path .= '/';
96         }
97
98         if ($path[0] != '/') {
99             $path = '/'.$path;
100         }
101
102         $server = common_config('avatar', 'server');
103
104         if (empty($server)) {
105             $server = common_config('site', 'server');
106         }
107
108         $ssl = common_config('avatar', 'ssl');
109
110         if (is_null($ssl)) { // null -> guess
111             if (common_config('site', 'ssl') == 'always' &&
112                 !common_config('avatar', 'server')) {
113                 $ssl = true;
114             } else {
115                 $ssl = false;
116             }
117         }
118
119         $protocol = ($ssl) ? 'https' : 'http';
120
121         return $protocol.'://'.$server.$path.$filename;
122     }
123
124     function displayUrl()
125     {
126         $server = common_config('avatar', 'server');
127         if ($server && !empty($this->filename)) {
128             return Avatar::url($this->filename);
129         } else {
130             return $this->url;
131         }
132     }
133
134     static function defaultImage($size)
135     {
136         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
137                                   AVATAR_STREAM_SIZE => 'stream',
138                                   AVATAR_MINI_SIZE => 'mini');
139         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
140     }
141 }