]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Design.php
Make errors work correctly
[quix0rs-gnu-social.git] / classes / Design.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 define('BACKGROUND_ON', 1);
25 define('BACKGROUND_OFF', 2);
26 define('BACKGROUND_TILE', 4);
27
28 /**
29  * Table Definition for design
30  */
31
32 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
33 require_once INSTALLDIR . '/lib/webcolor.php';
34
35 class Design extends Memcached_DataObject
36 {
37     ###START_AUTOCODE
38     /* the code below is auto generated do not remove the above tag */
39
40     public $__table = 'design';                          // table name
41     public $id;                              // int(4)  primary_key not_null
42     public $backgroundcolor;                 // int(4)
43     public $contentcolor;                    // int(4)
44     public $sidebarcolor;                    // int(4)
45     public $textcolor;                       // int(4)
46     public $linkcolor;                       // int(4)
47     public $backgroundimage;                 // varchar(255)
48     public $disposition;                     // tinyint(1)   default_1
49
50     /* Static get */
51     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Design',$k,$v); }
52
53     /* the code above is auto generated do not remove the tag below */
54     ###END_AUTOCODE
55
56     function showCSS($out)
57     {
58         $css = '';
59
60         $bgcolor = Design::toWebColor($this->backgroundcolor);
61
62         if (!empty($bgcolor)) {
63             $css .= 'body { background-color: #' . $bgcolor->hexValue() . ' }' . "\n";
64         }
65
66         $ccolor  = Design::toWebColor($this->contentcolor);
67
68         if (!empty($ccolor)) {
69             $css .= '#content, #site_nav_local_views .current a { background-color: #';
70             $css .= $ccolor->hexValue() . '} '."\n";
71         }
72
73         $sbcolor = Design::toWebColor($this->sidebarcolor);
74
75         if (!empty($sbcolor)) {
76             $css .= '#aside_primary { background-color: #'. $sbcolor->hexValue() . ' }' . "\n";
77         }
78
79         $tcolor  = Design::toWebColor($this->textcolor);
80
81         if (!empty($tcolor)) {
82             $css .= 'html body { color: #'. $tcolor->hexValue() . ' }'. "\n";
83         }
84
85         $lcolor  = Design::toWebColor($this->linkcolor);
86
87         if (!empty($lcolor)) {
88             $css .= 'a { color: #' . $lcolor->hexValue() . ' }' . "\n";
89         }
90
91         if (!empty($this->backgroundimage) &&
92             $this->disposition & BACKGROUND_ON) {
93
94            $repeat = ($this->disposition & BACKGROUND_TILE) ?
95                'background-repeat:repeat;' :
96                'background-repeat:no-repeat;';
97
98             $css .= 'body { background-image:url(' .
99                 Design::url($this->backgroundimage) .
100                 '); ' . $repeat . ' background-attachment:fixed; }' . "\n";
101         }
102
103         if (0 != mb_strlen($css)) {
104             $out->style($css);
105         }
106     }
107
108     static function toWebColor($color)
109     {
110         if ($color === null || $color === '') {
111             return null;
112         }
113
114         try {
115             return new WebColor($color);
116         } catch (WebColorException $e) {
117             // This shouldn't happen
118             common_log(LOG_ERR, "Unable to create web color for $color",
119                 __FILE__);
120             return null;
121         }
122     }
123
124     static function filename($id, $extension, $extra=null)
125     {
126         return $id . (($extra) ? ('-' . $extra) : '') . $extension;
127     }
128
129     static function path($filename)
130     {
131         $dir = common_config('background', 'dir');
132
133         if ($dir[strlen($dir)-1] != '/') {
134             $dir .= '/';
135         }
136
137         return $dir . $filename;
138     }
139
140     static function url($filename)
141     {
142         if (StatusNet::isHTTPS()) {
143
144             $sslserver = common_config('background', 'sslserver');
145
146             if (empty($sslserver)) {
147                 // XXX: this assumes that background dir == site dir + /background/
148                 // not true if there's another server
149                 if (is_string(common_config('site', 'sslserver')) &&
150                     mb_strlen(common_config('site', 'sslserver')) > 0) {
151                     $server = common_config('site', 'sslserver');
152                 } else if (common_config('site', 'server')) {
153                     $server = common_config('site', 'server');
154                 }
155                 $path   = common_config('site', 'path') . '/background/';
156             } else {
157                 $server = $sslserver;
158                 $path   = common_config('background', 'sslpath');
159                 if (empty($path)) {
160                     $path = common_config('background', 'path');
161                 }
162             }
163
164             $protocol = 'https';
165
166         } else {
167
168             $path = common_config('background', 'path');
169
170             $server = common_config('background', 'server');
171
172             if (empty($server)) {
173                 $server = common_config('site', 'server');
174             }
175
176             $protocol = 'http';
177         }
178
179         if ($path[strlen($path)-1] != '/') {
180             $path .= '/';
181         }
182
183         if ($path[0] != '/') {
184             $path = '/'.$path;
185         }
186
187         return $protocol.'://'.$server.$path.$filename;
188     }
189
190     function setDisposition($on, $off, $tile)
191     {
192         if ($on) {
193             $this->disposition |= BACKGROUND_ON;
194         } else {
195             $this->disposition &= ~BACKGROUND_ON;
196         }
197
198         if ($off) {
199             $this->disposition |= BACKGROUND_OFF;
200         } else {
201             $this->disposition &= ~BACKGROUND_OFF;
202         }
203
204         if ($tile) {
205             $this->disposition |= BACKGROUND_TILE;
206         } else {
207             $this->disposition &= ~BACKGROUND_TILE;
208         }
209     }
210
211     /**
212      * Return a design object based on the configured site design.
213      *
214      * @return Design a singleton design object for the site.
215      */
216
217     static function siteDesign()
218     {
219         static $siteDesign = null;
220
221         if (empty($siteDesign)) {
222
223             $siteDesign = new Design();
224
225             $attrs = array('backgroundcolor',
226                            'contentcolor',
227                            'sidebarcolor',
228                            'textcolor',
229                            'linkcolor',
230                            'backgroundimage',
231                            'disposition');
232
233             foreach ($attrs as $attr) {
234                 $val = common_config('design', $attr);
235                 if ($val !== false) {
236                     $siteDesign->$attr = $val;
237                 }
238             }
239         }
240
241         return $siteDesign;
242     }
243 }