예제 #1
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));
 }
예제 #2
0
 protected function handle()
 {
     $tree = StructureModel::getTree();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         try {
             // 检查参数
             $name = $request->request->get('name');
             if (!$name) {
                 throw new \Exception("结构名称不能为空");
             }
             // 检查search是否重复
             $search = $request->request->get('search');
             if ($search) {
                 $structure = StructureModel::search($search);
                 if ($structure) {
                     throw new \Exception("键名为 {$search} 的目录结构已存在");
                 }
             } else {
                 $search = null;
             }
             $description = $request->request->get('description');
             // 类型
             $type = $request->request->get('type');
             if (!$type) {
                 throw new \Exception("结构的类型不能为空");
             }
             if (!StructureTypeBusiness::isValid($type)) {
                 throw new \Exception("结构类型错误");
             }
             $parent_structure_id = $request->request->get('parent_id');
             if (!$parent_structure_id) {
                 throw new \Exception("请选择父结构");
             }
             $parent_structure = StructureModel::getStructure($parent_structure_id);
             if (!$parent_structure) {
                 throw new \Exception("父结构不存在");
             }
             $metadata = $request->request->get('metadata');
             $now = time();
             $parent_structure->createChildNode(array('name' => $name, 'search' => $search, 'description' => $description, 'type' => $type, 'metadata' => $metadata, 'create_timestamp' => $now, 'update_timestamp' => $now));
         } catch (\Exception $e) {
             $session = $this->getSession();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_structure'));
     }
     return $this->render('structure/add.html.twig', array('tree' => $tree));
 }
예제 #3
0
 protected function handle()
 {
     $structure = StructureModel::getStructure($this->id);
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $session = $this->getSession();
         try {
             $name = $posts->get('name');
             // 检查search是否重复
             $search = $request->request->get('search');
             if ($search) {
                 $s = StructureModel::search($search);
                 if ($s->id != $this->id) {
                     throw new \Exception("键名为 {$search} 的目录结构已存在");
                 }
             } else {
                 $search = null;
             }
             $description = $posts->get('description');
             if (!$name) {
                 throw new \Exception("结构名称不能为空");
             }
             $type = $posts->get('type');
             if (!$type) {
                 throw new \Exception("结构类型不能为空");
             }
             if (!StructureTypeBusiness::isValid($type)) {
                 throw new \Exception("结构类型错误");
             }
             $metadata = $posts->get('metadata');
             $structure->name = $name;
             $structure->search = $search;
             $structure->description = $description;
             $structure->type = $type;
             $structure->metadata = $metadata;
             $structure->updateTimestamp = time();
             StructureModel::saveStructure($structure);
             $session->addFlash('success', '编辑成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_structure'));
     }
     return $this->render('structure/edit.html.twig', array('structure' => $structure));
 }