home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopsblock.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  23.2 KB  |  541 lines

  1. <?php
  2. // $Id: xoopsblock.php 1099 2007-10-19 01:08:14Z dugris $
  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.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35. require_once XOOPS_ROOT_PATH."/kernel/object.php";
  36.  
  37. class 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.         $id = intval($id);
  80.         $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE bid = '.$id;
  81.         $arr = $this->db->fetchArray($this->db->query($sql));
  82.         $this->assignVars($arr);
  83.     }
  84.  
  85.     function store()
  86.     {
  87.         if ( !$this->cleanVars() ) {
  88.             return false;
  89.         }
  90.         foreach ( $this->cleanVars as $k=>$v ) {
  91.             ${$k} = $v;
  92.         }
  93.         if ( empty($bid) ) {
  94.             $bid = $this->db->genId($this->db->prefix("newblocks")."_bid_seq");
  95.             $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());
  96.         } else {
  97.             $sql = "UPDATE ".$this->db->prefix("newblocks")." SET options=".$this->db->quoteString($options);
  98.             // a custom block needs its own name
  99.             if ( $block_type == "C" ) {
  100.                 $sql .= ", name=".$this->db->quoteString($name);
  101.             }
  102.             $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;
  103.         }
  104.         if ( !$this->db->query($sql) ) {
  105.             $this->setErrors("Could not save block data into database");
  106.             return false;
  107.         }
  108.         if ( empty($bid) ) {
  109.             $bid = $this->db->getInsertId();
  110.         }
  111.         return $bid;
  112.     }
  113.  
  114.     function delete()
  115.     {
  116.         $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $this->getVar('bid'));
  117.         if ( !$this->db->query($sql) ) {
  118.             return false;
  119.         }
  120.         $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'));
  121.         $this->db->query($sql);
  122.         $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $this->getVar('bid'));
  123.         $this->db->query($sql);
  124.         return true;
  125.     }
  126.  
  127.     /**
  128.     * do stripslashes/htmlspecialchars according to the needed output
  129.     *
  130.     * @param $format      output use: S for Show and E for Edit
  131.     * @param $c_type    type of block content
  132.     * @returns string
  133.     */
  134.     function getContent($format = 'S', $c_type = 'T')
  135.     {
  136.         switch ( $format ) {
  137.         case 'S':
  138.             // check the type of content
  139.             // H : custom HTML block
  140.             // P : custom PHP block
  141.             // S : use text sanitizater (smilies enabled)
  142.             // T : use text sanitizater (smilies disabled)
  143.             if ( $c_type == 'H' ) {
  144.                 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  145.             } elseif ( $c_type == 'P' ) {
  146.                 ob_start();
  147.                 echo eval($this->getVar('content', 'N'));
  148.                     $content = ob_get_contents();
  149.                     ob_end_clean();
  150.                 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
  151.             } elseif ( $c_type == 'S' ) {
  152.                 $myts =& MyTextSanitizer::getInstance();
  153.                 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  154.                 return $myts->displayTarea($content, 1, 1);
  155.             } else {
  156.                 $myts =& MyTextSanitizer::getInstance();
  157.                 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  158.                 return $myts->displayTarea($content, 1, 0);
  159.             }
  160.             break;
  161.         case 'E':
  162.             return $this->getVar('content', 'E');
  163.             break;
  164.         default:
  165.             return $this->getVar('content', 'N');
  166.             break;
  167.         }
  168.     }
  169.  
  170.     function buildBlock()
  171.     {
  172.         global $xoopsConfig, $xoopsOption;
  173.         $block = array();
  174.         // M for module block, S for system block C for Custom
  175.         if ( $this->getVar("block_type") != "C" ) {
  176.             // get block display function
  177.             $show_func = $this->getVar('show_func');
  178.             if ( !$show_func ) {
  179.                 return false;
  180.             }
  181.             // must get lang files b4 execution of the function
  182.             if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) {
  183.                 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) {
  184.                     include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php";
  185.                 } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) {
  186.                     include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php";
  187.                 }
  188.                 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file');
  189.                 $options = explode("|", $this->getVar("options"));
  190.                 if ( function_exists($show_func) ) {
  191.                     // execute the function
  192.                     $block = $show_func($options);
  193.                     if ( !$block ) {
  194.                         return false;
  195.                     }
  196.                 } else {
  197.                     return false;
  198.                 }
  199.             } else {
  200.                 return false;
  201.             }
  202.         } else {
  203.             // it is a custom block, so just return the contents
  204.             $block['content'] = $this->getContent("S",$this->getVar("c_type"));
  205.             if (empty($block['content'])) {
  206.                 return false;
  207.             }
  208.         }
  209.         return $block;
  210.     }
  211.  
  212.     /*
  213.     * Aligns the content of a block
  214.     * If position is 0, content in DB is positioned
  215.     * before the original content
  216.     * If position is 1, content in DB is positioned
  217.     * after the original content
  218.     */
  219.     function buildContent($position,$content="",$contentdb="")
  220.     {
  221.         if ( $position == 0 ) {
  222.             $ret = $contentdb.$content;
  223.         } elseif ( $position == 1 ) {
  224.             $ret = $content.$contentdb;
  225.         }
  226.         return $ret;
  227.     }
  228.  
  229.     function buildTitle($originaltitle, $newtitle="")
  230.     {
  231.         if ($newtitle != "") {
  232.             $ret = $newtitle;
  233.         } else {
  234.             $ret = $originaltitle;
  235.         }
  236.         return $ret;
  237.     }
  238.  
  239.     function isCustom()
  240.     {
  241.         if ( $this->getVar("block_type") == "C" ) {
  242.             return true;
  243.         }
  244.         return false;
  245.     }
  246.  
  247.     /**
  248.     * gets html form for editting block options
  249.     *
  250.     */
  251.     function getOptions()
  252.     {
  253.         global $xoopsConfig;
  254.         if ( $this->getVar("block_type") != "C" ) {
  255.             $edit_func = $this->getVar('edit_func');
  256.             if ( !$edit_func ) {
  257.                 return false;
  258.             }
  259.             if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) {
  260.                 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) {
  261.                     include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php";
  262.                 } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) {
  263.                     include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php";
  264.                 }
  265.                 include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file');
  266.                 $options = explode("|", $this->getVar("options"));
  267.                 $edit_form = $edit_func($options);
  268.                 if ( !$edit_form ) {
  269.                     return false;
  270.                 }
  271.                 return $edit_form;
  272.             } else {
  273.                 return false;
  274.             }
  275.         } else {
  276.             return false;
  277.         }
  278.     }
  279.  
  280.     /**
  281.     * get all the blocks that match the supplied parameters
  282.     * @param $side   0: sideblock - left
  283.     *        1: sideblock - right
  284.     *        2: sideblock - left and right
  285.     *        3: centerblock - left
  286.     *        4: centerblock - right
  287.     *        5: centerblock - center
  288.     *        6: centerblock - left, right, center
  289.     * @param $groupid   groupid (can be an array)
  290.     * @param $visible   0: not visible 1: visible
  291.     * @param $orderby   order of the blocks
  292.     * @returns array of block objects
  293.     */
  294.     function getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby="b.weight,b.bid", $isactive=1)
  295.     {
  296.         $db =& Database::getInstance();
  297.         $ret = array();
  298.         if ( !$asobject ) {
  299.             $sql = "SELECT b.bid ";
  300.         } else {
  301.             $sql = "SELECT b.* ";
  302.         }
  303.         $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";
  304.         if ( is_array($groupid) ) {
  305.             $sql .= " AND (l.gperm_groupid=".$groupid[0]."";
  306.             $size = count($groupid);
  307.             if ( $size  > 1 ) {
  308.                 for ( $i = 1; $i < $size; $i++ ) {
  309.                     $sql .= " OR l.gperm_groupid=".$groupid[$i]."";
  310.                 }
  311.             }
  312.             $sql .= ")";
  313.         } else {
  314.             $sql .= " AND l.gperm_groupid=".$groupid."";
  315.         }
  316.         $sql .= " AND b.isactive=".$isactive;
  317.         if ( isset($side) ) {
  318.             // get both sides in sidebox? (some themes need this)
  319.             if ( $side == XOOPS_SIDEBLOCK_BOTH ) {
  320.                 $side = "(b.side=0 OR b.side=1)";
  321.             } elseif ( $side == XOOPS_CENTERBLOCK_ALL ) {
  322.                 $side = "(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )";
  323.             } else {
  324.                 $side = "b.side=".$side;
  325.             }
  326.             $sql .= " AND ".$side;
  327.         }
  328.         if ( isset($visible) ) {
  329.             $sql .= " AND b.visible=$visible";
  330.         }
  331.         $sql .= " ORDER BY $orderby";
  332.         $result = $db->query($sql);
  333.         $added = array();
  334.         while ( $myrow = $db->fetchArray($result) ) {
  335.             if ( !in_array($myrow['bid'], $added) ) {
  336.                 if (!$asobject) {
  337.                     $ret[] = $myrow['bid'];
  338.                 } else {
  339.                     $ret[] = new XoopsBlock($myrow);
  340.                 }
  341.                 array_push($added, $myrow['bid']);
  342.             }
  343.         }
  344.         //echo $sql;
  345.         return $ret;
  346.     }
  347.  
  348.     function getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1)
  349.     {
  350.         $db =& Database::getInstance();
  351.         $ret = array();
  352.         $where_query = " WHERE isactive=".$isactive;
  353.         if ( isset($side) ) {
  354.             // get both sides in sidebox? (some themes need this)
  355.             if ( $side == 2 ) {
  356.                 $side = "(side=0 OR side=1)";
  357.             } elseif ( $side == 6 ) {
  358.                 $side = "(side=3 OR side=4 OR side=5 OR side=7 OR side=8 OR side=9)";
  359.             } else {
  360.                 $side = "side=".$side;
  361.             }
  362.             $where_query .= " AND ".$side;
  363.         }
  364.         if ( isset($visible) ) {
  365.             $where_query .= " AND visible=$visible";
  366.         }
  367.         $where_query .= " ORDER BY $orderby";
  368.         switch ($rettype) {
  369.         case "object":
  370.             $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
  371.             $result = $db->query($sql);
  372.             while ( $myrow = $db->fetchArray($result) ) {
  373.                 $ret[] = new XoopsBlock($myrow);
  374.             }
  375.             break;
  376.         case "list":
  377.             $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
  378.             $result = $db->query($sql);
  379.             while ( $myrow = $db->fetchArray($result) ) {
  380.                 $block = new XoopsBlock($myrow);
  381.                 $name = ($block->getVar("block_type") != "C") ? $block->getVar("name") : $block->getVar("title");
  382.                 $ret[$block->getVar("bid")] = $name;
  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.         $moduleid = intval($moduleid);
  400.         $db =& Database::getInstance();
  401.         if ( $asobject == true ) {
  402.             $sql = $sql = "SELECT * FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
  403.         } else {
  404.             $sql = "SELECT bid FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
  405.         }
  406.         $result = $db->query($sql);
  407.         $ret = array();
  408.         while( $myrow = $db->fetchArray($result) ) {
  409.             if ( $asobject ) {
  410.                 $ret[] = new XoopsBlock($myrow);
  411.             } else {
  412.                 $ret[] = $myrow['bid'];
  413.             }
  414.         }
  415.         return $ret;
  416.     }
  417.  
  418.     function getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
  419.     {
  420.         $isactive = intval($isactive);
  421.         $db =& Database::getInstance();
  422.         $ret = array();
  423.         $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1";
  424.         if ( is_array($groupid) ) {
  425.             $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).')';
  426.         } else {
  427.             if (intval($groupid) > 0) {
  428.                 $sql .= ' AND gperm_groupid='.intval($groupid);
  429.             }
  430.         }
  431.         $result = $db->query($sql);
  432.         $blockids = array();
  433.         while ( $myrow = $db->fetchArray($result) ) {
  434.             $blockids[] = $myrow['gperm_itemid'];
  435.         }
  436.         if (!empty($blockids)) {
  437.             $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
  438.             $sql .= ' AND b.isactive='.$isactive;
  439.             if (isset($visible)) {
  440.                 $sql .= ' AND b.visible='.intval($visible);
  441.             }
  442.             $module_id = intval($module_id);
  443.             if (!empty($module_id)) {
  444.                 $sql .= ' AND m.module_id IN (0,'.$module_id;
  445.                 if ($toponlyblock) {
  446.                     $sql .= ',-1';
  447.                 }
  448.                 $sql .= ')';
  449.             } else {
  450.                 if ($toponlyblock) {
  451.                     $sql .= ' AND m.module_id IN (0,-1)';
  452.                 } else {
  453.                     $sql .= ' AND m.module_id=0';
  454.                 }
  455.             }
  456.             $sql .= ' AND b.bid IN ('.implode(',', $blockids).')';
  457.             $sql .= ' ORDER BY '.$orderby;
  458.             $result = $db->query($sql);
  459.             while ( $myrow = $db->fetchArray($result) ) {
  460.                 $block =& new XoopsBlock($myrow);
  461.                 $ret[$myrow['bid']] =& $block;
  462.                 unset($block);
  463.             }
  464.         }
  465.         return $ret;
  466.     }
  467.  
  468.     function getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
  469.     {
  470.         $db =& Database::getInstance();
  471.         $ret = array();
  472.         $bids = array();
  473.         $sql = "SELECT DISTINCT(bid) from ".$db->prefix('newblocks');
  474.         if ($result = $db->query($sql)) {
  475.             while ( $myrow = $db->fetchArray($result) ) {
  476.                 $bids[] = $myrow['bid'];
  477.             }
  478.         }
  479.         $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'";
  480.         $grouped = array();
  481.         if ($result = $db->query($sql)) {
  482.             while ( $myrow = $db->fetchArray($result) ) {
  483.                 $grouped[] = $myrow['gperm_itemid'];
  484.             }
  485.         }
  486.         $non_grouped = array_diff($bids, $grouped);
  487.         if (!empty($non_grouped)) {
  488.             $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid';
  489.             $sql .= ' AND b.isactive='.intval($isactive);
  490.             if (isset($visible)) {
  491.                 $sql .= ' AND b.visible='.intval($visible);
  492.             }
  493.             $module_id = intval($module_id);
  494.             if (!empty($module_id)) {
  495.                 $sql .= ' AND m.module_id IN (0,'.$module_id;
  496.                 if ($toponlyblock) {
  497.                     $sql .= ',-1';
  498.                 }
  499.                 $sql .= ')';
  500.             } else {
  501.                 if ($toponlyblock) {
  502.                     $sql .= ' AND m.module_id IN (0,-1)';
  503.                 } else {
  504.                     $sql .= ' AND m.module_id=0';
  505.                 }
  506.             }
  507.             $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')';
  508.             $sql .= ' ORDER BY '.$orderby;
  509.             $result = $db->query($sql);
  510.             while ( $myrow = $db->fetchArray($result) ) {
  511.                 $block =& new XoopsBlock($myrow);
  512.                 $ret[$myrow['bid']] =& $block;
  513.                 unset($block);
  514.             }
  515.         }
  516.         return $ret;
  517.     }
  518.  
  519.     function countSimilarBlocks($moduleId, $funcNum, $showFunc = null)
  520.     {
  521.         $funcNum = intval($funcNum);
  522.         $moduleId = intval($moduleId);
  523.         if ($funcNum < 1 || $moduleId < 1) {
  524.             // invalid query
  525.             return 0;
  526.         }
  527.         $db =& Database::getInstance();
  528.         if (isset($showFunc)) {
  529.             // showFunc is set for more strict comparison
  530.             $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)));
  531.         } else {
  532.             $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum);
  533.         }
  534.         if (!$result = $db->query($sql)) {
  535.             return 0;
  536.         }
  537.         list($count) = $db->fetchRow($result);
  538.         return $count;
  539.     }
  540. }
  541. ?>