<?php

class Admin extends Controller {


	function Welcome()
	{

		parent::Controller();
		$this->load->helper(array('form', 'url'));
	}
	
/*
- Edit admin password
- Edit website title/keywords/description, SMS details
- Ability to search for groups and messages
- Ability to view/edit/delete messages
- Ability to add/edit/delete groups
- View SMS credits transaction - Filter results by group/date range
- Mass email all members
- View site statistics including number of messages
*/
   function logout()
	{
	//echo "Sdf";

	 $data = array( 
 'id' => '',
 'username' => '',
 'admin_logged_in' =>FALSE
	 );
	 
	 $this->session->set_userdata($data);
		
	header("location:".base_url()."admin/");
	
	}

	function index()
	{ 
	   	$this->load->view('admin/check_login');
	    $this->load->view('admin/header');
		$this->load->view('admin/index');
		$this->load->view('admin/footer');
		
	}
   
    
    //Login//

	function login()
	{
	//Load Main Model //
	 $this->load->Model('Main');
	 
   $email = $this->input->post('email');
   $password = $this->input->post('password');
   
   $login =  $this->Main->AdminLogin($email,$password);
   
   if($login == true)
   {
   
   redirect('admin/account');
   
   }
   else
   {
   echo "failure";
   
   }
	
	}
	
	
    function account()
	{
	
	$this->load->model('Main');
	
	$data['groups'] = $this->Main->get_num_groups();
	$data['users'] = $this->Main->get_num_users();
	$data['updates'] = $this->Main->get_num_updates();
	
	
	$this->load->view('admin/redirect_login'); 	
	$this->load->view('admin/header');
	$this->load->view('admin/account',$data);
	$this->load->view('admin/footer');

	} 
   
	
	function groups()
	{
	
	$this->load->model('Group');
	
	$name = $this->input->post('textarea');
	$query = $this->Group->get_all_groups($name);
	 
	 $data['query'] = $query;
	 
	$this->load->view('admin/redirect_login'); 	
    $this->load->view('admin/header');
	$this->load->view('admin/groups',$data);
	$this->load->view('admin/footer');
	
	}
	
	
	function messages()
	{
	$this->load->model('Message');
	
	$name = $this->input->post('textarea');
	$query = $this->Message->get_all_messages($name);
	 
	 $data['query'] = $query;
	 
   $this->load->view('admin/redirect_login');	
    $this->load->view('admin/header');
	$this->load->view('admin/messages',$data);
	$this->load->view('admin/footer');
	
	}
	
	
	
/*	
	function view_message()
	{
	
	
	
	}
*/	
    function edit_message($id)
	{
	
		//$this->output->enable_profiler(TRUE);
	
	$this->load->model('Message');
	
	$data['mtxt'] = $this->Message->MessageById($id);
    $data['mid']   = $id;
	
	$this->load->view('admin/redirect_login');
	$this->load->view('admin/header');
	$this->load->view('admin/edit_message',$data);
	$this->load->view('admin/footer');
	
	$change = $this->input->post('submit');
	if(!empty($change))
	{
	$gid =  $this->input->post('msg_id');
	$npost = $this->input->post('msg_txt');
	 $this->Message->ChangeMessage($gid,$npost);
    
	redirect('admin/messages');
	}
	
	}
	
	function delete_message($id)
	{
	$this->load->model('Message');
	
	if($this->Message->DestoryMessage($id)==TRUE)
	{
	redirect('admin/messages');
	}
	

	
	}
	
	
	
	function edit_group($id)
	{
	
	//$this->output->enable_profiler(TRUE);
	
	$this->load->model('Group');
	
	$data['gname'] = $this->Group->GroupName($id);
    $data['gid']   = $id;
	
	$this->load->view('admin/redirect_login');
	$this->load->view('admin/header');
	$this->load->view('admin/edit_group',$data);
	$this->load->view('admin/footer');
	
	$change = $this->input->post('submit');
	if(!empty($change))
	{
	$gid =  $this->input->post('group_id');
	$npost = $this->input->post('group_name');
	 $this->Group->ChangeGroupName($gid,$npost);
    
	redirect('admin/groups');
	}
	
	}
	
/*
	function view_group()
	{
	
	
	
	}
*/	
	function delete_group($id)
	{
	$this->load->model('Group');
	
	if($this->Group->AdminDestory($id)==TRUE)
	{
	redirect('admin/groups');
	}
	
	
	}
	
	
	function mass_email()
	{
	
	
	
	}
	

	

//****************** Webpages starts
function webpages()
	{
			
				$this->load->model('adminHome_model'); 
				$config['base_url'] = base_url().'index.php/admin/webpages/'; 
				$count=$this->adminHome_model->countRows8();
				$offset = (int) $this->uri->segment(3, 0);
				if($offset > $count)
					$offset=0;
				 $per_page=10;
				 $config['total_rows'] =$this->adminHome_model->countRows8();
				 $config['uri_segment'] = 3;
				 $config['num_links'] = 2 ;
				 $config['per_page'] = $per_page;
				 //$this->pagination->initialize($config); 
				 $result['offset']=$offset;
				 $result['data']=$this->adminHome_model->showAllwebpages($offset,$per_page);
				 //$result['page1']=$this->pagination->create_links();
				
				$this->load->view('admin/header');
	
	

				 $result['pageRequest'] = 'showWebPages';				 
				 $result['msg']='&nbsp;';
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');

		
	}


function edit($ID)
{

				$this->load->model('adminHome_model');
				$result['data']=$this->adminHome_model->showWebpageById($ID);
				
				
					$this->load->view('admin/header');
				$result['pageRequest'] = 'editWebPages';				 
				$result['msg']='&nbsp;';
				$this->load->view('admin/webpages',$result);
								 $this->load->view('admin/footer');

}
function edit1()
{


			
				$id=$this->input->post('pageid');
				
				$data1=$this->input->post('title');
				$data2=$this->input->post('keywords');
				$data3=$this->input->post('description');
				$data4=$this->input->post('name');

				$this->input->post('details');
				//print_r($_POST);
				
				$pattern = '/\'/';
				$replacement = '\\\'';
				$data =  preg_replace($pattern, $replacement, $this->input->post('details'));				
				


				
				
				$this->load->model('adminHome_model');
				$update=$this->adminHome_model->editwebpage($id,$data,$data1,$data2,$data3,$data4);				
				/*$this->load->model('gmodel');
							$tabName = 'webpages';
							$where['Id'] = $this->input->post('Id');
							$insert = $this->gmodel->gUpdate($tabName,$where,$data);*/
				
				$this->webPages();
				

			 
}

//****************** Webpages ends
//------------------  Email template Start

function emailTemplates()
	{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				 $this->load->model('adminHome_model');
				 $result['data']=$this->adminHome_model->showAllEmailTemplates();
				 $result['pageRequest'] = 'showEmailTemplate';				 
				 $result['msg']='&nbsp;';
				 $result['page']=1;
				 
				 $this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
	}

function editTemplate($ID)
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$result['data']=$this->adminHome_model->showWebpageById1($ID);
				$result['pageRequest'] = 'editEmailTemplate';				 
				$result['msg']='&nbsp;';
				$result['page']=1;
				$this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
}



function updateTemplate()
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{	
				$id=$this->input->post('pageid');
				
				$details=$this->input->post('details');
				$when = $this->input->post('when');
				
				/*$rules['details']="trim|required";
			
				$this->validation->set_rules($rules);
				if($this->validation->run()==FALSE)
				{
					 $result['pageRequest'] = 'showEmailTemplate';				 
					 $result['msg']='&nbsp;';
					 $result['page']=1;
				}
				else
				{*/
				$this->load->model('adminHome_model');
				$update=$this->adminHome_model->editTemplate($id,$details,$when);
				$this->emailTemplates();
				/*}*/
		}
}




function addNewEmailTemplate()
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$result['pageRequest'] = 'addEmailTemplate';				 
				$result['msg']='&nbsp;';
				$result['page']=1;
				$this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
}


function insertEmailTempalte()
	{
	
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$name = $this->input->post('name');
				$details = $this->input->post('details');
				$when = $this->input->post('when');
				$insert = $this->adminHome_model->insertEmailTemplate($name,$details,$when);
				$this->emailTemplates();
		}
	
	}


function deleteEmailTempate($id)
	{
	
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$name = $this->input->post('name');
				$details = $this->input->post('details');
				$insert = $this->adminHome_model->deleteEmailTemplate($id);
				$this->emailTemplates();
		}
	
	}



//===================  END EMAIL TEMPLATE

//------------------  SMS template Start

function SMSTemplates()
	{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				 $this->load->model('adminHome_model');
				 $result['data']=$this->adminHome_model->showAllEmailTemplates();
				 $result['pageRequest'] = 'showSMSTemplate';				 
				 $result['msg']='&nbsp;';
				 $result['page']=1;
				 
				 $this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
	}

function editSMSTemplate($ID)
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$result['data']=$this->adminHome_model->showWebpageById1($ID);
				$result['pageRequest'] = 'editSMSTemplate';				 
				$result['msg']='&nbsp;';
				$result['page']=1;
				$this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
}



function updateSMSTemplate()
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{	
				$id=$this->input->post('pageid');
				
				$details=$this->input->post('details');
				$when = $this->input->post('when');
				
				/*$rules['details']="trim|required";
			
				$this->validation->set_rules($rules);
				if($this->validation->run()==FALSE)
				{
					 $result['pageRequest'] = 'showEmailTemplate';				 
					 $result['msg']='&nbsp;';
					 $result['page']=1;
				}
				else
				{*/
				$this->load->model('adminHome_model');
				$update=$this->adminHome_model->editTemplate($id,$details,$when);
				$this->SMSTemplates();
				/*}*/
		}
}




function addNewSMSTemplate()
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$result['pageRequest'] = 'addSMSTemplate';				 
				$result['msg']='&nbsp;';
				$result['page']=1;
				$this->load->view('admin/header');
				 $this->load->view('admin/webpages',$result);
				 $this->load->view('admin/footer');
		}
}


function insertSMSTempalte()
	{
	
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$name = $this->input->post('name');
				$details = $this->input->post('details');
				$when = $this->input->post('when');
				$insert = $this->adminHome_model->insertEmailTemplate($name,$details,$when);
				$this->SMSTemplates();
		}
	
	}


function deleteSMSTempate($id)
	{
	
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{
				$this->load->model('adminHome_model');
				$name = $this->input->post('name');
				$details = $this->input->post('details');
				$insert = $this->adminHome_model->deleteEmailTemplate($id);
				$this->SMSTemplates();
		}
	
	}
	
function listuser()
{
	  
				   
				  $data['msg']='';
				   $sqlselect="select * from users";
				 // $data['query']=mysql_query($sqlselect) or die(mysql_error());
				  $query=mysql_query($sqlselect) or die(mysql_error());
				    
				//  $record=mysql_fetch_array($query);
				  //  $data['record']=$record; 
				 // $this->load->view('admin/redirect_login'); 	
                //  $this->load->view('admin/header');
				  // $this->load->view('admin/listuser',$data);
	            //   $this->load->view('admin/footer');
				   
				   //paging code starts here below
				   
				   
				   $this->load->library('pagination');

$config['base_url'] =base_url() . 'index.php/admin/listuser';
$count=mysql_num_rows($query);
$config['total_rows'] = $count;
$per_page=5;
$config['per_page'] = $per_page;
$offset=(int)$this->uri->segment(3,0);
$config['uri_segment']=3;
$config['num_links']=3;
if($offset>$count)
$offset=0;
//$config['per_page']=$per_page;

//$this->pagination->initialize($config);
$data['offset']=$offset;
$this->load->model('adminHome_model');

$data['result']=$this->adminHome_model->getAllUse($offset,$per_page);

//$data['page1']=$this->pagination->create_links();


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

$data['page1']=$this->pagination->create_links();
 $this->load->view('admin/redirect_login'); 
 $this->load->view('admin/header');	
 $this->load->view('admin/listuser',$data);
 $this->load->view('admin/footer');
//validation code ends here				   
				   
				  
					
			
}
function edituser($id)
{
		 $data['id']=$id;
		 $data['msg']='';
		 $this->load->view('admin/redirect_login');
		 $this->load->view('admin/header');
		 $this->load->view('admin/edit_user',$data);
		 $this->load->view('admin/footer');
 
}
function updateuser()
{
		   $data['msg']='';
		   $fullname=$this->input->post('fullname');
		   $username=$this->input->post('username');
		  // $password=$this->input->post('password');
		   $email=$this->input->post('email');
		   $number=$this->input->post('number');
		   $data['id']= $id=$this->input->post('recid');
		   $rules['fullname']='trim|required';
		  // $rules['username']='trim|required';
		  // $rules['password']='trim|required';
		   $rules['email']='trim|required|valid_email';
		   $rules['number']='trim|required';
		   
		   $fields['fullname']='fullname';
		 //  $fields['username']='username';
		  // $fields['password']='password';
		   $fields['email']='email';
		   $fields['number']='phonenumber';
		 
		   $this->validation->set_fields($fields);
		     $this->validation->set_rules($rules);
		   
		   
		    if($this->validation->run()==FALSE)
			{
				 
				 //errors are there
				   $data['msg']='<font color="red" size="2">Please clear the following errors</font>';
				   $this->load->view('admin/redirect_login');
				 $this->load->view('admin/header');
				 $this->load->view('admin/edit_user',$data);
				 $this->load->view('admin/footer');
		    }
			
			else
			{
			
		          //no errors are there
				 $sqlupdate="update users set fullname='$fullname',email='$email',number='$number'  where id='$id'";
				 $queryupdate=mysql_query($sqlupdate) or die(mysql_error());
				 
				 $data['msg']='Profile updated Sucessfully';
				
				 $this->load->view('admin/redirect_login');
				 $this->load->view('admin/header');
				 $this->load->view('admin/edit_user',$data);
				 $this->load->view('admin/footer');
			}
   
 } 
function deleteuser($id)
{
				  $sqldelete="delete  from users where id='$id'";
				  $query=mysql_query($sqldelete) or die(mysql_error());
				  $data['msg']='Record Delted Sucessfully';
				  $sqlselect="select * from users";
				  $data['query']=mysql_query($sqlselect) or die(mysql_error());
				  $this->load->view('admin/redirect_login');
					 $this->load->view('admin/header');
					 $this->load->view('admin/listuser',$data);
					 $this->load->view('admin/footer');
  

}


//===================  END SMS TEMPLATE

} //end of class 

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