/** * @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'); } }
/** * 获取返回数据的关联记录 * @access protected * @param mixed $result 返回数据 * @param string|array $name 关联名称 * @param boolean $return 是否返回关联数据本身 * @return array */ protected function getRelation(&$result, $name = '', $return = false) { if (!empty($this->_link)) { foreach ($this->_link as $key => $val) { $mappingName = !empty($val['mapping_name']) ? $val['mapping_name'] : $key; // 映射名称 if (empty($name) || true === $name || $mappingName == $name || is_array($name) && in_array($mappingName, $name)) { $mappingType = !empty($val['mapping_type']) ? $val['mapping_type'] : $val; // 关联类型 $mappingClass = !empty($val['class_name']) ? $val['class_name'] : $key; // 关联类名 $mappingFields = !empty($val['mapping_fields']) ? $val['mapping_fields'] : '*'; // 映射字段 $mappingCondition = !empty($val['condition']) ? $val['condition'] : '1=1'; // 关联条件 $mappingKey = !empty($val['mapping_key']) ? $val['mapping_key'] : $this->getPk(); // 关联键名 if (strtoupper($mappingClass) == strtoupper($this->name)) { // 自引用关联 获取父键名 $mappingFk = !empty($val['parent_key']) ? $val['parent_key'] : 'parent_id'; } else { $mappingFk = !empty($val['foreign_key']) ? $val['foreign_key'] : strtolower($this->name) . '_id'; // 关联外键 } // 获取关联模型对象 $model = D($mappingClass); switch ($mappingType) { case self::HAS_ONE: $pk = $result[$mappingKey]; $mappingCondition .= " AND {$mappingFk}='{$pk}'"; $relationData = $model->cache(true, 2)->where($mappingCondition)->field($mappingFields)->find(); if (!empty($val['relation_deep'])) { $model->getRelation($relationData, $val['relation_deep']); } break; case self::BELONGS_TO: if (strtoupper($mappingClass) == strtoupper($this->name)) { // 自引用关联 获取父键名 $mappingFk = !empty($val['parent_key']) ? $val['parent_key'] : 'parent_id'; } else { $mappingFk = !empty($val['foreign_key']) ? $val['foreign_key'] : strtolower($model->getModelName()) . '_id'; // 关联外键 } //TODO Patch for thinkphp3.2 by GREENCMS $mapping_foreign_key = !empty($val['mapping_foreign_key']) ? $val['mapping_foreign_key'] : $model->getPk(); $fk = $result[$mappingFk]; $mappingCondition .= " AND {$mapping_foreign_key}='{$fk}'"; $relationData = $model->cache(true, 2)->where($mappingCondition)->field($mappingFields)->find(); if (!empty($val['relation_deep'])) { $model->getRelation($relationData, $val['relation_deep']); } break; case self::HAS_MANY: $pk = $result[$mappingKey]; $mappingCondition .= " AND {$mappingFk}='{$pk}'"; $mappingOrder = !empty($val['mapping_order']) ? $val['mapping_order'] : ''; $mappingLimit = !empty($val['mapping_limit']) ? $val['mapping_limit'] : ''; // 延时获取关联记录 $relationData = $model->cache(true, 2)->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select(); if (!empty($val['relation_deep'])) { foreach ($relationData as $key => $data) { $model->getRelation($data, $val['relation_deep']); $relationData[$key] = $data; } } break; case self::MANY_TO_MANY: $pk = $result[$mappingKey]; $prefix = $this->tablePrefix; $mappingCondition = " {$mappingFk}='{$pk}'"; $mappingOrder = $val['mapping_order']; $mappingLimit = $val['mapping_limit']; $mappingRelationFk = $val['relation_foreign_key'] ? $val['relation_foreign_key'] : $model->getModelName() . '_id'; if (isset($val['relation_table'])) { $mappingRelationTable = preg_replace_callback("/__([A-Z_-]+)__/sU", function ($match) use($prefix) { return $prefix . strtolower($match[1]); }, $val['relation_table']); } else { $mappingRelationTable = $this->getRelationTableName($model); } $mappingRelationTable = $prefix . $mappingRelationTable; $sql = "SELECT b.{$mappingFields} FROM {$mappingRelationTable} AS a, " . $model->getTableName() . " AS b WHERE a.{$mappingRelationFk} = b.{$model->getPk()} AND a.{$mappingCondition}"; if (!empty($val['condition'])) { $sql .= ' AND ' . $val['condition']; } if (!empty($mappingOrder)) { $sql .= ' ORDER BY ' . $mappingOrder; } if (!empty($mappingLimit)) { $sql .= ' LIMIT ' . $mappingLimit; } //缓存key // $cache_key=md5($sql); // // if (S($cache_key)) { // // echo "cache hit $cache_key : $sql <br />"; // $relationData = S($sql); // if($relationData=="none"){ // $relationData=array(); // }else{ // $relationData= S($cache_key); // } // // } else { // // echo "cache miss $cache_key : $sql <br />"; // // $relationData = $this->query($sql); // // if(empty($relationData)){ // S($cache_key, "none", array('expire'=>get_opinion("DATA_CACHE_TIME"))); // }else{ // S($cache_key, $relationData, array('expire'=>get_opinion("DATA_CACHE_TIME"))); // } // } if ($key == 'Cat') { //key: relation_post_cat_$post_id // dump($mappingFk); // dump($pk); $relationData = CacheManager::getPostCatRelation($pk); if ($relationData == 'none') { // echo "Cat cache none {$pk} <br />"; $relationData = array(); } else { if ($relationData) { // echo "Cat cache hit {$pk}<br />"; } else { $relationData = $this->query($sql); if (empty($relationData)) { // echo "Cat is empty {$pk} <br />"; CacheManager::setPostCatRelation($pk, 'none'); } else { // echo "Cat cache missed {$pk} <br />"; CacheManager::setPostCatRelation($pk, $relationData); } } } } else { if ($key == 'Tag') { //key: relation_post_tag_$post_id $relationData = CacheManager::getPostTagRelation($pk); if ($relationData == 'none') { // echo "Tag cache none {$pk} <br />"; $relationData = array(); } else { if ($relationData) { // echo "Tag cache hit {$pk}<br />"; } else { $relationData = $this->query($sql); if (empty($relationData)) { // echo "Tag is empty {$pk} <br />"; CacheManager::setPostTagRelation($pk, 'none'); } else { // echo "Tag cache missed {$pk} <br />"; CacheManager::setPostTagRelation($pk, $relationData); } } } } else { // echo "no cache cover"; //todo improve performance $relationData = $this->query($sql); } } if (!empty($val['relation_deep'])) { foreach ($relationData as $key => $data) { $model->getRelation($data, $val['relation_deep']); $relationData[$key] = $data; } } break; } if (!$return) { if (isset($val['as_fields']) && in_array($mappingType, array(self::HAS_ONE, self::BELONGS_TO))) { // 支持直接把关联的字段值映射成数据对象中的某个字段 // 仅仅支持HAS_ONE BELONGS_TO $fields = explode(',', $val['as_fields']); foreach ($fields as $field) { if (strpos($field, ':')) { list($relationName, $nick) = explode(':', $field); $result[$nick] = $relationData[$relationName]; } else { $result[$field] = $relationData[$field]; } } } else { $result[$mappingName] = $relationData; } unset($relationData); } else { return $relationData; } } } } return $result; }
/** * 修改文章状态 * @param $post_id * @param $post_status * @return mixed */ public function changePostStatue($post_id, $post_status) { CacheManager::clearPostCacheById($post_id); return $this->where(array('post_id' => $post_id))->setField(array("post_status" => $post_status)); }
public function __construct() { parent::__construct(); CacheManager::clearLink(); CacheManager::clearMenu(); }