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

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

	public function index(){
		//echo "TEST";die;
		$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['blog_article_details'] = $this->blog_article_model->blogArticleDetails();
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/blog_details/article_list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function article_add(){
		$content_data = array();
		if($this->input->post()){
			$total_article = $this->common_model->getAll('b_article');
			$title_slug1 = strtolower(preg_replace('/[^A-Za-z0-9\. -]/', '', str_replace(' ', '-', $this->input->post('blog_title_2'))));
            $title_slug = preg_replace('/-{2,}/', '-', $title_slug1);
            $title_slug = substr($title_slug, 0, 25);
            $blogDetails_on = array(
            	'meta_tag' => $this->input->post('meta_title_2'),
            	'meta_description' => $this->input->post('meta_description_2'),
            	'meta_keywords' => $this->input->post('meta_keywords_2'),
            	'cat_id' => $this->input->post('cat_id'),
            	'blog_title' => $this->input->post('blog_title_2'),
            	'blog_desc' => $this->input->post('blog_desc_2'),
            	'blog_slug' => $title_slug,
            	'staus' => 1,
            	'create_date' => date('Y-m-d H:i:s'),
				'intro_text' => $this->input->post('intro_text_2'),
				'display_order' => count($total_article)+1,
				'lang_id'=>2,
            );
            $articleId = $this->common_model->insertData('b_article',$blogDetails_on);
            $languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr1['blog_id'] = $articleId;
					$arr1['blog_title'] = $this->input->post('blog_title_'.$value['id']);
					$arr1['blog_desc'] = $this->input->post('blog_desc_'.$value['id']);
					$arr1['meta_tag'] = $this->input->post('meta_title_'.$value['id']);
					$arr1['meta_keywords'] = $this->input->post('meta_keywords_'.$value['id']);
					$arr1['meta_description'] = $this->input->post('meta_description_'.$value['id']);
					$arr1['lang_id'] = $value['id'];
					$arr1['intro_text'] = $this->input->post('intro_text_'.$value['id']);
					$this->common_model->insertData('b_article_lang_values',$arr1);
				}
			}
			if($_FILES['logofile']['name']!='')
			{
				$filename = microtime().$_FILES['logofile']['name'];
				$imagefolder = 'img/media/';
				$uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
					'upload_path' 		=> FCPATH . $uploadpath . '/',
					'allowed_types' 	=> 'jpg|png|jpeg|PNG|JPG|JPEG',
					'encrypt_name' 		=> false,
					'file_name'			=> $filename,
					'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/article_add');
				}

				$uploaddata = $this->upload->data();
				$this->utility->pic_resize($uploadpath,$uploadpath,130,35);
				$file = array(
					'logo_path' 	=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
				);

				$this->common_model->UpdateData('b_article',$file,array('blog_id'=>$articleId));
			}
			if($_FILES['banner_image']['name']!='')
			{
				$filename = microtime().$_FILES['banner_image']['name'];
				$imagefolder = 'img/media/';
				$uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
					'upload_path' 		=> FCPATH . $uploadpath . '/',
					'allowed_types' 	=> 'jpg|png|jpeg|PNG|JPG|JPEG',
					'encrypt_name' 		=> false,
					'file_name'			=> $filename,
					'file_ext_tolower' 	=> true,
					//'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
				);

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

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

				$uploaddata = $this->upload->data();
				if($uploaddata)
				{
					$resize = $this->utility->pic_resize($uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),$uploadpath.'/thumb_article_banner/'.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),600,400);
					$file = array(
						'banner_image' 	=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
					);

					$this->common_model->UpdateData('b_article',$file,array('blog_id'=>$articleId));
				}
				
			}
            $this->utility->setMsg('Article details added','SUCCESS');	
			redirect(base_url().'admin/blog_article');
		}
		$content_data['category_list'] = $this->common_model->getAll('b_category',array('status'=>1,'lang_id'=>2));
		$content_data['total_article'] = $this->common_model->getAll('b_article');
		$content_data['language'] = $this->common_model->getAll('sys_languages');
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/blog_details/article_add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function article_edit($edit_id=''){
		if($edit_id!=''){
			$blog_id = base64_decode($edit_id);
			if($this->input->post()){
				$title_slug1 = strtolower(preg_replace('/[^A-Za-z0-9\. -]/', '', str_replace(' ', '-', $this->input->post('blog_title_2'))));
				$title_slug = preg_replace('/-{2,}/', '-', $title_slug1);
				$title_slug = substr($title_slug, 0, 25);
				$blogDetails_on = array(
	            	'meta_tag' => $this->input->post('meta_title_2'),
	            	'meta_keywords' => $this->input->post('meta_keywords_2'),
	            	'meta_description' => $this->input->post('meta_description_2'),
	            	'cat_id' => $this->input->post('cat_id'),
	            	'blog_title' => $this->input->post('blog_title_2'),
					'blog_slug'=>$title_slug,
	            	'blog_desc' => $this->input->post('blog_desc_2'),
					'intro_text' => $this->input->post('intro_text_2'),
					'lang_id'=>2,
					//'display_order' => $this->input->post('display_order')
	            );
	            $this->common_model->UpdateData('b_article',$blogDetails_on,array('blog_id'=>$blog_id));
				$languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$arr['blog_title'] = $this->input->post('blog_title_'.$value['id']);
						$arr['blog_desc'] = $this->input->post('blog_desc_'.$value['id']);
						$arr['meta_tag'] = $this->input->post('meta_title_'.$value['id']);
						$arr['meta_keywords'] = $this->input->post('meta_keywords_'.$value['id']);
						$arr['meta_description'] = $this->input->post('meta_description_'.$value['id']);
						$arr['intro_text'] = $this->input->post('intro_text_'.$value['id']);
						/*$exist = $this->common_model->getAllRow('b_article_lang_values',array('blog_id'=>$blog_id,'lang_id'=>$value['id']));
						if(empty($exist))
						{
							$arr1['blog_id'] = $blog_id;
							$arr1['blog_title'] = $this->input->post('blog_title_'.$value['id']);
							$arr1['blog_desc'] = $this->input->post('blog_desc_'.$value['id']);
							$arr1['meta_tag'] = $this->input->post('meta_title_'.$value['id']);
							$arr1['meta_description'] = $this->input->post('meta_description_'.$value['id']);
							$arr1['lang_id'] = $value['id'];
							$arr1['intro_text'] = $this->input->post('intro_text_'.$value['id']);
							$this->common_model->insertData('b_article_lang_values',$arr1);
						}
						else
						{
							$this->common_model->UpdateData('b_article_lang_values',$arr,array('blog_id'=>$blog_id,'lang_id'=>$value['id']));
						}*/
						$this->common_model->UpdateData('b_article_lang_values',$arr,array('blog_id'=>$blog_id,'lang_id'=>$value['id']));
						//echo $this->db->last_query(); die;
					}
				}
				if($_FILES['logofile']['name']!='')
				{
					$filename = microtime().$_FILES['logofile']['name'];
					$imagefolder = 'img/media/';
					$uploadpath = 'assets/userfiles/'. $imagefolder;
					$config = array(
						'upload_path' 		=> FCPATH . $uploadpath . '/',
						'allowed_types' 	=> 'jpg|png|jpeg|PNG|JPG|JPEG',
						'encrypt_name' 		=> false,
						'file_name'			=> $filename,
						'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/article_edit/'.$edit_id);
					}

					$uploaddata = $this->upload->data();
					$this->utility->pic_resize($uploadpath,$uploadpath,130,35);
					$file = array(
						'logo_path' 	=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
					);

					$this->common_model->UpdateData('b_article',$file,array('blog_id'=>$blog_id));
				}
				if($_FILES['banner_image']['name']!='')
				{
					$filename = microtime().$_FILES['banner_image']['name'];
					$imagefolder = 'img/media/';
					$uploadpath = 'assets/userfiles/'. $imagefolder;
					$config = array(
						'upload_path' 		=> FCPATH . $uploadpath . '/',
						'allowed_types' 	=> 'jpg|png|jpeg|PNG|JPG|JPEG',
						'encrypt_name' 		=> false,
						'file_name'			=> $filename,
						'file_ext_tolower' 	=> true,
						//'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
					);

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

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

					$uploaddata = $this->upload->data();
					if($uploaddata)
					{
						$resize = $this->utility->pic_resize($uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),$uploadpath.'/thumb_article_banner/'.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),600,400);
						$file = array(
							'banner_image' 	=> $uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),
						);

						$this->common_model->UpdateData('b_article',$file,array('blog_id'=>$blog_id));
					}
					
				}
	            $this->utility->setMsg('Article details updated','SUCCESS');	
				redirect(base_url().'admin/blog_article');
			}
			$content_data = array();
			$content_data['category_list'] = $this->common_model->getAll('b_category',array('status'=>1,'lang_id'=>2));
			$content_data['total_article'] = $this->common_model->getAll('b_article');
			$content_data['language'] = $this->common_model->getAll('sys_languages');
			$content_data['blogDetails'] = $this->common_model->getAllRow('b_article',array('blog_id'=>$blog_id)) ;
			$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_details/article_edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog_article/category_list');
		}
	}
	public function article_status($blog_id,$updated_status){
		$blog_id = base64_decode($blog_id);
		$update_status = array(
			'staus' => $updated_status
		);
		$this->common_model->UpdateData('b_article',$update_status,array('blog_id'=>$blog_id));
		$this->utility->setMsg('Status updated','SUCCESS');	
		redirect(base_url().'admin/blog_article');
	}
	public function visibility($blog_id,$updated_status){
		$blog_id = base64_decode($blog_id);
		$update_status = array(
			'is_home' => $updated_status
		);
		$this->common_model->UpdateData('b_article',$update_status,array('blog_id'=>$blog_id));
		$this->utility->setMsg('Home page show status updated','SUCCESS');	
		redirect(base_url().'admin/blog_article');
	}
	public function article_gallery($blog_id){
		$blog_id = base64_decode($blog_id);
		$content_data['gallery_list'] = $this->common_model->getAll('b_gallery',array('status'=>1,'b_article_id'=>$blog_id));
		if($this->input->post()){
			$uploaded_pictures = array();
			//echo '<pre>';print_r($_FILES);die;

			if ($_FILES['article_image']['name'][0]!='') {
                $filesCount = count($_FILES['article_image']['name']);
                for ($i = 0; $i < $filesCount; $i++) {
                    $_FILES['userFile']['name'] = time() . '_' . $_FILES['article_image']['name'][$i];
                    $_FILES['userFile']['type'] = $_FILES['article_image']['type'][$i];
                    $_FILES['userFile']['tmp_name'] = $_FILES['article_image']['tmp_name'][$i];
                    $_FILES['userFile']['error'] = $_FILES['article_image']['error'][$i];
                    $_FILES['userFile']['size'] = $_FILES['article_image']['size'][$i];
                    $dir = "assets/userfiles/blog_articles";
                    $config['upload_path'] = $dir;
                    $config['allowed_types'] = "*";
                    $config['encrypt_name'] = true;
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if ($this->upload->do_upload('userFile')) {
                        $fileData = $this->upload->data();
                        $picture_name = $fileData['file_name'];
                        $uploaded_pictures[] = array(
                        	'type' => 1,
                        	'path' => $picture_name,
                        	'status' => 1,
                        	'b_article_id' => $blog_id
                        );

                        $this->load->library('image_lib');
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = 'assets/userfiles/blog_articles/' . $picture_name;
                        $config['new_image'] = 'assets/userfiles/blog_articles/350_thumb/' . $picture_name;
                        $config['create_thumb'] = TRUE;
                        $config['maintain_ratio'] = TRUE;
                        $config['width'] = 350;
                        $config['height'] = 350;
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();
                        $this->image_lib->clear();

                        $this->load->library('image_lib');
                        $config1['image_library'] = 'gd2';
                        $config1['source_image'] = 'assets/userfiles/blog_articles/' . $picture_name;
                        $config1['new_image'] = 'assets/userfiles/blog_articles/900_thumb/' . $picture_name;
                        $config1['create_thumb'] = TRUE;
                        $config1['maintain_ratio'] = TRUE;
                        $config1['width'] = 900;
                        $config1['height'] = 600;
                        $config1['thumb_marker'] = '';
                        $this->image_lib->initialize($config1);
                        $this->image_lib->resize();
                        $this->image_lib->clear();
                        
                    } else {
                        $error = array('error' => $this->upload->display_errors());
                        print_r($error);
                        exit;
                    }
                }
            }if($this->input->post('video_url')!=''){
            	$url = $this->input->post('video_url');
            	/*if(strpos($url, 'youtube') > 0){
            		parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
					$uploaded_pictures[] = array(
	                	'type' => 2,
	                	'path' => $my_array_of_vars['v'],
	                	'status' => 1,
	                	'b_article_id' => $blog_id
	                );
            	}if(strpos($url, 'vimeo') > 0){
            		$vimeo_code = (int) substr(parse_url($url, PHP_URL_PATH), 1);
            		$uploaded_pictures[] = array(
	                	'type' => 3,
	                	'path' => $vimeo_code,
	                	'status' => 1,
	                	'b_article_id' => $blog_id
	                );
            	}*/
            	$uploaded_pictures[] = array(
	                	'type' => 3,
	                	'path' => $url,
	                	'status' => 1,
	                	'b_article_id' => $blog_id
	                );
				  

            }
            //echo "TEST";die;
            if(count($uploaded_pictures)==0){
            	$this->utility->setMsg('Please upload some images or add video url','ERROR');	
				redirect(base_url().'admin/blog_article/article_gallery/'.base64_encode($blog_id));
            }else{
            	//echo '<pre>';print_r($uploaded_pictures);die;
            	foreach($uploaded_pictures as $insert_value){
            		$this->common_model->insertData('b_gallery',$insert_value);
					
            	}
            	$this->utility->setMsg('Gallery updated successfully','SUCCESS');	
				redirect(base_url().'admin/blog_article/article_gallery/'.base64_encode($blog_id));
            }
		}
		//echo '<pre>';print_r($content_data);die;
		$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_details/article_gallery', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function delete_gallery($gallery_id,$blog_id){
		$gallery_id = base64_decode($gallery_id);
		//$blog_id = base64_decode($blog_id);
		$gallery_details = $this->common_model->getAllRow('b_gallery',array('gallery_id'=>$gallery_id)) ;
		if($gallery_details['type']==1){
			$explode_image = explode('.',$gallery_details['path']);
			$imageName = $explode_image[0].'_thumb.'.$explode_image[1];
			$main_image_path = 'assets/userfiles/blog_articles/'.$gallery_details['path'];
			$middle_thumb = 'assets/userfiles/blog_articles/350_thumb/'.$imageName;
			$big_thumb = 'assets/userfiles/blog_articles/900_thumb/'.$gallery_details['path'];
			if(file_exists($main_image_path)){
				unlink($main_image_path);
			}if(file_exists($middle_thumb)){
				unlink($middle_thumb);
			}if(file_exists($big_thumb)){
				unlink($big_thumb);
			}
		}
		$this->common_model->deleteData('b_gallery',array('gallery_id'=>$gallery_id));
		$this->utility->setMsg('Gallery deleted','SUCCESS');	
		redirect(base_url().'admin/blog_article/article_gallery/'.$blog_id);
	}
	public function category_list(){
		$content_data = array();
		$content_data['category'] = $this->common_model->getAll('b_category',array('lang_id'=>2),array('sys_languages','sys_languages.id = b_category.lang_id'),'','b_category.*,sys_languages.name');
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/blog_details/category_list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	
	public function category_add(){
		if($this->input->post()){
			$cat_name1 = strtolower(preg_replace('/[^A-Za-z0-9\. -]/', '', str_replace(' ', '-', $this->input->post('cat_name_2'))));
            $cat_name = preg_replace('/-{2,}/', '-', $cat_name1);
            $catDetails_con=array(
				'cat_name'=>$this->input->post('cat_name_2'),
				'cat_slug'=>$cat_name,
				'status'=>1,
				'add_date'=>date('Y-m-d H:i:s'),
				'lang_id'=>2,
			);//print_r($catDetails_con); die;
			$cat_id = $this->common_model->insertData('b_category',$catDetails_con);
			$languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr1['cat_id'] = $cat_id;
					$arr1['lang_id'] = $value['id'];
					$arr1['cat_name'] = $this->input->post('cat_name_'.$value['id']);
					$this->common_model->insertData('b_category_lang_values',$arr1);
				}
			}
			$this->utility->setMsg('Category details added','SUCCESS');	
			redirect(base_url().'admin/blog_article/category_list');
		}
		$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['language'] = $this->common_model->getAll('sys_languages');
		$data['content'] = $this->load->view('admin/blog_details/category_add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function category_edit($edit_id=''){
		if($edit_id!=''){
			$cat_id = base64_decode($edit_id);
			if($this->input->post()){
				$cat_name1 = strtolower(preg_replace('/[^A-Za-z0-9\. -]/', '', str_replace(' ', '-', $this->input->post('cat_name_2'))));
				$cat_name = preg_replace('/-{2,}/', '-', $cat_name1);
				$catDetails_con=array(
					'cat_name' => $this->input->post('cat_name_2'),
					'cat_slug'=>$cat_name,
					'lang_id'=>2,
				);
				$this->common_model->UpdateData('b_category',$catDetails_con,array('cat_id'=>$cat_id));
				$languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$arr['cat_name'] = $this->input->post('cat_name_'.$value['id']);
						$exist = $this->common_model->getAllRow('b_category_lang_values',array('cat_id'=>$cat_id,'lang_id'=>$value['id']));
						if(empty($exist))
						{
							$arr1['cat_id'] = $cat_id;
							$arr1['lang_id'] = $value['id'];
							$arr1['cat_name'] = $this->input->post('cat_name_'.$value['id']);
							$this->common_model->insertData('b_category_lang_values',$arr1);
						}
						else
						{
							$this->common_model->UpdateData('b_category_lang_values',$arr,array('cat_id'=>$cat_id,'lang_id'=>$value['id']));
						}
						//echo $this->db->last_query(); die;
					}
				}
				$this->utility->setMsg('Category details update','SUCCESS');	
				redirect(base_url().'admin/blog_article/category_list');
			}
			$content_data = array();
			$content_data['categoryDetails'] = $this->common_model->getAllRow('b_category',array('cat_id'=>$cat_id)) ;
			$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['language'] = $this->common_model->getAll('sys_languages');
			$data['content'] = $this->load->view('admin/blog_details/category_edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog_article/category_list');
		}
		
	}
	public function category_status($cat_id,$updated_status){
		$cat_id = base64_decode($cat_id);
		$existance = $this->common_model->getAll('b_article',array('cat_id'=>$cat_id)) ;
		if(count($existance)>0){
			$this->utility->setMsg('First you have to delete all the articles under this category.','ERROR');	
			redirect(base_url().'admin/blog_article/category_list');
		}else{
			$update_status = array(
				'status' => $updated_status
			);
			$this->common_model->UpdateData('b_category',$update_status,array('cat_id'=>$cat_id));
			$this->utility->setMsg('Status updated','SUCCESS');	
			redirect(base_url().'admin/blog_article');
		}
		
	}
	public function category_delete($cat_id='')
	{
		//echo $cat_id;die;
		if($cat_id!='')
		{
			$cat_id=base64_decode($cat_id);
			$existance = $this->common_model->getAll('b_article',array('cat_id'=>$cat_id)) ;
			//echo '<pre>';print_r($existance);die;
			if(count($existance)>0){
				$this->utility->setMsg('First you have to delete all the articles under this category.','ERROR');	
				redirect(base_url().'admin/blog_article/category_list');
			}else{
				$this->common_model->deleteData('b_category',array('cat_id'=>$cat_id));
				$this->utility->setMsg('Category deleted','SUCCESS');	
				redirect(base_url().'admin/blog_article/category_list');
			}
			// $this->common_model->deleteData('b_category',array('cat_id'=>$cat_id));
			// $this->utility->setMsg('Category deleted','SUCCESS');	
			// redirect(base_url().'admin/blog_article/category_list');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog_article/category_list');
		}
	}
	public function article_delete($cat_id='')
	{
		//echo $cat_id;die;
		if($cat_id!='')
		{
			$cat_id=base64_decode($cat_id);
			$this->common_model->deleteData('b_article',array('blog_id'=>$cat_id));
			$this->common_model->deleteData('b_gallery',array('b_article_id'=>$cat_id));
			$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 display_order($display_order,$type,$blog_id){
		if($type == 1)
		{
			$new = $display_order-1;
		}
		else
		{
			$new = $display_order+1;
		}
		$update_status = array(
				'display_order' => $new,
			);
		$get = $this->common_model->getAllRow('b_article',array('display_order'=>$new));
		$this->common_model->UpdateData('b_article',array('display_order'=>$display_order),array('blog_id'=>$get['blog_id']));
		$this->common_model->UpdateData('b_article',$update_status,array('blog_id'=>$blog_id));
		$this->utility->setMsg('Display order updated','SUCCESS');	
		redirect(base_url().'admin/blog_article');
	}
	public function add_text($edit_id='',$uri){
		if($edit_id!=''){
			$cat_id = base64_decode($edit_id);
			if($this->input->post()){
				$catDetails_con=array(
					'text' => $this->input->post('text'),
				);
				$this->common_model->UpdateData('b_gallery',$catDetails_con,array('gallery_id'=>$cat_id));
				$this->utility->setMsg('Gallery text update','SUCCESS');	
				redirect(base_url().str_replace('-', '/', $uri));
			}
			$content_data = array();
			$content_data['galleryDetails'] = $this->common_model->getAllRow('b_gallery',array('gallery_id'=>$cat_id)) ;
			$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_details/add_text', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().str_replace('-', '/', $uri));
		}
	}
	public function terms(){
		//echo "TEST";die;
		$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['terms'] = $this->common_model->getAll('std_contents',array('lang_id'=>2));
		//echo '<pre>';print_r($content_data);die;
		$data['content'] = $this->load->view('admin/blog_details/terms', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function terms_edit($edit_id=''){
		if($edit_id!=''){
			$terms_id = base64_decode($edit_id);
			if($this->input->post()){
				$languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						/*$arr['text'] = $this->input->post('terms_'.$value['id']);
						$exist = $this->common_model->getAllRow('std_contents',array('id'=>$terms_id,'lang_id'=>$value['id']));
						if(empty($exist))
						{
							$arr1['lang_id'] = $value['id'];
							$arr1['text'] = $this->input->post('terms_'.$value['id']);
							$this->common_model->insertData('std_contents',$arr1);
						}
						else
						{
							$this->common_model->UpdateData('std_contents',$arr,array('id'=>$terms_id,'lang_id'=>$value['id']));
						}*/
						//echo $this->db->last_query(); die;
						$arr1['lang_id'] = $value['id'];
						$arr1['text'] = $this->input->post('terms_'.$value['id']);
						$this->common_model->UpdateData('std_contents',$arr1,array('lang_id'=>$value['id']));
					}
				}
				$this->utility->setMsg('Terms details update','SUCCESS');	
				redirect(base_url().'admin/blog_article/terms');
			}
			$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['language'] = $this->common_model->getAll('sys_languages');
			$data['content'] = $this->load->view('admin/blog_details/terms_edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}else{
			//echo "Edit Failed";die;
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/blog_article/terms');
		}
		
	}
	public function delete_logo($enc_userID='',$uri='')
	{
		$blog_id=base64_decode($enc_userID);
		$details = $this->common_model->getAllRow('b_article',array('blog_id'=>$blog_id));
		if($details)
		{
			@unlink(FCPATH.'assets/userfiles/img/media/'.$details['logo_path']);
		}
		$this->common_model->UpdateData('b_article',array('logo_path'=>''),array('blog_id'=>$blog_id));
		$this->utility->setMsg('Logo deleted successfully','SUCCESS');	
		redirect(base_url().str_replace('-', '/', $uri));
	}
}