예제 #1
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));
         }
     }
 }
예제 #2
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));
 }
예제 #3
0
 public function __invoke()
 {
     /** @var StructureModel $root_node */
     $root_node = StructureModel::getRootNode();
     if (!$root_node) {
         // 没有根分类,需要自动创建
         $node = new StructureModel();
         $node->name = '根';
         $node->description = '根';
         $now = time();
         $node->createTimestamp = $now;
         $node->updateTimestamp = $now;
         $node->preOrderTreeLeft = 0;
         $node->preOrderTreeRight = 1;
         $root_node = StructureModel::createStructure($node);
     }
     /** @var StructureModel[] $sub_tree */
     $sub_tree = $root_node->getSubTree();
     $list = array();
     $path_node = null;
     foreach ($sub_tree as $i => $node) {
         if ($i == 0) {
             continue;
         }
         $list[$node->id] = $node;
         try {
             if ($node->type == 'menu') {
                 if ($node->metadata) {
                     $path = $node->metadata;
                 } else {
                     $path = '/cate/' . ($node->search ? $node->search : $node->id);
                 }
                 if ($path == $this->getRequest()->getPathInfo()) {
                     $path_node = $node;
                 }
             }
         } catch (\Exception $e) {
             // do nothing
         }
     }
     StructureModelBusiness::preOrderTree2RecursiveTree($list, $root_node);
     $breadcrumbs = array();
     if ($path_node) {
         /** @var StructureModel[] $nodes */
         $nodes = $path_node->getNodeSinglePath();
         array_shift($nodes);
         foreach ($nodes as $node) {
             $breadcrumbs[$node->id] = $node->name;
         }
     }
     return $this->render('fragment/navigation.html.twig', array('navigation' => $root_node, 'breadcrumbs' => $breadcrumbs));
 }
예제 #4
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));
 }
예제 #5
0
 protected function handle()
 {
     $request = $this->getRequest();
     $structure = StructureModel::getStructure($this->id);
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         try {
             $structure->remove();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_structure'));
     }
     return $this->render('structure/delete.html.twig', array('structure' => $structure));
 }
예제 #6
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));
 }
예제 #7
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));
 }
예제 #8
0
 /**
  * 保存
  * @param StructureModel $structure
  * @return StructureModel
  * @throws \Exception
  */
 public static function saveStructure(StructureModel $structure)
 {
     return self::editStructure($structure->toArray());
 }
예제 #9
0
 protected function handle()
 {
     $tags = TagModel::allTags();
     $tree = StructureModel::getTree();
     $session = $this->getSession();
     $article = ArticleModel::getArticle($this->id);
     if (!$article) {
         $session->addFlash('error', '文章不存在');
         return new RedirectResponse($this->generateUrl('admin_www_article'));
     }
     // 文章已有的标签
     $article_tags = ArticleTagModel::allRelationship(function (QueryBuilder $qb) use($article) {
         /** @var ArticleModel $article */
         $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article->id);
     });
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $db = WWWDatabase::getDb();
         try {
             $title = $posts->get('title');
             $content = $posts->get('content');
             $summary = $posts->get('summary');
             $structure_id = $posts->get('structure_id');
             $tags_id = $posts->get('tags');
             if (!$tags_id) {
                 $tags_id = array();
             }
             if (!$title) {
                 throw new \Exception('文章标题不能为空');
             }
             if (!$structure_id) {
                 throw new \Exception('请选择栏目');
             }
             $structure = StructureModel::getStructure($structure_id);
             if (!$structure) {
                 throw new \Exception('栏目不存在');
             }
             // 开启事务
             $db->transaction();
             // 创建文章
             $article->title = $title;
             $article->content = $content;
             $article->summary = $summary;
             $article->structureId = $structure_id;
             $article->updateTimestamp = time();
             // 保存
             // 解析媒体
             // 删除已有媒体
             $medias = MediaModel::allMedias(function (QueryBuilder $qb) use($article) {
                 $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article->id);
             });
             foreach ($medias as $media_info) {
                 MediaModel::deleteMedia($media_info['id']);
             }
             $cdn_url = $this->getContainer()->getParameter('cdn_url');
             $cdn_url = str_replace('/', '\\/', $cdn_url);
             // 解析图片
             $pictures = ArticleBusiness::parsePictures($cdn_url, $content);
             foreach ($pictures as $picture_source) {
                 $media = new MediaModel();
                 $media->type = 'picture';
                 $media->source = $picture_source;
                 $media->articleId = $article->id;
                 // 保存
                 MediaModel::createMedia($media);
             }
             ArticleModel::saveArticle($article);
             // 清空原有分类
             foreach ($article_tags as $article_tag) {
                 $tag_id = $article_tag['tag_id'];
                 $tag = TagModel::getTag($tag_id);
                 if ($tag) {
                     $tag->articleCount -= 1;
                     // 保存
                     TagModel::saveTag($tag);
                 }
                 // 删除关联
                 $article_tag = ArticleTagModel::getRelationship($article->id, $tag->id);
                 if ($article_tag) {
                     ArticleTagModel::deleteRelationship($article->id, $tag->id);
                 }
             }
             // 添加标签的文章数量
             foreach ($tags_id as $tag_id) {
                 $tag = TagModel::getTag($tag_id);
                 if (!$tag) {
                     throw new \Exception('标签不存在');
                 }
                 $tag->articleCount += 1;
                 // 保存tag标签
                 TagModel::saveTag($tag);
                 // 添加文章与标签的关系
                 $article_tag = new ArticleTagModel();
                 $article_tag->articleId = $article->id;
                 $article_tag->tagId = $tag->id;
                 ArticleTagModel::createRelationship($article_tag);
             }
             /*// 添加插件
               $plugin_ids = $posts->get('plugins');
               if (!$plugin_ids) {
                   $plugin_ids = array();
               }
               // 删除不要的插件
               foreach ($article_plugins as $article_plugin) {
                   if (!in_array($article_plugin['plugin_id'], $plugin_ids)) {
                       ArticlePluginModel::deleteRelationship($article_plugin['article_id'], $article_plugin['plugin_id']);
                       unset($article_plugins[$article_plugin['plugin_id']]);
                   }
               }
               // 检查插件是否存在
               foreach ($plugin_ids as $plugin_id) {
                   if (!isset($plugins[$plugin_id])) {
                       throw new \Exception("插件不存在或者未启用");
                   }
                   if (!isset($article_plugins[$plugin_id])) {
                       // 创建文章插件关系
                       $article_plugin = new ArticlePluginModel();
                       $article_plugin->articleId = $article->id;
                       $article_plugin->pluginId = $plugin_id;
                       // 存储默认数据
                       $article_plugin->sources = $plugins[$plugin_id]['sources'];
                       $article_plugin->applyCode = $plugins[$plugin_id]['default_code'];
                       $article_plugin->options = $plugins[$plugin_id]['default_options'];
                       // 保存
                       ArticlePluginModel::createRelationship($article_plugin);
                   }
               }*/
             $db->commit();
             $session->addFlash('success', '保存成功');
             return new RedirectResponse($this->generateUrl('admin_www_article_edit', array('id' => $this->id)));
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('admin_www_article_edit', array('id' => $this->id)));
         }
     }
     return $this->render('article/edit.html.twig', array('article' => $article, 'tree' => $tree, 'tags' => $tags, 'article_tags' => $article_tags));
 }
예제 #10
0
    protected function handle()
    {
        $request = $this->getRequest();
        $columns = array('ID', '标题', '创建', '更新', '发布', '栏目');
        $fields = array('id', 'title', 'create_timestamp', 'update_timestamp', 'publish_timestamp', 'structure_id');
        if ($request->isXmlHttpRequest()) {
            $posts = $request->request;
            $type = $posts->get('type');
            $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 = ArticleModel::getCount(function (QueryBuilder $qb) use($search_value, $type) {
                    if ($search_value) {
                        $qb->orWhere($qb->expr()->like("`title`", ":title"))->setParameter(":title", "%{$search_value}%");
                    }
                    if ($type == 'published') {
                        $qb->andWhere($qb->expr()->gt('`publish_timestamp`', 0));
                    } elseif ($type == 'trashed') {
                        $qb->andWhere($qb->expr()->eq('`publish_timestamp`', 0));
                    }
                });
            }
            $page = $page_offset / $page_size + 1;
            $pager = ArticleModel::listArticles($page, $page_size, function (QueryBuilder $qb) use($search_value, $type) {
                if ($search_value) {
                    $qb->orWhere($qb->expr()->like("`title`", ":title"))->setParameter(":title", "%{$search_value}%");
                }
                if ($type == 'published') {
                    $qb->andWhere($qb->expr()->gt('`publish_timestamp`', 0));
                } elseif ($type == 'trashed') {
                    $qb->andWhere($qb->expr()->eq('`publish_timestamp`', 0));
                }
                $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])) {
                        if ($field == 'create_timestamp' || $field == 'update_timestamp') {
                            // 时间
                            $line[] = date('Y-m-d H:i:s', $v[$field]);
                        } elseif ($field == 'publish_timestamp') {
                            if ($v[$field]) {
                                $line[] = date('Y-m-d H:i:s', $v[$field]);
                            } else {
                                $line[] = '未发布';
                            }
                        } elseif ($field == 'structure_id') {
                            $structure_id = $v[$field];
                            $structure = StructureModel::getStructure($structure_id);
                            $line[] = $structure->name;
                        } else {
                            $line[] = $v[$field];
                        }
                    }
                }
                $edit_url = $this->generateUrl('admin_www_article_edit', array('id' => $v['id']));
                $carousel_url = $this->generateUrl('admin_www_article_carousel', array('id' => $v['id']));
                $plugin_url = $this->generateUrl('admin_www_plugin_edit', array('id' => $v['id']));
                $operation = '<div class="btn-group">
<a href="javascript:void(0);" data-toggle="dropdown" aria-expanded="false">操作 <i class="fa fa-angle-down"></i></a>
<ul class="dropdown-menu pull-right" role="menu">
<li role="presentation"><a href="' . $edit_url . '"><i class="fa fa-edit"></i> 编辑</a></li>
<li role="presentation"><a href="' . $carousel_url . '"><i class="fa fa-picture-o"></i> 轮播</a></li>
<li role="presentation"><a href="' . $plugin_url . '"><i class="fa fa-paperclip"></i> 插件</a></li>
</ul>
</div>';
                $line[] = $operation;
                $records['data'][] = $line;
            }
            return new JsonResponse($records);
        }
        return $this->render('article/index.html.twig', array('columns' => $columns));
    }
예제 #11
0
 protected function handle()
 {
     $tree = StructureModel::getTree();
     $tags = TagModel::allTags();
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         $posts = $request->request;
         $db = WWWDatabase::getDb();
         try {
             $title = $posts->get('title');
             $content = $posts->get('content');
             $summary = $posts->get('summary');
             $structure_id = $posts->get('structure_id');
             if (!$title) {
                 throw new \Exception('文章标题不能为空');
             }
             if (!$structure_id) {
                 throw new \Exception('请选择栏目');
             }
             $structure = StructureModel::getStructure($structure_id);
             if (!$structure) {
                 throw new \Exception('栏目不存在');
             }
             $tags_id = $posts->get('tags');
             if (!$tags_id) {
                 $tags_id = array();
             }
             // 开启事务
             $db->transaction();
             // 创建文章
             $article = new ArticleModel();
             $article->title = $title;
             $article->content = $content;
             $article->summary = $summary;
             $article->structureId = $structure_id;
             $now = time();
             $article->createTimestamp = $now;
             $article->updateTimestamp = $now;
             // 保存
             $article = ArticleModel::createArticle($article);
             // 添加标签的文章数量
             foreach ($tags_id as $tag_id) {
                 $tag = TagModel::getTag($tag_id);
                 if (!$tag) {
                     throw new \Exception('标签不存在');
                 }
                 $tag->articleCount += 1;
                 // 保存tag标签
                 TagModel::saveTag($tag);
                 // 添加文章与标签的关系
                 $article_tag = new ArticleTagModel();
                 $article_tag->articleId = $article->id;
                 $article_tag->tagId = $tag->id;
                 ArticleTagModel::createRelationship($article_tag);
             }
             // 解析媒体
             $cdn_url = $this->getContainer()->getParameter('cdn_url');
             $cdn_url = str_replace('/', '\\/', $cdn_url);
             // 解析图片
             $pictures = ArticleBusiness::parsePictures($cdn_url, $content);
             foreach ($pictures as $picture_source) {
                 $media = new MediaModel();
                 $media->type = 'picture';
                 $media->source = $picture_source;
                 $media->articleId = $article->id;
                 // 保存
                 MediaModel::createMedia($media);
             }
             $db->commit();
             $session->addFlash('success', '创建成功');
             return new RedirectResponse($this->generateUrl('admin_www_article_edit', array('id' => $article->id)));
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
             return new RedirectResponse($this->generateUrl('admin_www_article_add'));
         }
     }
     return $this->render('article/add.html.twig', array('tree' => $tree, 'tags' => $tags));
 }
예제 #12
0
 protected function handle()
 {
     /** @var StructureModel $root_node */
     $root_node = StructureModel::getRootNode();
     if (!$root_node) {
         // 没有根分类,需要自动创建
         $node = new StructureModel();
         $node->name = '根';
         $node->description = '根';
         $now = time();
         $node->createTimestamp = $now;
         $node->updateTimestamp = $now;
         $node->preOrderTreeLeft = 0;
         $node->preOrderTreeRight = 1;
         $root_node = StructureModel::createStructure($node);
     }
     /** @var StructureModel[] $sub_tree */
     $sub_tree = $root_node->getSubTree();
     $list = array();
     foreach ($sub_tree as $i => $node) {
         if ($i == 0) {
             continue;
         }
         $list[$node->id] = $node;
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         try {
             $root_category = $root_node->toArray();
             if (isset($root_category['depth'])) {
                 unset($root_category['depth']);
             }
             if (isset($root_category['width'])) {
                 unset($root_category['width']);
             }
             if (isset($root_category['children'])) {
                 unset($root_category['children']);
             }
             // 保存根菜单
             StructureModel::editStructure($root_category);
             $serialized_output = $request->request->get('serialized_output');
             $serialized_output = trim($serialized_output);
             $serialized_output = json_decode($serialized_output, true);
             $tree = StructureModelBusiness::formatTree($serialized_output, $list);
             $root_node->children = $tree;
             $list = StructureModelBusiness::recursiveTree2PreOrderTree($root_node);
             foreach ($list as $category) {
                 $category = $category->toArray();
                 if (isset($category['depth'])) {
                     unset($category['depth']);
                 }
                 if (isset($category['width'])) {
                     unset($category['width']);
                 }
                 if (isset($category['children'])) {
                     unset($category['children']);
                 }
                 // 保存根菜单
                 StructureModel::editStructure($category);
             }
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_structure'));
     }
     StructureModelBusiness::preOrderTree2RecursiveTree($list, $root_node);
     return $this->render('structure/index.html.twig', array('structure' => $root_node));
 }