public function save() { if (!$this->check_power('subject_category_manage')) { return; } $act = trim($this->input->post('act')); $id = intval($this->input->post('id')); $act == 'edit' && $id && ($detail = SubjectCategoryModel::get_subject_category($id)); if ($act == 'edit' && empty($detail)) { message('方法策略分类不存在'); return; } $name = trim($this->input->post('name')); if ($name == '') { message('请填写方法策略分类名称!'); return; } if ($act == 'edit') { $subject_id = $this->input->post('subject_id'); $subject_id = array_filter(array_unique($subject_id)); $subject_id_str = implode(',', $subject_id); $subject_category_name = array(); $result = $this->db->query("select b.name from {pre}subject_category_subject a\n left join {pre}subject_category b on a.subject_category_id=b.id\n \t where a.subject_id in ({$subject_id_str}) and a.subject_category_id<>'{$id}' ")->result_array(); if ($result) { foreach ($result as $row) { $subject_category_name[] = $row['name ']; } } if (in_array($name, $subject_category_name)) { message('该方法策略分类名称已经存在!'); } } else { $subject_id = $this->input->post('subject_id'); $subject_id = array_filter(array_unique($subject_id)); $subject_id_str = implode(',', $subject_id); $result = $this->db->query("select b.name from {pre}subject_category_subject a\n left join {pre}subject_category b on a.subject_category_id=b.id\n where a.subject_id in ({$subject_id_str}) ")->result_array(); $subject_category_name = array(); if ($result) { foreach ($result as $row) { $subject_category_name[] = $row['name ']; } } if (in_array($name, $subject_category_name)) { message('该方法策略分类名称已经存在!'); } } $subject_id = $this->input->post('subject_id'); if (!is_array($subject_id) || !count($subject_id)) { message('请选择学科'); } $subject_id = array_filter(array_unique($subject_id)); $data = array('name' => $name); $subject_data = array(); if ($act == 'add') { $data['ctime'] = date('Y-m-d H:i:s'); $rel = $this->db->insert('subject_category', $data); if ($rel) { $inserted_id = $this->db->insert_id(); foreach ($subject_id as $v) { $subject_data[] = array('subject_category_id' => $inserted_id, 'subject_id' => $v); } $this->db->insert_batch('subject_category_subject', $subject_data); admin_log('add', 'subject_category', $inserted_id); } else { message('方法策略分类添加失败'); } } else { $rel = $this->db->update('subject_category', $data, array('id' => $id)); if ($rel) { $this->db->trans_start(); $insert_data = array(); $query = array('subject_category_id' => $id); $old_subject_categories = SubjectCategoryModel::get_subject_category_subjects($id, true, true); foreach ($subject_id as $v) { if (!isset($old_subject_categories[$v])) { $insert_data[] = array('subject_category_id' => $id, 'subject_id' => $v); } unset($old_subject_categories[$v]); } if (count($insert_data)) { $this->db->insert_batch('subject_category_subject', $insert_data); } $delete_ids = array_keys($old_subject_categories); if (count($delete_ids)) { $this->db->where('subject_category_id', $id)->where_in('subject_id', $delete_ids)->delete('subject_category_subject'); } admin_log('edit', 'subject_category', $id); $this->db->trans_complete(); } else { message('方法策略分类修改失败'); } } if ($act == 'add') { message('方法策略分类添加成功', 'admin/subject_category/index'); } else { message('方法策略分类修改成功', 'admin/subject_category/index'); } }