<?php
// $Id: table_modify.php,v 1.4 2005/10/28 11:56:02 ohwada Exp $

// 2005-09-27 K.OHWADA
// BUG 3031: timeout occurs if many waiting links
// add function getMidArrayNew() getMidArrayMod()

// 2005-01-20 K.OHWADA
// add zip, state, cityAaddr2

// 2004-11-28 K.OHWADA
// add table_link_base

// 2004-11-20 K.OHWADA
// bug fix: dont save user comment 

// 2004-10-20 K.OHWADA
// recommend site, mutual site

// 2004/08/10 K.OHWADA
// enable to install this module two or more. 
// some functions move form weblinksLink

//=========================================================
// class table_modify
// require class/link.php
// 2004/01/14 K.OHWADA
//=========================================================

class table_modify extends table_link_base
{
// tablefield
	var $mid;
	var $mode;
	var $muid;

// local

//---------------------------------------------------------
// constructor
//---------------------------------------------------------
function table_modify()
{
	table_link_base::table_link_base( 'modify' );
}

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

	return $instance;
}

//---------------------------------------------------------
// insert
//---------------------------------------------------------
// for addLinkLink()
// mode: 0=new, 1=modify
function insert($mode=0)
{
	$muid   = $this->xoops_user_uid;
	$passwd = $this->make_passwd();
	$search = $this->make_search();
	$cids   = $this->_make_cids();

	$time = time();
	$time_create = $time;
	$time_update = $time;

	$sql  = "INSERT INTO $this->table (";

	$sql .= "mode, muid, lid, cids, ";
	$sql .= "uid, ";
	$sql .= "title, url, ";
	$sql .= "name, nameflag, ";
	$sql .= "mail, mailflag, ";
	$sql .= "company, ";
	$sql .= "zip, state, city, ";
	$sql .= "addr, addr2, ";
	$sql .= "tel, fax, ";
	$sql .= "description, ";
	$sql .= "usercomment, ";
	$sql .= "admincomment, ";
	$sql .= "rss_url, rss_flag, ";
	$sql .= "recommend, mutual, ";
	$sql .= "passwd, ";
	$sql .= "search, ";
	$sql .= "banner, ";
	$sql .= "time_create, time_update ";

	$sql .= ") VALUES (";

	$sql .= "$mode, $muid, $this->lid, '$cids', ";
	$sql .= "$this->uid, ";
	$sql .= "'$this->title', '$this->url', ";
	$sql .= "'$this->name', $this->nameflag, ";
	$sql .= "'$this->mail', $this->mailflag, ";
	$sql .= "'$this->company', ";
	$sql .= "'$this->zip', '$this->state', '$this->city',";
	$sql .= "'$this->addr', '$this->addr2', ";
	$sql .= "'$this->tel', '$this->fax', ";
	$sql .= "'$this->description', ";
	$sql .= "'$this->usercomment', ";
	$sql .= "'$this->admincomment', ";
	$sql .= "'$this->rss_url', $this->rss_flag, ";
	$sql .= "$this->recommend, $this->mutual, ";
	$sql .= "'$passwd', ";
	$sql .= "'$this->search', ";
	$sql .= "'$this->banner', ";
	$sql .= "$time_create, $time_update ";

	$sql .= ")";

	return $this->query_insert($sql);
}

function _make_cids()
{
	return $this->convert_array_to_list($this->cid_arr, '&');
}

//---------------------------------------------------------
// delete
//---------------------------------------------------------
//for admin/modify.php
function delete($mid)
{
	$sql = "DELETE FROM $this->table WHERE mid= $mid";
	return $this->query_false($sql);
}

//---------------------------------------------------------
// select
//---------------------------------------------------------
// for admin/index.php
function getNumNew()
{
	$sql = "SELECT count(*) FROM $this->table WHERE mode=0";
    return $this->get_num_by_sql($sql);
}

// for admin/index.php
function getNumMod()
{
	$sql = "SELECT count(*) FROM $this->table WHERE mode=1";
    return $this->get_num_by_sql($sql);
}

// admin/modify.php
function getRowByMid($mid)
{
	$sql = "SELECT * FROM $this->table WHERE mid=$mid";

// change row_array to row
	return $this->get_row_by_sql($sql);
}

// admin/modify.php
// BUG 3031: timeout occurs if many waiting links
function getMidArrayNew($limit=0, $offset=0)
{
	$sql = "SELECT mid FROM $this->table WHERE mode=0 ORDER BY mid";
	return $this->get_first_row_by_sql($sql, $limit, $offset);
}

// admin/modify.php
// BUG 3031: timeout occurs if many waiting links
function getMidArrayMod($limit=0, $offset=0)
{
	$sql = "SELECT mid FROM $this->table WHERE mode=1 ORDER BY mid";
	return $this->get_first_row_by_sql($sql, $limit, $offset);
}

// not use
function getRowsNew()
{
	$sql = "SELECT * FROM $this->table WHERE mode=0";
	return $this->get_row_array_by_sql($sql);
}

// not use
function getRowsMod()
{
	$sql = "SELECT * FROM $this->table WHERE mode=1";
	return $this->get_row_array_by_sql($sql);
}

//---------------------------------------------------------
// modify link
// for modlink.php
//---------------------------------------------------------
function modReqLink( $post )
{
	$this->clear_items(); 
	$this->set_items( $post );
	$this->make_tbox_data_4_save();

	return $this->insert(1);
}

//---------------------------------------------------------
// appove new link
// for admin/modify.php
//---------------------------------------------------------
function makeEditDisplayAdmin($arr_rec='')
{
	if (!empty($arr_rec))  $this->make_field_value($arr_rec);

	$this->make_name_mail_edit_for_owner();

	$arr_1 = $this->make_data_4_edit();
	$arr_2 = $this->make_data_4_edit_admin();
	$arr_out = array_merge($arr_1, $arr_2);

	$arr_out['mid']     = $this->mid;
	$arr_out['muid']    = $this->muid;
	$arr_out['cid_arr'] = $this->_get_cid_array();

	return $arr_out;

}

function _get_cid_array()
{
	return $this->convert_list_to_array($this->cids, '&');
}

//---------------------------------------------------------
}
?>
