| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace cx_newsletter;
- class edit_message_activity
- extends activity {
- public function show_after_button() : string {
- return 'create';
- }
- public function inside_buttons() : array {
- return [ 'edit', 'save' ];
- }
- public function inside_inputs() : array {
- return [
- 'id' => 'int',
- 'title' => 'string',
- 'content' => 'string'
- ];
- }
- public function get_site_template_name() : string {
- return 'edit_message';
- }
- public function prepare_edit() : void {
- if (!$this->is_validated('id')) {
- $this->error_toast('ID of the message to edit is wrong.');
- return;
- }
-
- $mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- $message = $mapper->complete(new message($this->get_validated('id')));
- $this->set('id', $message->get_id());
- $this->set('title', $message->title);
- $this->set('content', $message->content);
- }
- public function create() : void {
- $message = new message();
- $message->title = $this->get_validated('title');
- $message->content = stripslashes($this->get_validated('content'));
- $mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- try {
- $message = $mapper->create($message);
- $this->success_toast('Created successfull.');
- $this->set('id', $message->get_id());
- $this->set('title', $message->title);
- $this->set('content', $message->content);
- } catch (\exception $exception) {
- $this->error_toast($exception->getMessage());
- }
- }
- public function own_validation() : ?string {
- if (!$this->is_validated('title')) {
- return 'Title is not sended.';
- }
- if (!$this->is_validated('content')) {
- return 'Content is not sended.';
- }
- $title = $this->get_validated('title');
- $content = $this->get_validated('content');
- if ($this->is_validated('id')) {
- $this->set('id', $this->get_validated('id'));
- }
- if (strlen($title) < 8) {
- return 'Title must be longer than 8 characters.';
- }
- if (\esc_html($title) !== $title) {
- return 'Title can not has HTML characters.';
- }
-
- return null;
- }
- public function save() : void {
- $result = $this->own_validation();
- if ($result !== null) {
- $this->warning_toast($result);
- return;
- }
- if (!$this->is_validated('id')) {
- $this->create();
- return;
- }
- $mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- $message = new message($this->get_validated('id'));
- $message->title = $this->get_validated('title');
- $message->content = stripslashes($this->get_validated('content'));
- try {
- $mapper->save($message);
- $this->success_toast('Message saved.');
- $this->set('id', $message->get_id());
- $this->set('title', $message->title);
- $this->set('content', $message->content);
- } catch (\exception $exception) {
- $this->error_toast($exception->getMessage());
- }
- }
- public function process() : self {
- if ($this->is_validated('title')) {
- $this->set('title', $this->get_validated('title'));
- }
- if ($this->is_validated('content')) {
- $this->set(
- 'content',
- stripslashes($this->get_validated('content'))
- );
- }
-
- if ($this->is_validated('id')) {
- $this->set('id', $this->get_validated('id'));
- }
-
- if ($this->is_received('save')) {
- $this->save();
- }
- if ($this->is_received('edit')) {
- $this->prepare_edit();
- }
- $content = '';
- if ($this->get('content') !== null) {
- $content = $this->get('content');
- }
- \ob_start();
- \wp_editor($content, 'content', [
- 'textarea_rows' => '',
- 'quicktags' => false,
- 'wpautoup' => false,
- 'media_buttons' => true,
- 'textarea_name' => 'content',
- 'teeny' => true,
- 'tinymce' => [
- 'content_css' => false,
- 'inline' => false
- ]
- ]);
- $result = \ob_get_clean();
- $this->set('editor', $result);
- return $this;
- }
- }
|