for the most part i think my code is messy right now possibly because iam not really used to CI. I currently have one controller called 'user' which i will show below. in this controller I have methods such as login,logout etc. standard enough stuff in here like creating a session when the user logs in and destroying it when a user logs out. iam also tracking time as the system mimics a 'check in' system to get a users hours worked. the next part of the project requires access to the 'clients' tablem in the database that a user works with and is going to display some info about this client when a button is pressed, the clients themselves do not use this system but information about them needs to be viewed. Iam just wondering should i continue to add to my current controller and model or setup seperate classes next,seems to me the controller is already relatively big? here is my controller code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
var $loggedin = FALSE;
var $cdata;
function __construct()
{
parent::__construct();
$this->load->model("dbaccess");
$this->cdata =array( "warning" => "","email"=> "","password"=> "","logintime"=>"","start"=>"","end"=>""
,"diff"=>"","totalhours"=>"","dis"=>$this);
}
public function index()
{
if($this->session->userdata('email'))
{
$this->load->view('carerview',$this->cdata);
}
else
{
$this->load->view('mainview',$this->cdata);
}
}
public function login()
{
//get posted data. check if what is posted is in db.
// if it is set loggedin=true + redirect to carer page + save data in session. //get posted data. check if what is posted is in db.
if(isset($_POST['email']) )
{$this->cdata['email'] = $_POST['email'] ;}
else
{$this->cdata['email'] ="";}
if(isset($_POST['password']))
{$this->cdata['password'] = $_POST['password'];}
else
{$this->cdata['password'] ="";}
if($this->session->userdata('email'))
{
$this->loggedin = true;
}
else
{
$this->loggedin = $this->dbaccess->check_input($this->cdata['email'],$this->cdata['password']);
}
if($this->loggedin === TRUE && !$this->session->userdata('email'))
{
$this->start_session();
if(!$this->dbaccess->get_date_entry($this->session->userdata('email'),date("Y-m-d")))
{
$data = array("email"=>$this->session->userdata('email'),"date"=>date("Y-m-d"),
"hours"=>"0","starttime"=>date("Y-m-d H:i:s",$this->session->userdata('last_activity')));
$this->dbaccess->insert_daily_row($data);
}
$this->load->view('carerview',$this->cdata);
}
else if ($this->loggedin === TRUE && $this->session->userdata('email'))
{
$this->cdata['totalhours']= $this->session->userdata('totalhours');
$this->cdata['logintime']= $this->session->userdata('last_activity');
$this->load->view('carerview',$this->cdata);
}
else
{
$this->session->unset_userdata('last_activity');
$this->cdata['warning']="Check failed ! Please try again";
$this->load->view('mainview',$this->cdata);
}
}
private function start_session()
{
$this->load->library('session');
$this->session->set_userdata('email',$this->cdata['email']);
$this->cdata['totalhours']= $this->dbaccess->get_hours_by_date
($this->session->userdata('email'),date("Y-m-d",$this->session->userdata('last_activity')));
$this->session->set_userdata('totalhours',$this->cdata['totalhours']);
}
private function calculatedifference($starttime,$endtime) // delete checkin time from checkouttime
{
$diff= abs(strtotime($starttime) - strtotime($endtime));
$this->cdata['diff'] = date("H:i:s",$diff);
$this->cdata['start'] = date("Y-m-d H:i:s", strtotime( $starttime)+(1 * 3600));
$this->cdata['end']= date("Y-m-d H:i:s", strtotime($endtime) +(1 * 3600));
return date("H:i:s",$diff);
}
public function isLoggedIn()
{
return $this->loggedin;
}
public function logOut()
{
// update total hours and update checkout time.
if($this->session->userdata('email')){
$hours=$this->calculatedifference(date("Y-m-d H:i:s",$this->session->userdata('last_activity')),date("Y-m-d H:i:s"));
$data =array("endtime"=>date("Y-m-d H:i:s"),"hours"=>$hours,"email"=>$this->session->userdata('email'),"date"=>date("Y-m-d"));
$this->dbaccess->update_daily_row($data);}
$this->load->view('mainview',$this->cdata);
$this->session->sess_destroy();
}
public function admin()
{
$this->load->view('adminpage',$this->cdata);
}
}