<?php

class links_index
{
	var $mydirname;
	var $controller_name;
	var $Links;

	var $order_by;
	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();
	}

	function index($field = NULL, $order = NULL, $offset = 0)
	{
		$this->items($field, $order, $offset);
	}

	/** List Display
	 *
	**/
	function items($field = NULL, $order = NULL, $offset = 0)
	{
		$this->Links->setWhere('status', 0, '>');
		$this->Links->setWhere('date', time(), '<');
		$numrows = $this->Links->getCount();

		$links = array();
		$lang_cursortedby = $order_html = '';
		if ($numrows > 0) {
			$this->_set_navigation($field, $order, $offset, $numrows);

			$this->Links->clearWhere();
			$this->Links->setWhere('l.status', 0, '>');
			$this->Links->setWhere('l.date', time(), '<');
			$add_order = '';
			if (strtolower($field) == 'rating') {
				$add_order  = ',votes ' ;
				$add_order .= (strtoupper($order) == 'DESC') ? 'DESC': 'ASC';
			}
			$this->Links->setOrder($this->order_by . $add_order);

			$links            = $this->Links->getDatas($this->Links->per_page, $offset);
			$lang_cursortedby = sprintf(_MD_CURSORTEDBY, $this->Links->convertOrder($this->order_by));//TODO
			$order_html       = $this->Links->makeOrderHtml($this->controller_name, 'items', $this->order_by);
		}

		$data = array(
			'links'            => $links,
			'lang_cursortedby' => $lang_cursortedby,
			'order_html'       => $order_html,
			'nav'              => $this->nav_html,
			'navinfo'          => $this->navinfo,
			'lang_thereare'    => sprintf(_MD_THEREARE, $numrows),
			'categories'       => $this->Links->Categories->getCategories(),
		);

		$template = $this->Links->getTemplatePrefix() . '_index.html';
		$this->Links->view($template, $data);
	}

	/** List of Links by Submitter
	 *
	**/
	function user($submitter = 0, $field = NULL, $order = NULL, $offset = 0)
	{
		if ($submitter > 0) {
			$this->Links->setWhere('status', 0, '>');
			$this->Links->setWhere('submitter', $submitter, '=');
			$this->Links->setWhere('date', time(), '<');
			$numrows = $this->Links->getCount();

			$links = array();
			$lang_cursortedby = $order_html = '';
			if( $numrows > 0 ){
				$offset = intval($offset);
				$this->_set_navigation($field, $order, $offset, $numrows, $submitter);

				$this->Links->clearWhere();
				$this->Links->setWhere('l.status', 0, '>');
				$this->Links->setWhere('l.submitter', $submitter, '=');
				$this->Links->setWhere('l.date', time(), '<');
				$this->Links->setOrder($this->order_by);

				$links            = $this->Links->getDatas($this->Links->per_page, $offset);
				$lang_cursortedby = sprintf(_MD_CURSORTEDBY, $this->Links->convertOrder($this->order_by));
				$order_html       = $this->Links->makeOrderHtml($this->controller_name, 'user', $this->order_by, $submitter);
			}

			$member_handler =& xoops_gethandler('member');
			$submitter_OBJ =& $member_handler->getUser($submitter);
			if (! is_object($submitter_OBJ)) {
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
				exit();
			} else {
				$submitter_name = $submitter_OBJ->uname();
			}
			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => htmlspecialchars(sprintf(_MD_SUBMITTER_SITE, $submitter_name), ENT_QUOTES, _CHARSET));

			$data = array(
				'submitter_id'      => $submitter,
				'links'             => $links,
				'lang_cursortedby'  => $lang_cursortedby,
				'order_html'        => $order_html,
				'nav'               => $this->nav_html,
				'navinfo'           => $this->navinfo,
				'xoops_breadcrumbs' => $bread,
				'lang_thereare'     => sprintf(_MD_LINKUID_NUM, $submitter_name, $numrows),
			);

			$template = $this->Links->getTemplatePrefix() . '_index.html';
			$this->Links->view($template, $data);

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	/** Single Item Display by LID
	 *
	**/
	function item($lid = 0)
	{
		if (!empty($lid)) {

			if(! $link = $this->Links->getDataByLid($lid, TRUE)){
				$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
				exit();
			}

			if ($this->Links->accessCounterUp($link['lid'])) {
				$link['access']++;
			}

			// Navigation
			$nav_html = $future = '';
			if ($link['date'] < time()) {
				$extra_arg = array('c' => $this->controller_name, 'm' => 'item', 'start_name' => 'lid');
				$all_lid_arr = $this->Links->getAllLid();
				$nav = new links_PageNav(NULL, NULL, $extra_arg, $this->Links->uri_option_arr, $this->mydirname);
				$nav_num = 7;
				$nav_html = $nav->single_renderNav($lid, $all_lid_arr, $nav_num) ;
			} else {
				if (! $this->Links->isadmin) {
					$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
					exit();
				}
				$future = TRUE;
			}

			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => $link['title']);

			$data = array(
				'link'              => $link,
				'nav'               => $nav_html,
				'future'            => $future,
				'xoops_breadcrumbs' => $bread,
				'xoops_pagetitle'   => $this->Links->module_name .' : '. $link['title'],
			);

			$template = $this->Links->getTemplatePrefix() . '_single.html';
			$this->Links->view($template, $data);

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}


	function _set_navigation($field = NULL, $order = NULL, $offset = 0, $numrows = 0, $submitter = 0)
	{
		if(	!empty($field) AND !empty($order)
					AND in_array(strtolower($field), array('title', 'date', 'rating', 'hits', 'access'))
					AND in_array(strtoupper($order), array('ASC', 'DESC'))
				) {
			$field = strtolower($field);
			$order = strtoupper($order);
		} else {
			$field = 'date';
			$order = 'DESC';
		}
		$this->order_by = $field . ' ' . $order;

		if ($offset >= $numrows) $offset = 0;
		if ( $numrows > $this->Links->per_page ) {
			// Navigation
			$extra_arg = array(
					'f'   => $field,
					'ord' => $order,
					'p'   => $offset,
					'c'   => $this->controller_name,
					'm'   => ($submitter > 0 ? 'user': 'items')
				);
			if ($submitter > 0) $extra_arg = array('uid' => $submitter) + $extra_arg;

			$nav = new links_PageNav($numrows, $this->Links->per_page, $extra_arg, $this->Links->uri_option_arr, $this->mydirname);
			$this->nav_html = $nav->renderNav(3) ;

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

}
