<?php
class Mcollection2 extends Model
{
	
	function mcollection2(){
		parent::Model();
	}
	
	function get_gyartok(){
		$query = $this->db->where('deleted', 0)->orderby('name ASC')->get('coll2_man');
		
		if($query->num_rows() == 0){
            return false;
        } else {
            return $query->result();
		}
	}
	
	function get_gyartokcsop($group_id){
		//$query = $this->db->where('group_id', $group_id)->where('deleted', 0)->orderby('name ASC')->get('coll2_man');
		$query = $this->db->where("group_id LIKE '%;{$group_id};%'")->where('deleted', 0)->orderby('name ASC')->get('coll2_man');
		
		if($query->num_rows() == 0){
            return false;
        } else {
            return $query->result();
		}
	}
	
	function get_kategoriak($dir){
		
		$mid = $this->get_manBydir($dir)->id;
		
		if(!$mid || !is_numeric($mid))
			return false;
		
		$this->db->where('m_id ', $mid)->where('deleted ', 0);
		if(hun())
			$this->db->orderby('nev_hun ASC');
		else
			$this->db->orderby('nev_eng ASC');
		
		$query = $this->db->get('coll2_cat');
        
        if($query->num_rows() == 0){
            return false;
        } else {
            return $query->result();
		}
	}
	
	function get_objektumok($cid,$order=""){
		$query = $this->db->where('c_id',$cid)->where('deleted', 0);
		
		if($order)
			$this->db->orderby($order);
		
		$query = $this->db->get('collection2');
		
		if($query->num_rows() == 0){
            return false;
        } else {
            return $query->result();
		}
	}
	
	function get_elem($oid){
		$query = $this->db->where('o_id',$oid)->where('deleted', 0)->get('collection2');
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_manById($id){
		$query = $this->db->where('id', $id)->get('coll2_man');
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_manBydir($dir){
		$query = $this->db->where('dir', $dir)->get('coll2_man');
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_kategoria($cid){
		$query = $this->db->where('c_id', $cid)->get('coll2_cat');
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_elem_next($oid){
		$c_id = $this->get_elem($oid)->c_id;
		
		$query = $this->db->where('c_id',$c_id)->where('o_id > '.$oid)->where('deleted', 0)->orderby('o_id ASC')->limit('1')->get('collection2');
		
//		print $this->db->last_query();
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_elem_prev($oid){
		$c_id = $this->get_elem($oid)->c_id;
		
	//	$ordernev = hun() ? 'title_hun' : 'title_eng';
	//	$this->get_objektumok($cid,$ordernev.' ASC');
		
	//	$sql = "SELECT * FROM collection2 WHERE c_id={$c_id} AND deleted=0 AND {$ordernev}<'".$obj->$ordernev."'"
		
		
		$query = $this->db->where('c_id',$c_id)->where('o_id < '.$oid)->where('deleted', 0)->orderby('o_id DESC')->limit('1')->get('collection2');
		
//		print $this->db->last_query();
		
		if($query->num_rows() != 1){
            return false;
        } else {
            return $query->row();
		}
	}
	
	function get_sorszam($oid){
		$c_id = $this->get_elem($oid)->c_id;
		
		$query = $this->db->where('c_id',$c_id)->where('o_id < '.$oid)->where('deleted', 0)->orderby('o_id ASC')->get('collection2');
		
		return $query->num_rows();
		
	}
	
	function new_kategoria($gyartodir,$nevhun,$neveng){
		$gyarto = $this->get_manBydir($gyartodir);
		
		$tomb['m_id']    = $gyarto->id;
		$tomb['nev_hun'] = $nevhun;
		$tomb['nev_eng'] = $neveng;
		
		$this->db->insert('coll2_cat',$tomb);
		
		return mysql_insert_id();
	}
	
	
	function get_all_showdown(){
		$sql = "SELECT sum(`show`) as views, sum(`down`) as downloads FROM `collection2` o INNER JOIN `coll2_cat` c ON c.c_id=o.c_id  WHERE o.deleted=0 AND c.deleted=0";
		$res = $this->db->query($sql)->row();
		
		return $res;
	}
	
	function get_group_showdown($group_id){
		/*$group = $this->db->query("SELECT group_id FROM coll2_mangrp WHERE azon='{$groupazon}'")->row();
		$group_id = $group->group_id;*/
		$sql = "SELECT sum(`show`) as views, sum(`down`) as downloads FROM `collection2` o INNER JOIN `coll2_cat` c ON c.c_id=o.c_id  WHERE o.deleted=0 AND c.deleted=0 AND o.m_id IN (SELECT id FROM `coll2_man` WHERE group_id={$group_id})";
		$res = $this->db->query($sql)->row();
		
		return $res;
	}
	
	
	
	function get_elem2($oid){
		$sql = "SELECT o.o_id, m_id, c_id, datum, title, d.desc, o.show, down, tipus FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE lang='eng' AND o.deleted=0 AND o.o_id={$oid}";
		$sql .= " LIMIT 0,1";
		$re = $this->db->query($sql)->row();
		return $re;
	}
	
	function get_objektumok2($c_id,$ordernev='title'){
		// csak angol név szerint rendezve
		$sql = "SELECT o.o_id FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id WHERE o.c_id={$c_id} AND lang='eng' AND o.deleted=0 ORDER BY {$ordernev}";
		
		$query = $this->db->query($sql);
		
		if($query->num_rows() == 0){
			return false;
		} else {
			return $query->result();
		}
	}
	
	function get_elem_next2($object,$ordernev='title'){
		$object->$ordernev = mysql_real_escape_string($object->$ordernev);
		$sql = "SELECT o.o_id FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE o.c_id=".$object->c_id." AND lang='eng' AND o.deleted=0 AND {$ordernev}>'".$object->$ordernev."'";
		$sql .= " ORDER BY {$ordernev} LIMIT 0,1";
		
		$query = $this->db->query($sql);
		
		if($query->num_rows() == 0){
			return false;
		} else {
			return $query->row();
		}
	}
	function get_elem_prev2($object,$ordernev='title'){
		$object->$ordernev = mysql_real_escape_string($object->$ordernev);
		$sql = "SELECT o.o_id FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE o.c_id=".$object->c_id." AND lang='eng' AND o.deleted=0 AND {$ordernev}<'".$object->$ordernev."'";
		$sql .= " ORDER BY {$ordernev} LIMIT 0,1";
		
		$query = $this->db->query($sql);
		
		if($query->num_rows() == 0){
			return false;
		} else {
			return $query->row();
		}
	}
	function get_sorszam2($object,$ordernev='title'){
		$sql = "SELECT count(o.o_id) db FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE o.c_id=".$object->c_id." AND lang='eng' AND o.deleted=0 AND {$ordernev}<'".$object->$ordernev."'";
		$sql .= " ORDER BY {$ordernev}";
		
		$query = $this->db->query($sql)->row();
		return $query->db;
	}
	
	
	function get_elem_prev3($object,$ordernev='title'){
		$object->$ordernev = mysql_real_escape_string($object->$ordernev);
		$sql = "SELECT o.o_id FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE o.c_id=".$object->c_id." AND tipus='{$object->tipus}' AND lang='eng' AND o.deleted=0 AND {$ordernev}<'".$object->$ordernev."'";
		$sql .= " ORDER BY {$ordernev} DESC LIMIT 0,1";
		
		$query = $this->db->query($sql);
		
		if($query->num_rows() == 0){
			return false;
		} else {
			return $query->row();
		}
	}
	function get_elem_next3($object,$ordernev='title'){
		$object->$ordernev = mysql_real_escape_string($object->$ordernev);
		$sql = "SELECT o.o_id FROM collection2 o INNER JOIN coll2_desc d ON o.o_id=d.o_id";
		$sql .= " WHERE o.c_id=".$object->c_id." AND tipus='{$object->tipus}' AND lang='eng' AND o.deleted=0 AND {$ordernev}>'".$object->$ordernev."'";
		$sql .= " ORDER BY {$ordernev} LIMIT 0,1";
		
		$query = $this->db->query($sql);
		
		if($query->num_rows() == 0){
			return false;
		} else {
			return $query->row();
		}
	}
	
	function search($kifejezes){
		$search  = mysql_real_escape_string($kifejezes);
		$serachs = implode('%',explode(' ',$search));
		$searchwords = explode(' ',$search);
		$lang = hun() ? "hun" : "eng";
		
		/*
		$sql = "(SELECT o_id, title FROM coll2_desc d WHERE title LIKE '%{$search}%' AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0) ) ";
		$sql .= " UNION (SELECT o_id, title FROM coll2_desc d WHERE title LIKE '%{$serachs}%' AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0) ) ";
	//	$sql .= " UNION (SELECT o_id, title FROM coll2_desc d WHERE d.desc LIKE '%{$search}%' AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0) )";
		$sql .= " ORDER BY title ASC";
		*/
		
		
		
		
		
		$sql = "(SELECT o_id, title FROM coll2_desc d WHERE ";
		$sql .= "(title LIKE '%{$search}%' ";
		if( count($searchwords) > 1 ){
			$sql .= " OR title LIKE '%{$serachs}%' OR ( ";
			foreach( $searchwords as $szo ){
				$sql .= " title LIKE '%{$szo}%' AND ";
			}
			$sql = substr($sql,0,-4);
			$sql .= " )";
		}
		$sql .= ") AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0) ORDER BY title ) ";
		
		
		
		// saját súlyozás
	/*	$sql = "SELECT DISTINCT(o_id), title FROM `coll2_desc` d WHERE ( ";
		$sql .= "title LIKE '%{$search}%'";
		foreach( $searchwords as $szo ){
			$sql .= " OR title LIKE '%{$szo}%' ";
		}
		$sql .= ") AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0) ";
		$sql .= "ORDER BY ( CASE WHEN d.title LIKE '%{$search}%' THEN ".(count($searchwords)+1)." ELSE 0 END ";
		$i = count($searchwords);
		foreach( $searchwords as $szo ){
			$sql .= "+ CASE WHEN d.title LIKE '%{$szo}%' THEN {$i} ELSE 0 END ";
			$i--;
		}
		$sql .= ") desc";*/
		
		
		
		
		
		
		// ez csak 4 karaktertől működik, de ez lenne a legjobb ..
	//	$sql = "SELECT o_id, title FROM `coll2_desc` d WHERE MATCH (`title`) AGAINST ('{$serach}') AND lang='{$lang}' AND o_id IN (SELECT o_id FROM collection2 c WHERE c.o_id=d.o_id AND c.deleted=0)";
		
	//	print $sql;
	
		$sqlcache = $this->cache->get('ss-'.md5($sql));
		if($search && !$sqlcache){
			$result = $this->db->query($sql)->result();
			
			foreach($result as $obj){
				$objectum = $this->get_elem($obj->o_id);
				$ered = $this->db->query("SELECT dir FROM coll2_man WHERE id=".$objectum->m_id)->row();
				$obj->gyartodir = $ered->dir;
			}
			
			$this->cache->write($result, 'ss-'.md5($sql), (3600*24));
		}elseif( $sqlcache ){
			$result = $sqlcache;
		}
		else
			$result = false;
		
		return $result;
	}
	
}
?>