]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Design.php
change Laconica and Control Yourself to StatusNet in PHP files
[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('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->element('style', array('type' => 'text/css'), $css);
105         }
106     }
107
108     static function toWebColor($color)
109     {
110         if ($color == null) {
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         $path = common_config('background', 'path');
143
144         if ($path[strlen($path)-1] != '/') {
145             $path .= '/';
146         }
147
148         if ($path[0] != '/') {
149             $path = '/'.$path;
150         }
151
152         $server = common_config('background', 'server');
153
154         if (empty($server)) {
155             $server = common_config('site', 'server');
156         }
157
158         // XXX: protocol
159
160         return 'http://'.$server.$path.$filename;
161     }
162
163     function setDisposition($on, $off, $tile)
164     {
165         if ($on) {
166             $this->disposition |= BACKGROUND_ON;
167         } else {
168             $this->disposition &= ~BACKGROUND_ON;
169         }
170
171         if ($off) {
172             $this->disposition |= BACKGROUND_OFF;
173         } else {
174             $this->disposition &= ~BACKGROUND_OFF;
175         }
176
177         if ($tile) {
178             $this->disposition |= BACKGROUND_TILE;
179         } else {
180             $this->disposition &= ~BACKGROUND_TILE;
181         }
182     }
183
184     /**
185      * Return a design object based on the configured site design.
186      *
187      * @return Design a singleton design object for the site.
188      */
189
190     static function siteDesign()
191     {
192         static $siteDesign = null;
193
194         if (empty($siteDesign)) {
195
196             $siteDesign = new Design();
197
198             $attrs = array('backgroundcolor',
199                            'contentcolor',
200                            'sidebarcolor',
201                            'textcolor',
202                            'linkcolor',
203                            'backgroundimage',
204                            'disposition');
205
206             foreach ($attrs as $attr) {
207                 $val = common_config('design', $attr);
208                 if ($val !== false) {
209                     $siteDesign->$attr = $val;
210                 }
211             }
212         }
213
214         return $siteDesign;
215     }
216 }