<?php
class Baner_model extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';
	var $encryption_key = "i:Dvv%^uJKK9)_K<";

    function Baner_model()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->helper("log");
    }
    
    function getMaxBanerOrder($moduleId){
    	$sql = "SELECT MAX(baner_order) from baner WHERE baner_module_id='".$moduleId."' ";
    	$result = $this->db->query($sql);
    	if($result->num_rows() > 0){
    		$row = $result->row_array();
    		return $row['MAX(baner_order)'];
    	} else return 1;
    }
    
	function setMemoryForImage( $filename ){
	    $imageInfo = getimagesize($filename);
	    $MB = 1048576;  // number of bytes in 1M
	    $K64 = 65536;    // number of bytes in 64K
	    $TWEAKFACTOR = 1.5;  // Or whatever works for you
	    $memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
	                                           * $imageInfo['bits']
	                                           * $imageInfo['channels'] / 8
	                             + $K64
	                           ) * $TWEAKFACTOR
	                         );
	    //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also
	    //Default memory limit is 8MB so well stick with that.
	    //To find out what yours is, view your php.ini file.
	    $memoryLimit = 8 * $MB;
	    if (function_exists('memory_get_usage') &&
	        memory_get_usage() + $memoryNeeded > $memoryLimit)
	    {
	    	$memoryLimitMB = (integer) ini_get('memory_limit');
	        $newLimit = $memoryLimitMB + ceil( ( memory_get_usage()
	                                            + $memoryNeeded
	                                            - $memoryLimit
	                                            ) / $MB
	                                        );
	        ini_set( 'memory_limit', $newLimit . 'M' );
	        return true;
		}else
	    return false;
	}
    
    function addbanerphoto($moduleId, $foto_x, $foto_y, $header, $file, $change_size){
    	$plik = $_FILES['plik'];
    	
    	if(in_array($plik['type'], array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))){
    		$n = $plik['name'];
    		$r = explode(".", $n);
    		$nazwa_pliku = time().'.'.$r[count($r)-1];
    		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
    		if(@move_uploaded_file($plik['tmp_name'], 'pages/'.$folder.'/foto/baner/'.$nazwa_pliku)){
    			
    			if($change_size == true){
	    			$this->setMemoryForImage('pages/'.$folder.'/foto/baner/'.$nazwa_pliku);
	    			
	    			$size = getimagesize('pages/'.$folder.'/foto/baner/'.$nazwa_pliku);
	    			
	    			$this->load->library('image_lib');
	    			
	    			$config['maintain_ratio'] = TRUE;
	    			$config['source_image'] = 'pages/'.$folder.'/foto/baner/'.$nazwa_pliku;
	    			$config['image_library'] = 'gd2';
	    			$config['width'] = $foto_x;
	    			if($size[0] > $size[1])
	    				$config['height'] = $foto_x;//$foto_y;
	    			else $config['height'] = $foto_x * 5;
	    			
	    			$this->image_lib->initialize($config);
	    			$this->image_lib->resize();
	    			$this->image_lib->clear();
	    				
    			}
    			
    			$order = $this->getMaxBanerOrder($moduleId) + 1;
    			$data = array(
    				'baner_module_id' => $moduleId,
    				'baner_name' => $nazwa_pliku,
    				'baner_header' => $header,
    				'baner_order' => $order
    			);
    			$this->db->set('baner_added', 'sysdate()', FALSE);
    			$this->db->insert('baner', $data);
    			action_log('Dodawanie zdjęcia baneru', 'baner', 'insert', $this->db->insert_id(), $nazwa_pliku);
    		}
    		$pagename = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_pagename;
    		return reduce_double_slashes(prep_url($pagename).'/foto/baner/'.$nazwa_pliku);
    	} else {return false;}
    }
    
	    
    function setbanerphotosorder($photos){
    	$order = 0;
    	foreach ($photos as $baner){
    		$order++;
    		$this->db->where('ID_baner', $baner);
    		$this->db->update('baner', array('baner_order' => $order));
    		$nazwa = $this->db->get_where('baner', array('ID_baner' => $baner))->row()->baner_name;
    		action_log('Zmiana kolejności zdjęć baneru', 'baner', 'update', $baner, $nazwa);
    	}
    }

	
	function getbaner($id){
		$query = $this->db->get_where('baner', array('ID_baner' => $id));
		if($query->num_rows() > 0){
			$row = $query->row();
			return $row;
		} else return NULL;
	}
	
	function edytujbaner($id, $header){
		$data = array(
			'baner_header' => $header
		);
		$this->db->where('ID_baner', $id);
		$this->db->update('baner', $data);
		action_log('Edycja zdjęcia baneru', 'baner', 'update', $id, $header);
	}
	
	function cropbaner($foto, $x, $y, $x2, $y2, $h, $w){
		$n = explode('/', $foto);
		$nazwa_pliku = $n[count($n)-1];
		$folder = $this->db->get_where('users', array('ID_users' => $this->session->userdata('uzytkownik_id')))->row()->user_folder;
		
		$this->setMemoryForImage('pages/'.$folder.'/foto/baner/'.$nazwa_pliku);
		
		$this->load->library('image_lib');
	    			
    	$config['source_image'] = 'pages/'.$folder.'/foto/baner/'.$nazwa_pliku;
    	$config['image_library'] = 'gd2';
    	$config['maintain_ratio'] = FALSE;
    	$config['width'] = $w;
    	$config['height'] = $h;
    	$config['x_axis'] = $x;
		$config['y_axis'] = $y;
    	    	
    	$this->image_lib->initialize($config);
    	$this->image_lib->crop();
    	
    	$stara_nazwa = $nazwa_pliku;
    	$r = explode(".", $stara_nazwa);
    	$nazwa_pliku = time().'.'.$r[count($r)-1];
    	rename('./pages/'.$folder.'/foto/baner/'.$stara_nazwa, './pages/'.$folder.'/foto/baner/'.$nazwa_pliku);
    	
    	$this->db->where('baner_name', $stara_nazwa);
    	$this->db->update('baner', array('baner_name' => $nazwa_pliku));
	}
		
}
?>