<?php
class Newsletter_model extends Model {

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

    function Newsletter_model()
    {
        // Call the Model constructor
        parent::Model();
        
        $this->load->helper("log");
    }
    
    function get_adresy($user_id){
    	$this->db->order_by('newsletter_email', 'asc');
    	return $this->db->get_where('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_wypisany' => 0));
    }
    
    function get_wiadomosci($user_id){
    	$this->db->order_by('newsletter_config_title', 'asc');
    	return $this->db->get_where('newsletter_config', array('newsletter_config_user_id' => $user_id));
    }
    
    function get_wiadomosc($user_id, $id){
    	return $this->db->get_where('newsletter_config', array('newsletter_config_user_id' => $user_id, 'id_newsletter_config' => $id));
    }
    
    function addadres($user_id, $email){
    	$query = $this->db->get_where('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_wypisany' => 0, 'newsletter_email' => $email));
    	if($query && $query->num_rows() > 0){
    		return 0;
    	} else {
    		$this->db->insert('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_email' => $email, 'newsletter_hash' => sha1($user_id.$email.$user_id)));
    		return 1;
    	}
    }
    
    function removeadres($user_id, $id){
    	$this->db->delete('newsletter', array('newsletter_user_id' => $user_id, 'id_newsletter' => $id));
    }
    
    function addwiadomosc($user_id, $title, $text, $nadawca, $nadawca_email){
    	
    	$data = array(
    		'newsletter_config_user_id' => $user_id,
    		'newsletter_config_title' => $title,
    		'newsletter_config_text' => $text,
    		'newsletter_config_nadawca' => $nadawca,
    		'newsletter_config_nadawca_email' => $nadawca_email
    	);
    	$this->db->insert('newsletter_config', $data);
    	
    	return $this->db->insert_id();
    }
    
    function editwiadomosc($user_id, $id, $title, $text, $nadawca, $nadawca_email){
    	$data = array(
    		'newsletter_config_title' => $title,
    		'newsletter_config_text' => $text,
    		'newsletter_config_nadawca' => $nadawca,
    		'newsletter_config_nadawca_email' => $nadawca_email
    	);
    	$this->db->where('id_newsletter_config', $id);
    	$this->db->where('newsletter_config_user_id', $user_id);
    	$this->db->update('newsletter_config', $data);
    	return $this->db->affected_rows();
    }
    
    function removewiadomosc($user_id, $id){
    	$this->db->delete('newsletter_config', array('newsletter_config_user_id' => $user_id, 'id_newsletter_config' => $id));
    }
    
    function wyslijnewsletter($user_id, $id){
    	$adres = reduce_double_slashes(prep_url($this->db->get_where('users', array('ID_users' => $user_id))->row()->user_pagename.'/_anuluj_newsletter/_n_user/'));
    	
    	$query = $this->db->get_where('newsletter_config', array('newsletter_config_user_id' => $user_id, 'id_newsletter_config' => $id));
    	if($query && $query->num_rows() > 0){
    		$newsletter = $query->row();
    		
    		$q2 = $this->db->get_where('newsletter', array('newsletter_user_id' => $user_id, 'newsletter_wypisany' => 0));
    		if($q2 && $q2->num_rows() > 0){
    			foreach($q2->result() as $mail){
    				if(valid_email($mail->newsletter_email)){
    					$config['protocol'] = 'mail';
						$config['charset'] = 'utf-8';
						$config['wordwrap'] = TRUE;
						$config['mailtype'] = 'html';
						$config['priority'] = '3';
						
						$this->email->initialize($config); 
						$this->email->from($newsletter->newsletter_config_nadawca_email, $newsletter->newsletter_config_nadawca);
						$this->email->to($mail->newsletter_email);
						$this->email->subject($newsletter->newsletter_config_title);
						$this->email->message($newsletter->newsletter_config_text.'
						<br><p>Jeśli chcesz zrezygnować z newslettera kliknij ten adres: <a href="'.$adres.$mail->newsletter_hash.'">'.$adres.$mail->newsletter_hash.'</a></p>');
						
						//dołączyć link do wypisania
						
						$this->email->send();
    				}
    			}
    		}
    	}
    }
    
}

