/**
  * Delete an instance of an Article entity from the database
  *
  * @access public
  * @param Article $article
  * @return bool Return status of PDOStatement execute method
  */
 public function delete(Article $article)
 {
     // Delete associated tag entries first while the article still exists in the database.
     // MyISAM does not support transactions. Considering converting tables to InnoDB?
     $tagDAO = ArticleTagDAO::getInstance();
     $oldtags = $article->getTags();
     $article->setTags("");
     $tagDAO->updateTags($article);
     $query = "DELETE FROM " . $this->tableName . " WHERE id = ?";
     //echo $query;
     $stmt = self::$dbh->prepare($query);
     $params = array($article->id);
     $status = $stmt->execute($params);
     // If failed, revert tagged entries since MyISAM does not support transactions
     if (!$status) {
         $article->setTags($oldtags);
         $tagDAO->updateTags($article);
     }
     return $status;
 }
 public function __invoke($args)
 {
     if (count($args) < 1) {
         throw new OrongoScriptParseException("Argument missing for Articles.GetTags()");
     }
     $article = new Article($args[0]);
     return new OrongoList($article->getTags());
 }
 public function editAction($args = null)
 {
     global $tpl;
     //$id = isset($_GET['id']) ? $_GET['id'] : '';    //这里以后改进成$params['id']
     $id = isset($args['id']) ? (int) $args['id'] : '';
     //如果为空 提示出错
     $article = new Article('id', $id);
     $content_part = ContentPart::findById($id);
     $article->content = $content_part->content;
     $article->tags = $article->getTags();
     $tpl->assign('article', $article);
     //查出所有分类
     $categories = Category::findAll();
     $tpl->assign('cats', $categories);
     $tpl->display('admin/article_add.html');
     del_cache();
 }
 /**
  * Determine new tags for an article and now unused tags for an Article. Insert new ArticleTag and TaggedArticle entities and delete any unused tags by deleting the associated TaggedArticle entities
  *
  * @access public
  * @param Article $article
  */
 public function updateTags(Article $article)
 {
     $articleDAO = ArticleDAO::getInstance();
     $taggedDAO = TaggedArticleDAO::getInstance();
     // Obtain current tags for an article
     $this->resetQueryStrings();
     $this->query_reset_lock = true;
     $this->query_joins = " INNER JOIN {$taggedDAO->getTableName()} ON {$this->tableName}.id = {$taggedDAO->getTableName()}.tagId INNER JOIN {$articleDAO->getTableName()} ON {$taggedDAO->getTableName()}.articleId = {$articleDAO->getTableName()}.id ";
     $this->query_where = "WHERE {$articleDAO->getTableName()}.id = ?";
     $this->query_params = array($article->getId());
     $current_tags_array = $this->all();
     $this->query_reset_lock = false;
     // Remove unused tags
     $updated_tags = ArticleTag::tagsFromString($article->getTags());
     $tags_to_remove = array();
     foreach ($current_tags_array as $tag) {
         if (!in_array($tag->getName(), $updated_tags)) {
             $tags_to_remove[] = $tag->getId();
         }
     }
     //print_r ($tags_to_remove);
     if (!empty($tags_to_remove)) {
         $status = $taggedDAO->deleteByTagged($tags_to_remove, $article->id);
     }
     //print_r ($updated_tags);
     // Create new tags
     $this->resetQueryStrings();
     $this->query_reset_lock = true;
     $current_tags_string_array = array();
     foreach ($current_tags_array as $tag) {
         $current_tags_string_array[] = $tag->name;
     }
     //print_r ($current_tags_string_array);
     foreach ($updated_tags as $tag) {
         if (!in_array($tag, $current_tags_string_array)) {
             //$newtag = new ArticleTag ();
             //$newtag->setName ($tag);
             $newtag = $this->loadByName($tag);
             if (!$newtag) {
                 $newtag = new ArticleTag();
                 $newtag->setName($tag);
                 $this->insert($newtag);
             }
             // Insert new tag. Will fail if tag name already exists.
             //if ($this->insert ($newtag)) {
             //print_r ($newtag);
             //}
             // Tag already exists. Load it
             //else {
             //    $newtag = $this->loadByName ($newtag->name);
             //print_r ($newtag);
             // }
             $tagged = new TaggedArticle();
             $tagged->setArticleId($article->getId());
             $tagged->setTagId($newtag->getId());
             $status = $taggedDAO->insert($tagged);
         }
     }
     $this->query_reset_lock = false;
     return null;
 }
Example #5
0
 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');
 }