在大多数情况下,我认为我的代码现在很乱,可能是因为我真的不习惯 CI。我目前有一个名为“用户”的控制器,我将在下面显示。在这个控制器中,我有登录、注销等方法。这里有足够标准的东西,比如在用户登录时创建会话并在用户注销时销毁它。iam 还跟踪时间,因为系统模仿“签到”系统来让用户工作时间。项目的下一部分需要访问用户使用的数据库中的“客户”表,并且在按下按钮时将显示有关此客户的一些信息,客户本身不使用此系统,但有关他们的信息需要查看。我只是想知道我应该继续添加到我当前的控制器和模型还是接下来设置单独的类,在我看来控制器已经比较大了?这是我的控制器代码。
<?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);
}
}