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


class Request extends Links_Database
{
	var $requestid       = NULL;
	var $lid             = NULL;
	var $description     = NULL;
	var $modifysubmitter = NULL;
	var $reqdate         = NULL;
	var $ip              = 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 = 'requestid';
		$this->_set_field_rules();

		parent::__construct($mydirname);
	}

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

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

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

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

			$datas[$key]['mod_submitter_uid']   = $data['modifysubmitter'];
			$datas[$key]['mod_submitter_uname'] = htmlspecialchars($uname, ENT_QUOTES, _CHARSET);
			$datas[$key]['mod_submitter_email'] = htmlspecialchars(links_urlCheckReplace($email), ENT_QUOTES, _CHARSET);
			$datas[$key]['link_title']          = $link['title'];
			$datas[$key]['link_url']            = !empty($link['url']) ? $link['url']: $link['murl'];
			$datas[$key]['link_submitter_uid']  = $link['submitter'];
			$datas[$key]['link_submitter_uname'] = htmlspecialchars($submitter_uname, ENT_QUOTES, _CHARSET);
			$datas[$key]['link_submitter_email'] = htmlspecialchars(links_urlCheckReplace($submitter_email), ENT_QUOTES, _CHARSET);
		}

		return $datas;
	}

	function getById($requestid)
	{
		return $this->_get_data_by_id($requestid);
	}

	function statusChange($requestid, $status = 0)
	{
		//SET data
		$this->requestid = $requestid;
		$this->status    = $status;

		return $this->_save(TRUE);
	}

	function insert($lid)
	{
		//Clear
		unset($_POST['requestid']);
		unset($_POST['status']);
		//SET data
		$this->_get_POST_data();
		$this->lid             = $lid;
		$this->ip              = getenv("REMOTE_ADDR");
		$this->reqdate         = time();
		$this->modifysubmitter = $this->_Links->uid;;

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

	function deleteRequest($requestid)
	{
		return $this->_delete($requestid);
	}

	function _set_field_rules()
	{
		$rules = array();
		$rules['requestid']       = 'int';
		$rules['lid']             = 'int';
		$rules['description']     = 'text';
		$rules['modifysubmitter'] = 'int';
		$rules['reqdate']         = 'int';
		$rules['ip']              = 'varchar';
		$rules['status']          = 'int';
		$this->_rules = $rules;
	}
/*
CREATE TABLE request (
  requestid int(11) unsigned NOT NULL auto_increment,
  lid int(11) unsigned NOT NULL default '0',
  description text NOT NULL,
  modifysubmitter int(11) unsigned NOT NULL default '0',
  reqdate int(10) NOT NULL default '0',
  ip varchar(20) NOT NULL default '',
  status tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (requestid)
) TYPE=MyISAM;
*/



}
