NobuNobu
nobun****@users*****
2006年 4月 11日 (火) 17:42:17 JST
Index: xoops2jp/html/kernel/block.php diff -u xoops2jp/html/kernel/block.php:1.2.8.5 xoops2jp/html/kernel/block.php:1.2.8.6 --- xoops2jp/html/kernel/block.php:1.2.8.5 Thu Feb 9 18:09:32 2006 +++ xoops2jp/html/kernel/block.php Tue Apr 11 17:42:17 2006 @@ -1,5 +1,5 @@ <?php -// $Id: block.php,v 1.2.8.5 2006/02/09 09:09:32 minahito Exp $ +// $Id: block.php,v 1.2.8.6 2006/04/11 08:42:17 nobunobu Exp $ // ------------------------------------------------------------------------ // // XOOPS - PHP Content Management System // // Copyright (c) 2000 XOOPS.org // @@ -33,10 +33,14 @@ exit(); } -/** - * @author Kazumi Ono <onoka****@xoops*****> - * @copyright copyright (c) 2000 XOOPS.org - **/ +if (!defined('SHOW_SIDEBLOCK_LEFT')) { + define ('SHOW_SIDEBLOCK_LEFT', 1); + define ('SHOW_SIDEBLOCK_RIGHT', 2); + define ('SHOW_CENTERBLOCK_LEFT', 4); + define ('SHOW_CENTERBLOCK_RIGHT', 8); + define ('SHOW_CENTERBLOCK_CENTER', 16); + define ('SHOW_BLOCK_ALL', 31); +} /** * A block @@ -48,6 +52,7 @@ **/ class XoopsBlock extends XoopsObject { + var $mBlockFlagMapping = array(); /** * constructor @@ -83,11 +88,17 @@ if (is_array($id)) { $this->assignVars($id); } else { - $blkhandler =& xoops_gethandler('block'); - $obj =& $blkhandler->get($id); - $this->assignVars($obj->getVars()); + $this->load($id); } } + $this->mBlockFlagMapping = array( + 0 => false, + SHOW_SIDEBLOCK_LEFT => 0, + SHOW_SIDEBLOCK_RIGHT => 1, + SHOW_CENTERBLOCK_LEFT => 3, + SHOW_CENTERBLOCK_RIGHT => 4, + SHOW_CENTERBLOCK_CENTER => 5 + ); } /** @@ -106,6 +117,11 @@ { switch ( $format ) { case 'S': + // check the type of content + // H : custom HTML block + // P : custom PHP block + // S : use text sanitizater (smilies enabled) + // T : use text sanitizater (smilies disabled) if ( $c_type == 'H' ) { return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N')); } elseif ( $c_type == 'P' ) { @@ -116,10 +132,10 @@ return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content); } elseif ( $c_type == 'S' ) { $myts =& MyTextSanitizer::getInstance(); - return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 0, 1)); + return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 1, 1)); } else { $myts =& MyTextSanitizer::getInstance(); - return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 0, 0)); + return str_replace('{X_SITEURL}', XOOPS_URL.'/', $myts->displayTarea($this->getVar('content', 'N'), 1, 0)); } break; case 'E': @@ -131,7 +147,84 @@ } } - /** + function &buildBlock() + { + $ret = false; + + $block = array(); + // M for module block, S for system block C for Custom + if ( $this->getVar('block_type') != 'C' ) { + // get block display function + $show_func = $this->getVar('show_func'); + if ( !$show_func ) { + return $ret; + } + // must get lang files b4 execution of the function + if ( file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file')) ) { + $root=&XCube_Root::getSingleton(); + $languageManager=$root->getLanguageManager(); + $languageManager->loadBlockLanguage($this->getVar('dirname')); + + require_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file'); + $options = explode('|', $this->getVar('options')); + if ( function_exists($show_func) ) { + // execute the function + $block = $show_func($options); + if ( !$block ) { + return $ret; + } + } else { + return $ret; + } + } else { + return $ret; + } + } else { + // it is a custom block, so just return the contents + $block['content'] = $this->getContent('S',$this->getVar('c_type')); + if (empty($block['content'])) { + return $ret; + } + } + return $block; + } + + /* + * Aligns the content of a block + * If position is 0, content in DB is positioned + * before the original content + * If position is 1, content in DB is positioned + * after the original content + */ + function &buildContent($position,$content="",$contentdb="") + { + if ( $position == 0 ) { + $ret = $contentdb.$content; + } elseif ( $position == 1 ) { + $ret = $content.$contentdb; + } + return $ret; + } + + function &buildTitle($originaltitle, $newtitle="") + { + if ($newtitle != "") { + $ret = $newtitle; + } else { + $ret = $originaltitle; + } + return $ret; + } + + function isCustom() + { + if ( $this->getVar('block_type') == 'C' ) { + return true; + } + return false; + } + +/** * (HTML-) form for setting the options of the block * * @return string HTML for the form, FALSE if not defined for this block @@ -163,6 +256,77 @@ return false; } } + + // Some functions for for backward compatibility + // @deprecated + + function load($id) + { + $handler =& xoops_gethandler('block'); + $obj =& $handler->get($id); + foreach ($obj->vars as $k => $v) { + $this->assignVar($k, $v['value']); + } + } + + function store() + { + $handler =& xoops_gethandler('block'); + if($handler->insert($this)) { + return $this->getVar('bid'); + + } else { + return false; + } + } + + function delete() + { + $handler =& xoops_gethandler('block'); + return $handler->delete($this); + } + function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby="b.weight,b.bid", $isactive=1) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getAllBlocksByGroup($groupid, $asobject, $side, $visible, $orderby, $isactive); + return $ret; + } + function &getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getAllBlocks($rettype, $side, $visible, $orderby, $isactive); + return $ret; + } + function &getByModule($moduleid, $asobject=true) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getByModule($moduleid, $asobject); + return $ret; + } + function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getAllByGroupModule($groupid, $module_id, $toponlyblock, $visible, $orderby, $isactive); + return $ret; + } + function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid') + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getBlocks($groupid, $mid, $blockFlag, $orderby); + return $ret; + } + function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->getNonGroupedBlocks($module_id, $toponlyblock, $visible, $orderby, $isactive); + return $ret; + } + function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) + { + $handler =& xoops_gethandler('block'); + $ret =& $handler->countSimilarBlocks($moduleId, $funcNum, $showFunc); + return $ret; + } } @@ -206,7 +370,7 @@ */ function &createByInfo($info) { - $block=&$this->create(); + $block =& $this->create(); $options=isset($info['options']) ? $info['options'] : null; $edit_func=isset($info['edit_func']) ? $info['edit_func'] : null; @@ -342,7 +506,7 @@ return $ret; } while ($myrow = $this->db->fetchArray($result)) { - $block = new XoopsBlock(); + $block =& $this->create(false); $block->assignVars($myrow); if (!$id_as_key) { $ret[] =& $block; @@ -370,7 +534,7 @@ } while ($row = $this->db->fetchArray($result)) { - $block = new XoopsBlock(); + $block =& $this->create(false); $block->assignVars($row); $ret[] =& $block; @@ -398,5 +562,339 @@ } return $ret; } + + /** + * get all the blocks that match the supplied parameters + * @param $side 0: sideblock - left + * 1: sideblock - right + * 2: sideblock - left and right + * 3: centerblock - left + * 4: centerblock - right + * 5: centerblock - center + * 6: centerblock - left, right, center + * @param $groupid groupid (can be an array) + * @param $visible 0: not visible 1: visible + * @param $orderby order of the blocks + * @returns array of block objects + */ + function &getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby="b.weight,b.bid", $isactive=1) + { + $ret = array(); + if ( !$asobject ) { + $sql = "SELECT b.bid "; + } else { + $sql = "SELECT b.* "; + } + $sql .= "FROM ".$this->db->prefix("newblocks")." b LEFT JOIN ".$this->db->prefix("group_permission")." l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1"; + if ( is_array($groupid) ) { + $sql .= " AND (l.gperm_groupid=".intval($groupid[0]).""; + $size = count($groupid); + if ( $size > 1 ) { + for ( $i = 1; $i < $size; $i++ ) { + $sql .= " OR l.gperm_groupid=".intval($groupid[$i]).""; + } + } + $sql .= ")"; + } else { + $sql .= " AND l.gperm_groupid=".intval($groupid).""; + } + $sql .= " AND b.isactive=".intval($isactive); + if ( isset($side) ) { + $side = intval($side); + // get both sides in sidebox? (some themes need this) + if ( $side == XOOPS_SIDEBLOCK_BOTH ) { + $side = "(b.side=0 OR b.side=1)"; + } elseif ( $side == XOOPS_CENTERBLOCK_ALL ) { + $side = "(b.side=3 OR b.side=4 OR b.side=5)"; + } else { + $side = "b.side=".$side; + } + $sql .= " AND ".$side; + } + if ( isset($visible) ) { + $sql .= " AND b.visible=".intval($visible); + } + $sql .= " ORDER BY ".addslashes($orderby); + $result = $this->db->query($sql); + $added = array(); + while ( $myrow = $this->db->fetchArray($result) ) { + if ( !in_array($myrow['bid'], $added) ) { + if (!$asobject) { + $ret[] = $myrow['bid']; + } else { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[] =& $block; + } + array_push($added, $myrow['bid']); + } + } + return $ret; + } + function &getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1) + { + $ret = array(); + $where_query = " WHERE isactive=".intval($isactive); + if ( isset($side) ) { + $side = intval($side); + // get both sides in sidebox? (some themes need this) + if ( $side == 2 ) { + $side = "(side=0 OR side=1)"; + } elseif ( $side == 6 ) { + $side = "(side=3 OR side=4 OR side=5)"; + } else { + $side = "side=".$side; + } + $where_query .= " AND ".$side; + } + if ( isset($visible) ) { + $visible = intval($visible); + $where_query .= " AND visible=$visible"; + } + $where_query .= " ORDER BY ".addslashes($orderby); + switch ($rettype) { + case "object": + $sql = "SELECT * FROM ".$this->db->prefix("newblocks")."".$where_query; + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[] =& $block; + } + break; + case "list": + $sql = "SELECT * FROM ".$this->db->prefix("newblocks")."".$where_query; + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $name = ($block->getVar("block_type") != "C") ? $block->getVar("name") : $block->getVar("title"); + $ret[$block->getVar("bid")] = $name; + unset($block); + } + break; + case "id": + $sql = "SELECT bid FROM ".$this->db->prefix("newblocks")."".$where_query; + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $ret[] = $myrow['bid']; + } + break; + } + //echo $sql; + return $ret; + } + + function &getByModule($moduleid, $asobject=true) + { + $moduleid = intval($moduleid); + if ( $asobject == true ) { + $sql = $sql = "SELECT * FROM ".$this->db->prefix("newblocks")." WHERE mid=".$moduleid.""; + } else { + $sql = "SELECT bid FROM ".$this->db->prefix("newblocks")." WHERE mid=".$moduleid.""; + } + $result = $this->db->query($sql); + $ret = array(); + while( $myrow = $this->db->fetchArray($result) ) { + if ( $asobject ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[] =& $block; + } else { + $ret[] = $myrow['bid']; + } + } + return $ret; + } + + function &getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) + { + $ret = array(); + $sql = "SELECT DISTINCT gperm_itemid FROM ".$this->db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1"; + if ( is_array($groupid) ) { + $sql .= ' AND gperm_groupid IN ('.addslashes(implode(',', $groupid)).')'; + } else { + if (intval($groupid) > 0) { + $sql .= ' AND gperm_groupid='.intval($groupid); + } + } + $result = $this->db->query($sql); + $blockids = array(); + while ( $myrow = $this->db->fetchArray($result) ) { + $blockids[] = $myrow['gperm_itemid']; + } + if (!empty($blockids)) { + $sql = 'SELECT b.* FROM '.$this->db->prefix('newblocks').' b, '.$this->db->prefix('block_module_link').' m WHERE m.block_id=b.bid'; + $sql .= ' AND b.isactive='.$isactive; + if (isset($visible)) { + $sql .= ' AND b.visible='.intval($visible); + } + if ($module_id !== false) { + $sql .= ' AND m.module_id IN (0,'.intval($module_id); + if ($toponlyblock) { + $sql .= ',-1'; + } + $sql .= ')'; + } else { + if ($toponlyblock) { + $sql .= ' AND m.module_id IN (0,-1)'; + } else { + $sql .= ' AND m.module_id=0'; + } + } + $sql .= ' AND b.bid IN ('.implode(',', $blockids).')'; + $sql .= ' ORDER BY '.$orderby; + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[$myrow['bid']] =& $block; + unset($block); + } + } + return $ret; + } + + /** + * Return block instance array by $groupid, $mid and $blockFlag. + * This function is new function of Cube and used from controller. + * @author minahito + */ + function &getBlocks($groupid, $mid=false, $blockFlag=SHOW_BLOCK_ALL, $orderby='b.weight,b.bid') + { + $root =& XCube_Root::getSingleton(); + $this->db =& $root->mController->getDB(); + + $ret = array(); + $sql = "SELECT DISTINCT gperm_itemid FROM ".$this->db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1"; + if ( is_array($groupid) ) { + $sql .= ' AND gperm_groupid IN ('.addslashes(implode(',', $groupid)).')'; + } else { + if (intval($groupid) > 0) { + $sql .= ' AND gperm_groupid='.intval($groupid); + } + } + $result = $this->db->query($sql); + $blockids = array(); + while ( $myrow = $this->db->fetchArray($result) ) { + $blockids[] = $myrow['gperm_itemid']; + } + if (!empty($blockids)) { + $sql = 'SELECT b.* FROM '.$this->db->prefix('newblocks').' b, '.$this->db->prefix('block_module_link').' m WHERE m.block_id=b.bid'; + $sql .= ' AND b.isactive=1 AND b.visible=1 '; + if ($mid !== false && $mid !== 0) { + $sql .= ' AND m.module_id IN (0,'.intval($mid).')'; + } else { + $sql .= ' AND m.module_id=0'; + } + + // + // SIDE + // + if ($blockFlag != SHOW_BLOCK_ALL) { + $arr = array(); + if ($blockFlag & SHOW_SIDEBLOCK_LEFT) { + $arr[] = "b.side=" . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_LEFT]; + } + if ($blockFlag & SHOW_SIDEBLOCK_RIGHT) { + $arr[] = "b.side=" . $this->mBlockFlagMapping[SHOW_SIDEBLOCK_RIGHT]; + } + if ($blockFlag & SHOW_CENTERBLOCK_LEFT) { + $arr[] = "b.side=" . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_LEFT]; + } + if ($blockFlag & SHOW_CENTERBLOCK_CENTER) { + $arr[] = "b.side=" . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_CENTER]; + } + if ($blockFlag & SHOW_CENTERBLOCK_RIGHT) { + $arr[] = "b.side=" . $this->mBlockFlagMapping[SHOW_CENTERBLOCK_RIGHT]; + } + + $sql .= " AND (" . implode(" OR ", $arr) . ")"; + } + + $sql .= ' AND b.bid IN ('.implode(',', $blockids).')'; + $sql .= ' ORDER BY '.addslashes($orderby); + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[$myrow['bid']] =& $block; + unset($block); + } + } + return $ret; + } + + function &getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) + { + $ret = array(); + $bids = array(); + $sql = "SELECT DISTINCT(bid) from ".$this->db->prefix('newblocks'); + if ($result = $this->db->query($sql)) { + while ( $myrow = $this->db->fetchArray($result) ) { + $bids[] = $myrow['bid']; + } + } + $sql = "SELECT DISTINCT(p.gperm_itemid) from ".$this->db->prefix('group_permission')." p, ".$this->db->prefix('groups')." g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'"; + $grouped = array(); + if ($result = $this->db->query($sql)) { + while ( $myrow = $this->db->fetchArray($result) ) { + $grouped[] = $myrow['gperm_itemid']; + } + } + $non_grouped = array_diff($bids, $grouped); + if (!empty($non_grouped)) { + $sql = 'SELECT b.* FROM '.$this->db->prefix('newblocks').' b, '.$this->db->prefix('block_module_link').' m WHERE m.block_id=b.bid'; + $sql .= ' AND b.isactive='.intval($isactive); + if (isset($visible)) { + $sql .= ' AND b.visible='.intval($visible); + } + $module_id = intval($module_id); + if (!empty($module_id)) { + $sql .= ' AND m.module_id IN (0,'.$module_id; + if ($toponlyblock) { + $sql .= ',-1'; + } + $sql .= ')'; + } else { + if ($toponlyblock) { + $sql .= ' AND m.module_id IN (0,-1)'; + } else { + $sql .= ' AND m.module_id=0'; + } + } + $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')'; + $sql .= ' ORDER BY '.addslashes($orderby); + $result = $this->db->query($sql); + while ( $myrow = $this->db->fetchArray($result) ) { + $block =& $this->create(false); + $block->assignVars($myrow); + $ret[$myrow['bid']] =& $block; + unset($block); + } + } + return $ret; + } + + function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) + { + $funcNum = intval($funcNum); + $moduleId = intval($moduleId); + if ($funcNum < 1 || $moduleId < 1) { + // invalid query + return 0; + } + if (isset($showFunc)) { + // showFunc is set for more strict comparison + $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s", $this->db->prefix('newblocks'), $moduleId, $funcNum, $this->db->quoteString(trim($showFunc))); + } else { + $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $this->db->prefix('newblocks'), $moduleId, $funcNum); + } + if (!$result = $this->db->query($sql)) { + return 0; + } + list($count) = $this->db->fetchRow($result); + return $count; + } } ?> \ No newline at end of file