source: temp/test-xoops.ec-cube.net/html/class/xoopsblock.php @ 405

Revision 405, 22.5 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: xoopsblock.php,v 1.5 2006/05/01 02:37:24 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35require_once XOOPS_ROOT_PATH."/kernel/object.php";
36
37class XoopsBlock extends XoopsObject
38{
39    var $db;
40
41    function XoopsBlock($id = null)
42    {
43        $this->db =& Database::getInstance();
44        $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
45        $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
46        $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
47        $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
48        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
49        //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
50        $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
51        $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
52        $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
53        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
54        $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
55        $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
56        $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
57        $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
58
59        $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
60        $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
61        $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
62        $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
63
64        $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
65        $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
66        $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
67
68        if ( !empty($id) ) {
69            if ( is_array($id) ) {
70                $this->assignVars($id);
71            } else {
72                $this->load(intval($id));
73            }
74        }
75    }
76
77    function load($id)
78    {
79        $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE bid = '.$id;
80        $arr = $this->db->fetchArray($this->db->query($sql));
81        $this->assignVars($arr);
82    }
83
84    function store()
85    {
86        if ( !$this->cleanVars() ) {
87            return false;
88        }
89        foreach ( $this->cleanVars as $k=>$v ) {
90            ${$k} = $v;
91        }
92        if ( empty($bid) ) {
93            $bid = $this->db->genId($this->db->prefix("newblocks")."_bid_seq");
94            $sql = sprintf("INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, %s, %s, %s, %s, %u, %u, %u, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u)", $this->db->prefix('newblocks'), $bid, $mid, $func_num, $this->db->quoteString($options), $this->db->quoteString($name), $this->db->quoteString($title), $this->db->quoteString($content), $side, $weight, $visible, $this->db->quoteString($block_type), $this->db->quoteString($c_type), 1, $this->db->quoteString($dirname), $this->db->quoteString($func_file), $this->db->quoteString($show_func), $this->db->quoteString($edit_func), $this->db->quoteString($template), $bcachetime, time());
95        } else {
96            $sql = "UPDATE ".$this->db->prefix("newblocks")." SET options=".$this->db->quoteString($options);
97            // a custom block needs its own name
98            if ( $block_type == "C" ) {
99                $sql .= ", name=".$this->db->quoteString($name);
100            }
101            $sql .= ", isactive=".$isactive.", title=".$this->db->quoteString($title).", content=".$this->db->quoteString($content).", side=".$side.", weight=".$weight.", visible=".$visible.", c_type=".$this->db->quoteString($c_type).", template=".$this->db->quoteString($template).", bcachetime=".$bcachetime.", last_modified=".time()." WHERE bid=".$bid;
102        }
103        if ( !$this->db->query($sql) ) {
104            $this->setErrors("Could not save block data into database");
105            return false;
106        }
107        if ( empty($bid) ) {
108            $bid = $this->db->getInsertId();
109        }
110        return $bid;
111    }
112
113    function delete()
114    {
115        $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $this->getVar('bid'));
116        if ( !$this->db->query($sql) ) {
117            return false;
118        }
119        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'block_read' AND gperm_itemid = %u AND gperm_modid = 1", $this->db->prefix('group_permission'), $this->getVar('bid'));
120        $this->db->query($sql);
121        $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $this->getVar('bid'));
122        $this->db->query($sql);
123        return true;
124    }
125
126    /**
127    * do stripslashes/htmlspecialchars according to the needed output
128    *
129    * @param $format      output use: S for Show and E for Edit
130    * @param $c_type    type of block content
131    * @returns string
132    */
133    function &getContent($format = 'S', $c_type = 'T')
134    {
135        switch ( $format ) {
136        case 'S':
137            // check the type of content
138            // H : custom HTML block
139            // P : custom PHP block
140            // S : use text sanitizater (smilies enabled)
141            // T : use text sanitizater (smilies disabled)
142            if ( $c_type == 'H' ) {
143                $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
144            } elseif ( $c_type == 'P' ) {
145                ob_start();
146                echo eval($this->getVar('content', 'N'));
147                $content = ob_get_contents();
148                ob_end_clean();
149                $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
150            } elseif ( $c_type == 'S' ) {
151                $myts =& MyTextSanitizer::getInstance();
152                $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 1, 1));
153            } else {
154                $myts =& MyTextSanitizer::getInstance();
155                $ret = str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 1, 0));
156            }
157            break;
158        case 'E':
159            $ret = $this->getVar('content', 'E');
160            break;
161        default:
162            $ret = $this->getVar('content', 'N');
163            break;
164        }
165        return $ret;
166    }
167
168    function &buildBlock()
169    {
170        global $xoopsConfig, $xoopsOption;
171        $ret = false;
172        $block = array();
173        // M for module block, S for system block C for Custom
174        if ( $this->getVar("block_type") != "C" ) {
175            // get block display function
176            $show_func = $this->getVar('show_func');
177            if ( !$show_func ) {
178                return $ret;
179            }
180            // must get lang files b4 execution of the function
181            if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) {
182                if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) {
183                    include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php";
184                } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) {
185                    include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php";
186                }
187                include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file');
188                $options = explode("|", $this->getVar("options"));
189                if ( function_exists($show_func) ) {
190                    // execute the function
191                    $block = $show_func($options);
192                    if ( !$block ) {
193                        return $ret;
194                    }
195                } else {
196                    return $ret;
197                }
198            } else {
199                return $ret;
200            }
201        } else {
202            // it is a custom block, so just return the contents
203            $block['content'] = $this->getContent("S",$this->getVar("c_type"));
204            if (empty($block['content'])) {
205                return $ret;
206            }
207        }
208        return $block;
209    }
210
211    /*
212    * Aligns the content of a block
213    * If position is 0, content in DB is positioned
214    * before the original content
215    * If position is 1, content in DB is positioned
216    * after the original content
217    */
218    function &buildContent($position,$content="",$contentdb="")
219    {
220        if ( $position == 0 ) {
221            $ret = $contentdb.$content;
222        } elseif ( $position == 1 ) {
223            $ret = $content.$contentdb;
224        }
225        return $ret;
226    }
227
228    function &buildTitle($originaltitle, $newtitle="")
229    {
230        if ($newtitle != "") {
231            $ret = $newtitle;
232        } else {
233            $ret = $originaltitle;
234        }
235        return $ret;
236    }
237
238    function isCustom()
239    {
240        if ( $this->getVar("block_type") == "C" ) {
241            return true;
242        }
243        return false;
244    }
245
246    /**
247    * gets html form for editting block options
248    *
249    */
250    function getOptions()
251    {
252        global $xoopsConfig;
253        if ( $this->getVar("block_type") != "C" ) {
254            $edit_func = $this->getVar('edit_func');
255            if ( !$edit_func ) {
256                return false;
257            }
258            if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) {
259                if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) {
260                    include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php";
261                } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) {
262                    include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php";
263                }
264                include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file');
265                $options = explode("|", $this->getVar("options"));
266                $edit_form = $edit_func($options);
267                if ( !$edit_form ) {
268                    return false;
269                }
270                return $edit_form;
271            } else {
272                return false;
273            }
274        } else {
275            return false;
276        }
277    }
278
279    /**
280    * get all the blocks that match the supplied parameters
281    * @param $side   0: sideblock - left
282    *        1: sideblock - right
283    *        2: sideblock - left and right
284    *        3: centerblock - left
285    *        4: centerblock - right
286    *        5: centerblock - center
287    *        6: centerblock - left, right, center
288    * @param $groupid   groupid (can be an array)
289    * @param $visible   0: not visible 1: visible
290    * @param $orderby   order of the blocks
291    * @returns array of block objects
292    */
293    function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby="b.weight,b.bid", $isactive=1)
294    {
295        $db =& Database::getInstance();
296        $ret = array();
297        if ( !$asobject ) {
298            $sql = "SELECT b.bid ";
299        } else {
300            $sql = "SELECT b.* ";
301        }
302        $sql .= "FROM ".$db->prefix("newblocks")." b LEFT JOIN ".$db->prefix("group_permission")." l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1";
303        if ( is_array($groupid) ) {
304            $sql .= " AND (l.gperm_groupid=".$groupid[0]."";
305            $size = count($groupid);
306            if ( $size  > 1 ) {
307                for ( $i = 1; $i < $size; $i++ ) {
308                    $sql .= " OR l.gperm_groupid=".$groupid[$i]."";
309                }
310            }
311            $sql .= ")";
312        } else {
313            $sql .= " AND l.gperm_groupid=".$groupid."";
314        }
315        $sql .= " AND b.isactive=".$isactive;
316        if ( isset($side) ) {
317            // get both sides in sidebox? (some themes need this)
318            if ( $side == XOOPS_SIDEBLOCK_BOTH ) {
319                $side = "(b.side=0 OR b.side=1)";
320            } elseif ( $side == XOOPS_CENTERBLOCK_ALL ) {
321                $side = "(b.side=3 OR b.side=4 OR b.side=5)";
322            } else {
323                $side = "b.side=".$side;
324            }
325            $sql .= " AND ".$side;
326        }
327        if ( isset($visible) ) {
328            $sql .= " AND b.visible=$visible";
329        }
330        $sql .= " ORDER BY $orderby";
331        $result = $db->query($sql);
332        $added = array();
333        while ( $myrow = $db->fetchArray($result) ) {
334            if ( !in_array($myrow['bid'], $added) ) {
335                if (!$asobject) {
336                    $ret[] = $myrow['bid'];
337                } else {
338                    $ret[] =& new XoopsBlock($myrow);
339                }
340                array_push($added, $myrow['bid']);
341            }
342        }
343        //echo $sql;
344        return $ret;
345    }
346
347    function &getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1)
348    {
349        $db =& Database::getInstance();
350        $ret = array();
351        $where_query = " WHERE isactive=".$isactive;
352        if ( isset($side) ) {
353            // get both sides in sidebox? (some themes need this)
354            if ( $side == 2 ) {
355                $side = "(side=0 OR side=1)";
356            } elseif ( $side == 6 ) {
357                $side = "(side=3 OR side=4 OR side=5)";
358            } else {
359                $side = "side=".$side;
360            }
361            $where_query .= " AND ".$side;
362        }
363        if ( isset($visible) ) {
364            $where_query .= " AND visible=$visible";
365        }
366        $where_query .= " ORDER BY $orderby";
367        switch ($rettype) {
368        case "object":
369            $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
370            $result = $db->query($sql);
371            while ( $myrow = $db->fetchArray($result) ) {
372                $ret[] =& new XoopsBlock($myrow);
373            }
374            break;
375        case "list":
376            $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
377            $result = $db->query($sql);
378            while ( $myrow = $db->fetchArray($result) ) {
379                $block =& new XoopsBlock($myrow);
380                $name = ($block->getVar("block_type") != "C") ? $block->getVar("name") : $block->getVar("title");
381                $ret[$block->getVar("bid")] = $name;
382                unset($block);
383            }
384            break;
385        case "id":
386            $sql = "SELECT bid FROM ".$db->prefix("newblocks")."".$where_query;
387            $result = $db->query($sql);
388            while ( $myrow = $db->fetchArray($result) ) {
389                $ret[] = $myrow['bid'];
390            }
391            break;
392        }
393        //echo $sql;
394        return $ret;
395    }
396
397    function &getByModule($moduleid, $asobject=true)
398    {
399        $db =& Database::getInstance();
400        if ( $asobject == true ) {
401            $sql = $sql = "SELECT * FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
402        } else {
403            $sql = "SELECT bid FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
404        }
405        $result = $db->query($sql);
406        $ret = array();
407        while( $myrow = $db->fetchArray($result) ) {
408            if ( $asobject ) {
409                $ret[] =& new XoopsBlock($myrow);
410            } else {
411                $ret[] = $myrow['bid'];
412            }
413        }
414        return $ret;
415    }
416
417    function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
418    {
419        $db =& Database::getInstance();
420        $ret = array();
421        $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1";
422        if ( is_array($groupid) ) {
423            $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).')';
424        } else {
425            if (intval($groupid) > 0) {
426                $sql .= ' AND gperm_groupid='.$groupid;
427            }
428        }
429        $result = $db->query($sql);
430        $blockids = array();
431        while ( $myrow = $db->fetchArray($result) ) {
432            $blockids[] = $myrow['gperm_itemid'];
433        }
434        if (!empty($blockids)) {
435            $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
436            $sql .= ' AND b.isactive='.$isactive;
437            if (isset($visible)) {
438                $sql .= ' AND b.visible='.intval($visible);
439            }
440            $module_id = intval($module_id);
441            if (!empty($module_id)) {
442                $sql .= ' AND m.module_id IN (0,'.$module_id;
443                if ($toponlyblock) {
444                    $sql .= ',-1';
445                }
446                $sql .= ')';
447            } else {
448                if ($toponlyblock) {
449                    $sql .= ' AND m.module_id IN (0,-1)';
450                } else {
451                    $sql .= ' AND m.module_id=0';
452                }
453            }
454            $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
455            $sql .= ' ORDER BY '.$orderby;
456            $result = $db->query($sql);
457            while ( $myrow = $db->fetchArray($result) ) {
458                $block =& new XoopsBlock($myrow);
459                $ret[$myrow['bid']] =& $block;
460                unset($block);
461            }
462        }
463        return $ret;
464    }
465
466    function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
467    {
468        $db =& Database::getInstance();
469        $ret = array();
470        $bids = array();
471        $sql = "SELECT DISTINCT(bid) from ".$db->prefix('newblocks');
472        if ($result = $db->query($sql)) {
473            while ( $myrow = $db->fetchArray($result) ) {
474                $bids[] = $myrow['bid'];
475            }
476        }
477        $sql = "SELECT DISTINCT(p.gperm_itemid) from ".$db->prefix('group_permission')." p, ".$db->prefix('groups')." g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'";
478        $grouped = array();
479        if ($result = $db->query($sql)) {
480            while ( $myrow = $db->fetchArray($result) ) {
481                $grouped[] = $myrow['gperm_itemid'];
482            }
483        }
484        $non_grouped = array_diff($bids, $grouped);
485        if (!empty($non_grouped)) {
486            $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
487            $sql .= ' AND b.isactive='.$isactive;
488            if (isset($visible)) {
489                $sql .= ' AND b.visible='.intval($visible);
490            }
491            $module_id = intval($module_id);
492            if (!empty($module_id)) {
493                $sql .= ' AND m.module_id IN (0,'.$module_id;
494                if ($toponlyblock) {
495                    $sql .= ',-1';
496                }
497                $sql .= ')';
498            } else {
499                if ($toponlyblock) {
500                    $sql .= ' AND m.module_id IN (0,-1)';
501                } else {
502                    $sql .= ' AND m.module_id=0';
503                }
504            }
505            $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')';
506            $sql .= ' ORDER BY '.$orderby;
507            $result = $db->query($sql);
508            while ( $myrow = $db->fetchArray($result) ) {
509                $block =& new XoopsBlock($myrow);
510                $ret[$myrow['bid']] =& $block;
511                unset($block);
512            }
513        }
514        return $ret;
515    }
516
517    function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
518    {
519        $funcNum = intval($funcNum);
520        $moduleId = intval($moduleId);
521        if ($funcNum < 1 || $moduleId < 1) {
522            // invalid query
523            return 0;
524        }
525        $db =& Database::getInstance();
526        if (isset($showFunc)) {
527            // showFunc is set for more strict comparison
528            $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s", $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc)));
529        } else {
530            $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum);
531        }
532        if (!$result = $db->query($sql)) {
533            return 0;
534        }
535        list($count) = $db->fetchRow($result);
536        return $count;
537    }
538}
539?>
Note: See TracBrowser for help on using the repository browser.