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

class Cms extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_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(){
		$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['cms'] = $this->common_model->getAllMultipleJoin('cms_page','',array(array('sys_languages','cms_page.language_id = sys_languages.id'),array('sys_files','sys_files.id = cms_page.thumb_id')),'','sys_languages.name as lang_name,cms_page.*') ;
		$data['content'] = $this->load->view('admin/cms/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function add()
	{
		if($this->input->post())
		{
			$title=$this->utility->info_cleanQuery($this->input->post('title'));
			$content=$this->utility->info_cleanQuery($this->input->post('content'));
			$language=$this->utility->info_cleanQuery($this->input->post('language'));
			$pageVideo=$this->utility->info_cleanQuery($this->input->post('pageVideo'));
			$pageTitle=$this->utility->info_cleanQuery($this->input->post('page_title'));
			$sub_head=$this->utility->info_cleanQuery($this->input->post('sub_head'));
			$page_title=$this->utility->info_cleanQuery($this->input->post('page_title'));
			$meta_title=$this->utility->info_cleanQuery($this->input->post('meta_title'));
			$meta_keywords=$this->utility->info_cleanQuery($this->input->post('meta_keywords'));
			$meta_desc=$this->utility->info_cleanQuery($this->input->post('meta_desc'));
			$preview_alt =$this->utility->info_cleanQuery($this->input->post('preview_alt'));
			$page_alt =$this->utility->info_cleanQuery($this->input->post('page_alt'));
			
			$title_alias = preg_replace('/[^\00-\255]+/u', '', $title);

			$cms_con=array(
						'title'=>$title,
						'alias'=>strtolower(trim(str_replace(' ', '-', $title_alias))),
						'content'=>$content,
						'language_id'=>$language,
						'video_url'=>$pageVideo,
						'section_id'=>$this->input->post('secton_id'),
						'page_title'=>$pageTitle,
						'sub_head'=>$sub_head,
						'pagetitle'=>$page_title,
						'meta_title'=>$meta_title,
						'meta_keywords'=>$meta_keywords,
						'meta_desc'=>$meta_desc,
						'preview_alt'=>$preview_alt,
						'page_alt'=>$page_alt,
				);
			$cmsId = $this->common_model->insertData('cms_page',$cms_con);
			if($_FILES['previewImage']['name']!='')
			{
				$imagefolder = 'img/content/';
				$thumbfolder = 'img/menu/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
		        $thumbpath = 'assets/userfiles/'. $thumbfolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'jpg|png',
		            'encrypt_name' 		=> false,
		            'file_ext_tolower' 	=> true,
		            'file_name'			=> $preview_alt,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

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

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

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

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

				$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' 	=> 'Bild von ' . $username,
		        );
		        $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('cms_page',array('image_id'=>$fileId),array('id'=>$cmsId));
				$resizeBlur = $this->avater($uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1));
		        /******************thumb image************************/	
		        $file = array(
		            'user_id' 	=> 1,
		            'path' 		=> $thumbfolder,
		            'filename' 	=> $uploaddata['raw_name'].'_thumb',
		            'extension' => substr($uploaddata['file_ext'], 1),
		            'mimetype' 	=> $uploaddata['file_type'],
		            'is_image' 	=> $uploaddata['is_image'],
		            //'attr_alt' 	=> 'Bild von ' . $username,
		        );
		        $imagesize = getimagesize($uploaddata['full_path']);
		        $file['width'] 	= $imagesize[0];
		        $file['height'] = $imagesize[1];
		        $thumbId = $this->common_model->insertData('sys_files',$file);
		        $this->common_model->UpdateData('cms_page',array('thumb_id'=>$thumbId),array('id'=>$cmsId));
				//echo $this->db->last_query(); die;
			}
			if($_FILES['pageImage']['name']!='')
			{
				$imagefolder = 'img/site-introduction/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'jpg|png',
		            'encrypt_name' 		=> false,
		            'file_ext_tolower' 	=> true,
		            'file_name'			=> $page_alt,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

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

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

		        $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' 	=> 'Bild von ' . $username,
		        );

		        $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('cms_page',array('page_image_id'=>$fileId),array('id'=>$cmsId));
			}
			$sectiontitle = $this->input->post('section_title');
			$section = $this->input->post('section_content');
			$section_video = $this->input->post('section_video');
			$content_image_alt = $this->input->post('content_image_alt');
			/*if($_FILES['section_image']['name']!='')
			{
				for($i=0; $i<count($_FILES['section_image']['name']); $i++) {
					$section_title = $sectiontitle[$i];
					$section_content = $section[$i];
					$section_video = $section_video[$i];
					$arrSection = array(
										'page_id'=>$cmsId,
										'content_title'=>$section_title,
										'page_content'=>$section_content,
										'page_video'=>$section_video,
										);
					$sectionId = $this->common_model->insertData('cms_page_content',$arrSection);
					if($_FILES['section_image']['name'][$i]!='')
					{
						

						$_FILES['userFile']['name'] = $_FILES['section_image']['name'][$i];
		                $_FILES['userFile']['type'] = $_FILES['section_image']['type'][$i];
		                $_FILES['userFile']['tmp_name'] = $_FILES['section_image']['tmp_name'][$i];
		                $_FILES['userFile']['error'] = $_FILES['section_image']['error'][$i];
		                $_FILES['userFile']['size'] = $_FILES['section_image']['size'][$i];

						$imagefolder = 'img/section-image/';
				        $uploadpath = 'assets/userfiles/'. $imagefolder;
						$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);
				        $this->upload->initialize($config);
				        if(!$this->upload->do_upload('userFile')) {

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

				        $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' 	=> 'Bild von ' . $username,
				        );

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

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


				        $sectionImgId = $this->common_model->insertData('sys_files',$file);
				        $this->common_model->UpdateData('cms_page_content',array('content_image_id'=>$sectionImgId),array('id'=>$sectionId));
					}
				}
			}
			else
			{
				for($i=0; $i<count($_FILES['section_image']['name']); $i++) {
					$section_title = $sectiontitle[$i];
					$section_content = $section[$i];
					$section_video = $section_video[$i];
					$arrSection = array(
										'page_id'=>$cmsId,
										'content_title'=>$section_title,
										'page_content'=>$section_content,
										'page_video'=>$section_video,
										);
					$sectionId = $this->common_model->insertData('cms_page_content',$arrSection);
				}	
			}*/ 
			foreach ($sectiontitle as $key => $sec_title) {
				$section_title = $sectiontitle[$key];
				$section_content = $section[$key];
				$sectionvideo = $section_video[$key];
				$contentimagealt = $content_image_alt[$key];
				$arrSection = array(
									'page_id'=>$cmsId,
									'content_title'=>$section_title,
									'page_content'=>$section_content,
									'page_video'=>$sectionvideo,
									'content_image_alt'=>$contentimagealt,
									);
				$sectionId = $this->common_model->insertData('cms_page_content',$arrSection);
				
				if($_FILES['section_image']['name'][$key]!='')
				{
					$_FILES['userFile']['name'] = $_FILES['section_image']['name'][$key];
	                $_FILES['userFile']['type'] = $_FILES['section_image']['type'][$key];
	                $_FILES['userFile']['tmp_name'] = $_FILES['section_image']['tmp_name'][$key];
	                $_FILES['userFile']['error'] = $_FILES['section_image']['error'][$key];
	                $_FILES['userFile']['size'] = $_FILES['section_image']['size'][$key];
	                $imagefolder = 'img/section-image/';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
					$config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'jpg|png',
			            'encrypt_name' 		=> false,
			            'file_ext_tolower' 	=> true,
			            'file_name'			=> $contentimagealt
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );

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

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

			        $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' 	=> 'Bild von ' . $username,
			        );

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

			        $file['width'] 	= $imagesize[0];
			        $file['height'] = $imagesize[1];
			        
			        $sectionImgId = $this->common_model->insertData('sys_files',$file);
			        if($sectionImgId)
			        {
			        	$this->common_model->UpdateData('cms_page_content',array('content_image_id'=>$sectionImgId),array('id'=>$sectionId));
			        }
			        
				}
			}
			$this->utility->setMsg('Cms details added','SUCCESS');	
			redirect(base_url().'admin/cms');
		}
		$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','','','id DESC') ;
		$data['content'] = $this->load->view('admin/cms/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function delete($enc_userID='')
	{
		if($enc_userID!='')
		{
			$cmsId=base64_decode($enc_userID);
			$this->common_model->deleteData('cms_page_content',array('page_id'=>$cmsId));
			$this->common_model->deleteData('cms_page',array('id'=>$cmsId));
			$this->utility->setMsg('Cms page deleted','SUCCESS');	
			redirect(base_url().'admin/cms');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/cms');
		}
	}
	public function edit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$cmsId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$title=$this->utility->info_cleanQuery($this->input->post('title'));
				$content=$this->utility->info_cleanQuery($this->input->post('content'));
				$language=$this->utility->info_cleanQuery($this->input->post('language'));
				$pageVideo=$this->utility->info_cleanQuery($this->input->post('pageVideo'));
				$pageTitle=$this->utility->info_cleanQuery($this->input->post('page_title'));
				$sub_head=$this->utility->info_cleanQuery($this->input->post('sub_head'));
				$page_title=$this->utility->info_cleanQuery($this->input->post('page_title'));
				$meta_title=$this->utility->info_cleanQuery($this->input->post('meta_title'));
				$meta_keywords=$this->utility->info_cleanQuery($this->input->post('meta_keywords'));
				$meta_desc=$this->utility->info_cleanQuery($this->input->post('meta_desc'));
				$preview_alt =$this->utility->info_cleanQuery($this->input->post('preview_alt'));
				$page_alt =$this->utility->info_cleanQuery($this->input->post('page_alt'));
				$banner_text =$this->utility->info_cleanQuery($this->input->post('banner_text'));
				
				$title_alias = preg_replace('/[^\00-\255]+/u', '', $title);

				$titleExist = $this->common_model->getAllRow('cms_page',array('title'=>$title_alias,'id !='=>$cmsId));
				//echo $this->db->last_query();die;
				$cms_con=array(
							'title'=>$title,
							//'alias'=>strtolower(trim(str_replace(' ', '-', $title))),
							'content'=>$content,
							'language_id'=>$language,
							'video_url'=>$pageVideo,
							'section_id'=>$this->input->post('secton_id'),
							'page_title'=>$pageTitle,
							'sub_head'=>$sub_head,
							'pagetitle'=>$page_title,
							'meta_title'=>$meta_title,
							'meta_keywords'=>$meta_keywords,
							'meta_desc'=>$meta_desc,
							'preview_alt'=>$preview_alt,
							'page_alt'=>$page_alt,
							'banner_text'=>$banner_text,
					);
				if(count($titleExist)>0)
				{
					$titleNew = $title_alias.'-'.$cmsId;
					$cms_con = array_merge(['alias'=>strtolower(trim(str_replace(' ', '-', $titleNew)))],$cms_con);
				}
				else
				{
					$cms_con = array_merge(['alias'=>strtolower(trim(str_replace(' ', '-', $title_alias)))],$cms_con);
				}
				$this->common_model->UpdateData('cms_page',$cms_con,array('id'=>$cmsId));
				if($_FILES['previewImage']['name']!='')
				{
					$imagefolder = 'img/content/';
					$thumbfolder = 'img/menu/';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
			        $thumbpath = 'assets/userfiles/'. $thumbfolder;
					$config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'jpg|png',
			            'encrypt_name' 		=> false,
			            'file_ext_tolower' 	=> true,
			            'file_name'			=> $this->getFileName($preview_alt),
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );
					//print_r($config); die;
			        $this->load->library('upload', $config);
			       
			        if(!$this->upload->do_upload('previewImage')) {

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

			        $uploaddata = $this->upload->data();
			        //print_r($uploaddata); die;
			        $config['image_library'] = 'gd2';
					$config['source_image'] = $uploadpath.'/'.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1);
					$config['new_image']  =   FCPATH . $thumbpath;
					$config['create_thumb'] = TRUE;
					$config['maintain_ratio'] = TRUE;
					$config['width']         = 275;
					$config['height']       = 160;

					$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' 	=> 'Bild von ' . $username,
			        );
			        $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('cms_page',array('image_id'=>$fileId),array('id'=>$cmsId));
					$resizeBlur = $this->avater($uploaddata['raw_name'] .'.'. substr($uploaddata['file_ext'], 1));
			        /******************thumb image************************/	
			        $file = array(
			            'user_id' 	=> 1,
			            'path' 		=> $thumbfolder,
			            'filename' 	=> $uploaddata['raw_name'].'_thumb',
			            'extension' => substr($uploaddata['file_ext'], 1),
			            'mimetype' 	=> $uploaddata['file_type'],
			            'is_image' 	=> $uploaddata['is_image'],
			            //'attr_alt' 	=> 'Bild von ' . $username,
			        );
			        $imagesize = getimagesize($uploaddata['full_path']);
			        $file['width'] 	= $imagesize[0];
			        $file['height'] = $imagesize[1];
			        $thumbId = $this->common_model->insertData('sys_files',$file);
			        $this->common_model->UpdateData('cms_page',array('thumb_id'=>$thumbId),array('id'=>$cmsId));
					//echo $this->db->last_query(); die;
				}
				if($_FILES['pageImage']['name']!='')
				{
					$imagefolder = 'img/site-introduction/';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
					$config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'jpg|png',
			            'encrypt_name' 		=> false,
			            'file_ext_tolower' 	=> true,
			            'file_name'			=> $this->getFileName($page_alt),
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );

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

			            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
						redirect(base_url().'admin/cms/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' 	=> 'Bild von ' . $username,
			        );

			        $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('cms_page',array('page_image_id'=>$fileId),array('id'=>$cmsId));
				}
				$sectiontitle = $this->input->post('section_title');
				$section = $this->input->post('section_content');
				$section_video = $this->input->post('section_video');
				
				/*if($_FILES['section_image']['name']!='')
				{
					for($i=0; $i<count($_FILES['section_image']['name']); $i++) {
						if($section[$i]!='')
						{
							$section_title = $sectiontitle[$i];
							$section_content = $section[$i];
							$section_video = $section_video[$i];
							$arrSection = array(
												'page_id'=>$cmsId,
												'content_title'=>$section_title,
												'page_content'=>$section_content,
												'page_video'=>$section_video,
												);
							$sectionId = $this->common_model->insertData('cms_page_content',$arrSection,array('page_id'=>$cmsId));
						}
						
						if($_FILES['section_image']['name'][$i]!='')
						{
							$_FILES['userFile']['name'] = $_FILES['section_image']['name'][$i];
			                $_FILES['userFile']['type'] = $_FILES['section_image']['type'][$i];
			                $_FILES['userFile']['tmp_name'] = $_FILES['section_image']['tmp_name'][$i];
			                $_FILES['userFile']['error'] = $_FILES['section_image']['error'][$i];
			                $_FILES['userFile']['size'] = $_FILES['section_image']['size'][$i];

							$imagefolder = 'img/section-image/';
					        $uploadpath = 'assets/userfiles/'. $imagefolder;
							$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);
					        $this->upload->initialize($config);
					        if(!$this->upload->do_upload('userFile')) {

					            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
								redirect(base_url().'admin/cms/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' 	=> 'Bild von ' . $username,
					        );

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

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


					        $sectionImgId = $this->common_model->insertData('sys_files',$file);
					        $this->common_model->UpdateData('cms_page_content',array('content_image_id'=>$sectionImgId),array('id'=>$sectionId));
						}
					}
				}*/
				
					$pageDetails = $this->common_model->getAll('cms_page_content',array('page_id'=>$cmsId));
					//print_r($pageDetails); 
					//print_r($_FILES); die;
					foreach ($pageDetails as $key => $value) {
						$sectiontitle = $this->input->post('section_title_'.$pageDetails[$key]['id']);
						$section_content = $this->input->post('section_content_'.$pageDetails[$key]['id']);
						$section_video = $this->input->post('section_video_'.$pageDetails[$key]['id']);
						$content_image_alt = $this->input->post('content_image_alt_'.$pageDetails[$key]['id']);
						$arrSection = array(
											'content_title'=>$sectiontitle,
											'page_content'=>$section_content,
											'page_video'=>$section_video,
											'content_image_alt'=>$content_image_alt,
											);
						$this->common_model->UpdateData('cms_page_content',$arrSection,array('id'=>$pageDetails[$key]['id']));
						//print_r($_FILES['section_image_'.$pageDetails[$key]['id']]['name']); die;
						if($_FILES['section_image_'.$pageDetails[$key]['id']]['name']!='')
						{
							$imagefolder = 'img/section-image/';
					        $uploadpath = 'assets/userfiles/'. $imagefolder;
							$config = array(
					            'upload_path' 		=> FCPATH . $uploadpath . '/',
					            'allowed_types' 	=> 'jpg|png',
					            'encrypt_name' 		=> false,
					            'file_ext_tolower' 	=> true,
					            'file_name'			=> $this->getFileName($content_image_alt),
					            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
					        );

					        $this->load->library('upload', $config);
					        $this->upload->initialize($config);
					        if(!$this->upload->do_upload('section_image_'.$pageDetails[$key]['id'])) {

					            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
								redirect(base_url().'admin/cms/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' 	=> 'Bild von ' . $username,
					        );

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

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


					        $sectionImgId = $this->common_model->insertData('sys_files',$file);
					        if($sectionImgId)
					        {
					        	$this->common_model->UpdateData('cms_page_content',array('content_image_id'=>$sectionImgId),array('id'=>$pageDetails[$key]['id']));
							}
						}
					}


				/***************NEW SECTION*****************/	
					$sectiontitle = $this->input->post('section_title');
					$section = $this->input->post('section_content');
					$section_video = $this->input->post('section_video');
					$content_image_alt = $this->input->post('content_image_alt');
					foreach ($sectiontitle as $key => $sec_title) {
						$section_title = $sectiontitle[$key];
						$section_content = $section[$key];
						$sectionvideo = $section_video[$key];
						$contentimage_alt = $content_image_alt[$key];
						$arrSection = array(
											'page_id'=>$cmsId,
											'content_title'=>$section_title,
											'page_content'=>$section_content,
											'page_video'=>$sectionvideo,
											'content_image_alt'=>$contentimage_alt,
											);
						$sectionId = $this->common_model->insertData('cms_page_content',$arrSection);
						
						if($_FILES['section_image']['name'][$key]!='')
						{
							$_FILES['userFile']['name'] = $_FILES['section_image']['name'][$key];
			                $_FILES['userFile']['type'] = $_FILES['section_image']['type'][$key];
			                $_FILES['userFile']['tmp_name'] = $_FILES['section_image']['tmp_name'][$key];
			                $_FILES['userFile']['error'] = $_FILES['section_image']['error'][$key];
			                $_FILES['userFile']['size'] = $_FILES['section_image']['size'][$key];
			                $imagefolder = 'img/section-image/';
					        $uploadpath = 'assets/userfiles/'. $imagefolder;
							$config = array(
					            'upload_path' 		=> FCPATH . $uploadpath . '/',
					            'allowed_types' 	=> 'jpg|png',
					            'encrypt_name' 		=> false,
					            'file_ext_tolower' 	=> true,
					            'file_name'			=> $contentimage_alt,
					            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
					        );

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

					            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
								redirect(base_url().'admin/cms/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' 	=> 'Bild von ' . $username,
					        );

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

					        $file['width'] 	= $imagesize[0];
					        $file['height'] = $imagesize[1];
					        
					        $sectionImgId = $this->common_model->insertData('sys_files',$file);
					        if($sectionImgId)
					        {
					        	$this->common_model->UpdateData('cms_page_content',array('content_image_id'=>$sectionImgId),array('id'=>$sectionId));
					        }
					        
						}
					}
				/***************NEW SECTION*****************/	
				
				$this->utility->setMsg('Cms details updated','SUCCESS');	
				redirect(base_url().'admin/cms/edit/'.$enc_userID);
			}
			$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['cmsSectionDetails'] = $this->common_model->getAllMultipleJoin('cms_page_content',array('cms_page_content.page_id'=>$cmsId),array(array('sys_files','sys_files.id = cms_page_content.content_image_id')),'','cms_page_content.*, sys_files.filename,sys_files.path,sys_files.extension ') ;
			$content_data['cmsDetails'] = $this->common_model->getAllRowMultipleJoin('cms_page',array('cms_page.id'=>$cmsId),array(array('sys_languages','cms_page.language_id = sys_languages.id'),array('sys_files','sys_files.id = cms_page.image_id')),'','sys_languages.name as lang_name,cms_page.*,sys_files.path,sys_files.filename,sys_files.extension') ;
			//print_r($content_data); die;
			$content_data['language'] = $this->common_model->getAll('sys_languages') ;
			$data['content'] = $this->load->view('admin/cms/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/cms');
		}	
	}
	public function deleteContentDetails()
	{
		$id=$this->input->post('id');
		$this->common_model->deleteData('cms_page_content',array('id'=>$id));
	}
	public function 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')) ;
		//$where = "id in (421,618)";
		$content_data['terms'] = $this->common_model->getAllMultipleJoin('std_contents',array('lang_id'=>2)) ;
		//echo $this->db->last_query(); die;
		$data['content'] = $this->load->view('admin/cms/termslist', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function termsedit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$archieveId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$this->load->library('Utility');
				//$text=$this->utility->info_cleanQuery($this->input->post('text'));
				$languages = $this->common_model->getAll('sys_languages');
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$exist = $this->common_model->getAllRow('std_contents',array('lang_id'=>$value['id']));
						if(empty($exist))
						{
							$arr1['lang_id'] = $value['id'];
							$arr1['text'] = $this->input->post('text_'.$value['id']);
							$this->common_model->insertData('std_contents',$arr1);
						}
						else
						{ 	
							$archDetails_con=array(
										'text'=>$this->input->post('text_'.$value['id']),
									);
							$this->common_model->UpdateData('std_contents',$archDetails_con,array('id'=>$archieveId));
						}
					}
				}	
				$this->utility->setMsg('Terms update','SUCCESS');	
				redirect(base_url().'admin/cms/termsedit/'.$enc_userID);
			}

			$content_data = array();
			$content_data['terms'] = $this->common_model->getAllRowMultipleJoin('std_contents',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/cms/termsedit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/cms/terms');
		}
	}
	public function privacy(){
		$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')) ;
		//$where = "id in (421,618)";
		$content_data['privacy'] = $this->common_model->getAllMultipleJoin('privacy_policy',array('lang_id'=>2)) ;
		//echo $this->db->last_query(); die;
		$data['content'] = $this->load->view('admin/cms/privacylist', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function privacyedit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$archieveId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$this->load->library('Utility');
				//$text=$this->utility->info_cleanQuery($this->input->post('text'));
				$languages = $this->common_model->getAll('sys_languages');
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						$archDetails_con=array(
										'content'=>$this->input->post('content_'.$value['id']),
									);
							$this->common_model->UpdateData('privacy_policy',$archDetails_con,array('lang_id'=>$value['id']));
						
					}
				}	
				$this->utility->setMsg('Privacy update','SUCCESS');	
				redirect(base_url().'admin/cms/privacyedit/'.$enc_userID);
			}

			$content_data = array();
			$content_data['privacy'] = $this->common_model->getAllRowMultipleJoin('privacy_policy',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/cms/privacyedit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/cms/terms');
		}
	}
	/***************************************/
	public function dirToArray($dir){
  
			$result = array();
			$cdir = scandir($dir);
		   foreach ($cdir as $key => $value)
		   {
			  if (!in_array($value,array(".","..")))
			  {
				 if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
				 {
					$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
				 }
				 else
				 {
					$result[] = $value;
				 }
			  }
		   }
  		return $result;
	}
	
	public function createThumb($spath, $dpath, $maxd) {
	$info = getimagesize($spath);
    $mime = $info['mime'];
	//var_dump($mime);
	if($mime=='image/png'){
		
			$src = ImageCreateFromPNG($spath);
			if (!$src) {return false;} else {
		  $srcw=imagesx($src);
		  $srch=imagesy($src);
		  if ($srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
		  else {$width=$maxd;$height=floor($srch*$width/$srcw);}
		  if ($width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;}  //if image is actually smaller than you want, leave small (remove this line to resize anyway)
		  $thumb=imagecreatetruecolor($width, $height);
			imagealphablending($thumb, false);
		 //if ($height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
		 // else {
			imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
			//  }
			
				imagepng($thumb, $dpath,9);
			}
		 return true;
	}else{
	
		$src=@imagecreatefromjpeg($spath);

	if (!$src) {return false;} else {
	  $srcw=imagesx($src);
	  $srch=imagesy($src);
	  if ($srcw<$srch) {$height=$maxd;$width=floor($srcw*$height/$srch);}
	  else {$width=$maxd;$height=floor($srch*$width/$srcw);}
	  if ($width>$srcw && $height>$srch) {$width=$srcw;$height=$srch;}  //if image is actually smaller than you want, leave small (remove this line to resize anyway)
	  $thumb=imagecreatetruecolor($width, $height);
	// if ($height<100) {imagecopyresized($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));}
	//  else {
		imagecopyresampled($thumb, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
		//  }
		
			imagejpeg($thumb, $dpath,90);
		}
		 return true;
	}
  
 
	}
	//avater
	public function avater($picval=''){
		//$sizes=array('60','80','120','160','240','320');
	$sizes=array('250','500','750','1000');
	   $picdir[]=$this->dirToArray('assets');
		$avatar=$picdir[0]['userfiles']['img']['content'];
				foreach($sizes as $size){
				
				$toWidth=$toHeight=$size;
				//destination path
				$folderpath = 'assets/userfiles/img/content/'.$toWidth;

				$org_path = 'assets/userfiles/img/content/'; //source path
				if (!file_exists($folderpath)) { // file exist in target folder
					mkdir($folderpath, 0777, true);
				}
				$source_path = $org_path.'/'.$picval;
				$d_path = $folderpath.'/'.$picval;
				$this->createThumb($source_path,$d_path,$toWidth);
			}//end of avaters resizer
		
		return '1';
	}
	function getFileName($text)
	{
		if($text)
		{
			$text1 = str_replace(' ', '_', $text);
			$text2 = str_replace(',', '_', $text1);
			$text3 = str_replace('(', '_', $text2);
			$text4 = str_replace(')', '_', $text3);
			$text5 = str_replace('-', '_', $text4);
			$name = $text5.rand(0,20);
			$previewExist = $this->common_model->getAll('sys_files',array('filename'=>$name));
			if(count($previewExist)>0)
			{
				$name = $text5.rand(0,20);
			}
			return $name;
		}
	}
}