load->library('session'); $this->load->_model('Model_purchase','purchase'); $this->load->_model('Model_country','country'); $this->load->_model('Model_productprice','productprice'); } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'add')//添加 { $this->_add(); } else if($arg == 'edit')//修改 { $this->_edit($arg_array); } else if($arg == 'del')//修改 { $this->_del(); } else if($arg == 'rows')//修改 { $this->_rows(); } else { $this->_index(); } } //管理 public function _index() { $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); $where = "1=1 "; if($title) { $where .= " and title like '%$title%'"; } //数据排序 $order_str = "px desc,id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } //取得信息列表 $info_list = $this->purchase->find_all($where,'id,title,bm,yyid,px',$order_str,$start,$perpage); foreach ($info_list as $key=>$value) { //$country = $this->country->read($value['country']); //$info_list[$key]['country'] = $country['name']; } $total = $this->purchase->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list)); echo json_encode($rows);exit; } $this->_Template('purchase',$this->data); } //添加 public function _add() { $post = $this->input->post(NULL, TRUE); if(isset($post['title'])) { $bm = $this->input->post('bm',true); $n = $this->purchase->find_all("bm = '$bm'"); if(count($n) > 0) { echo json_encode(array('msg'=>'供应商编码必须唯一','success'=>false));exit; } if($this->purchase->insert($post)) { echo json_encode(array('msg'=>'添加成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit; } } $country = $this->country->find_all(); $this->data['country'] = $country; $this->_Template('purchase_add',$this->data); } //修改 public function _edit($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $bm = $this->input->post('bm',true); $n = $this->purchase->find_all("bm = '$bm'"); if(count($n) > 0 && $n[0]['id'] != $post['id']) { echo json_encode(array('msg'=>'供应商编码必须唯一','success'=>false));exit; } $id = $this->input->post('id',true); if($this->purchase->save($post,$id)) { echo json_encode(array('msg'=>'修改成功','success'=>true));exit; } else { echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit; } } $arg_array = $arg_array[0]; $purchase = $this->purchase->read($arg_array); $country = $this->country->find_all(); $this->data['country'] = $country; $this->data['purchase'] = $purchase; $this->_Template('purchase_edit',$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->purchase->remove($v); } echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true)); } } //获取数据 public function _rows() { $post = $this->input->post(NULL, TRUE); if(isset($post['rows'])) { $rows = $this->input->post('rows',true); $an = array(); $data = $this->productprice->find_all(); foreach ($data as $k=>$v) { $an[] = array('id'=>$v['id'],'title'=>$v['supplier']); } $price = array(); if($rows != "null") { $a = $this->purchase->read($rows);//找出次ID的权限内容 if($a['price']) { $price = explode('|',trim($a['price'],'|'));//数组化权限内容 } } echo json_encode(array('msg'=>($an),'power'=>($price),'success'=>true)); } } }