public function handle() { $session = $this->getSession(); $publisher = PublisherModel::getPublisher($this->id); if (!$publisher) { $session->addFlash('error', '发布者不存在'); return new RedirectResponse($this->generateUrl('admin_www_publisher')); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; try { $name = $posts->get('name'); $description = $posts->get('description'); $avatar = $posts->get('avatar'); if (!$name) { throw new \Exception('发布者名称不能为空'); } if (!$description) { throw new \Exception('发布者描述不能为空'); } $publisher->name = $name; $publisher->description = $description; $publisher->avatar = $avatar; $publisher->updateTimestamp = time(); // 保存 PublisherModel::savePublisher($publisher); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_publisher_edit', array('id' => $this->id))); } return $this->render('publisher/edit.html.twig', array('publisher' => $publisher)); }
protected function handle() { $tree = StructureModel::getTree(); $publishers = PublisherModel::allPublishers(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $ids = $request->request->get('ids'); $publisher_id = $request->request->get('publisher_id'); $structure_id = $request->request->get('structure_id'); $session = $this->getSession(); $db = ArticleModel::getDb(); try { $db->transaction(); if (!$publisher_id) { throw new \Exception('请选择一个发布者'); } if (!$structure_id) { throw new \Exception('请选择发布所属目录'); } $publisher = PublisherModel::getPublisher($publisher_id); if (!$publisher) { throw new \Exception('发布者不存在'); } foreach ($ids as $id) { $article = ArticleModel::getArticle($id); if ($article) { $article->publishTimestamp = time(); $article->structureId = $structure_id; ArticleModel::saveArticle($article); } // 添加文章发布者关系 $article_publisher = new ArticlePublisherModel(); $article_publisher->articleId = $article->id; $article_publisher->publisherId = $publisher->id; ArticlePublisherModel::createRelationship($article_publisher); } // 添加发布者数据 $publisher->articleCount += count($ids); PublisherModel::savePublisher($publisher); $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_article')); } else { $ids = $request->query->get('ids'); $ids = json_decode($ids); if (!$ids) { return $this->render('@TachigoWWWAdmin/error-modal.html.twig', array('message' => '没有选中任何文章')); } else { $pager = ArticleModel::listArticles(1, count($ids), function (QueryBuilder $qb) use($ids) { $qb->where($qb->expr()->in('id', $ids)); }); return $this->render('article/publish.html.twig', array('articles' => $pager, 'publishers' => $publishers, 'tree' => $tree)); } } }
protected function handle() { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $ids = $request->request->get('ids'); $session = $this->getSession(); $db = ArticleModel::getDb(); try { $db->transaction(); foreach ($ids as $id) { $article = ArticleModel::getArticle($id); if ($article) { $article->publishTimestamp = 0; ArticleModel::saveArticle($article); } // 修改发布者信息 $article_publishers = ArticlePublisherModel::allRelationship(function (QueryBuilder $qb) use($article) { $qb->andWhere($qb->expr()->eq('article_id', ':article_id'))->setParameter(':article_id', $article->id); }); if ($article_publishers) { foreach ($article_publishers as $article_publisher) { $publisher_id = $article_publisher['publisher_id']; // 删除发布者文章关系 ArticlePublisherModel::deleteRelationship($article->id, $publisher_id); // 修改发布者数据 $publisher = PublisherModel::getPublisher($publisher_id); if ($publisher) { $publisher->articleCount -= 1; PublisherModel::savePublisher($publisher); } } } } $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_article')); } else { $ids = $request->query->get('ids'); $ids = json_decode($ids); if (!$ids) { return $this->render('@TachigoWWWAdmin/error-modal.html.twig', array('message' => '没有选中任何文章')); } else { $pager = ArticleModel::listArticles(1, count($ids), function (QueryBuilder $qb) use($ids) { $qb->where($qb->expr()->in('id', $ids)); }); return $this->render('article/trash.html.twig', array('articles' => $pager)); } } }