protected function handle() { $session = $this->getSession(); $article_id = $this->articleId; $article = ArticleModel::getPortalArticle($article_id); if (!$article) { $session->addFlash('error', '文章不存在'); return new RedirectResponse($this->generateUrl('admin_portal_article_manage')); } $article->publishTimestamp = time(); ArticleModel::savePortalArticle($article); $session->addFlash('success', '操作成功'); return new RedirectResponse($this->generateUrl('admin_portal_article_manage')); }
protected function handle() { $tree = CategoryModel::getTree(); $session = $this->getSession(); $article_id = $this->articleId; $article = ArticleModel::getPortalArticle($article_id); if (!$article) { $session->addFlash('error', '文章不存在'); return new RedirectResponse($this->generateUrl('admin_portal_article_manage')); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $title = $posts->get('title'); $content = $posts->get('content'); $thumbnails = $posts->get('thumbnails'); $category_id = $posts->get('category_id'); $category_valid = false; foreach ($tree as $category) { if ($category->categoryId == $category_id) { $category_valid = true; } } if (!$category_valid) { throw new \Exception("文章分类不能为空"); } if (empty($thumbnails)) { $thumbnails = array(); } $reset_thumbnails = $posts->get('reset_thumbnails'); $cdn_url = $this->getContainer()->getParameter('cdn_url'); $cdn_url = str_replace('/', '\\/', $cdn_url); try { if (empty($title)) { throw new \Exception('标题不能为空'); } $article->title = $title; $new_thumbnails = array(); $pattern = "/<\\s*img\\s+[^>]*?src\\s*=\\s*(\\'|\")({$cdn_url}.*?)\\1[^>]*?\\/?\\s*>/i"; preg_match_all($pattern, $content, $new_thumbnails); $new_thumbnails = $new_thumbnails[0]; foreach ($new_thumbnails as $key => $thumbnail) { $matches = array(); preg_match('/src=\\"(.*?)\\"/', $thumbnail, $matches); $new_thumbnails[$key] = $matches[1]; } if ($reset_thumbnails) { $thumbnails = $new_thumbnails; } $article->content = $content; $article->thumbnails = implode('<>', $thumbnails); $article->categoryId = $category_id; ArticleModel::savePortalArticle($article); $session->addFlash('success', '操作成功'); return new RedirectResponse($this->generateUrl('admin_portal_article_edit', array('article_id' => $article_id))); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('admin_portal_article_edit', array('article_id' => $article_id))); } } return $this->render('article/edit-article.html.twig', array('article' => $article, 'tree' => $tree)); }