Пример #1
0
 protected function handle()
 {
     $forum_id = $this->forumId;
     $forum = ForumModel::getForum($forum_id);
     if (!$forum) {
         $session = $this->getSession();
         $session->addFlash('error', '论坛不存在');
         return new RedirectResponse($this->generateUrl('admin_forum_index'));
     }
     // 查询出论坛的所有分组
     $groups = GroupModel::allGroup(function (QueryBuilder $qb) use($forum) {
         $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $forum->id);
         $qb->addOrderBy('weight', 'desc');
         $qb->addOrderBy('create_timestamp');
     });
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         // 对分组进行排序
         $session = $this->getSession();
         $posts = $request->request;
         $weights = $posts->get('weight');
         $db = WWWDatabase::getDb();
         $db->transaction();
         try {
             foreach ($weights as $group_id => $weight) {
                 $group = GroupModel::getGroup($group_id);
                 if ($group) {
                     $group->weight = $weight;
                     // 保存
                     GroupModel::saveGroup($group);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
             $db->rollback();
         }
         return new RedirectResponse($this->generateUrl('admin_forum_group_index', array('forum_id' => $forum_id)));
     }
     return $this->render('group/index.html.twig', array('forum' => $forum, 'groups' => $groups));
 }
Пример #2
0
 protected function handle()
 {
     $board_id = $this->id;
     $board = BoardModel::getBoard($board_id);
     if (!$board) {
         throw new \Exception('论坛版块不存在');
     }
     $group = GroupModel::getGroup($board->groupId);
     if (!$group) {
         throw new \Exception('论坛分组不存在');
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         $posts = $request->request;
         $db = WWWDatabase::getDb();
         $db->transaction();
         try {
             $name = $posts->get('name');
             if (!$name) {
                 throw new \Exception('版块名称不能为空');
             }
             $description = $posts->get('description');
             $search = $posts->get('search');
             if ($search) {
                 // 检查重复性
                 $g = BoardModel::search($search, $group->forumId, $group->id);
                 if ($g && $g->id != $board->id) {
                     throw new \Exception('URL占位符已被使用');
                 }
             } else {
                 $search = null;
             }
             $icon = $posts->get('icon');
             $weight = $posts->get('weight');
             $board_per_row = $posts->get('board_per_row');
             $board_per_row = intval($board_per_row);
             if (!$board_per_row) {
                 $board_per_row = 1;
             }
             // 创建
             $board->name = $name;
             $board->search = $search;
             $board->description = $description;
             $board->icon = $icon;
             $board->weight = intval($weight);
             $board->boardPerRow = $board_per_row;
             $now = time();
             $board->updateTimestamp = $now;
             // 保存
             BoardModel::saveBoard($board);
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', '操作失败');
         }
         return new JsonResponse(array('status' => 1, 'data' => 'success'));
     }
     return $this->render('board/edit.html.twig', array('board' => $board));
 }
Пример #3
0
 protected function handle()
 {
     // 系统中的未启用的插件
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     $unused_plugins = array();
     $used_plugins = array();
     $data = PluginModel::allPlugins();
     foreach ($data as $row) {
         $row['dependency'] = json_decode($row['dependency'], true);
         $used_plugins[$row['id']] = $row;
     }
     foreach ($all_plugins as $plugin_id => $plugin) {
         $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
         if (!in_array($plugin_id, $used_plugins)) {
             $unused_plugins[$plugin_id] = $plugin;
         }
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $db = WWWDatabase::getDb();
         $session = $this->getSession();
         $db->transaction();
         try {
             $plugins = PluginModel::allPlugins(function (QueryBuilder $qb) use($ids) {
                 foreach ($ids as $k => $id) {
                     $qb->orWhere($qb->expr()->eq('id', ':id_' . $k))->setParameter(':id_' . $k, $id);
                 }
             });
             // 检查是否有依赖
             // 找出不删的插件
             $not_delete_used_plugins = array();
             foreach ($used_plugins as $used_plugin) {
                 if (!in_array($used_plugin['id'], $ids)) {
                     $not_delete_used_plugins[] = $used_plugin;
                 }
             }
             foreach ($not_delete_used_plugins as $not_delete_used_plugin) {
                 if (($intersect = array_intersect(array_values($not_delete_used_plugin['dependency']), array_values($ids))) != null) {
                     $dependent_plugin_ids = implode(',', $intersect);
                     throw new \Exception("插件'{$not_delete_used_plugin['id']}'依赖'{$dependent_plugin_ids}',不能弃用");
                 }
             }
             // 删除插件
             foreach ($plugins as $plugin) {
                 PluginModel::deletePlugin($plugin['id']);
                 // 删除插件与文章的关系
                 $article_plugins = ArticlePluginModel::allRelationship(function (QueryBuilder $qb) use($plugin) {
                     $qb->where($qb->expr()->eq('plugin_id', ':plugin_id'))->setParameter(':plugin_id', $plugin['id']);
                 });
                 foreach ($article_plugins as $article_plugin) {
                     ArticlePluginModel::deleteRelationship($article_plugin['article_id'], $plugin['id']);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_plugins'));
     } else {
         $ids = $request->query->get('ids');
         if (is_string($ids)) {
             $ids = json_decode($ids, true);
         }
         if (!$ids) {
             throw new \Exception('没有选择任何插件');
         }
         // 显示要删除的插件
         $plugins = PluginModel::allPlugins(function (QueryBuilder $qb) use($ids) {
             foreach ($ids as $k => $id) {
                 $qb->orWhere($qb->expr()->eq('id', ':id_' . $k))->setParameter(':id_' . $k, $id);
             }
         });
         // 检查是否有依赖
         // 找出不删的插件
         $not_delete_used_plugins = array();
         foreach ($used_plugins as $used_plugin) {
             if (!in_array($used_plugin['id'], $ids)) {
                 $not_delete_used_plugins[] = $used_plugin;
             }
         }
         foreach ($not_delete_used_plugins as $not_delete_used_plugin) {
             if (($intersect = array_intersect(array_values($not_delete_used_plugin['dependency']), array_values($ids))) != null) {
                 $dependent_plugin_ids = implode(',', $intersect);
                 throw new \Exception("插件'{$not_delete_used_plugin['id']}'依赖'{$dependent_plugin_ids}',不能弃用");
             }
         }
         return $this->render('plugin/delete.html.twig', array('plugins' => $plugins));
     }
 }
Пример #4
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));
 }
Пример #5
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));
 }
Пример #6
0
 protected function handle()
 {
     $all_plugins = $this->getContainer()->getParameter('plugins');
     if ($all_plugins) {
         $all_plugins = $all_plugins->toArray();
     } else {
         $all_plugins = array();
     }
     if (!isset($all_plugins[$this->id])) {
         throw new \Exception('插件不存在');
     }
     $unused_plugins = array();
     $used_plugins = array();
     $data = PluginModel::allPlugins();
     foreach ($data as $row) {
         $used_plugins[$row['id']] = $row;
     }
     if (isset($used_plugins[$this->id])) {
         throw new \Exception('插件已启用');
     }
     foreach ($all_plugins as $plugin_id => $plugin) {
         $plugin['id'] = $plugin_id;
         $plugin['apply_code'] = htmlspecialchars($plugin['apply_code']);
         $plugin['dependency'] = isset($plugin['dependency']) ? $plugin['dependency'] : array();
         if (!$plugin['dependency']) {
             $plugin['dependency'] = array();
         }
         if (!in_array($plugin_id, $used_plugins)) {
             $unused_plugins[$plugin_id] = $plugin;
         }
         $all_plugins[$plugin_id] = $plugin;
     }
     $plugin_info = $all_plugins[$this->id];
     $request = $this->getRequest();
     // 检查依赖
     $dependency_plugins = array();
     try {
         $dependency_plugins = $this->checkDependency($plugin_info, $all_plugins, $used_plugins);
         if ($request->getMethod() == 'POST') {
             $db = WWWDatabase::getDb();
             $session = $this->getSession();
             $db->transaction();
             try {
                 // 安装依赖的插件
                 foreach ($dependency_plugins as $dependency_plugin) {
                     if (!$dependency_plugin['enabled']) {
                         $plugin = new PluginModel();
                         $plugin->id = $dependency_plugin['id'];
                         $plugin->name = $dependency_plugin['name'];
                         $plugin->description = $dependency_plugin['description'];
                         $plugin->sources = json_encode($dependency_plugin['sources']);
                         $plugin->applyCode = $dependency_plugin['apply_code'];
                         $plugin->dependency = json_encode($dependency_plugin['dependency']);
                         $plugin->options = json_encode($dependency_plugin['options']);
                         // 保存
                         PluginModel::createPlugin($plugin);
                     }
                 }
                 // 安装指定插件
                 $plugin = new PluginModel();
                 $plugin->id = $plugin_info['id'];
                 $plugin->name = $plugin_info['name'];
                 $plugin->description = $plugin_info['description'];
                 $plugin->sources = json_encode($plugin_info['sources']);
                 $plugin->applyCode = $plugin_info['apply_code'];
                 $plugin->dependency = json_encode($plugin_info['dependency']);
                 $plugin->options = json_encode($plugin_info['options']);
                 PluginModel::createPlugin($plugin);
                 $db->commit();
                 $session->addFlash('success', '操作成功');
             } catch (\Exception $e) {
                 $db->rollback();
                 $session->addFlash('error', $e->getMessage());
             }
             return new RedirectResponse($this->generateUrl('admin_www_plugins'));
         }
     } catch (\Exception $e) {
         if ($request->getMethod() != 'POST') {
             throw $e;
         }
     }
     return $this->render('plugin/add.html.twig', array('plugin' => $plugin_info, 'dependency_plugins' => $dependency_plugins));
 }
Пример #7
0
 protected function handle()
 {
     $request = $this->getRequest();
     $article = ArticleModel::getArticle($this->id);
     if (!$article) {
         $session = $this->getSession();
         $session->addFlash('error', '文章不存在');
         return new RedirectResponse($this->generateUrl('admin_www_article'));
     }
     $pictures = 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');
     });
     // 已有的轮播图
     $data = 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');
     });
     $carousels = array();
     foreach ($data as $media) {
         $metadata = json_decode($media['metadata'], true);
         unset($media['metadata']);
         $original_media_id = $metadata['media_id'];
         $description = $metadata['description'];
         $media['description'] = $description;
         $carousels[$original_media_id] = $media;
     }
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $post_carousels = $posts->get('carousels');
         if (!$post_carousels) {
             $post_carousels = array();
         }
         $session = $this->getSession();
         $db = WWWDatabase::getDb();
         try {
             $db->transaction();
             // 清空已有轮播图
             $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 $carousel) {
                 MediaModel::deleteMedia($carousel['id']);
             }
             // 添加新轮播
             foreach ($post_carousels as $original_media_id => $info) {
                 $carousel = new MediaModel();
                 $carousel->articleId = $article->id;
                 $carousel->type = 'carousel';
                 $carousel->source = $info['source'];
                 $carousel->metadata = json_encode(array('media_id' => $original_media_id, 'description' => $info['description']));
                 // 保存
                 MediaModel::createMedia($carousel);
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_www_article_carousel', array('id' => $article->id)));
     }
     return $this->render('article/carousel.html.twig', array('pictures' => $pictures, 'carousels' => $carousels, 'article' => $article));
 }
Пример #8
0
 protected function handle()
 {
     $group_id = $this->groupId;
     $group = GroupModel::getGroup($group_id);
     if (!$group) {
         throw new \Exception('论坛分组不存在');
     }
     $forum = ForumModel::getForum($group->forumId);
     $board_id = intval($this->boardId);
     $parent_board = null;
     if ($board_id) {
         $parent_board = BoardModel::getBoard($board_id);
         if (!$parent_board) {
             throw new \Exception('父论坛版块不存在');
         }
     }
     $request = $this->getRequest();
     $session = $this->getSession();
     if ($request->getMethod() == 'POST') {
         $posts = $request->request;
         $db = WWWDatabase::getDb();
         $db->transaction();
         try {
             $name = $posts->get('name');
             if (!$name) {
                 throw new \Exception('版块名称不能为空');
             }
             $description = $posts->get('description');
             $search = $posts->get('search');
             if ($search) {
                 // 检查重复性
                 $g = BoardModel::search($search, $group->forumId, $group->id);
                 if ($g) {
                     throw new \Exception('URL占位符已被使用');
                 }
             } else {
                 $search = null;
             }
             $icon = $posts->get('icon');
             // 创建
             $board = new BoardModel();
             $board->name = $name;
             $board->search = $search;
             $board->description = $description;
             $board->icon = $icon;
             $now = time();
             $board->createTimestamp = $now;
             $board->updateTimestamp = $now;
             $board->forumId = $group->forumId;
             $board->groupId = $group->id;
             $board->parentId = $board_id;
             // 路径
             $path = array();
             while ($parent_board != null) {
                 $path[] = $parent_board->id;
                 if ($parent_board->parentId != 0) {
                     $parent_board = BoardModel::getBoard($parent_board->parentId);
                 } else {
                     $parent_board = null;
                 }
             }
             $board->path = implode(',', array_reverse($path));
             // 保存
             BoardModel::createBoard($board);
             $group->boardCount += 1;
             GroupModel::saveGroup($group);
             $forum->boardCount += 1;
             ForumModel::saveForum($forum);
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', '操作失败');
         }
         if ($board_id) {
             return new RedirectResponse($this->generateUrl('admin_forum_board_index', array('id' => $board_id)));
         } else {
             return new RedirectResponse($this->generateUrl('admin_forum_group_index', array('forum_id' => $group->forumId)));
         }
     }
     return $this->render('board/add.html.twig', array('group_id' => $group_id, 'board_id' => $board_id));
 }