<?php
class links_admin_request
{
	var $mydirname;
	var $controller_name;
	var $Links;
	var $constprefMI;
	var $_db_model;

	var $nav_html = NULL;
	var $navinfo = NULL;

	/** Constructor
	 *
	**/
	function __construct($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		$this->constprefMI = '_MI_'. $this->mydirname;
		$this->_db_model = $this->Links->setDbModel(str_replace('admin_', '', $this->controller_name));
	}

	function index($status = 0, $offset = 0)
	{
		$per_page = 20;

		$total_count = $this->_db_model->getTotalCount($status);

		$data = array();
		if ($total_count > 0) {
			$data = $this->_db_model->getRequests($status, $per_page, $offset);

			if ($total_count > $per_page) {
				$this->_set_navigation($total_count, $per_page, $status, $offset);
			}
		}

		$data = array(
			'requests'         => $data,
			'totalmodrequests' => $total_count,
			'status'           => $status,
			'mod_name'         => htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET),
			'menu_name'        => htmlspecialchars(constant($this->constprefMI .'_ADMENU4'), ENT_QUOTES, _CHARSET),
			'gticket'          => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_admin_request'),
			'nav'              => $this->nav_html,
			'navinfo'          => $this->navinfo,
		);

		$template = $this->mydirname . '_admin_request.html';
		$this->Links->adminView($template, $data);
	}

	function delete()
	{
		$this->Links->basicPostCheck('links_admin_request', FALSE);
		$delete_requestid = ($_POST['delete_requestid']) ? intval($_POST['delete_requestid']): 0;

		if ($this->_db_model->getById($delete_requestid)) {
			if ($this->_db_model->deleteRequest($delete_requestid)) {
				$message = _MD_A_LINKS_CAT_DELETED;
			} else {
				$message = _MD_A_LINKS_CAT_DELETED_ERR;
			}
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=request', 2, $message);
			exit;
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=request', 2, _MD_CANT_GET_DATA);
			exit;
		}
	}


	function change_status($requestid = 0, $current_page_status = 0)
	{
		$broken = $this->_db_model->getById($requestid);

		if ($broken && $broken['status'] == $current_page_status) {
			$new_status = ($current_page_status+1)%2;
			if ($this->_db_model->statusChange($requestid, $new_status) ){
				$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=request&amp;s='. $new_status, 2, _MD_A_STATUS_CHANGE_OK);
				exit;
			} else {
				$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=request&amp;s='. $current_page_status, 2, _MD_A_STATUS_CHANGE_NG);
				exit;
			}
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=request&amp;s='. $current_page_status, 2, _MD_CANT_GET_DATA);
		}
	}

	function _set_navigation($numrows, $per_page, $status = 0, $offset = 0)
	{
		$extra_arg = array(
				's' => $status,
				'p' => $offset,
				'c' => str_replace('admin_', '', $this->controller_name),
				'm' => 'index'
			);
		$options = $this->Links->uri_option_arr;
		$options['url_approach'] = 0;
		$options['admin'] = TRUE;

		$nav = new links_PageNav($numrows, $per_page, $extra_arg, $options, $this->mydirname);
		$this->nav_html = $nav->renderNav(5) ;

		$last = $offset + $per_page ;
		if( $last > $numrows ) $last = $numrows ;
		$this->navinfo = sprintf( _MD_LINKS_NAVINFO , $offset + 1 , $last , $numrows ) ;
	}

}

