예제 #1
0
 /**
  * Execute the controller.
  *
  * @return  mixed Return executed result.
  *
  * @throws  \LogicException
  * @throws  \RuntimeException
  */
 public function execute()
 {
     $model = new PostModel();
     $data = $this->input->getVar('post');
     $data['text'] = $this->input->getByPath('post.text', null, InputFilter::RAW);
     $data = new Data($data);
     $isNew = !$data['id'];
     try {
         $model->validate($data);
         if (!$isNew) {
             $oldData = (new DataMapper('posts'))->findOne($data['id']);
             $oldData->bind($data);
             $data = $oldData;
             $data->modified = (new Date())->format('Y-m-d H:i:s');
         } else {
             $data->blog = Blog::get()->id;
             $data->type = $this->input->get('type', 'post');
             $data->type = $data->type == 'post' ? $data->type : 'static';
             $data->created = (new Date())->format('Y-m-d H:i:s');
         }
         $data->author = $data->author ?: Author::get(User::get()->id, Blog::get()->id)->id;
         $text = preg_split('/(\\<\\!--\\s*\\{READMORE\\}\\s*--\\>)/', $data['text'], 2);
         $data->introtext = isset($text[0]) ? $text[0] : null;
         $data->fulltext = isset($text[1]) ? $text[1] : null;
         $data = $model->save($data);
     } catch (ValidFailException $e) {
         $return['msg'] = $e->getMessage();
         $return['success'] = false;
         $this->respond($return, 500);
         return false;
     } catch (\Exception $e) {
         if (WINDWALKER_DEBUG) {
             throw $e;
         }
         $return['msg'] = 'Save fail';
         $return['success'] = false;
         $this->respond($return, 500);
         return false;
     }
     $return['msg'] = 'Save success';
     $return['success'] = true;
     $return['item'] = $data;
     $this->respond($return, 200);
     return true;
 }
 /**
  * Execute the controller.
  *
  * @return  mixed Return executed result.
  *
  * @throws  \LogicException
  * @throws  \RuntimeException
  */
 public function execute()
 {
     $model = new PostsModel();
     $blog = Ioc::get('current.blog', 'front');
     $author = (new DataMapper('authors'))->findOne(['blog' => $blog->id, 'owner' => 1]);
     $user = (new DataMapper('users'))->findOne(['id' => $author->user]);
     $this->data['blog'] = $blog;
     $this->data['ownerUser'] = $user;
     $this->data['ownerAuthor'] = $author;
     $this->data['user'] = User::get();
     $this->data['author'] = Author::get($user->id, $blog->id);
     // Statics
     $model['blog.id'] = $blog->id;
     $model['list.start'] = null;
     $model['list.limit'] = null;
     $model['blog.published'] = true;
     $model['post.type'] = 'static';
     $model['post.ordering'] = 'id asc';
     $this->data['statics'] = $model->getItems();
     $this->data['blog']->link = 'http://' . $this->data['blog']->alias . '.windspeaker.co';
     $this->data['blog']->params = new Registry($this->data['blog']->params);
     $this->data['meta'] = new Data();
     return $this->doExecute();
 }