load->library('session'); $this->load->_model('Model_study','study'); $this->load->_model('Model_studyclass','studyclass'); $this->load->_model('Model_typeclass','typeclass'); $this->load->_model('Model_shop','shop'); $this->load->_model("Model_studytype","studytype"); } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'add')//添加 { $this->_add(); } else if($arg == 'edit')//修改 { $this->_edit($arg_array); } else if($arg == 'see') { $this->_see($arg_array); } else if($arg == 'del') { $this->_del($arg_array); } else { $this->_index($arg_array); } } protected function getUserPlat(){ $user = $this->user->get_api($_SESSION['api']); $this->data['user'] = $user;//登录的用户信息 $usersp = explode('|',trim($user['shop'],'|'));$sid = ''; foreach ($usersp as $v) { $sid .= " id = ".$v." or"; } $result = $this->shop->find_all(rtrim($sid,'or'),'*',null,0,120); $plat_ids = array_column($result,'type'); $plat_ids = array_unique($plat_ids); $plat_list = $this->typeclass->find_all("id in (".implode(",",$plat_ids).")"); foreach ($plat_list as $k=>$v){ if($v['id'] == 269){ $plat_list[$k]['title'] = "独立站"; } } $this->data['plat_list'] = array_column($plat_list,'title','id'); } public function dd($data){ echo "
";
print_r($data);
die;
}
//管理
public function _index($arg_array)
{
$this->getUserPlat();
$post = $this->input->post(NULL, TRUE);
if(isset($post['page']))
{
$page = $this->input->post('page',true);
$perpage = $this->input->post('perpage',true);
$title = $this->input->post('title',true);
$count = $this->input->post('count',true);
$type = $this->input->post('type',true);
$class = $this->input->post("studyclass",true);
$where = "1=1 ";
//数据排序
$order_str = "id desc";
if(empty($page))
{
$start = 0;
$perpage = 1;
}
else
{
$start = ($page - 1)*$perpage;
}
if($title)
{
$where .= " and s.title like '%$title%'";
}
if($count)
{
$where .= " and s.content like '%$count%'";
}
if($class){
$where .= " and s.class = ".$class;
}
if($type)
{
$where .= " and st.type = '$type'";
$info_list = $this->studytype->query("select * from crowd_study as s left join crowd_studytype as st on s.id = st.article_id where ".$where." order by s.id desc" );
}
// if($type)
// {
// $where .= " and type = '$type'";
// $info_list = $this->studytype->query("select * from crowd_study as s left join crowd_studytype as st on s.id = st.article_id where ".$where." order by s.id desc" );
// }else{
// //取得信息列表
// $info_list = $this->study->find_all($where,'id,class,title,addtime',$order_str,$start,$perpage);
// }
$info_list = $this->studytype->query("select s.*,st.article_id from crowd_study as s left join crowd_studytype as st on s.id = st.article_id where ".$where." group by s.id order by s.id desc limit ".$perpage." offset ".$start );
$info_list = $info_list->result_array();
$final_list = [];
$all_plat = $this->data['plat_list'];
foreach($info_list as $k=>$v){
$tmp_plat_list = $this->studytype->find_all("article_id = ".$v['id']);
$plat_list_arr = [];
foreach($tmp_plat_list as $val){
if(isset($all_plat[$val['type']])){
$plat_list_arr[] = $all_plat[$val['type']];
}
}
$classtype = $this->studyclass->find("id = " . $v['class']);
$final_list[] = [
'id'=>$v['id'],
'title'=>$v['title'],
'class'=>$classtype['title'],
'plat'=> implode("|",$plat_list_arr),
'addtime'=>($v['edittime'] != 0)?date("Y-m-d H:i:s",$v['edittime']):date("Y-m-d H:i:s",$v['addtime'])
];
}
//格式化数据
// foreach ($info_list as $key=>$value)
// {
// // $type = $this->typeclass->read($value['type']);
// // $info_list[$key]['type'] = $type['title'];
// $f = $this->studyclass->read($value['class']);
// $info_list[$key]['class'] = $f['title'];
// $d = $this->study->read($value['id']);
// if($d['edittime'] != 0)
// {
// $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$d['edittime']);
// }
// else
// {
// $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
// }
//}
$total = $this->studytype->query("select count(*) from crowd_study as s left join crowd_studytype as st on s.id = st.article_id where ".$where." group by s.id ")->num_rows();
$pagenum = ceil($total/$perpage);
$over = $total-($start+$perpage);
$rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($final_list));
echo json_encode($rows);exit;
}
//$this->dd($this->data);
$studyclass = $this->studyclass->find_all();
$this->data['see'] = (isset($arg_array[0]))?'see':'';
$this->data['studyclass'] = $studyclass;
$this->_Template('study',$this->data);
}
//添加
public function _add()
{
$this->getUserPlat();
$post = $this->input->post(NULL, TRUE);
if(isset($post['title']))
{
$type_ids = $post['type'];
$type_ids = explode(",",$type_ids);
unset($post['type']);
$post['addtime'] = time();
$article_id = $this->study->insert($post);
if($article_id)
{
$insert_arr = [];
foreach($type_ids as $v){
if(!empty($v)){
$this->studytype->insert(
[
'type'=>$v,
'article_id'=>$article_id,
'addtime'=>time(),
]
);
}
}
echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
}
else
{
echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
}
}
$studyclass = $this->studyclass->find_all();
$this->data['studyclass'] = $studyclass;
$this->_Template('study_add',$this->data);
}
//修改
public function _edit($arg_array)
{
$this->getUserPlat();
$post = $this->input->post(NULL, TRUE);
if(isset($post['id']))
{
$id = $this->input->post('id',true);
$post['title'] = $this->input->post('title',true);
$post['edittime'] = time();
// $f = $this->studyclass->read($post['class']);
// $post['type'] = $f['type'];
$type_ids = $post['type'];
$type_ids = explode(",",$type_ids);
unset($post['type']);
$post['addtime'] = time();
if($this->study->save($post,$id))
{
$this->studytype->query("delete from crowd_studytype where article_id = ".$id);
$insert_arr = [];
foreach($type_ids as $v){
if(!empty($v)){
$this->studytype->insert(
[
'type'=>$v,
'article_id'=>$id,
'addtime'=>time(),
]
);
}
}
echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
}
else
{
echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
}
}
$arg_array = $arg_array[0];
$study = $this->study->read($arg_array);
$this->data['study'] = $study;
$studyclass = $this->studyclass->find_all();
$this->data['studyclass'] = $studyclass;
$plat_list = $this->studytype->find_all("article_id = ".$arg_array);
$plat_ids = array_column($plat_list,'type');
$this->data['plat_ids'] = $plat_ids;
$this->_Template('study_edit',$this->data);
}
public function _see($arg_array)
{
$this->getUserPlat();
$arg_array = $arg_array[0];
$study = $this->study->read($arg_array);
$this->data['study'] = $study;
$studyclass = $this->studyclass->find_all();
$this->data['studyclass'] = $studyclass;
$this->_Template('study_see',$this->data);
}
//删除
public function _del()
{
$post = $this->input->post(NULL, TRUE);
if(isset($post['s']))
{
$id_arr = $this->input->post('s');
$id_arr = explode(',',$id_arr);
if(!$id_arr)
{
echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
}
//循环删除记录
foreach ($id_arr as $v)
{
$this->study->remove($v);
if(!empty($v)){
$this->studytype->query("delete from crowd_studytype where article_id = ".$v);
}
}
echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
}
}
}