$this->setErrors("Could not save block data into database");
return false;
}
if ( empty($bid) ) {
$bid = $this->db->getInsertId();
}
return $bid;
}
function delete()
{
$sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $this->getVar('bid'));
if ( !$this->db->query($sql) ) {
return false;
}
$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'));
$this->db->query($sql);
$sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $this->getVar('bid'));
$this->db->query($sql);
return true;
}
/**
* do stripslashes/htmlspecialchars according to the needed output
*
* @param $format output use: S for Show and E for Edit
* 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)
{
$db =& Database::getInstance();
$ret = array();
if ( !$asobject ) {
$sql = "SELECT b.bid ";
} else {
$sql = "SELECT b.* ";
}
$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";
if ( is_array($groupid) ) {
$sql .= " AND (l.gperm_groupid=".$groupid[0]."";
$size = count($groupid);
if ( $size > 1 ) {
for ( $i = 1; $i < $size; $i++ ) {
$sql .= " OR l.gperm_groupid=".$groupid[$i]."";
}
}
$sql .= ")";
} else {
$sql .= " AND l.gperm_groupid=".$groupid."";
}
$sql .= " AND b.isactive=".$isactive;
if ( isset($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 OR b.side=7 OR b.side=8 OR b.side=9 )";
} else {
$side = "b.side=".$side;
}
$sql .= " AND ".$side;
}
if ( isset($visible) ) {
$sql .= " AND b.visible=$visible";
}
$sql .= " ORDER BY $orderby";
$result = $db->query($sql);
$added = array();
while ( $myrow = $db->fetchArray($result) ) {
if ( !in_array($myrow['bid'], $added) ) {
if (!$asobject) {
$ret[] = $myrow['bid'];
} else {
$ret[] = new XoopsBlock($myrow);
}
array_push($added, $myrow['bid']);
}
}
//echo $sql;
return $ret;
}
function getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1)
{
$db =& Database::getInstance();
$ret = array();
$where_query = " WHERE isactive=".$isactive;
if ( isset($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 OR side=7 OR side=8 OR side=9)";
} else {
$side = "side=".$side;
}
$where_query .= " AND ".$side;
}
if ( isset($visible) ) {
$where_query .= " AND visible=$visible";
}
$where_query .= " ORDER BY $orderby";
switch ($rettype) {
case "object":
$sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
$result = $db->query($sql);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = new XoopsBlock($myrow);
}
break;
case "list":
$sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query;
$sql = "SELECT bid FROM ".$db->prefix("newblocks")."".$where_query;
$result = $db->query($sql);
while ( $myrow = $db->fetchArray($result) ) {
$ret[] = $myrow['bid'];
}
break;
}
//echo $sql;
return $ret;
}
function getByModule($moduleid, $asobject=true)
{
$moduleid = intval($moduleid);
$db =& Database::getInstance();
if ( $asobject == true ) {
$sql = $sql = "SELECT * FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
} else {
$sql = "SELECT bid FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid."";
}
$result = $db->query($sql);
$ret = array();
while( $myrow = $db->fetchArray($result) ) {
if ( $asobject ) {
$ret[] = new XoopsBlock($myrow);
} else {
$ret[] = $myrow['bid'];
}
}
return $ret;
}
function getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1)
{
$isactive = intval($isactive);
$db =& Database::getInstance();
$ret = array();
$sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1";
if ( is_array($groupid) ) {
$sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).')';
} else {
if (intval($groupid) > 0) {
$sql .= ' AND gperm_groupid='.intval($groupid);
}
}
$result = $db->query($sql);
$blockids = array();
while ( $myrow = $db->fetchArray($result) ) {
$blockids[] = $myrow['gperm_itemid'];
}
if (!empty($blockids)) {
$sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$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);
}
$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(',', $blockids).')';
$sql .= ' ORDER BY '.$orderby;
$result = $db->query($sql);
while ( $myrow = $db->fetchArray($result) ) {
$block =& new XoopsBlock($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)
{
$db =& Database::getInstance();
$ret = array();
$bids = array();
$sql = "SELECT DISTINCT(bid) from ".$db->prefix('newblocks');
if ($result = $db->query($sql)) {
while ( $myrow = $db->fetchArray($result) ) {
$bids[] = $myrow['bid'];
}
}
$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'";
$grouped = array();
if ($result = $db->query($sql)) {
while ( $myrow = $db->fetchArray($result) ) {
$grouped[] = $myrow['gperm_itemid'];
}
}
$non_grouped = array_diff($bids, $grouped);
if (!empty($non_grouped)) {
$sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$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 '.$orderby;
$result = $db->query($sql);
while ( $myrow = $db->fetchArray($result) ) {
$block =& new XoopsBlock($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;
}
$db =& Database::getInstance();
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", $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc)));
} else {
$sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum);