<?

require_once('class.stdModule.php');
require_once('class.contentcore.php');

class extranet extends stdModule {

	var $file_types = array();

	function extranet() {
		global $globalSettings;
		$this->stdModule('ifex0200');

		$this->_variables->private->options	= array('CH1'=>'Check 1', 'CH2'=> 'Check 2','CH3'=>'Check 3');

		$this->_cc = new interChange_ContentCore('TICH0105', $this->_variables->public->org_id, $this->_variables->public->lng_code, $globalSettings->general->project->code);

		if (is_numeric($_SESSION['session_usr_id'])) {
			$this->get_user_groups($_SESSION['session_usr_id']);
		}

	}

	function getCategories() {
		$sql = "SELECT * FROM tbl_ich_trc_categories 
				WHERE cat_org_id = ".$this->_variables->public->org_id."
				AND cat_lng_code = '".$this->_variables->public->lng_code."'
				ORDER BY cat_title";

		$cats = array();
		$rs = $this->_db->query($sql);
		while ($r = $rs->row()) {
			$rights = $this->_cc->getAuthorizations('tbl_ich_trc_categories', $r['CAT_ID']);
			if (is_numeric($rights) && $rights > 0) {
				$cats[] = $r + array('rights'=> $rights);
			}
			$rs->moveNext();
		}
		$rs->Close();

		return $cats;
	}

	function getCategory($id) {
		$sql = "SELECT * FROM tbl_ich_trc_categories 
				WHERE cat_id = ".$id."
				AND cat_org_id = ".$this->_variables->public->org_id;

		$rs = $this->_db->query($sql);
		$cat = $rs->Row();
		$cat['rights'] = $this->_cc->getAuthorizations('tbl_ich_trc_categories', $cat['CAT_ID']);

		$rs->Close();
		return $cat;
	}

	function getResourcesByCat($cat_id) {
		$sql = "SELECT rsc_uri as uri
				,	rsc_name as name
				,	rsc_size as size
				,	rsc_title as title
				,	rsc_description as description
				,	rsc_ico_mimetype as ico_mimetype
				,	ico_description as ico_description
				,	date_format(rsc_changedate, '%Y-%m-%d') as changedate
				,	date_format(rsc_createdate , '%Y-%m-%d') as createdate
				FROM tbl_ich_trc_resources
				LEFT JOIN tbl_ich_trc_icons ON ico_mimetype = rsc_ico_mimetype
				WHERE rsc_cat_id = ".$cat_id."
				AND rsc_org_id = ".$this->_variables->public->org_id."
				ORDER BY rsc_title";

		$res = array();
		$rs = $this->_db->query($sql);
		while ($r = $rs->row()) {
			$res[] = $r;
			$rs->moveNext();
		}
		$rs->Close();

		return $res;

	}

	function getResourceDetails($uri) {
		$sql = "SELECT
					rsc_uri as uri
				,	rsc_changedate as changedate
				,	rsc_createdate as createdate
				,	rsc_cat_id as category
				,	rsc_ico_mimetype as mimetype
				,	ico_description as ico_description
				,	rsc_size as size
				,	rsc_name as name
				,	rsc_title as title
				,	rsc_description as description
				,	rsc_options
				FROM tbl_ich_trc_resources 
				LEFT JOIN tbl_ich_trc_icons ON ico_mimetype = rsc_ico_mimetype
				WHERE rsc_uri = '".$uri."'";
		$rs = $this->_db->query($sql);
		$res = $rs->Row();

		$res['options'] = array();
		$options = explode(':', $res['rsc_options']);
		foreach ($this->_variables->private->options as $opt_code => $opt_name) {
			$res['options'][$opt_code] = in_array($opt_code, $options) ? '1' : '0';
		}

		$res['cat_rights'] = $this->_cc->getAuthorizations('tbl_ich_trc_categories', $res['category']);

		$rs->Close();

		return $res;
	}

	function deleteUri($uri) {

		$this->_cc->killResource("tbl_ich_rsc_resources","",$uri);

	}

	function saveUri($data) {

		$uri = (isset($data['uri']) && strlen($data['uri']) > 0) ? $data['uri'] : $this->_cc->newURI();
		
		$this->_cc->putResource($data, 'tbl_ich_rsc_resources', '', $uri, 'interchange_upload', $uri);

		$options_on = array();
		foreach ($data['options'] as $opt_code => $value) {
			if ($value) $options_on[] = $opt_code;
		}
		$meta_data['options'] = implode(':', $options_on);
		$meta_data['description'] = $data['description'];

		$this->_cc->putResourceMeta($uri, $meta_data);

		return $uri;

	}

	function _init_types() {
		if (count($this->file_types) == 0) {
			$types = $this->_cc->getTypes('DOC:IMG:MED:PRG');
			foreach ($types as $t) {
				if ($t['ICO_MIMETYPE']) {
					$this->file_types[$t['ICO_MIMETYPE']] = $t['ICO_DESCRIPTION'];
				}
			}
		}
	}

	function get_user_groups ($usr_id) {
		global $g_obj_ti3a0365;
		
		if (!isset($this->usr_groups) || !is_array($this->usr_groups)) {

			$groups = array();

			/*$sql = "SELECT grp_code, grp_description 
					FROM tbl_i3a_group_admins, tbl_i3a_group_definitions 
					WHERE gam_grp_id = grp_id AND gam_usr_id= ".$usr_id	." ORDER BY lower(grp_description) ASC";*/

			$sql = "SELECT grp_code, grp_description 
					FROM tbl_i3a_group_members, tbl_i3a_group_definitions 
					WHERE mgp_grp_id = grp_id AND mgp_usr_id= ".$usr_id	." ORDER BY lower(grp_description) ASC";

			$rs = $g_obj_ti3a0365->query($sql);
			while ($r = $rs->row()) {
				$groups[$r['GRP_CODE']] = $r['GRP_DESCRIPTION'];
				$rs->moveNext();
			}
			$rs->Close();

			$this->usr_groups = $groups;
		}
		
	}

	function searchResources($q, $options=array()) {
//		echo "searchResources($q)";
	
		$this->_init_types();
		

		$rsc_list = $this->_cc->getURIs(	'DCC'
										,	$this->_variables->public->lng_code
										,	false
										,	false
										,	false
										,	$q
										,	null
										,	null
										,	'rsc_title'
										,	'ASC'
										,	0
										,	10000
										,	null
										,	null
										,	$options
										);

		foreach ($rsc_list as $uri => $rsc) {
			
//			$rsc_list[$uri]['title'] .=  ' ('.$rsc['options'] .')';
			$rsc_list[$uri]['cat_rights'] = $this->_cc->getAuthorizations('tbl_ich_trc_categories', $rsc['category']);
			$rsc_list[$uri]['ico_description'] = $this->file_types[$rsc['mimetype']];

		}
//		echo "<pre>";
//		print_r($rsc_list);
//		echo "</pre>";

		return $rsc_list;

	}

}