protected function handle() { /** @var CategoryModel[] $tree */ $tree = CategoryModel::getTree(); /** @var CategoryModel $root_node */ $root_node = CategoryModel::getRootNode(); $categories = array(); foreach ($tree as $category) { $categories[$category->categoryId] = $category; } $request = $this->getRequest(); $category_id = $request->query->get('category_id'); if (!$category_id || $category_id == $root_node->categoryId) { $category_id = null; } $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 10; $pager = ArticleModel::listPortalArticles($page, $size, function (QueryBuilder $qb) use($category_id) { if ($category_id) { $qb->andWhere($qb->expr()->eq('category_id', ':category_id'))->setParameter(':category_id', $category_id); } $qb->addOrderBy('create_timestamp', 'DESC'); }); $pager->setQuery(array('category_id' => $category_id)); return $this->render('article/manage-article.html.twig', array('tree' => $tree, 'pager' => $pager, 'categories' => $categories)); }
protected function handle() { $tree = CategoryModel::getTree(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { try { // 检查参数 $category_name = $request->request->get('category_name'); if (!$category_name) { throw new \Exception("子分类链接标题不能为空"); } $category_desc = $request->request->get('category_desc'); $enabled = $request->request->get('enabled'); $category_type = $request->request->get('category_type'); if (!$category_type) { throw new \Exception("子分类链接的类型不能为空"); } if (!in_array($category_type, array('article-menu', 'menu', 'page', 'link', 'article'))) { throw new \Exception("子分类链接的类型错误"); } $enabled = intval($enabled); $parent_category_id = $request->request->get('parent_category_id'); if (!$parent_category_id) { throw new \Exception("请选择父链接"); } $parent_category = CategoryModel::getPortalCategory($parent_category_id); if (!$parent_category) { throw new \Exception("父链接不存在"); } $now = time(); $parent_category->createChildNode(array('category_name' => $category_name, 'category_desc' => $category_desc, 'category_type' => $category_type, 'enabled' => $enabled, 'create_timestamp' => $now, 'update_timestamp' => $now)); } catch (\Exception $e) { $session = $this->getSession(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_portal_category_manage')); } return $this->render('category/add-category-modal.html.twig', array('tree' => $tree)); }
protected function handle() { /** @var CategoryModel[] $tree */ $tree = CategoryModel::getTree(); $session = $this->getSession(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $title = $posts->get('title'); $content = $posts->get('content'); $category_id = $posts->get('category_id'); $op = $posts->get('op'); $cdn_url = $this->getContainer()->getParameter('cdn_url'); $cdn_url = str_replace('/', '\\/', $cdn_url); try { if (empty($title)) { throw new \Exception('标题不能为空'); } $category_valid = false; foreach ($tree as $category) { if ($category->categoryId == $category_id) { $category_valid = true; } } if (!$category_valid) { throw new \Exception("文章分类不能为空"); } $article = new ArticleModel(); $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]; } $thumbnails = $new_thumbnails; $summary = strip_tags($content); $article->title = $title; $article->summary = $summary; $article->content = $content; $article->thumbnails = implode('<>', $thumbnails); $now = time(); $article->createTimestamp = $now; if ($op == 'submit-publish') { $article->publishTimestamp = $now; } $article->userId = 0; // 官方发布 $article->categoryId = $category_id; ArticleModel::createPortalArticle($article); $session->addFlash('success', '操作成功'); return new RedirectResponse($this->generateUrl('admin_portal_article_manage')); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('admin_portal_article_add')); } } return $this->render('article/add-article.html.twig', array('tree' => $tree)); }
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)); }