]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Avatar.php
staticGet for sub-Managed_DataObject classes now calls parent
[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     function pkeyGet($kv)
68     {
69         return Memcached_DataObject::pkeyGet('Avatar', $kv);
70     }
71
72     /**
73      * Where should the avatar go for this user?
74      */
75     static function filename($id, $extension, $size=null, $extra=null)
76     {
77         if ($size) {
78             return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
79         } else {
80             return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
81         }
82     }
83
84     static function path($filename)
85     {
86         $dir = common_config('avatar', 'dir');
87
88         if ($dir[strlen($dir)-1] != '/') {
89             $dir .= '/';
90         }
91
92         return $dir . $filename;
93     }
94
95     static function url($filename)
96     {
97         $path = common_config('avatar', 'path');
98
99         if ($path[strlen($path)-1] != '/') {
100             $path .= '/';
101         }
102
103         if ($path[0] != '/') {
104             $path = '/'.$path;
105         }
106
107         $server = common_config('avatar', 'server');
108
109         if (empty($server)) {
110             $server = common_config('site', 'server');
111         }
112
113         $ssl = common_config('avatar', 'ssl');
114
115         if (is_null($ssl)) { // null -> guess
116             if (common_config('site', 'ssl') == 'always' &&
117                 !common_config('avatar', 'server')) {
118                 $ssl = true;
119             } else {
120                 $ssl = false;
121             }
122         }
123
124         $protocol = ($ssl) ? 'https' : 'http';
125
126         return $protocol.'://'.$server.$path.$filename;
127     }
128
129     function displayUrl()
130     {
131         $server = common_config('avatar', 'server');
132         if ($server && !empty($this->filename)) {
133             return Avatar::url($this->filename);
134         } else {
135             return $this->url;
136         }
137     }
138
139     static function defaultImage($size)
140     {
141         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
142                                   AVATAR_STREAM_SIZE => 'stream',
143                                   AVATAR_MINI_SIZE => 'mini');
144         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
145     }
146 }