public function addAction() { global $tpl; //如果是分表则通过$_POST['content'], $_POST['content_part']区分 //$data = isset($_POST['article']) ? $_POST['article'] : array(); $data = array(); if (isset($_POST['add']) || isset($_POST['save'])) { $data = $_POST['content']; //判断ID是否存在 if (!$data['id']) { //add $data['slug'] = !empty($data['slug']) ? dealWords($data['slug']) : null; $data['created_uid'] = AuthUser::getId(); $data['created_date'] = time(); $article = new Article($data); if ($article->save()) { $data_part = array(); $data_part = $_POST['content_part']; $data_part['id'] = $article->id; $content_part = new ContentPart($data_part); $content_part->insert(); $article->content = $content_part->content; //save tags $article->saveTags(trim($_POST['tags'])); //update category $category = new Category('id', $article->cid); $category->count++; $category->save(); $article->category = $category->name; $article->category_slug = $category->slug; //删除上一篇缓存 $prev = $article->previous(); $this->delPostHtml($prev->id); } } else { //update $data['updated_uid'] = AuthUser::getId(); $data['updated_date'] = time(); $old_article = new Article('id', $data['id']); $article = new Article($data); if ($article->save()) { $data_part = array(); $data_part = $_POST['content_part']; $data_part['id'] = $article->id; $content_part = new ContentPart($data_part); $content_part->save(); $article->content = $content_part->content; $article->saveTags($_POST['tags']); //更新文章分类统计 if ($old_article->cid != $article->cid) { $c1 = new Category('id', $article->cid); $c1->count++; $c1->save(); $c2 = new Category('id', $old_article->cid); $c2->count--; $c2->save(); } } } //$_POST = array(); $tpl->assign('article', $article); //生成静态页面 $status = isset($data['status']) ? $data['status'] : 'draft'; if ($status == 'publish') { $this->createPostHtml($article); } else { if ($status == 'draft') { $this->delPostHtml($article->id); } } del_cache(); if (isset($_POST['add'])) { header('location:index.php?job=admin_article'); } } $categories = Category::findAll(); $tpl->assign('cats', $categories); $tpl->display('admin/article_add.html'); }
public function showAction($args = null) { global $tpl, $app; $id = isset($args['id']) ? (int) $args['id'] : ''; //如果为空 提示出错 try { $article = new Article('id', $id); if (!$article->id) { throw new Exception('文章不存在'); } $content_part = ContentPart::findById($id); //语法加亮处理 $article->content = processContent($content_part->content); $article->tags = $article->getTags(); $article->category = $this->categories[$article->cid]->name; $article->category_slug = $this->categories[$article->cid]->slug; //上一篇文章 $previous = $article->previous(); //下一篇文章 $next = $article->next(); //首页最近文章 //$recent_post = Article::getPost(5, true); } catch (Exception $e) { //die($e->getMessage()); $app->error($e->getMessage(), SITE_URL); } $tpl->assign('post', $article); $tpl->assign('next', $next); $tpl->assign('previous', $previous); //首页post静态页 if (DEBUG === false) { $fp = fopen(SYSTEM_ROOT . "post/{$id}.html", 'w'); fputs($fp, $tpl->fetch(DEFAULT_TEMPLATE . '/post.html')); fclose($fp); } $tpl->display(DEFAULT_TEMPLATE . '/post.html'); }