<?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 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()
	{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{		
				$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)
{
		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->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()
{
		if($this->session->userdata('logged_in')==NULL)
		{
				header("location:".base_url()."index.php/admin/");
		}
		else
		{	

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

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


				$rules['title']="trim|required";
				$rules['keywords']="trim|required";
				$rules['description']="trim|required";
				$this->validation->set_rules($rules);
				if($this->validation->run()==FALSE)
				{
						$this->edit($id);
				}
				else
				{
				
				
				$this->load->model('adminHome_model');
				$update=$this->adminHome_model->editwebpage($id,$data,$data1,$data2,$data3);				
				/*$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();
		}
	
	}



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

} //end of class 

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