<?php

class Posts extends Controller {
  function Posts(){
    parent::Controller();
    session_start();

	if ($_SESSION['userid'] < 1){
    	redirect('login','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);
	$this->load->library('pagination');

	$config['base_url'] = base_url().'admin/posts/';
	$config['total_rows'] = '200';
	$config['per_page'] = '5'; 

	$this->pagination->initialize($config); 

	$this->pagination->create_links();
	$data['title'] = "Διαχείρηση Άρθρων";
	$data['hideColumn']="hideboth";
	$data['main'] = 'admin/posts_home';
	$data['posts'] = $this->MPosts->getAllPosts();
	$data['cats'] = $this->MCats->getTopCategories();
	$this->load->vars($data);
	$this->load->view('admin/dashboard_tpl');  
  }
  function post($id){
  
  	$data['page'] = $this->MDashboard->getPageName($id);
	$page_title=$data['page']['name'];
	$cat_id=$data['page']['id'];
	$data['liID']=$this->uri->segment(4);
	$data['title'] = 'Διαχείριση Σελίδας "'.$page_title.'"';
	$data['hideColumn']="hideboth";
	$data['main'] = 'admin/posts_home';
	$data['page_title'] = $page_title;
	$data['cat_id'] = $cat_id;
	$data['posts'] = $this->MDashboard->getAllPostsbyCategory($id);
//	$data['page_content'] = $this->MDashboard->getPageContent($id);
	$this->load->vars($data);
	$this->load->view('admin/dashboard_tpl');  
  }

  
  function create($cat_id){
  $data['page'] = $this->MDashboard->getPageName($cat_id);
	$page_title=$data['page']['name'];
	//$data['page'] = $this->MDashboard->getPost($cat_id);
	//$category_id=$data['page']['category_id'];
   	$data['liID']=$this->uri->segment(4);
	if ($this->input->post('title')){
  		$this->MDashboard->addPost();
  		$this->session->set_flashdata('message','Post created');
  		redirect('admin/posts/post/'.$cat_id,'refresh');
  	}else{
		$data['title'] = 'Δημιουργία Άρθρου σελίδα "'.$page_title.'"';
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/posts_create';
		$data['cat_id'] = $cat_id;
		//$data['cats'] = $this->MDashboard->getCategoriesDropDown();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');    
	} 
  }
  
  function edit($id=0){
  
  	$data['page'] = $this->MDashboard->getPost($id);
	$cat_id=$data['page']['category_id'];
  	if ($this->input->post('title')){
  		$this->MDashboard->updatePost();
  		$this->session->set_flashdata('message','Το άρθρο ανανεώθηκε');
  		redirect('admin/posts/post/'.$cat_id,'refresh');
  	}else{
		//$id = $this->uri->segment(4);
		$data['liID']=$this->uri->segment(3);
		$data['title'] = "Επεξεργασία Άρθρου";
		$data['hideColumn']="hideboth";
		$data['main'] = 'admin/posts_edit';
		$data['post'] = $this->MDashboard->getPost($id);
		//$data['cats']  = $this->MCats->getCategoriesDropDown();
		$this->load->vars($data);
		$this->load->view('admin/dashboard_tpl');    
	}
  }
  
  function delete($id){
  $data['page'] = $this->MDashboard->getPost($id);
	$cat_id=$data['page']['category_id'];
	//$id = $this->uri->segment(4);
	$this->MDashboard->deletePost($id);
	$this->session->set_flashdata('message','Το άρθρο διαγράφηκε');
	redirect('admin/posts/post/'.$cat_id,'refresh');
  }


	
}//end class
?>