在下面的代码中,我在Index()函数和teamSetgoalstatus()中编写了两次函数。如何避免两次写入。
class User_dashboardController extends Controller
{
public function actionIndex()
{
$set_dates=new SettingsForm;
$set_dates=$set_dates->get_all_data();
$setting_date=new SettingsForm;
$where = 'where setting_content = :setting_content and year = :year';
$list = array('setting_content','year');
$data = array('PMS_display_format',date('Y'));
$settings_data = $setting_date->get_setting_data($where,$data,$list);
//print_r($settings_data);die();
$curr_year=date("Y");
$team_kra_appr=array();
$Employee_id = Yii::app()->user->getState("Employee_id");
$kra_info=new KpiAutoSaveForm;
$emp_data = new EmployeeForm;
$cluster = new ClusterForm;
$Employee_id = Yii::app()->user->getState("Employee_id");
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($Employee_id);
$emp_data_desc =$emp_data->get_employee_data($where,$data,$list);
$Reporting_officer1_id=$emp_data_desc['0']['Email_id'];
$where= 'where Reporting_officer1_id=:Reporting_officer1_id';
$list= array('Reporting_officer1_id');
$data= array($Reporting_officer1_id);
$team_members=$emp_data->get_employee_data($where,$data,$list);
$team=$emp_data->get_employee_data($where,$data,$list);
$team_members= implode(', ', array_column($team_members, 'Employee_id'));
$data=$team_members;
$array = $team_members;
$array = explode( ',', $array );
foreach ($array as &$value){
$value = "'" . trim($value)."'";
}
$array = implode(', ', $array);
$year1=$settings_data['0']['setting_type'];
$set_goal_sub=$kra_info->get_team_members_kra_sub($array,$year1);
$employee_data =new EmployeeForm;
$team_pend_appr=array();
$kra_team_pend=$kra_info->get_pending_goal_team($array,$year1);
for ($i=0; $i < count($kra_team_pend); $i++) {
$Employee_id=$kra_team_pend[$i]['Employee_id'];
$employee_data =new EmployeeForm;
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($kra_team_pend[$i]['Employee_id']);
$team_pend_appr = $employee_data->get_employee_data($where,$data,$list);
}
if (isset($emp_data_desc) && count($emp_data_desc)>0) {
$where = 'where Email_id = :Email_id';
$list = array('Email_id');
$data = array($emp_data_desc['0']['cluster_appraiser']);
$cluster_head =$emp_data->get_employee_data($where,$data,$list);
}
//print_r($cluster_head);die();
$Employee_id = Yii::app()->user->getState("Employee_id");
if (isset($emp_data_desc) && count($emp_data_desc)>0) {
$where = 'where cluster_name = :cluster_name';
$list = array('cluster_name');
$data = array($emp_data_desc['0']['cluster_name']);
$emp_data_desc =$cluster->get_cluster_data($where,$data,$list,'department');
}
// echo "<pre>";print_r($data);echo "</pre>";
// echo count($kra_mid_appr);die();
$team_set=array();
$set_goal_sub=$kra_info->get_team_members_kra_sub($array,$year1);
for($i=0;$i<count($set_goal_sub);$i++){
$Employee_id=$set_goal_sub[$i]['Employee_id'] ;
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($set_goal_sub[$i]['Employee_id']);
$team_set = $emp_data->get_employee_data($where,$data,$list);
}
$kpi_appr=$kra_info->get_disinct_kra_appr_team($array,$year1);
for($i=0;$i<count($kpi_appr);$i++){
$Employee_id=$kpi_appr[$i]['Employee_id'];
$employee_data1=new EmployeeForm;
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($kpi_appr[$i]['Employee_id']);
$team_kra_appr[$i] = $employee_data1->get_employee_data($where,$data,$list);
}
$my_activity=new NotificationsForm;
$where ='where Employee_id = :Employee_id order by id desc';
$list=array('Employee_id');
$data=array($Employee_id);
$my_recent_act=$my_activity->get_notifications($where,$data,$list);
// echo "<pre>";
// print_r($my_recent_act);die();
// echo "</pre>";
$this->render('//site/script_file');
$this->render('//site/header_view_layout');
$this->render('//site/user_dashboard',
array(
'team'=>$team,
'team_set'=>$team_set,
'team_pend_appr'=>$team_pend_appr,
'team_kra_appr'=>$team_kra_appr,
));
$this->render('//site/footer_view_layout');
}
function actionteamSetgoalstatus()
{
$status = $_POST['status'];
$value = explode('_',$status);
$Employee_id = Yii::app()->user->getState("Employee_id");
$kra_info=new KpiAutoSaveForm;
$emp_data=new EmployeeForm;
$employee_data=new EmployeeForm;
$curr_year=date("Y");
$kra_all_data=$kra_info->getdata();
$setting_date=new SettingsForm;
$where = 'where setting_content = :setting_content and year = :year';
$list = array('setting_content','year');
$data = array('PMS_display_format',date('Y'));
$settings_data = $setting_date->get_setting_data($where,$data,$list);
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($Employee_id);
$emp_data_desc =$emp_data->get_employee_data($where,$data,$list);
$Reporting_officer1_id=$emp_data_desc['0']['Email_id'];
$where= 'where Reporting_officer1_id=:Reporting_officer1_id';
$list= array('Reporting_officer1_id');
$data= array($Reporting_officer1_id);
$team_members=$emp_data->get_employee_data($where,$data,$list);
$team_members= implode(', ', array_column($team_members, 'Employee_id'));
$data=$team_members;
$array = $team_members;
$array = explode( ',', $array );
foreach ($array as &$value1){
$value1= "'" . trim($value1)."'";
}
$array = implode(', ', $array);
$year1=$settings_data['0']['setting_type'];
$set_goal_sub=$kra_info->get_team_members_kra_sub($array,$year1);
if ($value[1] =='Submitted') {
for($i=0;$i<count($set_goal_sub);$i++){
$Employee_id=$set_goal_sub[$i]['Employee_id'] ;
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($set_goal_sub[$i]['Employee_id']);
$kra_data[$i] = $emp_data->get_employee_data($where,$data,$list);
}
}
else if($value[1] == 'Pending')
{
$kra_team_pend=$kra_info->get_pending_goal_team($array,$year1);
for ($i=0; $i < count($kra_team_pend); $i++) {
$Employee_id=$kra_team_pend[$i]['Employee_id'];
$employee_data =new EmployeeForm;
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($kra_team_pend[$i]['Employee_id']);
$kra_data[$i] = $employee_data->get_employee_data($where,$data,$list);
}
}
else if($value[1] == 'Approved')
{
$kpi_appr=$kra_info->get_disinct_kra_appr_team($array,$year1);
for($i=0;$i<count($kpi_appr);$i++){
$Employee_id=$kpi_appr[$i]['Employee_id'];
$where = 'where Employee_id = :Employee_id';
$list = array('Employee_id');
$data = array($kpi_appr[$i]['Employee_id']);
$kra_data[$i] = $employee_data->get_employee_data($where,$data,$list);
}
}
$content = '';
if (isset($kra_data) && count($kra_data)>0) {
for ($i=0; $i < count($kra_data); $i++) {
if($value[1]=="Submitted")
{
if($content == '')
{
$content = '<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Submitted".'</td>'.'</tr>';
}
else
{
$content = $content.'<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Submitted".'</td>'.'</tr>';
}
}
else if($value[1]=="Pending")
{
if($content == '')
{
$content = '<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Pending".'</td>'.'</tr>';
}
else
{
$content = $content.'<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Pending".'</td>'.'</tr>';
}
}
else if($value[1]=="Approved")
{
if($content == '')
{
$content = '<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Approved".'</td>'.'</tr>';
}
else
{
$content = $content.'<tr>'.'<td>'.$kra_data[$i]['0']['Employee_id'].'</td>'.'<td>'.$kra_data[$i]['0']['Emp_fname']." ".$kra_data[$i]['0']['Emp_lname'].'</td>'.'<td>'."Approved".'</td>'.'</tr>';
}
}
}
}
else
{
$content = "No Record Found";
}
print_r($content);die();
}
}
答案 0 :(得分:0)
创建一个commonHelper文件并在此类中编写所有实用程序函数的更好方法,只要您需要使用它就像这样:
import time
class Object(object):
pass
def get_throttler(rate, per):
scope = Object()
scope.allowance = rate
scope.last_check = time.time()
def throttler(fn):
current = time.time()
time_passed = current - scope.last_check;
scope.last_check = current;
scope.allowance = scope.allowance + time_passed * (rate / per)
if (scope.allowance > rate):
scope.allowance = rate
if (scope.allowance < 1):
pass
else:
fn()
scope.allowance = scope.allowance - 1
return throttler
答案 1 :(得分:0)
你的Controller是普通的PHP,就像普通的PHP一样。只需提取你想要的那部分代码,然后创建一个公共,私有或受保护的新功能(我认为在这种情况下你不会在任何其他地方需要它,所以它可以是私有的)。并调用它,就像在$this->yourFunction($params...)
的普通类中一样。
Yii的控制器是Yii在运行时实例化的普通类。