/**
  * @param $id
  */
 public function posts($id = -1, $new_post = false)
 {
     $PostEvent = new PostsEvent();
     $this->post_id = $post_id = $id ? (int) $id : false;
     $Posts = new PostsLogic();
     if (IS_POST) {
         $post_data = $_POST;
         $post_data['post_modified'] = date("Y-m-d H:m:s", time());
         $post_data['post_type'] = $_POST['post_type'] ? $_POST['post_type'] : 'single';
         D("post_cat")->where(array("post_id" => $post_data['post_id']))->delete();
         D("post_tag")->where(array("post_id" => $post_data['post_id']))->delete();
         if (!empty($_POST['cats'])) {
             foreach ($_POST['cats'] as $cat_id) {
                 D("Post_cat")->add(array("cat_id" => $cat_id, "post_id" => $post_data['post_id']));
             }
         }
         if (!empty($_POST['tags'])) {
             foreach ($_POST['tags'] as $tag_id) {
                 D("Post_tag")->add(array("tag_id" => $tag_id, "post_id" => $post_data['post_id']));
             }
         }
         if ($post_data['post_type'] == 'single') {
             $url = U('Admin/Posts/index');
         } elseif ($post_data['post_type'] == 'page') {
             $url = U('Admin/Posts/page');
         } else {
             $url = U('Admin/Posts/index');
         }
         CacheManager::clearPostCacheById($id);
         if ($Posts->where(array("post_id" => $post_data["post_id"]))->save($post_data)) {
             $this->jsonReturn(1, "已经更新", $url);
         } else {
             //处理失败
             cookie('post_add' . $post_data["post_id"], gzcompress(json_encode($post_data)), 3600000);
             //支持大约2.8万个字符 Ueditor计算方法,所有中文和英文数字都算一个字符计算
             $this->jsonReturn(0, "更新失败", $url);
         }
     } else {
         $this->initEditor($id);
         //投稿员只能看到自己的
         if (!$this->noVerify()) {
             $where['user_id'] = get_current_user_id();
         }
         $where["post_id"] = $post_id;
         $post = D('Posts')->relation(true)->where($where)->find();
         if (empty($post)) {
             $this->error("不存在该记录");
         }
         if ($new_post) {
             $post['post_status'] = 'publish';
         }
         $tpl_type_list = $PostEvent->getTplList();
         //投稿员只能看到权限内部的分类
         if (!$this->noVerify()) {
             $user = D('User', 'Logic')->detail((int) $_SESSION[get_opinion('USER_AUTH_KEY')]);
             $role = D('Role')->where(array('id' => $user["user_role"]["role_id"]))->find();
             $cats = D('Cats', 'Logic')->where(array('in', json_decode($role["cataccess"])))->select();
             foreach ($cats as $key => $value) {
                 $cats[$key]['cat_slug'] = $cats[$key]['cat_name'];
             }
             $tags = array();
         } else {
             $cats = D('Cats', 'Logic')->category();
             $tags = D('Tags', 'Logic')->select();
         }
         $this->assign("cats", $cats);
         $this->assign("tags", $tags);
         $this->assign('tpl_type', gen_opinion_list($tpl_type_list, $post['post_template']));
         $this->assign('post_status', gen_opinion_list(get_opinion("post_status"), $post['post_status']));
         $this->assign('post_type', gen_opinion_list(get_opinion("post_type"), $post['post_type']));
         $this->assign("info", $post);
         $this->assign("handle", U('Admin/Posts/posts', array('id' => $id, 'new_post' => $new_post), true, false));
         $this->assign("action", '编辑文章');
         $this->assign("action_name", 'posts');
         $this->display('post_v3');
     }
 }
 /**
  * @param $id
  */
 public function posts($id = -1)
 {
     $this->action = '编辑文章';
     $this->action_name = 'posts';
     $this->post_id = $post_id = $_GET['id'] ? (int) $_GET['id'] : false;
     $Posts = new PostsLogic();
     if (IS_POST) {
         $post_data = $_POST;
         $post_data['post_modified'] = date("Y-m-d H:m:s", time());
         $post_data['post_type'] = $_POST['post_type'] ? $_POST['post_type'] : 'single';
         M("post_cat")->where(array("post_id" => $post_data['post_id']))->delete();
         M("post_tag")->where(array("post_id" => $post_data['post_id']))->delete();
         if (!empty($_POST['cats'])) {
             foreach ($_POST['cats'] as $cat_id) {
                 M("Post_cat")->add(array("cat_id" => $cat_id, "post_id" => $post_data['post_id']));
             }
         }
         if (!empty($_POST['tags'])) {
             foreach ($_POST['tags'] as $tag_id) {
                 M("Post_tag")->add(array("tag_id" => $tag_id, "post_id" => $post_data['post_id']));
             }
         }
         if ($post_data['post_type'] == 'single') {
             $url = U('Admin/Posts/index');
         } elseif ($post_data['post_type'] == 'page') {
             $url = U('Admin/Posts/page');
         } else {
             $url = U('Admin/Posts/index');
         }
         if ($Posts->save($post_data)) {
             $this->jsonReturn(1, "已经更新", $url);
         } else {
             $this->jsonReturn(0, "更新失败", $url);
         }
     } else {
         //投稿员只能看到自己的
         if (!$this->noVerify()) {
             $where['user_id'] = get_current_user_id();
         }
         $where["post_id"] = $post_id;
         $post = D('Posts')->relation(true)->where($where)->find();
         if (empty($post)) {
             $this->error("不存在该记录");
         }
         $PostEvent = new PostsEvent();
         $tpl_type_list = $PostEvent->get_tpl_type_list();
         $this->assign('tpl_type', gen_opinion_list($tpl_type_list, $post['post_template']));
         //投稿员只能看到自己的
         if (!$this->noVerify()) {
             $user_id = (int) $_SESSION[C('USER_AUTH_KEY')];
             $user = D('User', 'Logic')->detail($user_id);
             $role_id = $user["user_role"]["role_id"];
             $role = D('Role')->where(array('id' => $role_id))->find();
             $where['cat_id'] = array('in', json_decode($role["cataccess"]));
             $cats = D('Cats', 'Logic')->where($where)->select();
             foreach ($cats as $key => $value) {
                 $cats[$key]['cat_slug'] = $cats[$key]['cat_name'];
             }
         } else {
             $cats = D('Cats', 'Logic')->category();
             $tags = D('Tags', 'Logic')->select();
         }
         $this->assign("cats", $cats);
         $this->assign("tags", $tags);
         $this->assign("info", $post);
         $this->assign("handle", U('Admin/Posts/posts', array('id' => $id), true, false));
         $this->assign("publish", "更新");
         $this->display('add');
     }
 }