From 1b82af24e8dd4dc03f4ad29a9a23c8ed3c77a467 Mon Sep 17 00:00:00 2001 From: Daniel Cebrian Date: Wed, 15 Feb 2023 15:14:17 +0100 Subject: [PATCH] #9073 added view edit tip --- .../godmode/setup/welcome_tips.php | 84 ++++++--- .../include/class/TipsWindow.class.php | 160 ++++++++++++++++-- .../include/styles/tips_window.css | 4 + 3 files changed, 213 insertions(+), 35 deletions(-) diff --git a/pandora_console/godmode/setup/welcome_tips.php b/pandora_console/godmode/setup/welcome_tips.php index ec56b0ff2b..531d47bf3a 100644 --- a/pandora_console/godmode/setup/welcome_tips.php +++ b/pandora_console/godmode/setup/welcome_tips.php @@ -39,13 +39,14 @@ try { return; } -if ($view === 'create') { - if ($action === 'create') { +if ($view === 'create' || $view === 'edit') { + // IF exists actions + if ($action === 'create' || $action === 'edit') { $files = $_FILES; $id_lang = get_parameter('id_lang', ''); - $title = io_safe_input(get_parameter('title', '')); - $text = io_safe_input(get_parameter('text', '')); - $url = io_safe_input(get_parameter('url', '')); + $title = get_parameter('title', ''); + $text = get_parameter('text', ''); + $url = get_parameter('url', ''); $enable = get_parameter_switch('enable', ''); $errors = []; @@ -68,24 +69,68 @@ if ($view === 'create') { $errors[] = __('Text is empty'); } - if (count($errors) === 0) { - if (count($files) > 0) { - $uploadImages = $tipsWindow->uploadImages($files); - } + switch ($action) { + case 'create': + if (count($errors) === 0) { + if (count($files) > 0) { + $uploadImages = $tipsWindow->uploadImages($files); + } - $response = $tipsWindow->createTip($id_lang, $title, $text, $url, $enable, $uploadImages); + $response = $tipsWindow->createTip($id_lang, $title, $text, $url, $enable, $uploadImages); - if ($response === 0) { - $errors[] = __('Error in insert tip'); - } + if ($response === 0) { + $errors[] = __('Error in insert tip'); + } + } + + $tipsWindow->viewCreate($errors); + return; + + case 'edit': + $idTip = get_parameter('idTip', ''); + if (empty($idTip) === false) { + if (count($errors) === 0) { + if (count($files) > 0) { + $uploadImages = $tipsWindow->uploadImages($files); + } + + $response = $tipsWindow->updateTip($idTip, $id_lang, $title, $text, $url, $enable, $uploadImages); + + if ($response === 0) { + $errors[] = __('Error in update tip'); + } + } + + $tipsWindow->viewEdit($idTip, $errors); + } + return; + + default: + $tipsWindow->draw(); + return; } - $tipsWindow->viewCreate($errors); - } else { - $tipsWindow->viewCreate(); + + return; } - return; + // If not exists actions + switch ($view) { + case 'create': + $tipsWindow->viewCreate(); + return; + + case 'edit': + $idTip = get_parameter('idTip', ''); + if (empty($idTip) === false) { + $tipsWindow->viewEdit($idTip); + } + return; + + default: + $tipsWindow->draw(); + return; + } } if ($action === 'delete') { @@ -104,6 +149,7 @@ if ($action === 'delete') { } $tipsWindow->draw($errors); -} else { - $tipsWindow->draw(); + return; } + +$tipsWindow->draw(); diff --git a/pandora_console/include/class/TipsWindow.class.php b/pandora_console/include/class/TipsWindow.class.php index 79da5e84bc..b7a48e46d5 100644 --- a/pandora_console/include/class/TipsWindow.class.php +++ b/pandora_console/include/class/TipsWindow.class.php @@ -56,13 +56,6 @@ class TipsWindow */ public $ajaxController; - /** - * Array of tips - * - * @var array - */ - public $tips = []; - /** * Generates a JSON error. @@ -178,6 +171,23 @@ class TipsWindow } + public function getTipById($idTip) + { + $tip = db_get_row( + 'twelcome_tip', + 'id', + $idTip, + ); + if ($tip !== false) { + $tip['title'] = io_safe_output($tip['title']); + $tip['text'] = io_safe_output($tip['text']); + $tip['url'] = io_safe_output($tip['url']); + } + + return $tip; + } + + public function getRandomTip($return=false) { $exclude = get_parameter('exclude', ''); @@ -196,11 +206,14 @@ class TipsWindow $sql .= ' ORDER BY RAND()'; $tip = db_get_row_sql($sql); - $tip['files'] = $this->getFilesFromTip($tip['id']); - $tip['title'] = io_safe_output($tip['title']); - $tip['text'] = io_safe_output($tip['text']); - $tip['url'] = io_safe_output($tip['url']); + if (empty($tip) === false) { + $tip['files'] = $this->getFilesFromTip($tip['id']); + + $tip['title'] = io_safe_output($tip['title']); + $tip['text'] = io_safe_output($tip['text']); + $tip['url'] = io_safe_output($tip['url']); + } if ($return) { if (empty($tip) === false) { @@ -430,8 +443,17 @@ class TipsWindow $data[$key]['title'] = io_safe_output($row['title']); $data[$key]['text'] = io_safe_output($row['text']); $data[$key]['url'] = io_safe_output($row['url']); - - $data[$key]['actions'] = '
'; + $data[$key]['actions'] = '
'; + $data[$key]['actions'] .= ''; + $data[$key]['actions'] .= html_print_input_image( + 'button_edit_tip', + 'images/edit.png', + '', + '', + true + ); + $data[$key]['actions'] .= ''; + $data[$key]['actions'] .= ''; $data[$key]['actions'] .= html_print_input_image( 'button_delete_tip', 'images/delete.png', @@ -442,6 +464,7 @@ class TipsWindow ); $data[$key]['actions'] .= html_print_input_hidden('idTip', $row['id'], true); $data[$key]['actions'] .= ''; + $data[$key]['actions'] .= '
'; } if (empty($data) === true) { @@ -536,6 +559,111 @@ class TipsWindow } + public function viewEdit($idTip, $errors=null) + { + $tip = $this->getTipById($idTip); + if ($tip === false) { + return; + } + + $files = $this->getFilesFromTip($idTip); + + ui_require_javascript_file('tipsWindow'); + ui_require_css_file('tips_window'); + + if ($errors !== null) { + if (count($errors) > 0) { + foreach ($errors as $key => $value) { + ui_print_error_message($value); + } + } else { + ui_print_success_message(__('Tip edited')); + } + } + + $table = new stdClass(); + $table->width = '100%'; + $table->class = 'databox filters'; + + $table->style[0] = 'font-weight: bold'; + + $table->data = []; + $table->data[0][0] = __('Images'); + $table->data[0][1] = html_print_input_hidden('number_images', 0, true); + $table->data[0][1] .= html_print_div(['id' => 'inputs_images'], true); + $table->data[0][1] .= html_print_button(__('Add image'), 'button_add_image', false, '', '', true); + $table->data[1][0] = __('Language'); + $table->data[1][1] = html_print_select_from_sql( + 'SELECT id_language, name FROM tlanguage', + 'id_lang', + $tip['id_lang'], + '', + '', + '0', + true + ); + $table->data[2][0] = __('Title'); + $table->data[2][1] = html_print_input_text('title', $tip['title'], '', 35, 100, true); + $table->data[3][0] = __('Text'); + $table->data[3][1] = html_print_textarea('text', 5, 1, $tip['text'], '', true); + $table->data[4][0] = __('Url'); + $table->data[4][1] = html_print_input_text('url', $tip['url'], '', 35, 100, true); + $table->data[5][0] = __('Enable'); + $table->data[5][1] = html_print_checkbox_switch('enable', 1, ($tip['enable'] === '1') ? true : false, true); + + echo '
'; + html_print_table($table); + echo '
'; + html_print_submit_button(__('Send'), 'submit_button', false, ['class' => 'sub next']); + + echo '
'; + echo '
'; + } + + + public function updateTip($id, $id_lang, $title, $text, $url, $enable, $images=null) + { + db_process_sql_begin(); + hd($enable, true); + $idTip = db_process_sql_update( + 'twelcome_tip', + [ + 'id_lang' => $id_lang, + 'title' => io_safe_input($title), + 'text' => io_safe_input($text), + 'url' => io_safe_input($url), + 'enable' => $enable, + ], + ['id' => $id] + ); + if ($idTip === false) { + db_process_sql_rollback(); + return false; + } + + if ($images !== null) { + foreach ($images as $key => $image) { + $res = db_process_sql_insert( + 'twelcome_tip_file', + [ + 'twelcome_tip_file' => $idTip, + 'filename' => $image, + 'path' => 'images/tips/', + ] + ); + if ($res === false) { + db_process_sql_rollback(); + return false; + } + } + } + + db_process_sql_commit(); + + return true; + } + + public function createTip($id_lang, $title, $text, $url, $enable, $images=null) { db_process_sql_begin(); @@ -543,9 +671,9 @@ class TipsWindow 'twelcome_tip', [ 'id_lang' => $id_lang, - 'title' => $title, - 'text' => $text, - 'url' => $url, + 'title' => io_safe_input($title), + 'text' => io_safe_input($text), + 'url' => io_safe_input($url), 'enable' => $enable, ] ); diff --git a/pandora_console/include/styles/tips_window.css b/pandora_console/include/styles/tips_window.css index aa9a4f050b..db1c852bc0 100644 --- a/pandora_console/include/styles/tips_window.css +++ b/pandora_console/include/styles/tips_window.css @@ -179,3 +179,7 @@ span.disable { background-color: #e63c52; margin: 0 auto; } +.buttons_actions { + text-align: center; + display: flex; +}