在表单成功无效后取消设置会话

时间:2015-03-11 03:59:48

标签: codeigniter

当我取消选中修改&中的复选框时访问权限,如果表单已成功提交,则会从会话中取消设置

我想要实现的是我的编辑功能,如果未选中任何复选框,那么当表单提交成功时,它将从会话中取消 。因为需要从会话中取消设置的原因是因为当用户登录权限时,修改和访问被设置为会话。

如果我在编辑中尝试了这项工作后我怎样才能使这项工作成为现实,但如果复选框为空则不能取消设置

如果我使用此$this->session->unset_userdata('modify');它会取消设置会话中的所有修改数组,我只需要取消设置与未选中的复选框匹配的那个。

public function edit() {

$this->load->model('admin/user/model_user_group');

if ($this->input->server('REQUEST_METHOD') == 'POST') {

$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());

if ($this->session->userdata('user_id') == TRUE) {

if (isset($_POST['permission[access]'])) {

$this->session->unset_userdata('permission[access]');

}

if (isset($_POST['permission[modify]'])) {

$this->session->unset_userdata('permission[modify]');

}

}

redirect('admin/users_group');

}

$this->getForm();

}

public function getForm() {
$data['title'] = "Users Group";

$this->load->model('admin/user/model_user_group');

$user_group_info = $this->model_user_group->getUserGroup($this->uri->segment(4));

if ($this->uri->segment(4) == FALSE) {
    $data['name'] = $this->input->post('name');
} else {
    $data['name'] = $user_group_info['name'];
}

if ($this->uri->segment(4) == FALSE) {
    $data['user_group_id'] = $this->input->post('user_group_id');
} else {
    $data['user_group_id'] = $user_group_info['user_group_id'];
}

$ignore = array(
'admin',
'dashboard',
'filemanager',
'login',
'menu',
'register',
'online',
'customer_total',
'user_total',
'chart',
'activity',
'logout',
'footer',
'header',
'permission'
);

$data['permissions'] = array();

$files = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');

foreach ($files as $file) {

    $permission =  basename(strtolower($file), '.php');

    if (!in_array($permission, $ignore)) {
        $data['permissions'][] = $permission;
    }

}

$permission_access = $this->input->post('permission');

if (isset($permission_access)) {
    if (isset($permission_access['access'])) {
        $data['access'] = $permission_access['access'];
    } elseif (!empty($user_group_info['permission']['access'])) {
        $data['access'] = $user_group_info['permission']['access'];
    } else {
        $data['access'] = array();
    }
}

$permission_modify = $this->input->post('permission');

if (isset($permission_modify)) {
    if (isset($permission_modify['modify'])) {
        $data['modify'] = $permission_modify['modify'];
    } elseif (!empty($user_group_info['permission']['modify'])) {
        $data['modify'] = $user_group_info['permission']['modify'];
    } else {
        $data['modify'] = array();
    }
}

$this->load->view('template/user/users_group_form.tpl', $data);

}

1 个答案:

答案 0 :(得分:0)

我已经找到它以使其工作我从数据库获得会话然后重新设置表单中的会话,而不使用unset_sessions();

public function edit() {

    $this->load->model('admin/user/model_user_group');

    if ($this->input->server('REQUEST_METHOD') == 'POST') {

        $this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());

        $user_group_query = $this->db->query("SELECT permission FROM " . $this->CI->db->dbprefix . "user_group 
            WHERE user_group_id = '" . (int)$this->session->userdata('user_group_id') . "'");

        $permissions = unserialize($user_group_query->row('permission'));

        $this->session->set_userdata($permissions);

        redirect('admin/users_group');

    }

    $this->getForm();
}