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; }
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'); }