<?php
/**
 * Sort.php - This is an inefficient implementation of storing search result in session. Have to rewrite the whole thing.
 * 			  TODO: FILE IS OBSOLETE.
 *
 * @author $Author: dtong $
 * @version $Id: sort.php,v 1.31 2011/04/06 11:47:51 dtong Exp $
 * @copyright Copyright (c) 2009, Tiller Software Co., Ltd.
*/
return;

require_once(APPPATH . 'helpers/osautil_helper.php');
require_once(APPPATH . 'libraries/mapping/display_list.php');

class Sort extends CI_Controller {
	function __construct() {
		parent::__construct();
		echo "search/sort.php - FILE IS OBSOLETE.";
		exit;
	}

	//TODO: Have to rewrite this
	//Mapping specific paging call. This is used when user clicks on one of the page numbers.
	function get($handle, $searchid, $default_col, $image_sort, $paging=FALSE, $current_page=FALSE, $is_sorting=TRUE) {
		//Always use handle_sort for paging here
		$lib = "mapping/Handle_{$handle}";
		$obj = "handle_{$handle}";
		if ( !osa_lib_exists($lib) ) {
			osa_errorlog(__METHOD__ . " - Invalid search handle $handle");
			echo osa_ajaxmsg(lang('gen_internalservererror'));
			exit;
		}

		$this->load->library($lib, array('request'=>$_REQUEST, 'searchid'=>$searchid ));

		$this->$obj->is_sorting  = $is_sorting;
		$this->$obj->default_col = $default_col;
		$this->$obj->image_sort  = $image_sort;
      //This is the page number from 1 to ...
		$this->$obj->paging 	 = $paging;
		$this->$obj->current_page= $current_page;
		$this->$obj->perpage 	 = $this->config->item('cms_max_perpage');//40;

		$this->$obj->process();
	}

	function page($handle, $searchid, $default_col, $image_sort, $paging, $current_page=1) {
		if (is_numeric($paging)) {
			$paging = (int)$paging;
		}
		if (is_numeric($current_page)) {
			$current_page = (int)$current_page;
		}
		//TODO: The logic below is not flexible and should be modified
		//admin skip divid when sort nohandle
		//$skip_key  = array('user', 'course', 'department', 'strand', 'standard', 'benchmark','eq','goal' );
		$skip_key = $this->config->item('cms_mapping_nosearch_sessions');
		if ( !is_array($skip_key) ) {
			$skip_key = array();
		}
		if ( isset($skip_key) && in_array($searchid , $skip_key)==FALSE ) {
			//Always use handle_sort for paging here
		 	$this->get($handle, $searchid, $default_col, $image_sort, $paging, $current_page, FALSE);
		}
		else {
		 	$this->get_nohandle($handle, $searchid, $default_col, $image_sort, $paging, $current_page, FALSE);
		}
	}

	function get_nohandle($handle, $searchid, $default_col, $image_sort, $paging=FALSE, $current_page=FALSE, $is_sorting=TRUE) {
		$this->CI =  get_instance();
   	$this->CI->load->model('sort/searchsort_model');
   	$this->load->helper('form');
		osa_load_lang('admin');
		osa_load_lang('goal');
		$this->load->library('admin/admin_base');

   	$display = $this->CI->searchsort_model->getSort($searchid);
   	$display->default_col = $default_col;
   	$display->image_sort = $image_sort;
   	if ( $paging !== FALSE ) {
   		$display->paging = $paging;
   	}
   	if ( $current_page !== FALSE ) {
   		$display->current_page = $current_page;
   	}
   	/*
   	if ( array_key_exists( $default_col, $display->current_col ) ) {
			if ( $is_sorting ) {
				if ($display->image_sort==0)
					$display->image_sort =1;
				else
					$display->image_sort =0;
			}
		} else {
			$display->current_col= array();
			$display->current_col[$default_col] = 0;
			$display->image_sort  = 0;
			$display->default_col = $default_col;
		}
		*/
        //get template eq and subjectarea
	    $temp = $this->admin_base->getmenu();
	    if (isset($display->index)){
	    	$index = $display->index;
			if ( $index !== FALSE ) {
				$tmp_index = explode('_',$index);
				if ( count($tmp_index) > 1 ) {
					$index = $tmp_index[0];
				}
			}
			$template = $temp[$index][1];
	    }

		if(isset($template) && osa_get_template_value($template,'_filter') != FALSE){
			$listitem = osa_get_template_value($template,'_filter');
			asort($listitem,SORT_NUMERIC);
			$display->listitem	 = $listitem;
		}
		if($searchid != "eq" && $searchid != "goal") {
        	$display->listitem       = $this->admin_base->getlistItem($searchid);
		}
		$display->paging          = $paging;
		$display->current_page    = $current_page;
		$display->closebutton = TRUE;
		$this->CI->searchsort_model->page_top($display);
	 	$display->prepare_page();
	 	if ($searchid != "goal"){
	 		$this->load->view('admin/admin_list_view', array('display'=>$display));
	 	}
	 	else{
	 		$this->load->view('goal/goal_list_view', array('display'=>$display));
	 	}
		$this->CI->searchsort_model->page_bottom($display);
	}

	//TODO: Check this function
	function get_history($handle, $searchid, $default_col, $image_sort, $paging=FALSE, $current_page=FALSE, $is_sorting=TRUE) {
		$this->CI =  get_instance();
   	$this->CI->load->model('sort/searchsort_model');
   	$this->load->helper('form');
   	osa_load_lang('unit');

      $display = $this->CI->searchsort_model->getSort($searchid);
	   $display->default_col = $default_col;
      $display->image_sort = $image_sort;
      if ( $paging !== FALSE ) {
         $display->paging = $paging;
      }
      if ( $current_page !== FALSE ) {
         $display->current_page = $current_page;
      }
/*
   		if ( array_key_exists( $default_col, $display->current_col ) ) {
			if ( $is_sorting ) {
				if ($display->image_sort==0)
					$display->image_sort =1;
				else
					$display->image_sort =0;
			}
		} else {
			$display->current_col= array();
			$display->current_col[$default_col] = 0;
			$display->image_sort  = 0;
			$display->default_col = $default_col;
		}
*/
       // $display->listitem       = $this->admin_base->getlistItem($searchid);
		//$display->paging          = $paging;
		$display->closebutton     = FALSE;
		//$display->current_page    = $current_page;

	  //$this->CI->searchsort_model->page_top($display);
	   $display->allpage = TRUE;
	 	$display->prepare_page();
 	 	$this->load->view('unit/unit_history_view', array('display'=>$display));
	  //$this->CI->searchsort_model->page_bottom($display);
	}

}
?>