XPressME Integration Kit

Trac

source: trunk/include/xpress_block_render.php @ 165

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

Bump Ver0.28 #53 最近の記事内容ブロックのデザイン調整

モジュール外にブロックを配置したとき、ブロックCSSとして、テーマ内のstyle.cssとhotdateプラグインのCSSを取り込むようにした。 include/xpress_block_render.php xpress_get_plugin_css()
xpress_defaultのコンテンツ出力と同じ表示を行う all_in_oneタグを追加

File size: 9.5 KB
Line 
1<?php
2        require_once dirname( __FILE__ ) .'/xml.php' ;
3        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
4        global $xoops_config;
5       
6        if (!is_object($xoops_config)){ // is call other modules
7                require_once dirname(dirname( __FILE__ )) .'/class/config_from_xoops.class.php' ;
8                $xoops_config = new ConfigFromXoops;
9        }
10       
11        function xpress_block_cache_write($mydirname,$block_name,$block)
12        {
13                        $xml = xpress_XML_serialize($block);
14                        $xml_name = $block_name . '.xml';
15                        xpress_cache_write($mydirname,$xml_name,$xml);
16        }
17        function xpress_block_cache_read($mydirname,$block_name)
18        {
19                        $xml_name = $block_name . '.xml';
20                        $xml_data = xpress_cache_read($mydirname,$xml_name);
21                        return @xpress_XML_unserialize($xml_data);
22        }
23       
24        function get_block_id($mydirname,$func_file,$options)
25        {
26                $options_string = '';
27                foreach ($options as $val){
28                        if (!empty($options_string)) $options_string .='|';
29                        $options_string .= $val;
30                }
31                        $xoopsDB =& Database::getInstance();
32                        $block_tbl = $xoopsDB->prefix('newblocks');     
33                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
34
35                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
36                        $result =  $xoopsDB->query($sql, 0, 0);
37                        if ($xoopsDB->getRowsNum($result)  > 0){
38                                $row = $xoopsDB->fetchArray($result);
39                                $block_id = $row['bid'];
40                        }
41                        return $block_id;
42        }
43
44        function get_xpress_theme_name($mydirname)
45        {
46                global $wpdb;
47               
48                if (is_null($wpdb)){
49                        $xoopsDB =& Database::getInstance();
50                        $wp_prefix = $mydirname;
51                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
52
53                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
54                        $theme_name = '';       
55
56                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
57                        $result =  $xoopsDB->query($sql, 0, 0);
58                        if ($xoopsDB->getRowsNum($result)  > 0){
59                                $row = $xoopsDB->fetchArray($result);
60                                $theme_name = $row['option_value'];
61                        }
62                } else {
63                        $theme_name = get_option('template');
64                }
65                return $theme_name;
66        }
67
68        function get_block_stylesheet_url($mydirname)
69        {
70                global $xoops_config;
71                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
72                $select_theme = get_xpress_theme_name($mydirname);
73                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
74                if (file_exists($style_file))
75                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
76                else   
77                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/block_style.css';
78        }
79       
80        function get_stylesheet_url($mydirname)
81        {
82                global $xoops_config;
83                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
84                $select_theme = get_xpress_theme_name($mydirname);
85                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/style.css';
86                if (file_exists($style_file))
87                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/style.css';
88                else   
89                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/style.css';
90        }
91       
92        function xpress_get_plugin_css($mydirname = '')
93        {
94                $this_url = '/modules/'. $mydirname;
95                $call_url = $_SERVER['REQUEST_URI'];
96                if (!strstr($call_url,$this_url)){                     
97                        global $xoops_config;
98                       
99                        // Module Main CSS
100                        $output = "\n".'<style type="text/css">@import url('.get_stylesheet_url($mydirname).');</style>';
101                       
102                        // hotDate
103                        $output .= "\n".'<style type="text/css">@import url('.$xoops_config->module_url.'/wp-content/plugins/hotDates/hotDates.css);</style>';
104                        return $output;
105                }
106                return '';
107        }
108
109        function xpress_block_css_set($mydirname = '')
110        {
111                $style_url =  get_block_stylesheet_url($mydirname);
112               
113                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
114                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
115                // Module Main CSS Plugin Css
116                $csslink .= xpress_get_plugin_css($mydirname);
117
118                        if(array_key_exists('xoops_block_header', $tplVars)) {
119                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
120                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
121                                }
122                        } else {
123                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
124                        }
125        }
126       
127        function get_block_file_path($mydirname,$file_name)
128        {
129                global $xoops_config;
130                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
131                $select_theme = get_xpress_theme_name($mydirname);
132                $block_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/' . $file_name;
133
134                if (!file_exists($block_file))
135                        $block_file =  $xoops_config->xoops_root_path . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/' . $file_name;
136                return $block_file;
137        }
138       
139    function xpress_block_cache_found($mydirname,$block_name)
140    {
141        global $xoops_config;
142        if(defined('XOOPS_ROOT_PATH')){
143                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
144        } else {
145                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
146        }
147        $xml_name = $block_name . '.xml';
148
149        $filename = $cache_dir .$mydirname . '_' . $xml_name;
150                $cache_time = 0;
151//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
152        if (file_exists($filename)) {
153            return true;
154       } else {
155                        return false;
156                }
157    }
158       
159        function xpress_block_render($mydirname,$block_function_name,$options)
160        {
161                global $wpdb;
162                $func_file = $block_function_name;
163                $call_theme_function_name = str_replace(".php", "", $block_function_name);
164                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
165                $cache_title = str_replace(".php", "", $block_function_name);
166                $blockID =get_block_id($mydirname,$func_file,$options);         
167
168                $this_url = '/modules/'. $mydirname;
169                $call_url = $_SERVER['REQUEST_URI'];
170               
171                xpress_block_css_set($mydirname);
172                if (strstr($call_url,$this_url)){                       
173                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
174                        require_once $block_theme_file;
175                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
176                } else {
177                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
178                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
179                                $block = $xml['block'];
180                        } else {
181                                $block['err_message'] = sprintf(_MB_XPRESS_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
182                        }
183                }
184
185                $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
186                $tpl =& new XoopsTpl() ;
187                $tpl->assign( 'block' , $block ) ;
188                $ret['content'] = $tpl->fetch( $templates_file ) ;
189                return $ret ;
190        }
191       
192        function xpress_block_cache_refresh()
193        {
194                global $xoops_db;
195                $mid = get_xpress_modid();
196                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
197                $blocks = $xoops_db->get_results($sql);
198                $mydirname = get_xpress_dir_name();
199                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
200
201                foreach($blocks as $block){
202                        $func_file = $block->func_file;
203                        $call_theme_function_name = str_replace(".php", "", $func_file);
204                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
205                        $cache_title = str_replace(".php", "", $func_file);
206                        $blockID = $block->bid;
207                        $options = explode("|", $block->options);
208
209                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
210                        require_once $block_theme_file;
211                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
212                        $render_array['block'] = $render;
213                        $render_array['block']['options'] = $block->options;
214                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
215                                $render_serialize = xpress_XML_serialize($render_array);
216                                $render_md5 = md5($render_serialize);
217
218                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
219                                $cache_md5 = md5($cache_serialize);
220                               
221                                if ($render_md5 != $cache_md5){
222                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
223                                }
224                        } else {
225                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
226                        }
227                }
228        }
229       
230        function xpress_unnecessary_block_cache_delete()
231        {
232                global $xoops_db,$xoops_config;
233                $mid = get_xpress_modid();
234                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
235                $blocks = $xoops_db->get_results($sql);
236                $mydirname = get_xpress_dir_name();
237                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
238
239                $pattern ='';
240                foreach($blocks as $block){
241                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
242                        if (!empty($pattern))  $pattern .= '|';
243                        $pattern .= $cache_file_name;
244                }
245                $pattern = '(' . $pattern . ')';
246               
247                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
248                $cache_time = 0;
249        if ($dh = opendir($cache_dir)) {
250            while (($file = readdir($dh)) !== false) {
251                if (preg_match('/^' . preg_quote($mydirname) . '/', $file)) {
252                        if(! preg_match('/' . $pattern . '/', $file)) {
253                        unlink($cache_dir.$file);
254                    }
255                }
256            }
257            closedir($dh);
258        }
259    }
260       
261?>
Note: See TracBrowser for help on using the repository browser.