<?php
//  ------------------------------------------------------------------------ //
//                   This module; Copyright (c) 2005 gusagi                  //
//                        <http://good-tea.ddo.jp/dev>                       //
//  ------------------------------------------------------------------------ //

//*****************************************************************************/ 
// 直接呼び出された場合はexitする
//*****************************************************************************/ 
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;

if( ! class_exists( 'CssadminBlocks' ) ) {

class CssadminBlocks {

	var $blocks = array();
	var $del_blocks = array();

	/**
	 * CssadminModules
	 * 
	 * @param void
	 * @return void
	 */
	function CssadminModules() {
	}

	/**
	 * setBlockList
	 * 
	 * @param $mid
	 * @return void
	 */
	function setBlockList( $mid ) {
		// 初期処理
		$mid = intval( $mid );
		$xoopsDB =& Database::getInstance();
		$sql = '';
		$newblocks = $xoopsDB->prefix( 'newblocks' );
		include_once( XOOPS_ROOT_PATH.'/class/xoopsblock.php' ) ;
		// ブロック情報の取得
		$sql = "SELECT * FROM $newblocks";
		$sql .= " WHERE mid = $mid ORDER BY visible DESC, side, weight";
		$result = $xoopsDB->query( $sql );
		while( $myrow = $xoopsDB->fetchArray($result) ) {
			$block = new XoopsBlock( $myrow );
			$this->blocks[$block->getVar("bid")] = array(
				'bid' => $block->getVar("bid"), 
				'mid' => $block->getVar("mid"), 
				'title' => $block->getVar("title"), 
				'css' => '', 
				'current' => '', 
				'selected' => ''
			);
		}
	}

	/**
	 * getBlocks
	 * 
	 * @param void
	 * @return void
	 */
	function getBlocks() {
		return $this->blocks;
	}


	function setBlockDirCss( $dirname = null ) {
		$blocklist = $this->blocks;
		foreach( $blocklist as $key => $block ) {
			if ( ! empty($dirname) ) {
				$css_path = XOOPS_ROOT_PATH .'/modules/' .$dirname .'/css/';
			} else {
				$css_path = XOOPS_ROOT_PATH .'/css/';
			}
			if ( file_exists($css_path) && is_dir($css_path) ) {
				$handle = opendir( $css_path );
				while (false !== ($file = readdir($handle))) {
					if ( is_file($css_path .$file) && !preg_match("/^\..*$/",$file) ) {
						$this->blocks[$key]['css'][] = basename( $file );
						if ( !empty($this->blocks[$key]['current']) && 
							$this->blocks[$key]['current'] === basename( $file ) ) {
							$this->blocks[$key]['selected'] = basename( $file );
						}
					}
				}
				closedir($handle);
			}
		}
	}


	function setBlockDbCss( $mid ) {
		$mid = intval( $mid );
		$xoopsDB =& Database::getInstance();
		$cssadmin_blocks = $xoopsDB->prefix( 'cssadmin_blocks' );
		$newblocks = $xoopsDB->prefix( 'newblocks' );
		$sql = "SELECT cssadmin_blocks.bid, cssadmin_blocks.css FROM $cssadmin_blocks cssadmin_blocks";
		$sql .= " LEFT JOIN $newblocks newblocks ON newblocks.bid = cssadmin_blocks.bid";
		$sql .= " WHERE cssadmin_blocks.mid = $mid";
		$result = $xoopsDB->query( $sql );
		if ( $myrow = $xoopsDB->fetchArray($result) ) {
			do {
				$bid = intval( $myrow['bid'] );
				$this->blocks[$bid]['current'] = htmlSpecialChars( $myrow['css'], ENT_QUOTES );
			} while ( $myrow = $xoopsDB->fetchArray($result) );
		}
	}

	/**
	 * setDelBlocks
	 * 
	 * @param void
	 * @return void
	 */
	function setDelBlocks() {
		// 初期処理
		$xoopsDB =& Database::getInstance();
		$cssadmin_blocks = $xoopsDB->prefix( 'cssadmin_blocks' );
		$newblocks = $xoopsDB->prefix( 'newblocks' );
		$bid_list = array();
		$bids = '';
		// 存在している全ブロックのIDを取得
		$sql = "SELECT bid FROM $newblocks";
		$result = $xoopsDB->query( $sql );
		if ( $myrow = $xoopsDB->fetchArray($result) ) {
			do {
				$bid_list[] = intval($myrow['bid']);
			} while ( $myrow = $xoopsDB->fetchArray($result) );
		}
		// ブロックCSS設定に、存在していないブロックの情報が含まれていないか確認
		if ( ! empty($bid_list) ) {
			$bids = implode( ',', $bid_list );
			$sql = "SELECT bid FROM $cssadmin_blocks";
			$sql .= " WHERE bid NOT IN ( $bids )";
			$result = $xoopsDB->query( $sql );
			if ( $myrow = $xoopsDB->fetchArray($result) ) {
				do {
					$bid = intval($myrow['bid']);
					$this->del_blocks[$bid] = $bid;
				} while ( $myrow = $xoopsDB->fetchArray($result) );
			}
		}
	}

	/**
	 * getDelBlocks
	 * 
	 * @param void
	 * @return void
	 */
	function getDelBlocks() {
		return $this->del_blocks;
	}

// end of class
}

$CssadminBlocks = new CssadminBlocks();

}

?>