$instance
$instance : object
Reference to the CI singleton
角色管理
This class object is the super class that every library in CodeIgniter will be assigned to.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 角色管理
* @author lxn
*/
class Role extends CI_Controller
{
private static $data = array();
public function __construct()
{
parent::__construct();
$this->load->model('Role_model', 'role');
$this->load->model('Admin_model', 'role');
$this->load->model('Linkadmin_model', 'linkadmin');
$this->load->model('Public_model', 'public');
$this->load->model('Webcommon_model', 'common');//积分流水表
self::$data['header'] = $this->public->header('角色管理');
self::$data['left'] = $this->public->leftmenu('role');
self::$data['footer'] = $this->public->footer();
$this->load->library('Session');
$this->public->authentication();
}
/**
* 角色列表
*/
public function index()
{
if ($_GET) {
//获取Datatables发送的参数 必要
$draw = $_GET['draw'];//这个值作者会直接返回给前台
//排序
$order_column = $_GET['order']['0']['column'];//那一列排序,从0开始
$order_dir = $_GET['order']['0']['dir'];//ase desc 升序或者降序
//拼接排序sql
$orderSql = "";
//搜索
$search = $_GET['search']['value'];//获取前台传过来的过滤条件
//分页
$start = $_GET['start'];//从多少开始
$length = $_GET['length'];//数据长度
$limitSql = '';
$limitFlag = isset($_GET['start']) && $length != -1;
if ($limitFlag) {
$limitSql = " LIMIT " . intval($start) . ", " . intval($length);
}
//定义查询数据总记录数sql
$sumSql = "SELECT count(*) as sum FROM role as a ";
//定义过滤条件查询过滤后的记录数sql
$sumSqlWhere = "where a.roleName LIKE '%" . $search . "%'";
//条件过滤后记录数 必要
$recordsFiltered = 0;
//表的总记录数 必要
$recordsTotal = 0;
//查询总数
$total = $this->common->getsqldata($sumSql);
// var_dump($total);exit;
$recordsTotal = $total[0]['sum'];
$totalResultSql = "SELECT a.* FROM role as a ";
if (strlen($search) > 0) {
//记录总数
$total = $this->common->getsqldata($sumSql . $sumSqlWhere);
$recordsFiltered = $total[0]['sum'];
$list = $this->common->getsqldata($totalResultSql . $sumSqlWhere . $orderSql . $limitSql);
} else {
//记录总数
$recordsFiltered = $recordsTotal;
$list = $this->common->getsqldata($totalResultSql . $orderSql . $limitSql);
}
header("Content-Type:text/html;charset=utf-8");
foreach ($list as $key => $value) {
$linkIds = explode('|', $value['adminPermission']);
$linkIds = array_filter($linkIds);
$adminPermission = $this->linkadmin->getRoleLinkAdminList($linkIds);
$list[$key]['linkName'] = $adminPermission;
}
exit(json_encode(array(
"draw" => intval($draw),
"recordsTotal" => intval($recordsTotal),
"recordsFiltered" => intval($recordsFiltered),
"data" => $list
)));
} else {
$this->load->view('admincp/role/rolelist', self::$data);
}
}
/**
* 添加/修改页
*/
public function edit($roleId = 0)
{
$roleId = $this->input->get_post('roleId');
// 获取栏目信息
$linkInfo = $this->linkadmin->linkadminList(0, 0, true);
$roleInfo = $this->role->getRoleRow($roleId);
$linkIds = explode('|', $roleInfo['adminPermission']);
$linkIds = array_filter($linkIds);
self::$data['linkIds'] = $linkIds;
self::$data['roleId'] = $roleId;
self::$data['roleInfo'] = $roleInfo;
self::$data['linkInfo'] = $linkInfo;
$this->load->view('admincp/role/roleinfo', self::$data);
}
/**
* 添加/修改数据库
*/
public function updateData()
{
$url = trim($this->input->get_post('backurl'));
$referer = !empty($url) ? $this->config->item('base_url') . base64_decode($url) : $this->config->item('base_url') . '/admincp/role';
$roleId = $this->input->post('roleId');
$roleName = $this->input->post('roleName');
$linkIds = $this->input->post('linkIds');
//$roleBrief = $this->input->post('roleBrief');
if (!empty($linkIds)) {
$adminPermission = implode('|', $linkIds);
$adminPermission = '|' . $adminPermission . '|';
} else {
$adminPermission = '';
}
if (empty($roleId)) {
$roleData = array(
'roleName' => $roleName,
'adminPermission' => $adminPermission
//'rolebrief' => $roleBrief
);
$bool = $this->role->insertData($roleData);
$msg = $bool ? '角色添加成功!' : '角色添加失败!';
$isok = $bool ? 0 : 1;
} else {
$roleData = array(
'roleName' => $roleName,
'adminPermission' => $adminPermission
//'rolebrief' => $roleBrief
);
$bool = $this->role->updateData($roleData, $roleId);
$msg = $bool ? '角色修改成功!' : '角色修改失败!';
$isok = $bool ? 0 : 1;
}
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'role'));
exit;
}
/**
* 检查栏目名称和关键字唯一
* */
public function ajaxcheck()
{
$res = array('status' => 0);
$roleId = $this->input->post('roleId');
$roleName = $this->input->post('roleName');
$data = $this->role->checkUnique(trim($roleName), $roleId);
if (!empty($data)) {
$res['status'] = 1;
}
echo json_encode($res);
}
/**
* 删除角色
*/
public function delete()
{
$res = array(
'err' => 1,
'msg' => '系统错误'
);
$roleId = trim($this->input->get_post('roleId'));
$roleInfo = $this->role->getRoleRow($roleId);
if (empty($roleInfo)) {
$res['msg'] = '参数错误';
echo json_encode($res);
exit();
}
$bool = $this->role->delete($roleId);
$res['err'] = $bool ? 0 : 1;
$res['msg'] = $bool ? '删除成功' : '删除失败';
echo json_encode($res);
exit();
}
}