public function __construct()
 {
     AuthUser::load();
     if (!AuthUser::isLoggedIn()) {
         echo 'Please Login';
         header('location:index.php?job=login');
     }
     //首页最近文章
     //$this->recent_post = Article::getPost(5, true);
     //侧栏分类
     $this->categories = Category::findAll();
     if (is_array($this->categories) && count($this->categories) > 0) {
         $temp = array();
         foreach ($this->categories as $k => $v) {
             $temp[$v->id] = get_object_vars($v);
         }
         $this->cahce_categories = $temp;
         $temp = array();
     }
     //Tags 后期改进为热词形式
     $hot_tags = Tag::findAll(20);
     //mprint_r($hot_tags, '$hot_tags');
     if (count($hot_tags) > 0) {
         $first = current($hot_tags);
         $last = end($hot_tags);
         foreach ($hot_tags as $k => $v) {
             $tags_list[$k]['word'] = $v->name;
             $tags_list[$k]['size'] = tagClouds($v->count, $first->count, $last->count);
         }
     }
     $this->tags_list = $tags_list;
     $tags = Tag::findAll();
     //var_dump($tags);
     $content_tag = Record::findAllFrom('ContentTag');
     //var_dump($content_tag);
     //关系表中存在的文章ID以及tag集合到一个数组中tag_cache  避免在遍历生成文章静态页时重复读取数据库
     //遍历所有tag 组合出方便调用的形式
     if (is_array($tags) && count($tags) > 0) {
         $temp_tags = array();
         foreach ($tags as $k => $v) {
             $temp_tags[$v->id] = $v->name;
         }
     }
     //遍历关系表
     if (is_array($content_tag) && count($content_tag) > 0) {
         $this->cahce_tags = array();
         foreach ($content_tag as $k => $v) {
             if (isset($temp_tags[$v->tag_id])) {
                 $this->cahce_tags[$v->content_id][] = $temp_tags[$v->tag_id];
             }
         }
     }
     //清空临时数据
     $tags = $content_tag = $temp_tags = array();
 }
 public function createPostHtml($obj = null)
 {
     global $tpl;
     if (is_object($obj) && DEBUG === false) {
         $post = clone $obj;
         $post->content = processContent($post->content);
         //上一篇文章
         $previous = $obj->previous();
         //下一篇文章
         $next = $obj->next();
         //首页最近文章
         $recent_post = Article::getPost(5, false);
         //侧栏分类
         $categories = Category::findAll();
         //Tags
         $hot_tags = Tag::findAll(30);
         //mprint_r($hot_tags, '$hot_tags');
         if (count($hot_tags) > 0) {
             $first = current($hot_tags);
             $last = end($hot_tags);
             foreach ($hot_tags as $k => $v) {
                 $tags_list[$k]['word'] = $v->name;
                 $tags_list[$k]['size'] = tagClouds($v->count, $first->count, $last->count);
             }
         }
         //smarty
         $tpl->assign('post', $post);
         $tpl->assign('next', $next);
         $tpl->assign('previous', $previous);
         $tpl->assign('recent_post', $recent_post);
         $tpl->assign('categories', $categories);
         $tpl->assign('tag_list', $tags_list);
         $filetpl = SYSTEM_ROOT . 'templates/' . DEFAULT_TEMPLATE . '/post.html';
         $path = SYSTEM_ROOT . 'post/';
         //$filename = !empty($obj->slug) ? str_replace(' ', '-', trim($obj->slug)) : $obj->id;
         $filename = $obj->id;
         $file = $path . $filename . '.html';
         file_put_contents($file, $tpl->fetch($filetpl));
         @chmod($file, 0777);
     }
 }
Esempio n. 3
0
 public function __construct()
 {
     global $tpl, $app;
     $app->refresh();
     $this->cache = new FileCache();
     $this->cache->cachePath = DATA_DIR . 'cache/';
     //侧栏分类
     $categories = $this->cache->get('categories');
     if ($categories === false) {
         $categories = array();
         //获得所有分类
         $categories[0] = Category::findAll();
         if (is_array($categories[0]) && count($categories[0]) > 0) {
             foreach ($categories[0] as $k => $v) {
                 $categories[0][$k]->url = SITE_URL . 'category/' . $v->slug . '/';
                 //$this->categories[$v->id] = get_object_vars($v);
                 $categories[1][$v->id] = $v;
                 $categories[1][$v->slug] = $v;
             }
         }
         if (DEBUG === false) {
             $this->cache->set('categories', $categories, 7200);
             //缓存2小时
             del_cache();
         }
     }
     //mprint_r($categories[0], 'a');
     //分别以ID, slug为键值的分类组合
     $this->categories = $categories[1];
     //Tags 热词
     $tags_list = $this->cache->get('hot_tags');
     if ($tags_list === false) {
         $hot_tags = Tag::findAll(20);
         //mprint_r($hot_tags, '$hot_tags');
         if (count($hot_tags) > 0) {
             $first = current($hot_tags);
             $last = end($hot_tags);
             foreach ($hot_tags as $k => $v) {
                 $tags_list[$k]['word'] = $v->name;
                 $tags_list[$k]['size'] = tagClouds($v->count, $first->count, $last->count);
                 $tags_list[$k]['url'] = SITE_URL . 'tag/' . strtolower($v->name) . '/';
             }
         }
         if (DEBUG === false) {
             $this->cache->set('hot_tags', $tags_list, 7200);
         }
     }
     //首页最近文章
     $recent_post = $this->cache->get('recent_post');
     if ($recent_post === false) {
         $recent_post = Article::getPost(5);
         if (is_array($recent_post) && count($recent_post) > 0) {
             foreach ($recent_post as $key => $val) {
                 $recent_post[$key]->url = SITE_URL . 'post/' . $val->id . '.html';
             }
         }
         if (DEBUG === false) {
             $this->cache->set('recent_post', $recent_post, 7200);
             //缓存2小时
             del_cache();
         }
     }
     //mprint_r($recent_post, '$recent_post');
     $tpl->assign('recent_post', $recent_post);
     $tpl->assign('categories', $categories[0]);
     $tpl->assign('tag_list', $tags_list);
 }