|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +declare(strict_types=1); | 
|  | 4 | + | 
|  | 5 | +namespace App\Presenters; | 
|  | 6 | + | 
|  | 7 | +use Nette; | 
|  | 8 | +use Nette\Application\UI\Form; | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +final class PostPresenter extends Nette\Application\UI\Presenter | 
|  | 12 | +{ | 
|  | 13 | +	/** @var Nette\Database\Context */ | 
|  | 14 | +	private $database; | 
|  | 15 | + | 
|  | 16 | + | 
|  | 17 | +	public function __construct(Nette\Database\Context $database) | 
|  | 18 | +	{ | 
|  | 19 | +		$this->database = $database; | 
|  | 20 | +	} | 
|  | 21 | + | 
|  | 22 | + | 
|  | 23 | +	public function renderShow(int $postId): void | 
|  | 24 | +	{ | 
|  | 25 | +		$post = $this->database->table('posts')->get($postId); | 
|  | 26 | +		if (!$post) { | 
|  | 27 | +			$this->error('Post not found'); | 
|  | 28 | +		} | 
|  | 29 | + | 
|  | 30 | +		$this->template->post = $post; | 
|  | 31 | +		$this->template->comments = $post->related('comment')->order('created_at'); | 
|  | 32 | +	} | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +	protected function createComponentCommentForm(): Form | 
|  | 36 | +	{ | 
|  | 37 | +		$form = new Form; | 
|  | 38 | +		$form->addText('name', 'Your name:') | 
|  | 39 | +			->setRequired(); | 
|  | 40 | + | 
|  | 41 | +		$form->addEmail('email', 'Email:'); | 
|  | 42 | + | 
|  | 43 | +		$form->addTextArea('content', 'Comment:') | 
|  | 44 | +			->setRequired(); | 
|  | 45 | + | 
|  | 46 | +		$form->addSubmit('send', 'Publish comment'); | 
|  | 47 | +		$form->onSuccess[] = [$this, 'commentFormSucceeded']; | 
|  | 48 | + | 
|  | 49 | +		return $form; | 
|  | 50 | +	} | 
|  | 51 | + | 
|  | 52 | + | 
|  | 53 | +	public function commentFormSucceeded(Form $form, \stdClass $values): void | 
|  | 54 | +	{ | 
|  | 55 | +		$this->database->table('comments')->insert([ | 
|  | 56 | +			'post_id' => $this->getParameter('postId'), | 
|  | 57 | +			'name' => $values->name, | 
|  | 58 | +			'email' => $values->email, | 
|  | 59 | +			'content' => $values->content, | 
|  | 60 | +		]); | 
|  | 61 | + | 
|  | 62 | +		$this->flashMessage('Thank you for your comment', 'success'); | 
|  | 63 | +		$this->redirect('this'); | 
|  | 64 | +	} | 
|  | 65 | + | 
|  | 66 | + | 
|  | 67 | +	public function actionCreate(): void | 
|  | 68 | +	{ | 
|  | 69 | +		if (!$this->getUser()->isLoggedIn()) { | 
|  | 70 | +			$this->redirect('Sign:in'); | 
|  | 71 | +		} | 
|  | 72 | +	} | 
|  | 73 | + | 
|  | 74 | + | 
|  | 75 | +	public function actionEdit(int $postId): void | 
|  | 76 | +	{ | 
|  | 77 | +		if (!$this->getUser()->isLoggedIn()) { | 
|  | 78 | +			$this->redirect('Sign:in'); | 
|  | 79 | +		} | 
|  | 80 | + | 
|  | 81 | +		$post = $this->database->table('posts')->get($postId); | 
|  | 82 | +		if (!$post) { | 
|  | 83 | +			$this->error('Post not found'); | 
|  | 84 | +		} | 
|  | 85 | +		$this['postForm']->setDefaults($post->toArray()); | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | + | 
|  | 89 | +	protected function createComponentPostForm(): Form | 
|  | 90 | +	{ | 
|  | 91 | +		if (!$this->getUser()->isLoggedIn()) { | 
|  | 92 | +			$this->error('You need to log in to create or edit posts'); | 
|  | 93 | +		} | 
|  | 94 | + | 
|  | 95 | +		$form = new Form; | 
|  | 96 | +		$form->addText('title', 'Title:') | 
|  | 97 | +			->setRequired(); | 
|  | 98 | +		$form->addTextArea('content', 'Content:') | 
|  | 99 | +			->setRequired(); | 
|  | 100 | + | 
|  | 101 | +		$form->addSubmit('send', 'Save and publish'); | 
|  | 102 | +		$form->onSuccess[] = [$this, 'postFormSucceeded']; | 
|  | 103 | + | 
|  | 104 | +		return $form; | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | + | 
|  | 108 | +	public function postFormSucceeded(Form $form, \stdClass $values): void | 
|  | 109 | +	{ | 
|  | 110 | +		$postId = $this->getParameter('postId'); | 
|  | 111 | + | 
|  | 112 | +		if ($postId) { | 
|  | 113 | +			$post = $this->database->table('posts')->get($postId); | 
|  | 114 | +			$post->update($values); | 
|  | 115 | +		} else { | 
|  | 116 | +			$post = $this->database->table('posts')->insert($values); | 
|  | 117 | +		} | 
|  | 118 | + | 
|  | 119 | +		$this->flashMessage('Post was published', 'success'); | 
|  | 120 | +		$this->redirect('show', $post->id); | 
|  | 121 | +	} | 
|  | 122 | +} | 
0 commit comments