<?php
// $Id: config_store_handler.php,v 1.8 2006/10/07 04:56:50 ohwada Exp $

// 2006-10-05 K.OHWADA
// add renew_by_contry_code()

// 2006-09-20 K.OHWADA
// use XoopsGTicket
// add set_form_name_config()

// 2006-07-10 K.OHWADA
// this is new file
// porting from weblinks_config_store_handler

//================================================================
// Happy Linux Framework Module
// this file contain 2 class
//   happy_linux_config_form
//   happy_linux_config_store_handler
// 2006-07-10 K.OHWADA
//================================================================

//=========================================================
// class happy_linux_config_form
//=========================================================
class happy_linux_config_form extends happy_linux_form_lib
{
// set by childen class
	var $_config_define_handler;

// class
	var $_post;

// constant
	var $_FORM_NAME_CONFIG = 'config';
	var $_CC_MARK = '<span style="color:#0000ff;">#</span>';

// set parameter
	var $_button_extend = '';

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function happy_linux_config_form()
{
	$this->happy_linux_form();
	$this->set_form_name( $this->_FORM_NAME_CONFIG );

	$this->_config_define_handler =& happy_linux_config_define_handler::getInstance();
	$this->_post =& happy_linux_post::getInstance();

}

function set_config_handler($name, $dirname, $prefix)
{
	$this->_config_define_handler->set_config_handler($name, $dirname, $prefix);
}

function set_config_define( &$class )
{
	$this->_config_define_handler->set_config_define($class);
}

//---------------------------------------------------------
// main function
//---------------------------------------------------------
function get_post_get_op()
{
	$op = $this->_post->get_post_get('op');
	return $op;
}

function show_by_catid( $catid, $title='' )
{
	$config_arr =& $this->get_by_catid($catid);

	$form_name = $this->_FORM_NAME_CONFIG.'_'.$catid;
	$this->set_form_name( $form_name );

	if ($title)
	{
		$this->set_form_title($title);
	}

	$this->show( $config_arr );
}

function show($config_arr)
{
	if ( !is_array($config_arr) || empty($config_arr) )  return;

// form start
	echo $this->build_form_begin();
	echo $this->build_token();
	echo $this->build_html_input_hidden('op', 'save');
	echo $this->build_form_table_begin();
	echo $this->build_form_table_title();

// list from config array
	foreach ($config_arr as $id => $config) 
	{
		$title = $this->build_conf_caption( $config );
		$ele   = $this->build_conf_element( $config );
		$ele  .= $this->build_conf_hidden( $id );

		echo $this->build_form_table_line($title, $ele);
	}

	$button = $this->build_form_submit();

	if ($this->_button_extend)
	{
		$button .= $this->_button_extend;
		$this->_button_extend = '';	// clear by myself
	}

	echo $this->build_form_table_line('', $button, 'foot', 'foot');
	echo $this->build_form_table_end();
	echo $this->build_form_end();

// form end

}

//---------------------------------------------------------
// make config element
//---------------------------------------------------------
function build_conf_caption( $config )
{
	$cap = $this->build_conf_caption_ccflag($config['title'], $config['description'], $config['cc_flag']);
	return $cap;
}

function build_conf_caption_ccflag( $title, $desc, $cc_flag=false)
{
	$cap = $this->build_form_caption($title, $desc);
	if ( $cc_flag )
	{
		$cap = $this->_CC_MARK.' '.$cap;
	}
	return $cap;
}

function build_conf_element( $config )
{
	$formtype  = $config['formtype'];
	$valuetype = $config['valuetype'];
	$name      = $config['name'];
	$value     = $config['value'];
	$options   = $config['options'];
	$value     = $config['value'];
	$value_s   = $this->sanitize_text( $value );

	switch ( $formtype ) 
	{
		case 'textarea':
			$ele = $this->build_conf_textarea( $name, $value, $valuetype );
			break;

		case 'select':
			$ele = $this->build_html_select( $name, $value, $options );
			break;

		case 'select_multi':
			$ele = $this->build_html_select_multiple( $name, $value, $options );
			break;

		case 'radio':
		case 'radio_select':
			$ele = $this->build_html_input_radio_select( $name, $value, $options );
			break;

		case 'yesno':
		case 'radio_yesno':
			$ele = $this->build_form_radio_yesno( $name, $value );
			break;

		case 'yesno_check':
		case 'checkbox_yesno':
			$ele = $this->build_form_radio_yesno( $name, $value_s );
			break;

		case 'label':
			$ele = $value_s;
			break;

		case 'text_image':
			$ele = $this->build_conf_text_image( $name, $value );
			break;

		case 'label_image':
			$ele = $this->build_conf_label_image( $name, $value );
			break;

//		case 'group':
//			$ele = $this->build_conf_group( $config );
//			break;

//		case 'group_multi':
//			$ele = $this->build_conf_group_multi( $config );
//			break;

//		case 'user':
//			$ele = $this->build_conf_user( $config );
//			break;

//		case 'user_multi':
//			$ele = $this->build_conf_user_multi( $config );
//			break;

//		case 'password':
//			$ele = $this->build_conf_password( $config );
//			break;

		case 'textbox':
		default:
			$ele   = $this->build_html_input_text( $name, $value_s );
			break;
	}

	return $ele;
}


//---------------------------------------------------------
// make config form
//---------------------------------------------------------
function build_conf_textarea( $name, $value, $valuetype )
{
	if ($valuetype == 'array') 
	{
		if ($value != '')
		{
			$value_s = $this->sanitize_textarea( implode('|', $value ) );
			$text    = $this->build_html_textarea( $name, $value_s, 5, 50 );
		}
		else
		{
			$text = $this->build_html_textarea($name, '', 5, 50);
		}
	}
	else 
	{
		$value_s = $this->sanitize_textarea( $value );
		$text    = $this->build_html_textarea($name, $value_s, 5, 50);
	}

	return $text;
}

function build_conf_text_image( $name, $value, $size=50, $maxlength=255, $width=0, $height=0, $border=0, $alt='image')
{
	$text   = $this->build_html_input_text($name, $value, $size);
	$text  .= "<br /><br />\n";
	$text  .= $this->build_html_img_tag($value, $width, $height, $border, $alt);
	return $text;
}

function build_conf_label_image( $name, $value, $width=0, $height=0, $border=0, $alt='image')
{
	$text   = $value;
	$text  .= "<br /><br />\n";
	$text  .= $this->build_html_img_tag($value, $width, $height, $border, $alt);
	return $text;
}

function build_conf_textbox( $name, $value, $size=50, $maxlength=255)
{
	$value = $this->sanitize_text( $value );
	$text  = $this->build_html_input_text($name, $value, $size);
	return $text;
}

function build_conf_label( $value )
{
	$text = $this->sanitize_text( $value );
	return $text;
}

function build_conf_select( $name, $value, $options, $none=0, $none_name='---', $none_value='')
{
	$text    = $this->build_html_select($name, $value, $options, $none, $none_name, $none_value);
	return $text;
}

function build_conf_select_multi( $name, $value, $options, $none=0, $none_name='---', $none_value='')
{
	$text = $this->build_html_select_multiple($name, $value, $options, $none, $none_name, $none_value);
	return $text;
}

function build_conf_radio_select( $name, $value, $options, $del='')
{
	$text = $this->build_html_input_radio_select($name, $value, $options, $del);
	return $text;
}

function build_conf_radio_yesno( $name, $value )
{
	$text = $this->build_form_radio_yesno($name, $value);
	return $text;
}

function build_conf_checkbox_yesno( $name, $value )
{
	$text = $this->build_form_radio_yesno($name, $value);
	return $text;
}

function build_conf_hidden($id)
{
	$text = $this->build_html_input_hidden('conf_ids[]', $id);
	return $text;
}

//---------------------------------------------------------
// make config by name
//---------------------------------------------------------
function build_conf_caption_by_name($name)
{
	if ( empty($name) )
	{
		return '&nbsp;';
	}

	$title   = $this->get_by_name($name, 'title');
	$desc    = $this->get_by_name($name, 'description');
	$cc_flag = $this->get_by_name($name, 'cc_flag');
	$cap     = $this->build_conf_caption_ccflag( $title, $desc, $cc_flag);
	return $cap;
}

function build_conf_textbox_by_name($name, $size=5)
{
	if ( empty($name) )
	{
		return '&nbsp;';
	}

	$id      = $this->get_by_name($name, 'conf_id');
	$value   = $this->get_by_name($name, 'value');
	$value_s = $this->sanitize_text( $value );

	$text    = $this->build_html_input_text( $name, $value_s, $size );
	$text   .= $this->build_conf_hidden($id);
	return $text;
}

function build_conf_radio_yesno_by_name($name)
{
	if ( empty($name) )
	{
		return '&nbsp;';
	}

	$id      = $this->get_by_name($name, 'conf_id');
	$value   = $this->get_by_name($name, 'value');
	$value_s = $this->sanitize_text( $value );

	$text    = $this->build_form_radio_yesno( $name, $value_s );
	$text   .= $this->build_conf_hidden($id);
	return $text;
}

function build_conf_checkbox_by_name($name)
{
	if ( empty($name) )
	{
		return '&nbsp;';
	}

	$id      = $this->get_by_name($name, 'conf_id');
	$value   = $this->get_by_name($name, 'value');
	$value_s = $this->sanitize_text( $value );

	$text    = $this->build_form_checkbox_yesno( $name, $value_s );
	$text   .= $this->build_conf_hidden($id);
	return $text;
}

function build_conf_radio_select_by_name($name)
{
	if ( empty($name) )
	{
		return '&nbsp;';
	}

	$id      = $this->get_by_name($name, 'conf_id');
	$value   = $this->get_by_name($name, 'value');
	$options = $this->get_by_name($name, 'options');

	$text  = $this->build_html_input_radio_select( $name, $value, $options, '<br />' );
	$text .= $this->build_conf_hidden($id);
	return $text;
}

//---------------------------------------------------------
// config_define_handler
//---------------------------------------------------------
function load()
{
	$this->_config_define_handler->load();
}

function get_by_confid($id, $key)
{
	$val = $this->_config_define_handler->get_by_itemid($id, $key);
	return $val;
}

function get_by_name($name, $key)
{
	$val = $this->_config_define_handler->get_by_name($name, $key);
	return $val;
}

function &get_by_catid( $catid )
{
	$arr =& $this->_config_define_handler->get_caches_by_catid( $catid );
	return $arr;
}

//---------------------------------------------------------
// set parameter
//---------------------------------------------------------
function set_form_name_config( $value )
{
	$this->_FORM_NAME_CONFIG = $value;
}

function set_button_extend( $value )
{
	$this->_button_extend = $value;
}

function set_submit_button_extend( $name, $value )
{
	$button = $this->build_form_submit($name, $value);
	$this->set_button_extend( $button );
}

// --- class end ---
}


//================================================================
// class happy_linux_config_store_handler
//================================================================
class happy_linux_config_store_handler extends happy_linux_error
{
// set by chieldren class
	var $_handler;
	var $_define;

// class
	var $_post;

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function happy_linux_config_store_handler()
{
	$this->happy_linux_error();

	$this->_post =& happy_linux_post::getInstance();
}

function &getInstance()
{
	static $instance;
	if (!isset($instance)) 
	{
		$instance = new happy_linux_config_store_handler();
	}

	return $instance;
}

function set_handler($name, $dirname, $prefix)
{
	$this->_handler =& happy_linux_get_handler($name, $dirname, $prefix);
}

function set_define( &$class )
{
	$this->_define =& $class;
}

//---------------------------------------------------------
// load config
//---------------------------------------------------------
function load()
{
	$this->_handler->load();
}

//---------------------------------------------------------
// init config
//---------------------------------------------------------
function init()
{
	$this->_clear_errors();

	$define_arr = $this->_define->get_define();

// list from Define
	foreach ($define_arr as $id => $def) 
	{
		$name      = $def['name'];
		$valuetype = $def['valuetype'];
		$value     = $def['default'];

		$obj =& $this->_handler->create();
		$obj->set('conf_id',        $id );
		$obj->set('conf_name',      $name);
		$obj->set('conf_valuetype', $valuetype);
		$obj->setConfValueForInput($value);

		$ret = $this->_handler->insert($obj);
		if ( !$ret )
		{
			$this->_set_errors( $this->_handler->getErrors() );
		}

		unset($obj);
	}

	return $this->returnExistError();
}

//---------------------------------------------------------
// upgrade config
//---------------------------------------------------------
function upgrade()
{
	$this->_clear_errors();

	$define_arr = $this->_define->get_define();

// list from Define
	foreach ($define_arr as $id => $def) 
	{
		$obj =& $this->_handler->get_by_confid($id);
		if ( is_object($obj) )  continue;

// insert, when not in MySQL
		$name      = $def['name'];
		$valuetype = $def['valuetype'];
		$value     = $def['default'];

		$obj =& $this->_handler->create();
		$obj->set('conf_id',        $id );
		$obj->set('conf_name',      $name);
		$obj->set('conf_valuetype', $valuetype);
		$obj->setConfValueForInput($value);

		$ret = $this->_handler->insert($obj);
		if ( !$ret )
		{
			$this->_set_errors( $this->_handler->getErrors() );
		}

		unset($obj);
	}

	return $this->returnExistError();
}

function check_exist_by_name($name)
{
	$arr = $this->_handler->get_cache_by_name($name);
	if ( is_array($arr) && count($arr) )
	{
		return true;
	}
	return false;
}

//---------------------------------------------------------
// save config
//---------------------------------------------------------
function save()
{
	$this->_clear_errors();

	$confid_arr = $this->_post->get_post('conf_ids');

	$count = count($confid_arr);
	if ($count <= 0)  return true;	// no actuion

// list from POST 
	for ( $i=0; $i<$count; $i++ ) 
	{
		$id = $confid_arr[$i];

		$obj =& $this->_handler->get_by_confid($id);
		if ( !is_object($obj) )  continue;

		$name        = $obj->get('conf_name');
		$val_current = $obj->get('conf_value');
		$value       = $this->_post->get_post($name);

// update
		if ( is_array($value) || ($value != $val_current) )
		{
			$obj->setConfValueForInput($value);

			$ret = $this->_handler->update($obj);
			if ( !$ret )
			{
				$this->_set_errors( $this->_handler->getErrors() );
			}
		}

		unset($obj);
	}

	return $this->returnExistError();
}

//---------------------------------------------------------
// renew config by country code
//---------------------------------------------------------
function renew_by_country_code()
{
	$this->_clear_errors();

	$define_arr = $this->_define->get_define();

// list from Define
	foreach ($define_arr as $id => $def) 
	{
		$name      = $def['name'];
		$valuetype = $def['valuetype'];

		if ( isset($def['cc_flag']) && $def['cc_flag'])
		{
			$flag  = $def['cc_flag'];
			$value = $def['cc_value'];
		}
		else
		{	continue;	}

		$obj =& $this->_handler->get_by_confid($id);
		if ( !is_object($obj) )  continue;

		$obj->set('conf_id',        $id );
		$obj->set('conf_name',      $name);
		$obj->set('conf_valuetype', $valuetype);
		$obj->setConfValueForInput($value);

		$ret = $this->_handler->update($obj);
		if ( !$ret )
		{
			$this->_set_errors( $this->_handler->getErrors() );
		}

		unset($obj);
	}

	return $this->returnExistError();
}

//---------------------------------------------------------
// config_handler
//---------------------------------------------------------
function existsTable()
{
	$ret = $this->_handler->existsTable();
	return $ret;
}

function getCount()
{
	$count = $this->_handler->getCount();
	return $count;
}

function create_table()
{
	$ret = $this->_handler->create_table();
	if ( !$ret )
	{
		$this->_set_errors( $this->_handler->getErrors() );
	}

	return $ret;
}

function update_by_name($name, $value)
{
	$ret = $this->_handler->update_by_name($name, $value);
	return $ret;
}

// --- class end ---
}

?>