Пример #1
0
 /**
  * @Route("/detail/id/{id}",defaults={"id":1},requirements={"id"="\d+"})
  * @ParamConverter("article",class="TarsierHomeBundle:article")
  * @Method("GET")
  * @Template()
  */
 public function detailAction(article $article)
 {
     $data['article'] = $article;
     if ($this->getRequest()->cookies->get('userName') == null && $article->getStatus() != 1) {
         return $this->redirect($this->generateUrl('indexPage'));
     }
     $tagsArr = [];
     foreach ($article->getTag() as $tag) {
         $tagsArr[] = $tag->getName();
     }
     if (empty($tagsArr)) {
         $tagsArr[] = 'detail';
     }
     $c = new Common();
     $current_month = $c->getCurrentMonth();
     $em = $this->getDoctrine()->getManager();
     $tags = $this->getTags($em);
     $hot_article = $em->getRepository("TarsierHomeBundle:article")->getHotArticle();
     $nav_tags = array_intersect($tagsArr, $this->nav_arr);
     $data['current_month'] = $current_month;
     $data['tagsArr'] = $tagsArr;
     //面包屑导航
     $data['tags'] = $tags;
     //标签云
     $data['sid'] = md5($article->getTitle() . 'http://huajie1988.net/detail/id/' . $article->getId());
     $data['nav_tags'] = current($nav_tags);
     //导航栏
     $data['hot_article'] = $hot_article;
     //导航栏
     $data['friendlink'] = $this->getFriendLink();
     $data['is_mobile'] = $this->isMobile();
     $em = $this->getDoctrine()->getManager();
     $article->setClick($article->getClick() + 1);
     $em->persist($article);
     $em->flush();
     return $data;
 }
Пример #2
0
 /**
  * @Route("/article/add",name="articleadd")
  * @Template("TarsierAdminBundle:article:edit.html.twig")
  */
 public function articleAddAction()
 {
     if (!$this->isLogin()) {
         return $this->redirect($this->generateUrl('adminlogin'));
     }
     $userName = $this->getRequest()->cookies->get('userName');
     $article = new article();
     $form = $this->createFormBuilder($article, ['attr' => ['id' => 'form-save', 'class' => 'form-save']])->add('title', 'text', ['label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin', 'placeholder' => "Article Title"]])->add('status', 'choice', ['choices' => ['0' => 'Draft', '1' => 'Release', '2' => 'Delete'], 'label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin']])->add('tag', 'text', ['label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin']])->add('top', 'choice', ['choices' => ['0' => 'Not top article', '1' => 'Is top article'], 'label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin']])->add('sort', 'integer', ['label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin', 'placeholder' => "Article Sort"]])->add('front_cover', 'file', ['label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin'], 'required' => 0])->add('thumb', 'file', ['label_attr' => ['class' => ''], 'attr' => ['class' => 'form-control input-signin'], 'required' => 0])->add('content', 'textarea', ['label_attr' => ['class' => ''], 'attr' => ['id' => 'editor', 'class' => 'form-control input-signin', 'placeholder' => "Article Content"], 'required' => 0])->add('Save', 'submit', ['attr' => ['class' => 'btn btn-lg btn-primary btn-block form-save-btn']])->getForm();
     $form->handleRequest($this->getRequest());
     if ($form->isValid()) {
         $photo = $this->dealPhotoProcess($form->get('front_cover')->getData(), $form->get('thumb')->getData());
         $front_cover = $photo['front_cover'];
         $thumb = $photo['thumb'];
         $em = $this->getEm();
         $sem = $this->getEm('sqlite');
         $user = $this->getUserRepository()->findOneBy(['username' => $userName]);
         $ret_form = $this->getRequest()->get('form');
         $tags = explode(',', str_replace(',', ',', $ret_form['tag']));
         $tags_em = $this->getTagsRepository();
         $tags_list = [];
         foreach ($tags as $t) {
             $tag = $tags_em->findOneBy(['name' => $t]);
             if (empty($tag)) {
                 $tag = new tags();
                 $tag->setName($t);
                 $tag->setClick(1);
             } else {
                 $tag->setClick(count($tag->getArticle()) + 1);
             }
             $em->persist($tag);
             $em->flush();
             $tags_list[] = $tag;
         }
         $article = new article();
         $article->setTitle($ret_form['title']);
         $article->setCreateTime(time());
         $article->setSort($ret_form['sort']);
         $article->setStatus($ret_form['status']);
         $article->setTop($ret_form['top']);
         $article->setClick(0);
         foreach ($tags_list as $t) {
             $article->addTag($t);
         }
         $article->setContent($ret_form['content']);
         $article->setUser($user);
         $em->persist($article);
         $em->flush();
         $articleimg = new articleimg();
         $articleimg->setFrontCover($front_cover);
         $articleimg->setThumb($thumb);
         $articleimg->setArticleId($article->getId());
         $sem->persist($articleimg);
         $sem->flush();
         return $this->redirect($this->generateUrl('articlelist'));
     }
     $data = ['save_form' => $form->createView(), 'userName' => $userName, 'nav' => 'article', 'id' => 0];
     return $data;
 }