protected function handle() { $forum_id = $this->forumId; $forum = ForumModel::getForum($forum_id); $request = $this->getRequest(); $session = $this->getSession(); if (!$forum) { if ($request->isXmlHttpRequest()) { throw new \Exception('论坛不存在'); } else { $session->addFlash('error', '论坛不存在'); return new RedirectResponse($this->generateUrl('admin_forum_index')); } } if ($request->getMethod() == 'POST') { $posts = $request->request; $db = ForumDatabase::getDb(); $db->transaction(); try { $name = $posts->get('name'); if (!$name) { throw new \Exception('分组名称不能为空'); } $description = $posts->get('description'); $search = $posts->get('search'); if ($search) { // 检查重复性 $g = GroupModel::search($search, $forum_id); if ($g) { throw new \Exception('URL占位符已被使用'); } } else { $search = null; } $group = new GroupModel(); $group->name = $name; $group->search = $search; $group->description = $description; $group->forumId = $forum_id; $now = time(); $group->createTimestamp = $now; $group->updateTimestamp = $now; // 更改论坛数据 $forum->groupCount += 1; // 保存 GroupModel::createGroup($group); ForumModel::saveForum($forum); $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_group_index', array('forum_id' => $forum_id))); } return $this->render('group/add.html.twig', array('forum' => $forum)); }
protected function handle() { $forum_id = $this->id; $forum = ForumModel::getForum($forum_id); if (!$forum) { $session = $this->getSession(); $session->addFlash('error', '论坛不存在'); return new RedirectResponse($this->generateUrl('admin_forum_index')); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); try { // 检查 $name = $posts->get('name'); if (!$name) { throw new \Exception('论坛名称不能为空'); } if (strlen(trim($name)) < 2) { throw new \Exception('论坛名称至少2个字符'); } $description = $posts->get('description'); $search = $posts->get('search'); if ($search) { // 检查重复 $f = ForumModel::search($search); if ($f && $f->id != $forum->id) { throw new \Exception("URL占位符为 {$search} 的论坛已存在"); } } else { $search = null; } $icon = $posts->get('icon'); $logo = $posts->get('logo'); $slogan = $posts->get('slogan'); // 更新 $forum->name = $name; $forum->description = $description; $forum->search = $search; $forum->icon = $icon; $forum->logo = $logo; $forum->slogan = $slogan; $forum->updateTimestamp = time(); // 保存 ForumModel::saveForum($forum); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_edit', array('id' => $forum_id))); } return $this->render('forum/edit.html.twig', array('forum' => $forum)); }
/** * @return null|ForumModel * @throws \Exception */ protected function getForum() { $id_or_search = FORUM; // 优先search $forum = ForumModel::search($id_or_search); if (!$forum) { $forum = ForumModel::getForum($id_or_search); } if (!$forum) { throw new \Exception('论坛 ' . FORUM . ' 不存在'); } return $forum; }
public function bootstrapKernel(AppKernel $kernel) { // 将已有的论坛加入到左侧导航条的参数中 $container = self::getContainer(); $forums = ForumModel::allForums(); $container_parameters = $container->getParameters(); $forum_menus_tree = $container_parameters['menus_tree']['forum']; foreach ($forums as $i => $forum) { $forum_item = array('id' => 'forum.' . $forum['id'], 'title' => $forum['name'], 'link' => $this->generateUrl('admin_forum_group_index', array('forum_id' => $forum['id']))); $forum_menus_tree['children'][] = $forum_item; $container_parameters['menu.forum.' . $forum['id']] = $forum_item; } $container_parameters['menus_tree']['forum'] = $forum_menus_tree; // 保存 $container->setParameters($container_parameters); }
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)); }
protected function handle() { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); $posts = $request->request; try { $name = $posts->get('name'); if (!$name) { throw new \Exception('论坛名称不能为空'); } $description = $posts->get('description'); $description = trim($description); // 不能重复 $search = $posts->get('search'); if ($search) { $structure = ForumModel::search($search); if ($structure) { throw new \Exception("键名为 {$search} 的论坛已存在"); } } else { $search = null; } // 创建 $forum = new ForumModel(); $forum->name = $name; $forum->description = $description; $forum->search = $search; $now = time(); $forum->createTimestamp = $now; $forum->updateTimestamp = $now; // 保存 ForumModel::createForum($forum); $session->addFlash('success', '创建论坛成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_index')); } return $this->render('forum/add.html.twig'); }
protected function handle() { $request = $this->getRequest(); $columns = array('ID', '名称', '状态', '分组数', '版块数', '主题数', '回复数', '创建', '更新'); $fields = array('id', 'name', 'status', 'group_count', 'board_count', 'thread_count', 'post_count', 'create_timestamp', 'update_timestamp'); 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 = ForumModel::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 = ForumModel::listForums($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])) { if ($field == 'create_timestamp' || $field == 'update_timestamp') { // 时间 $line[] = date('Y-m-d H:i:s', $v[$field]); } elseif ($field == 'status') { if ($v[$field] == 1) { // 可用 $line[] = '<span class="text-success">有效</span>'; } elseif ($v[$field] == 0) { // 不可用 $line[] = '<span class="text-danger">无效</span>'; } } else { $line[] = $v[$field]; } } } $edit_url = $this->generateUrl('admin_forum_edit', array('id' => $v['id'])); $group_url = $this->generateUrl('admin_forum_group_index', array('forum_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="' . $group_url . '"><i class="fa fa-wrench"></i> 管理</a></li> </ul> </div>'; $line[] = $operation; $records['data'][] = $line; } return new JsonResponse($records); } return $this->render('forum/index.html.twig', array('columns' => $columns)); }
protected function handle() { $board_id = $this->id; $board = BoardModel::getBoard($board_id); $group = GroupModel::getGroup($board->groupId); $forum = ForumModel::getForum($group->forumId); // 子版块 $sub_boards = BoardModel::allBoards(function (QueryBuilder $qb) use($board) { $qb->andWhere($qb->expr()->eq('parent_id', ':parent_id'))->setParameter(':parent_id', $board->id); $qb->addOrderBy('weight', 'desc'); $qb->addOrderBy('create_timestamp'); }); $columns = array('ID', '标题', '作者', '回复'); // 主题 $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 = ThreadModel::getCount(function (QueryBuilder $qb) use($board, $search_value) { $qb->andWhere($qb->expr()->eq('board_id', ':board_id'))->setParameter(':board_id', $board->id); if ($search_value) { $qb->orWhere($qb->expr()->like("`title`", ":title"))->setParameter(":title", "%{$search_value}%"); } $qb->addOrderBy('update_timestamp', 'desc'); $qb->addOrderBy('create_timestamp', 'desc'); }); } $page = $page_offset / $page_size + 1; $pager = ThreadModel::listThreads($page, $page_size, function (QueryBuilder $qb) use($board, $search_value) { $qb->andWhere($qb->expr()->eq('board_id', ':board_id'))->setParameter(':board_id', $board->id); if ($search_value) { $qb->orWhere($qb->expr()->like("`title`", ":title"))->setParameter(":title", "%{$search_value}%"); } $qb->addOrderBy('update_timestamp', 'desc'); $qb->addOrderBy('create_timestamp', 'desc'); }); $total = $pager->getCount(); $records['recordsTotal'] = $total; $records['recordsFiltered'] = $total; $data = $pager->getData(); /*$columns = array( 'ID', '标题', '作者', '回复', );*/ foreach ($data as $k => $v) { $line = array(); $line[] = '<input type="checkbox" name="id[]" value="' . $v['id'] . '">'; $line[] = $v['id']; $line[] = $v['title']; $line[] = $v['author_name'] . '(' . date('Y-m-d H:i:s', $v['create_timestamp']) . ')'; $line[] = $v['reply_author_name'] . '(' . date('Y-m-d H:i:s', $v['reply_timestamp']) . ')'; $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"> </ul> </div>'; $line[] = $operation; $records['data'][] = $line; } return new JsonResponse($records); } else { return $this->render('board/index.html.twig', array('forum' => $forum, 'group' => $group, 'board' => $board, 'sub_boards' => $sub_boards, 'columns' => $columns)); } }
/** * 保存 * @param ForumModel $forum * @return ForumModel * @throws \Exception */ public static function saveForum(ForumModel $forum) { return self::editForum($forum->toArray()); }
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)); }