<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}

	public function index(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['archieve'] = $this->common_model->getAll('blog_archives','',array('sys_languages','blog_archives.language_id = sys_languages.id'),'','sys_languages.name as lang_name,blog_archives.*') ;
		$data['content'] = $this->load->view('admin/blog/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function delete($enc_userID='')
	{
		if($enc_userID!='')
		{
			$userId=base64_decode($enc_userID);
			$this->common_model->deleteData('blog_archives',array('id'=>$userId));
			$this->utility->setMsg('Archieve deleted','SUCCESS');	
			redirect(base_url().'admin/blog');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog');
		}
	}
	public function edit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$archieveId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$name=$this->utility->info_cleanQuery($this->input->post('name'));
				$language=$this->utility->info_cleanQuery($this->input->post('language'));
				
				$archDetails_con=array(
							'name'=>$name,
							'alias'=>str_replace(' ','-',trim($name)),
							'language_id'=>$language
					);
				$this->common_model->UpdateData('blog_archives',$archDetails_con,array('id'=>$archieveId));

				$this->utility->setMsg('Archieve details update','SUCCESS');	
				redirect(base_url().'admin/blog/edit/'.$enc_userID);
			}

			$content_data = array();
			$content_data['archieveDetails'] = $this->common_model->getAllRow('blog_archives',array('id'=>$archieveId)) ;
			$content_data['languages'] = $this->common_model->getAll('sys_languages');
			$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
			$data['content'] = $this->load->view('admin/blog/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog');
		}
	}
	public function add()
	{
		if($this->input->post())
		{
			$name=$this->utility->info_cleanQuery($this->input->post('name'));
			$language=$this->utility->info_cleanQuery($this->input->post('language'));
			
			$archDetails_con=array(
						'name'=>$name,
						'alias'=>str_replace(' ','-',trim($name)),
						'language_id'=>$language,
						'created'=>time()
				);
			$this->common_model->insertData('blog_archives',$archDetails_con);

			$this->utility->setMsg('Archieve details update','SUCCESS');	
			redirect(base_url().'admin/blog');
		}

		$content_data = array();
		$content_data['languages'] = $this->common_model->getAll('sys_languages');
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$data['content'] = $this->load->view('admin/blog/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function article(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['article'] = $this->common_model->getAll('blog_articles','',array('sys_languages','sys_languages.id = blog_articles.lang_id'),'','*,blog_articles.id as id') ;
		$data['content'] = $this->load->view('admin/articles/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function article_delete($enc_userID='')
	{
		if($enc_userID!='')
		{
			$articleId=base64_decode($enc_userID);
			$this->common_model->deleteData('blog_articles',array('id'=>$articleId));
			$this->utility->setMsg('Article deleted','SUCCESS');	
			redirect(base_url().'admin/blog/article');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog/article');
		}
	}
	public function article_visible($enc_userID='',$status='')
	{
		if($enc_userID!='')
		{
			if($status==1)
			{
				$update = 1;
			}
			else
			{
				$update = 0;
			}
			$articleId=base64_decode($enc_userID);
			$this->common_model->UpdateData('blog_articles',array('visible'=>$update),array('id'=>$articleId));
			$this->utility->setMsg('Articles status changed','SUCCESS');	
			redirect(base_url().'admin/blog/article');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog/article');
		}
	}
	public function article_edit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$articleId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$title=$this->utility->info_cleanQuery($this->input->post('title'));
				$subtitle=$this->utility->info_cleanQuery($this->input->post('subtitle'));
				$content=$this->utility->info_cleanQuery($this->input->post('content'));
				$sort=$this->utility->info_cleanQuery($this->input->post('sort'));
				
				$articleDetails_con=array(
							'title'=>$title,
							'teasertext'=>$subtitle,
							'content'=>$content,
							'sort'=>$sort,
							'lang_id'=>$this->input->post('language')
					);
				$this->common_model->UpdateData('blog_articles',$articleDetails_con,array('id'=>$articleId));
				if($_FILES['previewfile']['name']!='')
				{
					$imagefolder = 'img/blog-articles/articles';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
					/*if (!file_exists(FCPATH . $uploadpath)) {
					    mkdir(FCPATH . $uploadpath, 0777, true);
					}*/
			        $config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'jpg|png',
			            'encrypt_name' 		=> true,
			            'file_ext_tolower' 	=> true,
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );

			        $this->load->library('upload', $config);
			       
			        if(!$this->upload->do_upload('previewfile')) {

			            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
						redirect(base_url().'admin/blog/article_edit/'.$enc_userID);
			        }

			        $uploaddata = $this->upload->data();

			        $file = array(
			            'user_id' 	=> 1,
			            'path' 		=> $imagefolder,
			            'filename' 	=> $uploaddata['raw_name'],
			            'extension' => substr($uploaddata['file_ext'], 1),
			            'mimetype' 	=> $uploaddata['file_type'],
			            'is_image' 	=> $uploaddata['is_image'],
			            'attr_alt' 	=> '',
			        );

			        $imagesize = getimagesize($uploaddata['full_path']);

			        $file['width'] 	= $imagesize[0];
			        $file['height'] = $imagesize[1];


			        $fileId = $this->common_model->insertData('sys_files',$file);
			        $this->common_model->UpdateData('blog_articles',array('image_id'=>$fileId),array('id'=>$articleId));
					//echo $this->db->last_query(); die;
				}
				if($_FILES['logofile']['name']!='')
				{
					$imagefolder = 'img/media/';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
					/*if (!file_exists(FCPATH . $uploadpath)) {
					    mkdir(FCPATH . $uploadpath, 0777, true);
					}*/
			        $config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'jpg|png',
			            'encrypt_name' 		=> true,
			            'file_ext_tolower' 	=> true,
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );

			        $this->load->library('upload', $config);
			       
			        if(!$this->upload->do_upload('logofile')) {

			            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
						redirect(base_url().'admin/blog/article_edit/'.$enc_userID);
			        }

			        $uploaddata = $this->upload->data();

			        $file = array(
			            'user_id' 	=> 1,
			            'path' 		=> $imagefolder,
			            'filename' 	=> $uploaddata['raw_name'],
			            'extension' => substr($uploaddata['file_ext'], 1),
			            'mimetype' 	=> $uploaddata['file_type'],
			            'is_image' 	=> $uploaddata['is_image'],
			            'attr_alt' 	=> '',
			        );

			        $imagesize = getimagesize($uploaddata['full_path']);

			        $file['width'] 	= $imagesize[0];
			        $file['height'] = $imagesize[1];


			        $fileId = $this->common_model->insertData('sys_files',$file);
			        $this->common_model->UpdateData('blog_articles',array('logo_id'=>$fileId),array('id'=>$articleId));
				}
				$this->utility->setMsg('Archieve details update','SUCCESS');	
				redirect(base_url().'admin/blog/article_edit/'.$enc_userID);
			}

			$content_data = array();
			$content_data['articleDetails'] = $this->common_model->getAllRow('blog_articles',array('id'=>$articleId)) ;
			$content_data['previewImage'] = $this->common_model->getAllRow('sys_files',array('id'=>$content_data['articleDetails']['image_id'])) ;
			$content_data['logoImage'] = $this->common_model->getAllRow('sys_files',array('id'=>$content_data['articleDetails']['logo_id'])) ;
			$content_data['languages'] = $this->common_model->getAll('sys_languages');
			$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
			$data['content'] = $this->load->view('admin/articles/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog/articles');
		}
	}
	public function add_article()
	{
		
		if($this->input->post())
		{
			$title=$this->utility->info_cleanQuery($this->input->post('title'));
			$subtitle=$this->utility->info_cleanQuery($this->input->post('subtitle'));
			$content=$this->utility->info_cleanQuery($this->input->post('content'));
			$sort=$this->utility->info_cleanQuery($this->input->post('sort'));
			
			$articleDetails_con=array(
						'title'=>$title,
						'teasertext'=>$subtitle,
						'content'=>$content,
						'sort'=>$sort,
						'lang_id'=>$this->input->post('language')
				);
			$articleId = $this->common_model->insertData('blog_articles',$articleDetails_con);
			if($_FILES['previewfile']['name']!='')
			{
				$imagefolder = 'img/blog-articles/articles';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				/*if (!file_exists(FCPATH . $uploadpath)) {
				    mkdir(FCPATH . $uploadpath, 0777, true);
				}*/
		        $config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'jpg|png',
		            'encrypt_name' 		=> true,
		            'file_ext_tolower' 	=> true,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

		        $this->load->library('upload', $config);
		       
		        if(!$this->upload->do_upload('previewfile')) {

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/blog/add_article/');
		        }

		        $uploaddata = $this->upload->data();

		        $config['image_library'] = 'gd2';
				$config['source_image'] = $uploadpath.'/'.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1);
				$config['create_thumb'] = TRUE;
				$config['maintain_ratio'] = TRUE;
				$config['width']         = 350;
				$config['height']       = 350;

				$this->load->library('image_lib', $config);

				$this->image_lib->resize();

		        $file = array(
		            'user_id' 	=> 1,
		            'path' 		=> $imagefolder,
		            'filename' 	=> $uploaddata['raw_name'],
		            'extension' => substr($uploaddata['file_ext'], 1),
		            'mimetype' 	=> $uploaddata['file_type'],
		            'is_image' 	=> $uploaddata['is_image'],
		            'attr_alt' 	=> '',
		        );

		        $imagesize = getimagesize($uploaddata['full_path']);

		        $file['width'] 	= $imagesize[0];
		        $file['height'] = $imagesize[1];


		        $fileId = $this->common_model->insertData('sys_files',$file);
		        $this->common_model->UpdateData('blog_articles',array('image_id'=>$fileId),array('id'=>$articleId));
				//echo $this->db->last_query(); die;
			}
			if($_FILES['logofile']['name']!='')
			{
				$imagefolder = 'img/media/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				/*if (!file_exists(FCPATH . $uploadpath)) {
				    mkdir(FCPATH . $uploadpath, 0777, true);
				}*/
		        $config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'jpg|png',
		            'encrypt_name' 		=> true,
		            'file_ext_tolower' 	=> true,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

		        $this->load->library('upload', $config);
		       
		        if(!$this->upload->do_upload('logofile')) {

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/blog/add_article/');
		        }

		        $uploaddata = $this->upload->data();

		        $file = array(
		            'user_id' 	=> 1,
		            'path' 		=> $imagefolder,
		            'filename' 	=> $uploaddata['raw_name'],
		            'extension' => substr($uploaddata['file_ext'], 1),
		            'mimetype' 	=> $uploaddata['file_type'],
		            'is_image' 	=> $uploaddata['is_image'],
		            'attr_alt' 	=> '',
		        );

		        $imagesize = getimagesize($uploaddata['full_path']);

		        $file['width'] 	= $imagesize[0];
		        $file['height'] = $imagesize[1];


		        $fileId = $this->common_model->insertData('sys_files',$file);
		        $this->common_model->UpdateData('blog_articles',array('logo_id'=>$fileId),array('id'=>$articleId));
			}
			$this->utility->setMsg('Archieve details added','SUCCESS');	
			redirect(base_url().'admin/blog/article');
		}

		$content_data = array();
		$content_data['languages'] = $this->common_model->getAll('sys_languages');
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$data['content'] = $this->load->view('admin/articles/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
}