<?php
require_once dirname(__FILE__) .'/database.class.php';


class broken extends Links_Database
{
	var $reportid = NULL;
	var $lid      = NULL;
	var $sender   = NULL;
	var $ip       = NULL;
	var $repodate = NULL;
	var $status   = NULL;

	var $_primary_key = NULL;
	var $_class_name  = NULL;
	var $_rules       = NULL;

	/**
	 * Constructor
	 **/
	function __construct($mydirname)
	{
		$this->_class_name  = strtolower(get_class($this));
		$this->_primary_key = 'reportid';
		$this->_set_field_rules();

		parent::__construct($mydirname);
	}

	function getById($reportid)
	{
		return $this->_get_data_by_id($reportid);
	}
	
	function _status_change($reportid, $status = 0)
	{
		//SET data
		$this->reportid = $reportid;
		$this->status   = $status;

		return $this->_save(TRUE);
	}

	function getTotalCount($status = 0)
	{
		$this->setWhere('status', $status, '=');
	//	$this->_set_where(' status='. $status);
		$count = $this->getCount();
		return $count;
	}

	function getBrokens($status = 0, $per_page = NULL, $offset = 0)
	{
		global $xoopsConfig;

		$this->setWhere('status', $status, '=');
	//	$this->_set_where('status='. $status);
		$this->_set_order('repodate DESC');
		$datas = $this->_get_datas($per_page, $offset);

		foreach ($datas as $key => $data) {
			$link = $this->_Links->getDataByLid($data['lid']);
			//���񋟎�
			$uname = $email = "" ;
			if ($data['sender'] > 0) {
				$result = $this->_db->query("SELECT uname, email FROM ". $this->_db->prefix('users') ." WHERE uid=". $data['sender']);
				list($uname, $email) = $this->_db->fetchRow($result);
			} else {
				$uname = htmlspecialchars($xoopsConfig['anonymous'], ENT_QUOTES, _CHARSET);
			}
			//�����N���e��
			$owner = $owneremail = "" ;
			if ($link['submitter'] > 0) {
				$result = $this->_db->query("SELECT uname, email FROM ". $this->_db->prefix('users') ." WHERE uid=". $link['submitter']);
				list($owner, $owneremail) = $this->_db->fetchRow($result);
			} else {
				$owner = htmlspecialchars($xoopsConfig['anonymous'], ENT_QUOTES, _CHARSET);
			}

			$datas[$key]['sender_uname'] = htmlspecialchars($uname, ENT_QUOTES, _CHARSET);
			$datas[$key]['sender_email'] = htmlspecialchars(links_urlCheckReplace($email), ENT_QUOTES, _CHARSET);
			$datas[$key]['ownerid']      = $link['submitter'];
			$datas[$key]['owner_uname']  = htmlspecialchars($owner, ENT_QUOTES, _CHARSET);
			$datas[$key]['owner_email']  = htmlspecialchars(links_urlCheckReplace($owneremail), ENT_QUOTES, _CHARSET);
			$datas[$key]['title']        = $link['title'];
			$datas[$key]['url']          = !empty($link['url']) ? $link['url']: $link['murl'];
		}

		return $datas;
	}

	function deleteReport($reportid)
	{
		return $this->_delete($reportid);
	}

	function deleteLink($lid)
	{
		return $this->_Links->delete($lid);
	}

	function insert($lid)
	{
		//SET data
		$this->lid = $lid;
		$this->ip  = getenv("REMOTE_ADDR");
		$this->repodate = time();
		$this->sender = $this->_Links->uid;

		return $this->_save();
	/*	if ($this->_save()) {
			return TRUE;
		} else {
			return FALSE;
		}*/
	}

	function _set_field_rules()
	{
		$rules = array();
		$rules['reportid'] = 'int';
		$rules['lid']      = 'int';
		$rules['sender']   = 'int';
		$rules['ip']       = 'varchar';
		$rules['repodate'] = 'int';
		$rules['status']   = 'int';
		$this->_rules = $rules;
	}




/*
CREATE TABLE broken (
  reportid int(5) NOT NULL auto_increment,
  lid int(11) unsigned NOT NULL default '0',
  sender int(11) unsigned NOT NULL default '0',
  ip varchar(20) NOT NULL default '',
  repodate int(10) NOT NULL default '0',
  status tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (reportid),
  KEY lid (lid),
  KEY sender (sender),
  KEY ip (ip)
) TYPE=MyISAM;
*/



}