Working on new access system...
[estigi.git] / core / skin / skin.module
blob93f2a3a106a272b420efbc4310d4205135344409
1 <?php
3 /**
4  * \defgroup Skin Skin Module
5  */
6 /*@{*/
8 /**
9  * @file
10  * The skin module, which controls the output of content.
11  * The skin system allows for nearly all output of the system to be
12  * customized by user skins.
13  * @todo
14  *  If path_not_found or access denied it should not proccess certain aspects such as tabs and stuff
15  */
17 define('DEFAULT_SKIN', 'bluebreeze');
18 global $skin, $styles, $scripts;
20 function skin_ini($skin = NULL){
22         global $skin, $user;
24         //Use session variables when complete
25         if(user_access('select own skin', 'skin') && core_variable_get('skin per user', 0) == 1){
26                 $skin = db_query("SELECT theme FROM {PRE_}users WHERE uid = ".$user['uid']." AND theme != '' ", TRUE, TRUE);
27         }
29         //Select theme if no user specific theme was selected
30         if(!isset($skin['name'])){
31                 $skin['name'] = core_variable_get('skin_default', DEFAULT_SKIN);
32         }
34         $skin['path'] = dirname(core_get_filename($skin['name'], 'theme'));
36         //Load the templates engine
37         include_once($skin['path'].'/'.$skin['name'].'.template');
39         $ini = parse_ini_file($skin['path'].'/'.$skin['name'].'.ini', 'sections');
41         //Theme settings
42         $skin['data'] = db_query("SELECT * FROM {PRE_}system WHERE type = 'theme' AND name = '".$skin['name']."'", TRUE, TRUE);
44         //Get regions
45         call_user_func($skin['name'].'_boot');
46         $skin['regions']['list'] = $skin['regions'];
48         //Tabs
49         $skin['tabs'] = NULL;
52 /**
53  * This will set the themes default settings like photos in posts and so on
54  * @todo
55  *  This function may be merged with _ini
56  */
57 function skin_set_defaults(){
59         global $skin, $settings;
61         //Add site's name to title
62         if(!isset($skin['head_title'])){
63                 $skin['head_title'] = (isset($skin['page_title']) ? $skin['page_title'] : core_variable_get('site_name', 'localhost'));
64         }
66         //Site name
67         $skin['site_name'] = core_variable_get('site_name', 'esTigi');
69         //Site path
70         $skin['site_path'] = $settings['base_root'].$settings['base_url'] . $settings['base_path'];
72         //Get all variables for this skin, if any
73         core_variable_get(NULL, FALSE, $skin['name']);
74         if(count($settings['variables'][$skin['name']]) > 0){
75                 foreach($settings['variables'][$skin['name']] as $var => $content){
76                         $skin[$var] = $content;
77                 }
78         }
80         //Not all of this are skin related, but for now it should work. They will be moved to the general skin varibles
81         foreach($settings['variables']['system'] as $var => $content){
82                 $skin[$var] = $content;
83         }
85         //Set the correct logo
86         if($skin['display_logo'] == 0){
87                 $skin['logo'] = FALSE;
88         }
89         else{
90                 $skin['logo'] = $skin['path'] .'/'. $skin['logo'];
91         }
94 /**
95  * Add tabs to a page
96  *
97  * @param $tabs
98  *  An array of tabs to add
99  * @param $type
100  *  The type of tab options: primary, secundary
101  * @todo
102  *  Add proper support for secundary tabs
103  */
104 function skin_tabs($tabs, $type = 'primary'){
106         global $x, $skin;
108         if($tabs){
109                 foreach($tabs as $url => $tab){
110                         $class = ($x == $url ? 'tabs_'.$type.'_active' : 'tabs_'.$type);
111                         $skin['all_tabs'][$type] .= '<span class="'.$class.'"><a href="'.path_set_url($url).'" class="'.$class.'">'.$tab.'</a></span>';
112                 }
113         }
114         //Just to skin them
115         else{
116                 if($skin['all_tabs']){
117                         foreach($skin['all_tabs'] as $type => $tab){
118                                 $skin['tabs'] .= $tab . '<br>';
119                         }
120                 }
121         }
125  * Theme a tpl file provided by modules
126  * file format module-filename.tpl.php
127  * file format module-filename_VAR_.tpl.php (variable content files)
128  *     it will be converted to module-filename-var_name-.tpl.php
129  * @param $module
130  *  The module that is calling this
131  * @param $file
132  *  The name of the template file, without the .tpl.php extention
133  * @param $content
134  *  The content with which you want to dress your template
135  * @param $var
136  *  A variable portion of the name to replace _VAR_ with under certain situations.
137  * 
138  */
140 function skin_dressme_tpl($module, $file, $content, $var = NULL){
142         global $skin;
143         $ext = '.tpl.php';
144         $file .= $ext;
145         $module_dir = dirname(core_get_filename($module));
146         $fullfilename = NULL;
148         //Load the appropriate file
149         if(isset($var)){
150                 $filevar = str_replace('_VAR_', $var, $file);
151                 $type = 'skin';
152                 //Overriden file with variable name
153                 if(file_exists($skin['path'] . '/' . $filevar)){
154                         $fullfilename = $skin['path'] . '/' . $filevar;
155                 }
156                 //If not, we'll look for the generic file
157                 elseif(file_exists($skin['path'] . '/' . $file)){
158                         $fullfilename = $skin['path'] . '/' . $file;
159                 }
160                 //If not we'll see if there is a file with this overriden name in the modules directory
161                 elseif(file_exists($module_dir . '/' . $filevar)){
162                         $fullfilename = $module_dir . '/' . $filevar;
163                         $type = 'module';
164                 }
165         }
166         if(!isset($fullfilename)){
167                 //If this does not exist your module has problems
168                 $fullfilename = $module_dir . '/' . $file;
169                 $type = 'module';
170         }
172         return skin_get_content($fullfilename, $module, $content, $type);
176 * Theme a tpl function provided by modules
177 * function name format module_function()
178 * function format module_function_VAR_() (variable content files)
179 *     it will be converted to module_function-var_name()
181 function skin_dressme_func($module, $function, $content, $file, $var = NULL){
183         global $skin;
185         core_load($module, $file);
187         //Load the appropriate function
188         if(isset($var)){
189                 $funcvar         = str_replace('_VAR_', $var, $function);
190                 //$funcvar_skinned = str_replace('_VAR_', $var, $function);
191                 //Overriden file with variable name
192                 if(function_exists($funcvar.'_'.$skin['name'])){
193                         return call_user_func($funcvar.'_'.$skin['name'], $content);
194                 }
195                 //If not, we'll look for the generic function
196                 elseif(function_exists($function.'_'.$skin['name'])){
197                         return call_user_func($function.'_'.$skin['name'], $content);
198                 }
199                 //If not we'll see if there is a file with this overriden name in the modules directory
200                 elseif(function_exists($funcvar)){
201                         return call_user_func($funcvar, $content);
202                 }
203         }
205                 return call_user_func($function, $content);
209 function skin_links(){
211         global $links, $skin;
213         //$links[$nid]['type']
214         static $the_links;
216         if(!isset($the_links)){
217                 if(is_array($links) && count($links) > 0){
218                         foreach($links as $node_id => $link_data){
219                                 foreach($link_data as $link_type => $link_details){
220                                         $the_links[$node_id][$link_type] = '<div class="links_'.$link_type.'">';
221                                                 foreach($link_details as $link){
222                                                         $the_links[$node_id][$link_type] .= '<a href="'.path_set_url($link['href']).'" class="'.$link['class'].'"'.$link['attributes'].' >'.$link['text'].'</a> ';
223                                                 }
224                                         $the_links[$node_id][$link_type] .= '</div>';
225                                 }
226                         }
227                 }
228         }
230         $skin['links'] = $the_links;
232         return $the_links;
237  * Return a themed image.
239  * @param $path
240  *   Either the path of the image file (relative to base path)
241  * @param $alt
242  *   The alternative text for text-based browsers.
243  * @param $title
244  *   The title text is displayed when the image is hovered in some popular browsers.
245  * @param $attributes
246  *   List of attributes to be placed in the img tag.
247  * @return
248  *   A string containing the image tag.
249  * @todo
250  *  Give the posibility to indicate max(width|height) and adjust acordingly
251  * @todo
252  *  Skin full url path images
254  */
255 function skin_image($path, $alt = '', $title = '', $attributes = NULL) {
257         global $settings;
259         $url = $settings['base_root'] . $settings['base_url'] . $settings['base_path'] . $path;
261         return '<img src="'. $url .'" alt="'. $alt .'" title="'. $title .'" '. (isset($attributes) ? $attributes : '') .' />';
265  * Handles css loading
266  * This function handles the css used by modules and skins
267  * @todo
268  *  Overide module css with theme's custom css - Use skin.template for this
269  */
270 function skin_add_css($ref, $name, $type = 'theme'){
272         global $base_path, $skin;
274         static $styles;
276         if($ref != NULL)
277                 $path = dirname(core_get_filename($ref, $type));
279         //this should be validated with the skin settings
280         return $styles .= '<link type="text/css" rel="stylesheet" media="all" href="'.$path.'/'.$name.'" />';
284 function skin_add_script(){
286                $scripts .= '<script type="text/javascript" src="misc/js/jquery.js"></script>';
287         return  $scripts = '<script type="text/javascript" src="misc/js/jquery.validationEngine.js"></script>';
292  * Gets a template file with its variable parts replaced
293  * As seen on http://cr.php.net/manual/en/function.include.php
294  * @param $filename
295  *  The name of the file to use
296  * @param $module
297  *  The name of the module using the skins
298  * @param $content
299  *  An array with the content to be used in the skin. In the skin all variables will be accessible using a variable $foo[]
301  */
302 function skin_get_content($filename, $module = NULL, $content) {
304         global $skin, $base_path, $styles, $scripts;
306         //This will make the contents of the variables available to the tpl
307         if(isset($module)){
308                 $$module = $content;
309         }
311         if (is_file($filename)) {
312                 ob_start();
313                 include $filename;
314                 $contents = ob_get_contents();
315                 ob_end_clean();
316                 return $contents;
317         }
318         return false;
321 //No idea what this is doing
322 function skin_get_settings(){
324         global $skin;
329  * Give the final touches and load the skin
330  */
331 function skin_fini($content){
333         global $skin, $base_path;
335                           skin_add_css(core_variable_get('default_skin', DEFAULT_SKIN), 'style.css');
336         $skin['styles'] = skin_add_css(core_variable_get('default_skin', DEFAULT_SKIN), 'layout.css');
337         //$scripts = skin_add_script();
339         //$content = $content;
341         skin_set_defaults();
342         skin_tabs(FALSE);
344         //Create warnings and such
345         $warnings = system_warnings(NULL);
346         if(count($warnings) > 0){
347                 foreach($warnings as $type => $warning){
348                         $skin['messages'] .= skin_dressme_func('system', 'system_warning_skin_VAR_', array($warning, $type), 'skinned', '-'.$type);
349                 }
350         }
352         //Load the blocks
353         block_ini();
355         $filename = $skin['path'] . '/page.tpl.php';
356         //Create the variables as needed
358         //Load the index of the theme and print it
359         //It may be faster or less memory consuming to just print the page, we don't need the buffer any more really
360         //return skin_get_content($filename, '', $content);
361         include_once($filename);
366 /*@}*/