Beispiel #1
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $session = $this->getSession();
         try {
             $name = $posts->get('name');
             $description = $posts->get('description');
             $avatar = $posts->get('avatar');
             if (!$name) {
                 throw new \Exception('发布者名称不能为空');
             }
             if (!$description) {
                 throw new \Exception('发布者描述不能为空');
             }
             $publisher = new PublisherModel();
             $publisher->name = $name;
             $publisher->description = $description;
             $publisher->avatar = $avatar;
             $now = time();
             $publisher->createTimestamp = $now;
             $publisher->updateTimestamp = $now;
             // 保存
             PublisherModel::createPublisher($publisher);
             $session->addFlash('success', '操作成功');
             return new RedirectResponse($this->generateUrl('admin_www_publisher'));
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('admin_www_publisher_add'));
         }
     }
     return $this->render('publisher/add.html.twig');
 }
Beispiel #2
0
 public function handle()
 {
     $session = $this->getSession();
     $publisher = PublisherModel::getPublisher($this->id);
     if (!$publisher) {
         $session->addFlash('error', '发布者不存在');
         return new RedirectResponse($this->generateUrl('admin_www_publisher'));
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         try {
             $name = $posts->get('name');
             $description = $posts->get('description');
             $avatar = $posts->get('avatar');
             if (!$name) {
                 throw new \Exception('发布者名称不能为空');
             }
             if (!$description) {
                 throw new \Exception('发布者描述不能为空');
             }
             $publisher->name = $name;
             $publisher->description = $description;
             $publisher->avatar = $avatar;
             $publisher->updateTimestamp = time();
             // 保存
             PublisherModel::savePublisher($publisher);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_publisher_edit', array('id' => $this->id)));
     }
     return $this->render('publisher/edit.html.twig', array('publisher' => $publisher));
 }
Beispiel #3
0
 protected function handle()
 {
     $tree = StructureModel::getTree();
     $publishers = PublisherModel::allPublishers();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $publisher_id = $request->request->get('publisher_id');
         $structure_id = $request->request->get('structure_id');
         $session = $this->getSession();
         $db = ArticleModel::getDb();
         try {
             $db->transaction();
             if (!$publisher_id) {
                 throw new \Exception('请选择一个发布者');
             }
             if (!$structure_id) {
                 throw new \Exception('请选择发布所属目录');
             }
             $publisher = PublisherModel::getPublisher($publisher_id);
             if (!$publisher) {
                 throw new \Exception('发布者不存在');
             }
             foreach ($ids as $id) {
                 $article = ArticleModel::getArticle($id);
                 if ($article) {
                     $article->publishTimestamp = time();
                     $article->structureId = $structure_id;
                     ArticleModel::saveArticle($article);
                 }
                 // 添加文章发布者关系
                 $article_publisher = new ArticlePublisherModel();
                 $article_publisher->articleId = $article->id;
                 $article_publisher->publisherId = $publisher->id;
                 ArticlePublisherModel::createRelationship($article_publisher);
             }
             // 添加发布者数据
             $publisher->articleCount += count($ids);
             PublisherModel::savePublisher($publisher);
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_article'));
     } else {
         $ids = $request->query->get('ids');
         $ids = json_decode($ids);
         if (!$ids) {
             return $this->render('@TachigoWWWAdmin/error-modal.html.twig', array('message' => '没有选中任何文章'));
         } else {
             $pager = ArticleModel::listArticles(1, count($ids), function (QueryBuilder $qb) use($ids) {
                 $qb->where($qb->expr()->in('id', $ids));
             });
             return $this->render('article/publish.html.twig', array('articles' => $pager, 'publishers' => $publishers, 'tree' => $tree));
         }
     }
 }
Beispiel #4
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $session = $this->getSession();
         $db = ArticleModel::getDb();
         try {
             $db->transaction();
             foreach ($ids as $id) {
                 $article = ArticleModel::getArticle($id);
                 if ($article) {
                     $article->publishTimestamp = 0;
                     ArticleModel::saveArticle($article);
                 }
                 // 修改发布者信息
                 $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) {
                     $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article->id);
                 });
                 if ($article_publishers) {
                     foreach ($article_publishers as $article_publisher) {
                         $publisher_id = $article_publisher['publisher_id'];
                         // 删除发布者文章关系
                         ArticlePublisherModel::deleteRelationship($article->id, $publisher_id);
                         // 修改发布者数据
                         $publisher = PublisherModel::getPublisher($publisher_id);
                         if ($publisher) {
                             $publisher->articleCount -= 1;
                             PublisherModel::savePublisher($publisher);
                         }
                     }
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_article'));
     } else {
         $ids = $request->query->get('ids');
         $ids = json_decode($ids);
         if (!$ids) {
             return $this->render('@TachigoWWWAdmin/error-modal.html.twig', array('message' => '没有选中任何文章'));
         } else {
             $pager = ArticleModel::listArticles(1, count($ids), function (QueryBuilder $qb) use($ids) {
                 $qb->where($qb->expr()->in('id', $ids));
             });
             return $this->render('article/trash.html.twig', array('articles' => $pager));
         }
     }
 }
Beispiel #5
0
 public function __invoke($slug = null)
 {
     $structure = StructureModel::search($slug);
     if (!$structure) {
         $structure = StructureModel::getStructure($slug);
     }
     if (!$structure) {
         // 404
     }
     // 将所有文章用博客的形式展现
     // 筛选出所有发布的文章
     $request = $this->getRequest();
     $page = $request->query->get('page');
     $size = 15;
     $articles_pager = ArticleModel::listArticles($page, $size, function (QueryBuilder $qb) use($structure) {
         $qb->andWhere($qb->expr()->gt('publish_timestamp', 0));
         $qb->andWhere($qb->expr()->eq('structure_id', ':structure_id'))->setParameter(':structure_id', $structure->id);
         $qb->addOrderBy('publish_timestamp', 'desc');
     });
     $articles = $articles_pager->getData();
     foreach ($articles as $key => $article) {
         // 取出文章的图片媒体
         $article['media'] = MediaModel::allMedias(function (QueryBuilder $qb) use($article) {
             $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
             $qb->andWhere($qb->expr()->eq('type', ':type'))->setParameter(':type', 'picture');
         });
         // 取出文章的tag标签
         $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) {
             $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         });
         $article['tags'] = array();
         foreach ($article_tags as $article_tag) {
             $t = TagModel::getTag($article_tag['tag_id']);
             $article['tags'][] = $t->toArray();
         }
         // 发布者
         $article['publisher'] = array();
         $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) {
             $qb->where($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         });
         if ($article_publishers) {
             $article_publisher = array_shift($article_publishers);
             $publisher_id = $article_publisher['publisher_id'];
             $publisher = PublisherModel::getPublisher($publisher_id);
             if ($publisher) {
                 $article['publisher'] = $publisher;
             }
         }
         $articles[$key] = $article;
     }
     $articles_pager->setData($articles);
     return $this->render('category/blogs.html.twig', array('blog_posts' => $articles_pager));
 }
Beispiel #6
0
 public function __invoke($id = null)
 {
     $request = $this->getRequest();
     $page = $request->query->get('page');
     $size = 15;
     // 查询出发布者
     $publisher_id = $id;
     $publisher = PublisherModel::getPublisher($publisher_id);
     $publisher_articles = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($publisher) {
         $qb->where($qb->expr()->eq('publisher_id', ':publisher_id'))->setParameter(':publisher_id', $publisher->id);
     });
     $article_ids = array();
     foreach ($publisher_articles as $publisher_article) {
         $article_ids[] = $publisher_article['article_id'];
     }
     $article_ids = array_unique($article_ids);
     // 查询出文章
     $articles_pager = ArticleModel::listArticles($page, $size, function (QueryBuilder $qb) use($article_ids) {
         $qb->andWhere($qb->expr()->gt('publish_timestamp', 0));
         if ($article_ids) {
             $qb->andWhere($qb->expr()->in('id', $article_ids));
         } else {
             $qb->andWhere($qb->expr()->eq('id', 0));
         }
         $qb->addOrderBy('publish_timestamp', 'desc');
     });
     $articles = $articles_pager->getData();
     foreach ($articles as $key => $article) {
         // 取出文章的图片媒体
         $article['media'] = MediaModel::allMedias(function (QueryBuilder $qb) use($article) {
             $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
             $qb->andWhere($qb->expr()->eq('type', ':type'))->setParameter(':type', 'picture');
         });
         // 取出文章的tag标签
         $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) {
             $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         });
         $article['tags'] = array();
         foreach ($article_tags as $article_tag) {
             $t = TagModel::getTag($article_tag['tag_id']);
             $article['tags'][] = $t->toArray();
         }
         // 发布者
         $article['publisher'] = $publisher;
         $articles[$key] = $article;
     }
     $articles_pager->setData($articles);
     return $this->render('publisher/blogs.html.twig', array('blog_posts' => $articles_pager, 'publisher' => $publisher));
 }
 public function __invoke()
 {
     $articles = ArticleModel::listArticles(1, 10, function (QueryBuilder $qb) {
         $qb->andWhere($qb->expr()->gt('publish_timestamp', 0));
         $qb->addOrderBy('publish_timestamp', 'desc');
     });
     $articles = $articles->getData();
     foreach ($articles as $key => $article) {
         $article_publisher = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) {
             $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         });
         $publisher = PublisherModel::getPublisher($article_publisher[0]['publisher_id']);
         $articles[$key]['publisher'] = $publisher;
     }
     return $this->render('fragment/latest-articles.html.twig', array('articles' => $articles));
 }
Beispiel #8
0
 /**
  * 保存
  * @param PublisherModel $publisher
  * @return PublisherModel
  * @throws \Exception
  */
 public static function savePublisher(PublisherModel $publisher)
 {
     return self::editPublisher($publisher->toArray());
 }
Beispiel #9
0
 public function __invoke($id = null)
 {
     $request = $this->getRequest();
     $a_page = $request->query->get('a_page');
     if (!$a_page) {
         $a_page = 1;
     }
     $a_size = $request->query->get('a_size');
     $article = ArticleModel::getArticle($id);
     $article = $article->toArray();
     // 取出文章的tag标签
     $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     $article['tags'] = array();
     foreach ($article_tags as $article_tag) {
         $t = TagModel::getTag($article_tag['tag_id']);
         $article['tags'][] = $t->toArray();
     }
     // 分页
     if (!$a_size) {
         $content_pager = explode('<!-- pagebreak -->', $article['content']);
     } else {
         // 全文
         $content_pager = array(str_replace('<!-- pagebreak -->', '', $article['content']));
     }
     $article['content'] = $content_pager[$a_page - 1];
     if (!$a_size) {
         $count = count($content_pager);
     } else {
         $count = 0;
     }
     $a_pager = new Pager(array(), $count, $a_page, 1);
     $a_pager->setPageParamName('a_page');
     // 文章的发布者
     $article['publisher'] = array();
     $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     if ($article_publishers) {
         $article_publisher = array_shift($article_publishers);
         $publisher_id = $article_publisher['publisher_id'];
         $publisher = PublisherModel::getPublisher($publisher_id);
         if ($publisher) {
             $article['publisher'] = $publisher;
         }
     }
     // 轮播图
     $carousels = MediaModel::allMedias(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         $qb->andWhere($qb->expr()->eq('type', ':type'))->setParameter(':type', 'carousel');
     });
     foreach ($carousels as $k => $v) {
         $metadata = json_decode($v['metadata'], true);
         unset($v['metadata']);
         $v['description'] = '';
         if ($metadata) {
             $v['description'] = $metadata['description'];
         }
         $carousels[$k] = $v;
     }
     // 所有的插件
     $data = PluginModel::allPlugins();
     $plugins = array();
     foreach ($data as $row) {
         $row['sources'] = json_decode($row['sources'], true);
         $row['options'] = json_decode($row['options'], true);
         $row['apply_code'] = htmlspecialchars_decode($row['apply_code']);
         $plugins[$row['id']] = $row;
     }
     // 文章的插件
     $data = ArticlePluginModel::allRelationship(function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
     });
     foreach ($data as $row) {
         $row['sources'] = json_decode($row['sources'], true);
         $row['options'] = json_decode($row['options'], true);
         $row['apply_code'] = htmlspecialchars_decode($row['apply_code']);
         if (isset($plugins[$row['plugin_id']])) {
             $plugins[$row['plugin_id']] = $row;
         }
     }
     // 评论
     $c_page = $request->query->get('c_page');
     if (!$c_page) {
         $c_page = 1;
     }
     $c_size = $request->query->get('c_size');
     if (!$c_size) {
         $c_size = 3;
     }
     $c_pager = CommentModel::listComments($c_page, $c_size, function (QueryBuilder $qb) use($article) {
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article['id']);
         $qb->addOrderBy('create_timestamp');
     });
     $comments_data = $c_pager->getData();
     foreach ($comments_data as $k => $comment_data) {
         // 获取用户数据
         $comment_user_id = $comment_data['user_id'];
         /** @var \Tachigo\User\Aware\Component\UserAwareDispatcher $dispatcher */
         $dispatcher = $this->getContainer()->tag('tachigo_user_dispatcher');
         /** @var \Tachigo\User\Aware\Hook\UserHook $hook */
         $hook = $this->getContainer()->tag('tachigo_user_hook.user');
         $hook->setUserId($comment_user_id);
         $dispatcher->fire($hook);
         $comment_data['user'] = $hook->getResults();
         $comment_data['reply'] = array();
         if ($comment_data['comment_id']) {
             $reply_comment = CommentModel::getComment($comment_data['comment_id'])->toArray();
             $comment_user_id = $reply_comment['user_id'];
             /** @var \Tachigo\User\Aware\Component\UserAwareDispatcher $dispatcher */
             $dispatcher = $this->getContainer()->tag('tachigo_user_dispatcher');
             /** @var \Tachigo\User\Aware\Hook\UserHook $hook */
             $hook = $this->getContainer()->tag('tachigo_user_hook.user');
             $hook->setUserId($comment_user_id);
             $dispatcher->fire($hook);
             $reply_comment['user'] = $hook->getResults();
             $comment_data['reply'] = $reply_comment;
         }
         $comments_data[$k] = $comment_data;
     }
     $c_pager->setData($comments_data);
     $c_pager->setPageParamName('c_page');
     // 分页内容
     $pager_query = array('a_page' => $a_page, 'a_size' => $a_size, 'c_page' => $c_page, 'c_size' => $c_size);
     $a_pager->setQuery($pager_query);
     $c_pager->setQuery($pager_query);
     // 导航条
     $structure_id = $article['structure_id'];
     $structure = StructureModel::getStructure($structure_id);
     /** @var StructureModel[] $breadcrumbs */
     $breadcrumbs = $structure->getNodeSinglePath();
     array_shift($breadcrumbs);
     return $this->render('article/blog.html.twig', array('article' => $article, 'a_pager' => $a_pager, 'c_pager' => $c_pager, 'carousels' => $carousels, 'plugins' => $plugins, 'breadcrumbs' => $breadcrumbs));
 }
Beispiel #10
0
 protected function handle()
 {
     $columns = array('ID', '名称', '描述', '内容数量', '创建时间', '更新时间');
     $fields = array('id', 'name', 'description', 'article_count', 'create_timestamp', 'update_timestamp');
     $request = $this->getRequest();
     if ($request->isXmlHttpRequest()) {
         $posts = $request->request;
         $page_offset = $posts->get('start');
         $page_offset = intval($page_offset);
         $page_size = $posts->get('length');
         $page_size = intval($page_size);
         $is_all = false;
         if ($page_size < 0) {
             $is_all = true;
         }
         $s_echo = $posts->get('draw');
         $s_echo = intval($s_echo);
         $search = $posts->get('search');
         $search_value = $search['value'];
         $records = array();
         $records['data'] = array();
         $records['draw'] = $s_echo;
         $records['recordsTotal'] = 0;
         $records['recordsFiltered'] = 0;
         if ($is_all) {
             $page_size = PublisherModel::getCount(function (QueryBuilder $qb) use($search_value) {
                 if ($search_value) {
                     $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%");
                 }
             });
         }
         $page = $page_offset / $page_size + 1;
         $pager = PublisherModel::listPublishers($page, $page_size, function (QueryBuilder $qb) use($search_value) {
             if ($search_value) {
                 $qb->orWhere($qb->expr()->like("`name`", ":name"))->setParameter(":name", "%{$search_value}%");
             }
             $qb->addOrderBy('create_timestamp', 'desc');
         });
         $total = $pager->getCount();
         $records['recordsTotal'] = $total;
         $records['recordsFiltered'] = $total;
         $data = $pager->getData();
         foreach ($data as $k => $v) {
             $line = array();
             $line[] = '<input type="checkbox" name="id[]" value="' . $v['id'] . '">';
             foreach ($fields as $field) {
                 if (isset($v[$field]) || $v[$field] === null) {
                     if ($field == 'create_timestamp' || $field == 'update_timestamp') {
                         // 时间
                         $line[] = date('Y-m-d H:i:s', $v[$field]);
                     } elseif ($field == 'article_count') {
                         $line[] = intval($v[$field]);
                     } elseif (!$v[$field]) {
                         $line[] = '&nbsp;';
                     } else {
                         $line[] = $v[$field];
                     }
                 }
             }
             $edit_url = $this->generateUrl('admin_www_publisher_edit', array('id' => $v['id']));
             $line[] = '<a href="' . $edit_url . '">编辑</a>';
             $records['data'][] = $line;
         }
         return new JsonResponse($records);
     }
     return $this->render('publisher/index.html.twig', array('columns' => $columns));
 }