XPressME Integration Kit

Trac

source: trunk/include/xpress_block_render.php @ 97

Last change on this file since 97 was 97, checked in by toemon, 15 years ago

モジュール外にブロックを配置したとき、ブロックのスタイルシートを読み込まないバグを修正
#63 修正

File size: 5.4 KB
Line 
1<?php
2        require_once dirname( __FILE__ ) .'/xml.php' ;
3        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
4       
5        function xpress_block_cache_write($mydirname,$block_name,$block)
6        {
7                        $xml = xpress_XML_serialize($block);
8                        $xml_name = $block_name . '.xml';
9                        xpress_cache_write($mydirname,$xml_name,$xml);
10        }
11        function xpress_block_cache_read($mydirname,$block_name)
12        {
13                        $xml_name = $block_name . '.xml';
14                        $xml_data = xpress_cache_read($mydirname,$xml_name);
15                        return @xpress_XML_unserialize($xml_data);
16        }
17       
18        function get_block_id($mydirname,$func_file,$options)
19        {
20                $options_string = '';
21                foreach ($options as $val){
22                        if (!empty($options_string)) $options_string .='|';
23                        $options_string .= $val;
24                }
25                        $xoopsDB =& Database::getInstance();
26                        $block_tbl = $xoopsDB->prefix('newblocks');     
27                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
28
29                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
30                        $result =  $xoopsDB->query($sql, 0, 0);
31                        if ($xoopsDB->getRowsNum($result)  > 0){
32                                $row = $xoopsDB->fetchArray($result);
33                                $block_id = $row['bid'];
34                        }
35                        return $block_id;
36        }
37
38        function get_xpress_theme_name($mydirname)
39        {
40                global $wpdb;
41               
42                if (is_null($wpdb)){
43                        $xoopsDB =& Database::getInstance();
44                        $wp_prefix = $mydirname;
45                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
46
47                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
48                        $theme_name = '';       
49
50                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
51                        $result =  $xoopsDB->query($sql, 0, 0);
52                        if ($xoopsDB->getRowsNum($result)  > 0){
53                                $row = $xoopsDB->fetchArray($result);
54                                $theme_name = $row['option_value'];
55                        }
56                } else {
57                        $theme_name = get_option('template');
58                }
59                return $theme_name;
60        }
61
62        function get_block_stylesheet_url($mydirname)
63        {
64                global $xoops_config;
65                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
66                $select_theme = get_xpress_theme_name($mydirname);
67                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/style.css';
68                if (file_exists($style_file))
69                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/style.css';
70                else   
71                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/style.css';
72        }
73
74        function xpress_block_css_set($mydirname = '')
75        {
76                $style_url =  get_block_stylesheet_url($mydirname);
77                 
78                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
79                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
80                        if(array_key_exists('xoops_block_header', $tplVars)) {
81                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
82                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
83                                }
84                        } else {
85                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
86                        }
87        }
88       
89        function get_block_file_path($mydirname,$file_name)
90        {
91                global $xoops_config;
92                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
93                $select_theme = get_xpress_theme_name($mydirname);
94                $block_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/' . $file_name;
95
96                if (!file_exists($block_file))
97                        $block_file =  $xoops_config->xoops_root_path . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/' . $file_name;
98                return $block_file;
99        }
100       
101    function xpress_block_cache_found($mydirname,$block_name)
102    {
103        global $xoops_config;
104        if(defined('XOOPS_ROOT_PATH')){
105                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
106        } else {
107                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
108        }
109        $xml_name = $block_name . '.xml';
110
111        $filename = $cache_dir .$mydirname . '_' . $xml_name;
112                $cache_time = 0;
113//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
114        if (file_exists($filename)) {
115            return true;
116       } else {
117                        return false;
118                }
119    }
120       
121        function xpress_block_render($mydirname,$block_function_name,$options)
122        {
123                global $wpdb;
124                $func_file = $block_function_name;
125                $call_theme_function_name = str_replace(".php", "", $block_function_name);
126                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
127                $cache_title = str_replace(".php", "", $block_function_name);
128                $blockID =get_block_id($mydirname,$func_file,$options);         
129
130                $this_url = '/modules/'. $mydirname;
131                $call_url = $_SERVER['REQUEST_URI'];
132               
133                xpress_block_css_set($mydirname);
134                if (strstr($call_url,$this_url)){                       
135                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
136                        require_once $block_theme_file;
137                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
138                } else {
139                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
140                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
141                                $block = $xml['block'];
142                        } else {
143                                $block['err_message'] = sprintf(_MB_XPRESS_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
144                        }
145                }
146
147                $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
148                $tpl =& new XoopsTpl() ;
149                $tpl->assign( 'block' , $block ) ;
150                $ret['content'] = $tpl->fetch( $templates_file ) ;
151                return $ret ;
152        }
153       
154?>
Note: See TracBrowser for help on using the repository browser.