コード例 #1
0
 public function createPostAction()
 {
     if ($this->request->isPost()) {
         $post = new Post();
         $post->topic = $this->request->getPost('title');
         $post->content = str_replace('</div>', '\\n', str_replace('<div>', '', $this->request->getPost('post')));
         $post->is_service = $this->request->getPost('is_service');
         $post->created_at = date('d.m.Y, h:i');
         if ($post->save()) {
             $tt = explode(',', $this->request->getPost('tags'));
             foreach ($tt as $t) {
                 $tag = Tag::findFirst(array('conditions' => 'name = ?1', 'bind' => array(1 => $t)));
                 if ($tag) {
                     $tag_post = new PostTag();
                     $tag_post->post_id = $post->id;
                     $tag_post->tag_id = $tag->id;
                     if (!$tag_post->save()) {
                         message($this, 'd', 'Ошибка привязки категории к посту');
                         return $this->response->redirect();
                     }
                 } else {
                     $tag = new Tag();
                     $tag->name = $t;
                     if ($tag->save()) {
                         $tag_post = new PostTag();
                         $tag_post->post_id = $post->id;
                         $tag_post->tag_id = $tag->id;
                         if (!$tag_post->save()) {
                             message($this, 'd', 'Ошибка привязки категории к посту');
                             return $this->response->redirect();
                         }
                     } else {
                         message($this, 'd', 'Ошибка сохранения новой категории');
                         return $this->response->redirect();
                     }
                 }
             }
             message($this, 's', 'Новость успешно добавлена');
             return $this->response->redirect();
         } else {
             foreach ($post->getMessages() as $message) {
                 message($this, "d", "Ошибка: " . $message->getMessage() . " в поле " . $message->getField() . ". Тип: " . $message->getType());
             }
             return $this->response->redirect();
         }
     } else {
         $this->assets->collection('headerJs')->addJs("js/jquery-ui.min.js");
         $this->assets->collection('headerCss')->addCss("css/jquery-ui.min.css")->addCss("css/jquery-ui.structure.min.css");
         $tt = Tag::find();
         $tags = "";
         foreach ($tt as $tag) {
             $tags .= '"' . $tag->name . '", ';
         }
         $tags = substr($tags, 0, -2);
         $this->view->setParamToView('tags', $tags);
     }
 }