Study.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Study extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_study','study');
  7. $this->load->_model('Model_studyclass','studyclass');
  8. $this->load->_model('Model_typeclass','typeclass');
  9. $this->load->_model('Model_shop','shop');
  10. $this->load->_model("Model_studytype","studytype");
  11. }
  12. //定义方法的调用规则 获取URI第二段值
  13. public function _remap($arg,$arg_array)
  14. {
  15. if($arg == 'add')//添加
  16. {
  17. $this->_add();
  18. }
  19. else if($arg == 'edit')//修改
  20. {
  21. $this->_edit($arg_array);
  22. }
  23. else if($arg == 'see')
  24. {
  25. $this->_see($arg_array);
  26. }
  27. else if($arg == 'del')
  28. {
  29. $this->_del($arg_array);
  30. }
  31. else
  32. {
  33. $this->_index($arg_array);
  34. }
  35. }
  36. protected function getUserPlat(){
  37. $user = $this->user->get_api($_SESSION['api']);
  38. $this->data['user'] = $user;//登录的用户信息
  39. $usersp = explode('|',trim($user['shop'],'|'));$sid = '';
  40. foreach ($usersp as $v)
  41. {
  42. $sid .= " id = ".$v." or";
  43. }
  44. $result = $this->shop->find_all(rtrim($sid,'or'),'*',null,0,120);
  45. $plat_ids = array_column($result,'type');
  46. $plat_ids = array_unique($plat_ids);
  47. $plat_list = $this->typeclass->find_all("id in (".implode(",",$plat_ids).")");
  48. foreach ($plat_list as $k=>$v){
  49. if($v['id'] == 269){
  50. $plat_list[$k]['title'] = "独立站";
  51. }
  52. }
  53. $this->data['plat_list'] = array_column($plat_list,'title','id');
  54. }
  55. public function dd($data){
  56. echo "<pre>";
  57. print_r($data);
  58. die;
  59. }
  60. //管理
  61. public function _index($arg_array)
  62. {
  63. $this->getUserPlat();
  64. $post = $this->input->post(NULL, TRUE);
  65. if(isset($post['page']))
  66. {
  67. $page = $this->input->post('page',true);
  68. $perpage = $this->input->post('perpage',true);
  69. $title = $this->input->post('title',true);
  70. $count = $this->input->post('count',true);
  71. $type = $this->input->post('type',true);
  72. $class = $this->input->post("studyclass",true);
  73. $where = "1=1 ";
  74. //数据排序
  75. $order_str = "id desc";
  76. if(empty($page))
  77. {
  78. $start = 0;
  79. $perpage = 1;
  80. }
  81. else
  82. {
  83. $start = ($page - 1)*$perpage;
  84. }
  85. if($title)
  86. {
  87. $where .= " and s.title like '%$title%'";
  88. }
  89. if($count)
  90. {
  91. $where .= " and s.content like '%$count%'";
  92. }
  93. if($class){
  94. $where .= " and s.class = ".$class;
  95. }
  96. if($type)
  97. {
  98. $where .= " and st.type = '$type'";
  99. $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" );
  100. }
  101. // if($type)
  102. // {
  103. // $where .= " and type = '$type'";
  104. // $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" );
  105. // }else{
  106. // //取得信息列表
  107. // $info_list = $this->study->find_all($where,'id,class,title,addtime',$order_str,$start,$perpage);
  108. // }
  109. $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 );
  110. $info_list = $info_list->result_array();
  111. $final_list = [];
  112. $all_plat = $this->data['plat_list'];
  113. foreach($info_list as $k=>$v){
  114. $tmp_plat_list = $this->studytype->find_all("article_id = ".$v['id']);
  115. $plat_list_arr = [];
  116. foreach($tmp_plat_list as $val){
  117. if(isset($all_plat[$val['type']])){
  118. $plat_list_arr[] = $all_plat[$val['type']];
  119. }
  120. }
  121. $classtype = $this->studyclass->find("id = " . $v['class']);
  122. $final_list[] = [
  123. 'id'=>$v['id'],
  124. 'title'=>$v['title'],
  125. 'class'=>$classtype['title'],
  126. 'plat'=> implode("|",$plat_list_arr),
  127. 'addtime'=>($v['edittime'] != 0)?date("Y-m-d H:i:s",$v['edittime']):date("Y-m-d H:i:s",$v['addtime'])
  128. ];
  129. }
  130. //格式化数据
  131. // foreach ($info_list as $key=>$value)
  132. // {
  133. // // $type = $this->typeclass->read($value['type']);
  134. // // $info_list[$key]['type'] = $type['title'];
  135. // $f = $this->studyclass->read($value['class']);
  136. // $info_list[$key]['class'] = $f['title'];
  137. // $d = $this->study->read($value['id']);
  138. // if($d['edittime'] != 0)
  139. // {
  140. // $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$d['edittime']);
  141. // }
  142. // else
  143. // {
  144. // $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
  145. // }
  146. //}
  147. $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();
  148. $pagenum = ceil($total/$perpage);
  149. $over = $total-($start+$perpage);
  150. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($final_list));
  151. echo json_encode($rows);exit;
  152. }
  153. //$this->dd($this->data);
  154. $studyclass = $this->studyclass->find_all();
  155. $this->data['see'] = (isset($arg_array[0]))?'see':'';
  156. $this->data['studyclass'] = $studyclass;
  157. $this->_Template('study',$this->data);
  158. }
  159. //添加
  160. public function _add()
  161. {
  162. $this->getUserPlat();
  163. $post = $this->input->post(NULL, TRUE);
  164. if(isset($post['title']))
  165. {
  166. $type_ids = $post['type'];
  167. $type_ids = explode(",",$type_ids);
  168. unset($post['type']);
  169. $post['addtime'] = time();
  170. $article_id = $this->study->insert($post);
  171. if($article_id)
  172. {
  173. $insert_arr = [];
  174. foreach($type_ids as $v){
  175. if(!empty($v)){
  176. $this->studytype->insert(
  177. [
  178. 'type'=>$v,
  179. 'article_id'=>$article_id,
  180. 'addtime'=>time(),
  181. ]
  182. );
  183. }
  184. }
  185. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  186. }
  187. else
  188. {
  189. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  190. }
  191. }
  192. $studyclass = $this->studyclass->find_all();
  193. $this->data['studyclass'] = $studyclass;
  194. $this->_Template('study_add',$this->data);
  195. }
  196. //修改
  197. public function _edit($arg_array)
  198. {
  199. $this->getUserPlat();
  200. $post = $this->input->post(NULL, TRUE);
  201. if(isset($post['id']))
  202. {
  203. $id = $this->input->post('id',true);
  204. $post['title'] = $this->input->post('title',true);
  205. $post['edittime'] = time();
  206. // $f = $this->studyclass->read($post['class']);
  207. // $post['type'] = $f['type'];
  208. $type_ids = $post['type'];
  209. $type_ids = explode(",",$type_ids);
  210. unset($post['type']);
  211. $post['addtime'] = time();
  212. if($this->study->save($post,$id))
  213. {
  214. $this->studytype->query("delete from crowd_studytype where article_id = ".$id);
  215. $insert_arr = [];
  216. foreach($type_ids as $v){
  217. if(!empty($v)){
  218. $this->studytype->insert(
  219. [
  220. 'type'=>$v,
  221. 'article_id'=>$id,
  222. 'addtime'=>time(),
  223. ]
  224. );
  225. }
  226. }
  227. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  228. }
  229. else
  230. {
  231. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  232. }
  233. }
  234. $arg_array = $arg_array[0];
  235. $study = $this->study->read($arg_array);
  236. $this->data['study'] = $study;
  237. $studyclass = $this->studyclass->find_all();
  238. $this->data['studyclass'] = $studyclass;
  239. $plat_list = $this->studytype->find_all("article_id = ".$arg_array);
  240. $plat_ids = array_column($plat_list,'type');
  241. $this->data['plat_ids'] = $plat_ids;
  242. $this->_Template('study_edit',$this->data);
  243. }
  244. public function _see($arg_array)
  245. {
  246. $this->getUserPlat();
  247. $arg_array = $arg_array[0];
  248. $study = $this->study->read($arg_array);
  249. $this->data['study'] = $study;
  250. $studyclass = $this->studyclass->find_all();
  251. $this->data['studyclass'] = $studyclass;
  252. $this->_Template('study_see',$this->data);
  253. }
  254. //删除
  255. public function _del()
  256. {
  257. $post = $this->input->post(NULL, TRUE);
  258. if(isset($post['s']))
  259. {
  260. $id_arr = $this->input->post('s');
  261. $id_arr = explode(',',$id_arr);
  262. if(!$id_arr)
  263. {
  264. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  265. }
  266. //循环删除记录
  267. foreach ($id_arr as $v)
  268. {
  269. $this->study->remove($v);
  270. if(!empty($v)){
  271. $this->studytype->query("delete from crowd_studytype where article_id = ".$v);
  272. }
  273. }
  274. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  275. }
  276. }
  277. }