<?php
class Subscribers extends Controller {
	function Subscribers(){
		parent::Controller();
		session_start();
	$uid=$this->session->userdata('adminuserid');

	if ($uid == ''){
    	redirect('admin','refresh');
    }
		$this->tinyMce = '
		<!-- TinyMCE -->
		<script type="text/javascript" src="'. base_url().'js/tiny_mce/tiny_mce_gzip.js"></script>
		<script type="text/javascript">
		tinyMCE_GZ.init({
			plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
			
			themes : "simple,advanced",
			languages : "en,el",
			disk_cache : true,
			debug : false
		});
		</script>
		<script type="text/javascript">
		tinyMCE.init({
			// General options
			mode : "textareas",
			language : "el",
			theme : "advanced",
			plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,


				entity_encoding : "raw",
				fix_list_elements : true,
				convert_urls : false,
				relative_urls : false,
				extended_valid_elements : "a[name|href|target|title|onclick|class|params],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"

		});
		</script>
		<!-- /TinyMCE -->
		';
	}
	
	function index(){
		$data['liID']=$this->uri->segment(2);
		$data['title'] = "Διαχείρηση Πελατών e-shop";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/subs_home';
		$data['subscribers'] = $this->MSubscribers->getAllSubscribers();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');
	}
	function nsubscribers(){
		$data['liID']=$this->uri->segment(2);
		$data['title'] = "Διαχείρηση Πελατών e-shop";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/subs_all';
		$data['subscribers'] = $this->MSubscribers->getAllAllSubscribers();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');
	}
	function newssubscribers(){
		$data['liID']=$this->uri->segment(2);
		$data['title'] = "Διαχείρηση Συνδρομητών eNewsletter";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/news_subs_home';
		$data['subscribers'] = $this->MSubscribers->getAllNewsSubscribers();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');
	}
	
	function delete($id){
		$this->MSubscribers->removeSubscriberID($id);
		$this->session->set_flashdata('message','O Πελάτης διαγράφηκε');
		redirect('admin/subscribers/index','refresh');
	}
	function delete_nsub($id){
		$this->MSubscribers->removeNewsSubscriberID($id);
		$this->session->set_flashdata('message','O Συνδρομητής διαγράφηκε');
		redirect('admin/subscribers/newssubscribers','refresh');
	}
	function enewsletter(){
		$data['liID']=$this->uri->segment(3);
		$data['title'] = "Αποστολή eNewsletter";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/enewsletter_home';
		$data['subscribers'] = $this->MSubscribers->getAllSubscribers();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');
		
	}
	function send_mail($id){
		$data['liID']=$this->uri->segment(3);
		$data['title'] = "Αποστολή E-mail";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/email_home';
		$data['subscriber'] = $this->MSubscribers->getSubscriber($id);
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');
		
	}
		function sendclientemail($id){
		$data['liID']=$this->uri->segment(3);
		$data['subscriber'] = $this->MSubscribers->getSubscriber($id);
  		$this->load->helper('file');
  		if (($this->input->post('subject')) && ($this->input->post('message'))){
 			$test = $this->input->post('test');
	  		$subject = $this->input->post('subject');
	  		$header = $this->input->post('eheader');
 			$msg = $this->input->post('message');
			
			$data['msg_title'] = $header;
			$data['comments'] = $msg;
			$data['post_msg'] = 'Best Regards,<Br><Br>Wines2u.gr';

			$this->load->library('parser');
			$emsg = $this->parser->parse('template_newsletter', $data, TRUE);

 			$this->email->clear();
			$this->email->from('no-reply@wines2u.gr', 'Wines2u.gr');
			$this->email->to($data['subscriber']['email']);
			$this->email->subject($subject);
			$this->email->message($emsg);		
			$this->email->send();
			$this->session->set_flashdata('message', "Το email στάλθηκε");
			redirect('admin/subscribers/send_mail/'.$data['subscriber']['id'],'refresh'); 
  	}else{
			$this->session->set_flashdata('message', "Δεν έχετε συμπληρώσει όλα τα απαραίτητα πεδία");
			redirect('admin/subscribers/send_mail/'.$data['subscriber']['id'],'refresh'); 
  }
}
	function sendemail(){
		$data['liID']=$this->uri->segment(3);
  		$this->load->helper('file');
  		if (($this->input->post('subject')) && ($this->input->post('message'))){
 			$test = $this->input->post('test');
	  		$subject = $this->input->post('subject');
 			$msg = $this->input->post('message');
			
			$data['msg_title'] = $subject;
			$data['comments'] = $msg;
			$data['post_msg'] = 'Best Regards,<Br><Br>Wines2u.gr';

			$this->load->library('parser');
			$emsg = $this->parser->parse('template_newsletter', $data, TRUE);

 		if ($test){
 			$this->email->clear();
			$this->email->from('no-reply@wines2u.gr', 'Wines2u.gr');
			//$this->email->to('dtangos@getid.gr');
			$this->email->to('info@wines2u.gr');
			$this->email->subject('Wines2u.gr “Newsletter",  www.wines2u.gr, '.date('F Y'));
			$this->email->message($emsg);		
			$this->email->send();
			$this->session->set_flashdata('message', "Το δοκιμαστικο email στάλθηκε");
			write_file('tmp/email.log', "|||".$msg);
			redirect('admin/subscribers/enewsletter','refresh'); 
 		}else{
			if ($this->input->post('sub_group')=='clients') {
 			$subs = $this->MSubscribers->getInitSubscribers();
			} elseif ($this->input->post('sub_group')=='subscribers') {
 			$subs = $this->MSubscribers->getNewsSubscribers();
			} else {
 			$subs = $this->MSubscribers->getAllSubscribersforNewsletter();
			}
			$i = count($subs);
			$j=0;
			//foreach ($subs as $key => $list){
			while($j<$i){
			for ($x = 1; $x <= 6; $x++){
			if ($j<$i){
				//$data['unsub'] = "<p><a href='". base_url()."page/unsubscribe/".$list['id']. "'>Διαγραφή από λίστα</a></p>";
				$this->email->clear();
				$this->email->from('no-reply@wines2u.gr', 'Wines2u.gr');
				$this->email->to($subs[$j]['email']);
				$this->email->subject('Wines2u.gr “Newsletter",  www.wines2u.gr, '.date('F Y'));
				$this->email->message($emsg);
				$this->email->send();
				echo $j.".Sending to :".$subs[$j]['email']."....".br(1);
				$j++;
				}
				else{
 				break;
				}
				}
				sleep(120);
			}
 			$this->session->set_flashdata('message',"Tο Newsletter στάλθηκε σε ".count($subs)." members");
			redirect('admin/subscribers/enewsletter','refresh'); 
			}
  	}else{
			$this->session->set_flashdata('message', "Δεν έχετε συμπληρώσει όλα τα απαραίτητα πεδία");
			redirect('admin/subscribers/enewsletter','refresh'); 
  }
  }
}//end class
?>
