/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $user = User::get(); $blog = Blog::get(); $type = $this->input->get('type'); $route = $type == 'static' ? 'statics' : 'posts'; try { if (!$id) { throw new ValidFailException('Where is your post ID?'); } if (!Author::isAdmin()) { throw new ValidFailException('Access deny'); } $postMapper = new DataMapper('posts'); $post = $postMapper->findOne($id); if ($post->blog != $blog->id) { throw new ValidFailException('You cannot change post of other blog.'); } $post['state'] = $this->input->get('state', 1); $postMapper->updateOne($post); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:' . $route), $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:' . $route), 'Fail', 'danger'); return false; } $this->setRedirect(Router::buildHttp('admin:' . $route), 'Success', 'success'); return true; }
/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $user = User::get(); $blog = Blog::get(); try { if (!$id) { throw new ValidFailException('No ID'); } if (!Author::isAdmin()) { throw new ValidFailException('Access deny'); } $postMapper = new DataMapper('posts'); $post = $postMapper->findOne($id); if ($post->blog != $blog->id) { throw new ValidFailException('You cannot delete post of other blog.'); } $postMapper->delete(['id' => $id]); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:posts'), $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:posts'), 'Delete fail', 'danger'); return false; } $this->setRedirect(Router::buildHttp('admin:posts'), 'Delete success', 'success'); return true; }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $files = $this->input->files; $field = $this->input->get('field', 'file'); $id = $this->input->get('id'); $author = Author::getAuthor($id); $user = User::get(); $blog = Blog::get(); try { if (!Author::isAdmin($blog, $user)) { throw new ValidFailException('You cannot edit this author.'); } $src = $files->getByPath($field . '.tmp_name', null, InputFilter::STRING); $name = $files->getByPath($field . '.name', null, InputFilter::STRING); if (!$src) { throw new \Exception('File not upload'); } $ext = pathinfo($name, PATHINFO_EXTENSION); $uuid = $author->uuid ?: Uuid::v4(); $src = Thumb::createThumb($src); $dest = sprintf('author/%s/%s.%s', sha1($uuid), md5($uuid), $ext); $result = S3Helper::put($src, $dest); File::delete($src); if (!$result) { throw new \Exception('Upload fail.'); } } catch (\Exception $e) { $response = new Response(); $response->setBody(json_encode(['error' => $e->getMessage()])); $response->setMimeType('text/json'); $response->respond(); exit; } $return = new Registry(); $return['filename'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['file'] = 'https://windspeaker.s3.amazonaws.com/' . $dest; $return['uuid'] = $uuid; if ($author->id) { $author->image = $return['filename']; (new DataMapper('authors'))->updateOne($author); } $response = new Response(); $response->setBody((string) $return); $response->setMimeType('text/json'); $response->respond(); exit; }