<?php
class Slider_model extends Model {

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

    function Slider_model()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->helper("log");
    }
    
    function getMaxSliderOrder($moduleId){
    	$sql = "SELECT MAX(slider_order) from slider WHERE slider_module_id='".$moduleId."' ";
    	$result = $this->db->query($sql);
    	if($result->num_rows() > 0){
    		$row = $result->row_array();
    		return $row['MAX(slider_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 addsliderphoto($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/slider/'.$nazwa_pliku)){
    			
    			if($change_size == true){
	    			$this->setMemoryForImage('pages/'.$folder.'/foto/slider/'.$nazwa_pliku);
	    			
	    			$size = getimagesize('pages/'.$folder.'/foto/slider/'.$nazwa_pliku);
	    			
	    			$this->load->library('image_lib');
	    			
	    			$config['maintain_ratio'] = TRUE;
	    			$config['source_image'] = 'pages/'.$folder.'/foto/slider/'.$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->getMaxSliderOrder($moduleId) + 1;
    			$data = array(
    				'slider_module_id' => $moduleId,
    				'slider_name' => $nazwa_pliku,
    				'slider_header' => $header,
    				'slider_order' => $order
    			);
    			$this->db->set('slider_added', 'sysdate()', FALSE);
    			$this->db->insert('slider', $data);
    			action_log('Dodawanie zdjęcia slidera', 'slider', '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/slider/'.$nazwa_pliku);
    	} else {return false;}
    }
    
	    
    function setsliderphotosorder($photos){
    	$order = 0;
    	foreach ($photos as $slider){
    		$order++;
    		$this->db->where('ID_slider', $slider);
    		$this->db->update('slider', array('slider_order' => $order));
    		$nazwa = $this->db->get_where('slider', array('ID_slider' => $slider))->row()->slider_name;
    		action_log('Zmiana kolejności zdjęć slidera', 'slider', 'update', $slider, $nazwa);
    	}
    }

	
	function getslider($id){
		$query = $this->db->get_where('slider', array('ID_slider' => $id));
		if($query->num_rows() > 0){
			$row = $query->row();
			return $row;
		} else return NULL;
	}
	
	function edytujslider($id, $header){
		$data = array(
			'slider_header' => $header
		);
		$this->db->where('ID_slider', $id);
		$this->db->update('slider', $data);
		action_log('Edycja zdjęcia slidera', 'slider', 'update', $id, $header);
	}
	
	function cropslider($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/slider/'.$nazwa_pliku);
		
		$this->load->library('image_lib');
	    			
    	$config['source_image'] = 'pages/'.$folder.'/foto/slider/'.$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/slider/'.$stara_nazwa, './pages/'.$folder.'/foto/slider/'.$nazwa_pliku);
    	
    	$this->db->where('slider_name', $stara_nazwa);
    	$this->db->update('slider', array('slider_name' => $nazwa_pliku));
	}
		
}
?>