<?php 
class posts extends CI_Controller {
function posts ()	 
	{  
    parent::__construct();	
    $this->load->library('session');	
    $this->load->Model('functions', 'fun');  //function in model 
    $this->load->library('pagination');
    $this->load->library('DX_Auth');	
	$this->dx_auth->check_uri_permissions();
	$this->load->helper('date');

    } 
function moveUp($type,$status,$id,$order)
{
    $this->fun->moveUp($type,$status,$id,$order);
    echo "<script type=\"text/javascript\">window.location.href= \"".base_url()."index.php/posts/index/".$type."/".$status."\";</script>";

}

function movefirst($type,$status,$id,$order)
{
    $this->fun->movefirst($type,$status,$id,$order);
    echo "<script type=\"text/javascript\">window.location.href= \"".base_url()."index.php/posts/index/".$type."/".$status."\";</script>";

} 
 

 function movedown($type,$status,$id,$order)
{
    $this->fun->movedown($type,$status,$id,$order);
    echo "<script type=\"text/javascript\">window.location.href= \"".base_url()."index.php/posts/index/".$type."/".$status."\";</script>";

} 
   
function movelast($type,$status,$id,$order)
{
    $this->fun->movelast($type,$status,$id,$order);
    echo "<script type=\"text/javascript\">window.location.href= \"".base_url()."index.php/posts/index/".$type."/".$status."\";</script>";

} 	
	function index($type,$status)
	{   
	    $this->load->model('posts_model');  /// model file name
	    	
	   //////pagination styl and config
	    $config['base_url'] = base_url().'index.php/posts/index/'.$type.'/'.$status.'';
		$config['total_rows'] = $this->posts_model->get_num_posts($type,$status);
		$config['per_page'] = 25;
		$config['num_links'] = 3; 
		$config['uri_segment'] = 5;	
		$config['full_tag_open']='<div class="pagination">';
		$config['full_tag_close'] = '</div>';
		$config['next_link'] = 'Next';
		$config['prev_link'] = 'Prev';
		$config['cur_tag_open'] = '<span class="current">';
		$config['cur_tag_close'] = '</span>';		
		$offset = $this->uri->segment(5,0);
		$this->session->set_userdata('offset',$offset);
		$this->pagination->initialize($config); 
		
		$this->load->view('header');
		
		//// load left and send query to creat menu
		$menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;$menu['status']=$status;
		$this->load->view('left',$menu);
		
		////// load content send query to get post 
		$data=array();
		$q=$this->posts_model->get_posts($type,$status,$config['per_page']);   ///////model class --->model func		
		$data['records']=$q;
		$data['type']=$type;
		$data['status']=$status;
		
		//get module sett
		$st_q= $this->fun->get_type_settings($type);
		$data['sett']=$st_q;
		
		$lang_q= $this->fun->get_lang();
		$data['lang']=$lang_q;


		$this->load->view('posts',$data);
		
		$this->load->view('footer');
	}
	
	
	function add_post($type,$status)
	{ 
	    $data=array();
	    $this->load->view('header');
	    
	    $menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;
		$menu['status']=$status;
		$this->load->view('left',$menu);
		$data['type']=$type;
		$data['status']=$status;
		
		//// get admin lang
		$lang_q= $this->fun->get_lang();
		$data['lang']=$lang_q;
		
		//// get module sett (fields)
		$st_q= $this->fun->get_type_settings($type);
		$data['sett']=$st_q;
		
		//// get cats
		$cats_q= $this->fun->get_cats($type);
		$data['cats']=$cats_q;
		
		$this->load->view('add_post',$data);
		$this->load->view('footer');

	}
	function save_add_post($type,$status)
	{
	//// ********* upload_image
	    
		 $file_name='';$more_file_name0='';$more_file_name1='';$more_file_name2='';
		 if($type=='servicesxx' or $type=='press' or $type=='sliders' or $type=='home_images' or $type=='models2'){

				if($_FILES['up0']['name'])
				{
						$this->load->library('upload');
						$config['upload_path'] = "../uploads/models/";
						$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx|rar|zip';
						$config['overwrite'] = false;
						$config['encrypt_name'] =true;	
						$this->upload->set_max_filesize(201);
						$this->upload->initialize($config);
						$url = basename($_FILES['up0']['name']);
						if(!empty($url)) // select img //
						{
							if (!$this->upload->do_upload('up0'))
							{
							$error = $this->upload->display_errors();
							echo $error;
							echo "<script type=\"text/javascript\">
							alert(\" Image not uploaded please try again \");
							//window.location = \"addnew\";
							</script>"; 
							} 
							else 
							{
							$upname = $this->upload->data();
							$more_file_name0 = $upname['file_name'];
							}
						 }
		        }
		}
		$config='';
		
		if($type=='services xx' or $type=='productsxx' or $type=='company')
		{
				for($y=0;$y<2;$y++)
				{
				$up_name='up'.$y;
							if($_FILES[$up_name]['name'])
							{
									$this->load->library('upload');
									$config['upload_path'] = "../uploads/posts/";
									$config['allowed_types'] = 'gif|jpg|png';
									$config['overwrite'] = false;
									$config['encrypt_name'] =true;	
									$this->upload->set_max_filesize(201);
									$this->upload->initialize($config);
									$url = basename($_FILES[$up_name]['name']);
									if(!empty($url)) // select img //
									{
										if (!$this->upload->do_upload($up_name))
										{
										$error = $this->upload->display_errors();
										echo $error;
										echo "<script type=\"text/javascript\">
										alert(\" Image not uploaded please try again \");
										//window.location = \"addnew\";
										</script>"; 
										} 
										else 
										{
										$upname = $this->upload->data();
										  if($y=='0'){$more_file_name0 = $upname['file_name'];}
										  if($y=='1'){$more_file_name1 = $upname['file_name'];}
										  if($y=='2'){$more_file_name2 = $upname['file_name'];}
										
										}
									 }
					        }

				
				 
				
				}
		
		}
		
				if($type!='career' and $type!='testimonials'){
		
							if($_FILES['pic']['name'])
							{
									
									$this->load->library('upload');
									$config['upload_path'] = "../uploads/posts/";
									$config['allowed_types'] = 'gif|jpg|png';
									$config['overwrite'] = false;
									$config['encrypt_name'] =true;	
									$this->upload->set_max_filesize(201);
									$this->upload->initialize($config);
									$url = basename($_FILES['pic']['name']);
									if(!empty($url)) // select img //
									{
										if (!$this->upload->do_upload('pic'))
										{
										$error = $this->upload->display_errors();
										echo $error;
										echo "<script type=\"text/javascript\">
										alert(\" Image not uploaded please try again \");
										//window.location = \"addnew\";
										</script>"; 
										} 
										else 
										{
										$upname = $this->upload->data();
										$file_name = $upname['file_name'];
										}
									 }
					        }
					}
	////////******* make thumb
			$config['image_library'] = 'gd2';
			$config['source_image'] = "../uploads/posts/".$file_name;
			$config['new_image'] = "../uploads/posts/small/".$file_name;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = "";
			$config['width'] = 250;
			$config['height'] = 150;
			$this->load->library('image_lib',$config);
			$this->image_lib->resize();
			
			////delete word error with editor
			$details=$this->input->post('details');
			$det=explode('<![endif]-->',$details);
			$det2=$det[count($det)-1];
			if($det2==''){$det2=$this->input->post('details');}

			$details_ar=$this->input->post('details_ar');
			$det_ar=explode('<![endif]-->',$details_ar);
			$det_ar2=$det_ar[count($det_ar)-1];
			if($det_ar2==''){$det_ar2=$this->input->post('details_ar');}
			
			$short=$this->input->post('short');
			$sh=explode('<![endif]-->',$short);
			$sh2=$sh[count($sh)-1];
			if($sh2==''){$sh2=$this->input->post('short');}
			
			$short_ar=$this->input->post('short_ar');
			$sh_ar=explode('<![endif]-->',$short_ar);
			$sh_ar2=$sh_ar[count($sh_ar)-1];
			if($sh_ar2==''){$sh_ar2=$this->input->post('short_ar');}
         ///// end delete word error

	$datestring = "%Y/%m/%d";
$time = time();
$add_date=mdate($datestring, $time);

	
	$data=array(
	    'module'=>$type,
	    'name'=>$this->input->post('name'),
	    'short'=>$this->input->post('short'),
	    'details'=>$det2,
	    'pic'=>$file_name,
	    'up1'=>$more_file_name0,
	    'up2'=>$more_file_name1,
	    'up3'=>$more_file_name2,
	    'cat'=>$this->input->post('cats'),
	    'name_ar'=>$this->input->post('name_ar'),
	    'short_ar'=>$this->input->post('short_ar'),
	    'details_ar'=>$det_ar2,
	    'txt1'=>$this->input->post('txt0'),
	    'txt2'=>$this->input->post('txt1'),
	    'txt3'=>$this->input->post('txt2'),
	    'area1'=>$this->input->post('area0'),
	    'area2'=>$this->input->post('area1'),
	    'area3'=>$this->input->post('area2'),
	     'txt_ar1'=>$this->input->post('txt_ar0'),
	    'txt_ar2'=>$this->input->post('txt_ar1'),
	    'txt_ar3'=>$this->input->post('txt_ar2'),
	    'area_ar1'=>$this->input->post('area_ar0'),
	    'area_ar2'=>$this->input->post('area_ar1'),
	    'area_ar3'=>$this->input->post('area_ar2'),
	   


        'add_date'=>$add_date
	    	    );
	    
	    $this->load->model('posts_model');   ///model file name
	    $last_id=$this->posts_model->add_new_post($data);  /////model class --->model func
	    
	    if($type=='career')
	    {
	    $visitor_id='52882'.$last_id;
	    $data2=array(
	    'job_id'=>$last_id,
	    'visitor_id'=>$visitor_id,
	    'location'=>$this->input->post('location'),
	    'f_p_time'=>$this->input->post('f_p_time'),
	    'r_t_type'=>$this->input->post('r_t_type'),
	    'band'=>$this->input->post('band')
	    );

	    $this->posts_model->add_new_job($data2);
	    }
	   echo "<script type=\"text/javascript\"> alert(\" Added successfully \") ;window.location.href= \"../../../posts/index/".$type."/".$status."\";</script>";    
	}




	function edit_post($type,$status,$id)
	{ 	
	        $data['qry'] = $this->fun->getone("posts", $id); 
	        $data['qry2'] = $this->fun->getjob("jobs", $id); 

	    
	         $lang_q= $this->fun->get_lang();
			
			$data['lang']=$lang_q;
			$st_q= $this->fun->get_type_settings($type);
			$data['sett']=$st_q;

	    
	    $this->load->view('header');
	    
	    $menu=array();
		$menu_q= $this->fun->get_modules();
		$menu['mods']=$menu_q;
		$menu['status']=$status;
		$this->load->view('left',$menu);
		
		$data['type']=$type;
		$data['status']=$status;
		
		$cats_q= $this->fun->get_cats($type);
		$data['cats']=$cats_q;
		
	    $this->load->view('edit_post', $data);
	    $this->load->view('footer');
	
	}
	
	function save_edite_post($type,$status,$id)
	{
	//// ********* upload_image
		$more_file_name0=$this->input->post('upp0');$more_file_name1=$this->input->post('upp1');$more_file_name2=$this->input->post('upp2');
	if($type=='servicesxx' or $type=='press' or $type=='home_images' or $type=='models' or $type=='models2'){
				if($_FILES['up0']['name'])
				{
						$this->load->library('upload');
						$config['upload_path'] = "../uploads/models/";
						$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx|zip|rar';
						$config['overwrite'] = false;
						$config['encrypt_name'] =true;	
						$this->upload->set_max_filesize(201);
						$this->upload->initialize($config);
						$url = basename($_FILES['up0']['name']);
						if(!empty($url)) // select img //
						{
							if (!$this->upload->do_upload('up0'))
							{
							$error = $this->upload->display_errors();
							echo $error;
							echo "<script type=\"text/javascript\">
							alert(\" Image not uploaded please try again \");
							//window.location = \"addnew\";
							</script>"; 
							} 
							else 
							{
							$upname = $this->upload->data();
							$more_file_name0 = $upname['file_name'];
							}
						 }
		        }
		}
		$config='';





	if($type=='modelsxx' or $type=='services xx' or $type=='company')
		{
				for($y=0;$y<2;$y++)
				{
				$up_name='up'.$y;
							if($_FILES[$up_name]['name'])
							{
									$this->load->library('upload');
									$config['upload_path'] = "../uploads/posts/";
									$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx|rar|zip';
									$config['overwrite'] = false;
									$config['encrypt_name'] =true;	
									$this->upload->set_max_filesize(201);
									$this->upload->initialize($config);
									$url = basename($_FILES[$up_name]['name']);
									if(!empty($url)) // select img //
									{
										if (!$this->upload->do_upload($up_name))
										{
										$error = $this->upload->display_errors();
										echo $error;
										echo "<script type=\"text/javascript\">
										alert(\" Image not uploaded please try again \");
										//window.location = \"addnew\";
										</script>"; 
										} 
										else 
										{
										$upname = $this->upload->data();
										  if($y=='0'){$more_file_name0 = $upname['file_name'];}
										  if($y=='1'){$more_file_name1 = $upname['file_name'];}
										  if($y=='2'){$more_file_name2 = $upname['file_name'];}
										
										}
									 }
					        }

				
				 
				
				}
		
		}
  				     $file_name=$this->input->post('pic2');

		if($type!='career' and $type!='testimonials'){

				     $file_name=$this->input->post('pic2');
					$this->load->library('upload');
					$config['upload_path'] = "../uploads/posts/";
					$config['allowed_types'] = 'gif|jpg|png';
					$config['overwrite'] = false;
					$config['encrypt_name'] =true;	
					$this->upload->set_max_filesize(201);
					$this->upload->initialize($config);
					$url = basename($_FILES['pic']['name']);
					if(!empty($url)) // select img //
					{
						if (!$this->upload->do_upload('pic'))
						{
						$error = $this->upload->display_errors();
						echo $error;
						echo "<script type=\"text/javascript\">
						alert(\" Image not uploaded please try again \");
						//window.location = \"addnew\";
						</script>"; 
						} 
						else 
						{
						$upname = $this->upload->data();
						$file_name = $upname['file_name'];
							////////******* make thumb
						$config['image_library'] = 'gd2';
						$config['source_image'] = "../uploads/posts/".$file_name;
						$config['new_image'] = "../uploads/posts/small/".$file_name;
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = TRUE;
						$config['thumb_marker'] = "";
						$config['width'] = 250;
						$config['height'] = 150;
						$this->load->library('image_lib',$config);
						$this->image_lib->resize();
			
						}
			        }
        
        
        } ///end if not career or testimonial
        ////delete word error with web
	        $details=$this->input->post('details');
			$det=explode('<![endif]-->',$details);
			$det2=$det[count($det)-1];
			if($det2==''){$det2=$this->input->post('details');}
			
			$details_ar=$this->input->post('details_ar');
			$det_ar=explode('<![endif]-->',$details_ar);
			$det_ar2=$det_ar[count($det_ar)-1];
			if($det_ar2==''){$det_ar2=$this->input->post('details_ar');}
			
			$short=$this->input->post('short');
			$sh=explode('<![endif]-->',$short);
			$sh2=$sh[count($sh)-1];
			if($sh2==''){$sh2=$this->input->post('short');}
			
			$short_ar=$this->input->post('short_ar');
			$sh_ar=explode('<![endif]-->',$short_ar);
			$sh_ar2=$sh_ar[count($sh_ar)-1];
			if($sh_ar2==''){$sh_ar2=$this->input->post('short_ar');}


$datestring = "%Y/%m/%d";
$time = time();
$add_date=mdate($datestring, $time);

	$data=array(
	    'module'=>$type,
	    'name'=>$this->input->post('name'),
	    'short'=>$sh2,
	    'details'=>$det2,
	    'pic'=>$file_name,
	    'up1'=>$more_file_name0,
	    'up2'=>$more_file_name1,
	    'up3'=>$more_file_name2,
	    'cat'=>$this->input->post('cats'),
	    'name_ar'=>$this->input->post('name_ar'),
	    'short_ar'=>$sh_ar2,
	    'details_ar'=>$det_ar2,
	     'txt1'=>$this->input->post('txt0'),
	    'txt2'=>$this->input->post('txt1'),
	    'txt3'=>$this->input->post('txt2'),
	    'area1'=>$this->input->post('area0'),
	    'area2'=>$this->input->post('area1'),
	    'area3'=>$this->input->post('area2'),
	     'txt_ar1'=>$this->input->post('txt_ar0'),
	    'txt_ar2'=>$this->input->post('txt1_ar1'),
	    'txt_ar3'=>$this->input->post('txt2_ar2'),
	    'area_ar1'=>$this->input->post('area_ar0'),
	    'area_ar2'=>$this->input->post('area_ar1'),
	    'area_ar3'=>$this->input->post('area_ar2'),
	     

	    'add_date'=>$add_date

	    );
	    
	    $this->load->model('posts_model');   ///model file name
	    $this->posts_model->update_post($data,$id);  
	    
		    if($type=='career'){
				     $data2=array(
				    'job_id'=>$id,
				    'location'=>$this->input->post('location'),
				    'f_p_time'=>$this->input->post('f_p_time'),
				    'r_t_type'=>$this->input->post('r_t_type'),
				    'band'=>$this->input->post('band')
				    );
			
				    $this->posts_model->update_job($data2,$id); /////model class --->model func
		    
		    }
	    echo "<script type=\"text/javascript\"> alert(\" Edited successfully \") ;window.location.href= \"../../../edit_post/".$type."/".$status."/".$id."\";</script>";
                		         
	}
	
	function delete_post($type,$status,$id)
	{ 	
	    $q= $this->fun->delete_one("posts", $id); 
	    
	    if($q)
	    {
	     echo "<script type=\"text/javascript\"> alert(\" Deleted successfully \") ;window.location.href= \"../../../index/".$type."/".$status."\";</script>";
	    }
	    else
	    {
	    echo "<script type=\"text/javascript\"> alert(\" Dont Deleted Please Try Again \") ;window.location.href= \"../../../index/".$type."/".$status."\";</script>";
	    }
	   
	
	}
	
	function change_status($type,$status)
	{
	   $actb=$this->input->post('act_all');
       $delb=$this->input->post('del_all');
       $notactb=$this->input->post('notact_all');
		       if($actb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		            $data=array('active'=>'yes');
		           foreach($selectd_id as $pid):
		             $this->fun->update_status('posts',$pid,$data);
		           endforeach;
		            $this->index($type,$status);
		           }
		           else
		           {
		            $this->index($type,$status);
		           }
		       }
		       
		       
		       if($notactb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		            $data=array('active'=>'no');
			           foreach($selectd_id as $pid):
			             $this->fun->update_status('posts',$pid,$data);
			           endforeach;
		             $this->index($type,$status);
		           }
		           else
		           {
		            $this->index($type,$status);
		           }
		       }
		       
		       
		       if($delb!='')
		       {
		           if(sizeof($this->input->post('list'))>0)
		           {
		           $selectd_id=$this->input->post('list');
		               foreach($selectd_id as $pid):
	                        $this->fun->delete_one("posts", $pid); 
			           endforeach;
		             $this->index($type,$status);
		           }
		           else
		           {
		            $this->index($type,$status);
		           }
		       }



	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */