From 2b66608023a2a4f4212984dc350cb5d67e3b4b97 Mon Sep 17 00:00:00 2001 From: Jakub Senko Date: Mon, 14 May 2018 21:14:41 +0200 Subject: [PATCH 1/2] [ticket/15664] Ajaxify ext actions in ACP PHPBB3-15664 --- phpBB/adm/style/acp_ext_actions.html | 6 ++ phpBB/adm/style/acp_ext_delete_data.html | 40 -------- phpBB/adm/style/acp_ext_disable.html | 34 ------- phpBB/adm/style/acp_ext_enable.html | 40 -------- phpBB/adm/style/acp_ext_list.html | 19 +--- phpBB/adm/style/admin.js | 58 +++++++++++ phpBB/adm/style/ajax.js | 28 +++++ phpBB/includes/acp/acp_extensions.php | 124 ++++++++++++++++------- phpBB/language/en/migrator.php | 2 +- tests/functional/extension_acp_test.php | 8 +- 10 files changed, 190 insertions(+), 169 deletions(-) create mode 100644 phpBB/adm/style/acp_ext_actions.html delete mode 100644 phpBB/adm/style/acp_ext_delete_data.html delete mode 100644 phpBB/adm/style/acp_ext_disable.html delete mode 100644 phpBB/adm/style/acp_ext_enable.html diff --git a/phpBB/adm/style/acp_ext_actions.html b/phpBB/adm/style/acp_ext_actions.html new file mode 100644 index 0000000000..b9c2febbe5 --- /dev/null +++ b/phpBB/adm/style/acp_ext_actions.html @@ -0,0 +1,6 @@ +{% for action in enabled.actions %} +{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} +{% endfor %} +{% for action in disabled.actions %} +{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} +{% endfor %} \ No newline at end of file diff --git a/phpBB/adm/style/acp_ext_delete_data.html b/phpBB/adm/style/acp_ext_delete_data.html deleted file mode 100644 index 0f3adb7cfe..0000000000 --- a/phpBB/adm/style/acp_ext_delete_data.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - -

{L_EXTENSIONS_ADMIN}

- -

{L_EXTENSIONS_EXPLAIN}

-

{L_EXTENSION_DELETE_DATA_EXPLAIN}

- - -
-

{L_MIGRATION_EXCEPTION_ERROR}

-

{MIGRATOR_ERROR}

-

{L_RETURN_TO_EXTENSION_LIST}

-
- -
-

{L_CONFIRM_MESSAGE}

-
- -
-
- {L_EXTENSION_DELETE_DATA} - - -
-
- -
-

{L_EXTENSION_DELETE_DATA_IN_PROGRESS}

-
- -
-

{L_EXTENSION_DELETE_DATA_SUCCESS}

-
-

{L_RETURN_TO_EXTENSION_LIST}

-
- - - diff --git a/phpBB/adm/style/acp_ext_disable.html b/phpBB/adm/style/acp_ext_disable.html deleted file mode 100644 index d2b5c46fe8..0000000000 --- a/phpBB/adm/style/acp_ext_disable.html +++ /dev/null @@ -1,34 +0,0 @@ - - - - -

{L_EXTENSIONS_ADMIN}

- -

{L_EXTENSIONS_EXPLAIN}

-

{L_EXTENSION_DISABLE_EXPLAIN}

- - -
-

{L_CONFIRM}

-

{L_CONFIRM_MESSAGE}

-
- -
-
- - -
-
- -
-

{L_EXTENSION_DISABLE_IN_PROGRESS}

-
- -
-

{L_EXTENSION_DISABLE_SUCCESS}

-
-

{L_RETURN_TO_EXTENSION_LIST}

-
- - - diff --git a/phpBB/adm/style/acp_ext_enable.html b/phpBB/adm/style/acp_ext_enable.html deleted file mode 100644 index 8a4a35359e..0000000000 --- a/phpBB/adm/style/acp_ext_enable.html +++ /dev/null @@ -1,40 +0,0 @@ - - - - -

{L_EXTENSIONS_ADMIN}

- -

{L_EXTENSIONS_EXPLAIN}

-

{L_EXTENSION_ENABLE_EXPLAIN}

- - -
-

{L_MIGRATION_EXCEPTION_ERROR}

-

{MIGRATOR_ERROR}

-

{L_RETURN_TO_EXTENSION_LIST}

-
- -
-

{L_CONFIRM}

-

{L_CONFIRM_MESSAGE}

-
- -
-
- - -
-
- -
-

{L_EXTENSION_ENABLE_IN_PROGRESS}

-
- -
-

{L_EXTENSION_ENABLE_SUCCESS}

-
-

{L_RETURN_TO_EXTENSION_LIST}

-
- - - diff --git a/phpBB/adm/style/acp_ext_list.html b/phpBB/adm/style/acp_ext_list.html index 5375342a5e..30caed313f 100644 --- a/phpBB/adm/style/acp_ext_list.html +++ b/phpBB/adm/style/acp_ext_list.html @@ -42,8 +42,8 @@ - - + + class="hidden"> {L_EXTENSIONS_ENABLED} @@ -59,17 +59,12 @@ {L_DETAILS} - - title="{enabled.actions.L_ACTION_EXPLAIN}" style="color: {enabled.actions.COLOR};">{enabled.actions.L_ACTION} -  |  - + - - - + class="hidden"> {L_EXTENSIONS_DISABLED} @@ -87,14 +82,10 @@ {L_DETAILS} - - title="{disabled.actions.L_ACTION_EXPLAIN}" style="color: {disabled.actions.COLOR};">{disabled.actions.L_ACTION} -  |  - + - diff --git a/phpBB/adm/style/admin.js b/phpBB/adm/style/admin.js index 551c78a4a3..b7c25b7c86 100644 --- a/phpBB/adm/style/admin.js +++ b/phpBB/adm/style/admin.js @@ -228,6 +228,64 @@ function parse_document(container) }); } +/** + * Extension actions helper functions + */ +function move_to_enabled(element) +{ + var disabled_header = document.querySelector('#ext_disabled_header'); + disabled_header.parentNode.insertBefore(element, disabled_header); + element.classList.remove('ext_disabled'); + element.classList.add('ext_enabled'); +} +function move_to_disabled(element) +{ + var table_body = document.querySelector('#ext_disabled_header').parentNode; + table_body.appendChild(element); + element.classList.remove('ext_enabled'); + element.classList.add('ext_disabled'); +} +function set_actions(container, actions) { + container.innerHTML = ''; + for (var i = 0; i < actions.length; i++) { + var a = document.createElement('a'); + a.href = actions[i].U_ACTION.split('&').join('&'); // replace all occurances + a.title = actions[i].L_ACTION_EXPLAIN; + if (actions[i].COLOR) { + a.style = actions[i].COLOR; + } + a.innerHTML = actions[i].L_ACTION; + // ajaxify this action as well + phpbb.ajaxify({ + selector: a, + refresh: false, + callback: actions[i].ACTION_AJAX + }); + + container.appendChild(a); + + if (i < actions.length - 1) { + container.innerHTML += ' | '; + } + } +} +function show_enabled_header() { + document.querySelector('#ext_enabled_header').classList.remove('hidden'); +} +function show_disabled_header() { + document.querySelector('#ext_disabled_header').classList.remove('hidden'); +} +function hide_disabled_header_if_empty() { + if (!document.querySelector('.ext_disabled')) { + document.querySelector('#ext_disabled_header').classList.add('hidden'); + } +} +function hide_enabled_header_if_empty() { + if (!document.querySelector('.ext_enabled')) { + document.querySelector('#ext_enabled_header').classList.add('hidden'); + } +} + /** * Run onload functions */ diff --git a/phpBB/adm/style/ajax.js b/phpBB/adm/style/ajax.js index 895bb056e5..644e74bef7 100644 --- a/phpBB/adm/style/ajax.js +++ b/phpBB/adm/style/ajax.js @@ -157,6 +157,34 @@ phpbb.addAjaxCallback('row_delete', function(res) { } }); +/** + * Callbacks for extension actions + */ +phpbb.addAjaxCallback('ext_enable', function(res) { + if (res.EXT_ENABLE_SUCCESS) { + move_to_enabled(this.parentNode.parentNode); + set_actions(this.parentNode, res.ACTIONS); + show_enabled_header(); + hide_disabled_header_if_empty(); + } +}); +phpbb.addAjaxCallback('ext_delete_data', function(res) { + if (res.EXT_DELETE_DATA_SUCCESS) { + move_to_disabled(this.parentNode.parentNode); + set_actions(this.parentNode, res.ACTIONS); + show_disabled_header(); + hide_enabled_header_if_empty(); + } +}); +phpbb.addAjaxCallback('ext_disable', function(res) { + if (res.EXT_DISABLE_SUCCESS) { + move_to_disabled(this.parentNode.parentNode); + set_actions(this.parentNode, res.ACTIONS); + show_disabled_header(); + hide_enabled_header_if_empty(); + } +}); + /** * Handler for submitting permissions form in chunks * This call will submit permissions forms in chunks of 5 fieldsets. diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index bfd2066274..4cfbe9a17c 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -220,13 +220,19 @@ class acp_extensions redirect($this->u_action); } - $this->tpl_name = 'acp_ext_enable'; - - $this->template->assign_vars(array( - 'PRE' => true, - 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_ENABLE_CONFIRM', $md_manager->get_metadata('display-name')), - 'U_ENABLE' => $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name), - )); + if (confirm_box(true)) + { + redirect($this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name)); + } + else + { + confirm_box(false, $this->user->lang('EXTENSION_ENABLE_CONFIRM', $md_manager->get_metadata('display-name')), build_hidden_fields(array( + 'i' => $id, + 'mode' => $mode, + 'action' => 'enable_pre', + 'ext_name' => $ext_name, + ))); + } break; case 'enable': @@ -253,9 +259,8 @@ class acp_extensions // Are we approaching the time limit? If so we want to pause the update and continue after refreshing if ((time() - $start_time) >= $safe_time_limit) { - $this->template->assign_var('S_NEXT_STEP', true); - meta_refresh(0, $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('enable.' . $ext_name)); + trigger_error('EXTENSION_ENABLE_IN_PROGRESS', E_USER_NOTICE); } } @@ -271,14 +276,22 @@ class acp_extensions } catch (\phpbb\db\migration\exception $e) { - $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); + trigger_error($this->user->lang('MIGRATION_EXCEPTION_ERROR', $e->getLocalisedMessage($this->user)), E_USER_WARNING); } - $this->tpl_name = 'acp_ext_enable'; + if ($this->request->is_ajax()) + { + $actions = $this->output_actions('enabled', [ + 'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . urlencode($ext_name), + ]); - $this->template->assign_vars(array( - 'U_RETURN' => $this->u_action . '&action=list', - )); + $json_response = new \phpbb\json_response; + $json_response->send(array( + 'EXT_ENABLE_SUCCESS' => true, + 'ACTIONS' => $actions, + )); + } + trigger_error($this->user->lang('EXTENSION_ENABLE_SUCCESS') . adm_back_link($this->u_action), E_USER_NOTICE); break; case 'disable_pre': @@ -287,13 +300,19 @@ class acp_extensions redirect($this->u_action); } - $this->tpl_name = 'acp_ext_disable'; - - $this->template->assign_vars(array( - 'PRE' => true, - 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DISABLE_CONFIRM', $md_manager->get_metadata('display-name')), - 'U_DISABLE' => $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name), - )); + if (confirm_box(true)) + { + redirect($this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name)); + } + else + { + confirm_box(false, $this->user->lang('EXTENSION_DISABLE_CONFIRM', $md_manager->get_metadata('display-name')), build_hidden_fields(array( + 'i' => $id, + 'mode' => $mode, + 'action' => 'disable_pre', + 'ext_name' => $ext_name, + ))); + } break; case 'disable': @@ -310,15 +329,25 @@ class acp_extensions $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('disable.' . $ext_name)); + trigger_error('EXTENSION_DISABLE_IN_PROGRESS', E_USER_NOTICE); } } $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_DISABLE', time(), array($ext_name)); - $this->tpl_name = 'acp_ext_disable'; + if ($this->request->is_ajax()) + { + $actions = $this->output_actions('disabled', [ + 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . urlencode($ext_name), + 'DELETE_DATA' => $this->u_action . '&action=delete_data_pre&ext_name=' . urlencode($ext_name), + ]); - $this->template->assign_vars(array( - 'U_RETURN' => $this->u_action . '&action=list', - )); + $json_response = new \phpbb\json_response; + $json_response->send(array( + 'EXT_DISABLE_SUCCESS' => true, + 'ACTIONS' => $actions, + )); + } + trigger_error($this->user->lang('EXTENSION_DISABLE_SUCCESS') . adm_back_link($this->u_action), E_USER_NOTICE); break; case 'delete_data_pre': @@ -326,13 +355,20 @@ class acp_extensions { redirect($this->u_action); } - $this->tpl_name = 'acp_ext_delete_data'; - $this->template->assign_vars(array( - 'PRE' => true, - 'L_CONFIRM_MESSAGE' => $this->user->lang('EXTENSION_DELETE_DATA_CONFIRM', $md_manager->get_metadata('display-name')), - 'U_PURGE' => $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name), - )); + if (confirm_box(true)) + { + redirect($this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name)); + } + else + { + confirm_box(false, $this->user->lang('EXTENSION_DELETE_DATA_CONFIRM', $md_manager->get_metadata('display-name')), build_hidden_fields(array( + 'i' => $id, + 'mode' => $mode, + 'action' => 'delete_data_pre', + 'ext_name' => $ext_name, + ))); + } break; case 'delete_data': @@ -351,20 +387,29 @@ class acp_extensions $this->template->assign_var('S_NEXT_STEP', true); meta_refresh(0, $this->u_action . '&action=delete_data&ext_name=' . urlencode($ext_name) . '&hash=' . generate_link_hash('delete_data.' . $ext_name)); + trigger_error('EXTENSION_DELETE_DATA_IN_PROGRESS', E_USER_NOTICE); } } $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EXT_PURGE', time(), array($ext_name)); } catch (\phpbb\db\migration\exception $e) { - $this->template->assign_var('MIGRATOR_ERROR', $e->getLocalisedMessage($this->user)); + trigger_error($this->user->lang('MIGRATION_EXCEPTION_ERROR', $e->getLocalisedMessage($this->user)), E_USER_WARNING); } - $this->tpl_name = 'acp_ext_delete_data'; + if ($this->request->is_ajax()) + { + $actions = $this->output_actions('disabled', [ + 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . urlencode($ext_name), + ]); - $this->template->assign_vars(array( - 'U_RETURN' => $this->u_action . '&action=list', - )); + $json_response = new \phpbb\json_response; + $json_response->send(array( + 'EXT_DELETE_DATA_SUCCESS' => true, + 'ACTIONS' => $actions, + )); + } + trigger_error($this->user->lang('EXTENSION_DELETE_DATA_SUCCESS') . adm_back_link($this->u_action), E_USER_NOTICE); break; case 'details': @@ -1003,9 +1048,11 @@ class acp_extensions * * @param string $block * @param array $actions + * @return array List of actions to be performed on the extension */ private function output_actions($block, $actions) { + $vars_ary = array(); foreach ($actions as $lang => $options) { $url = $options; @@ -1018,6 +1065,7 @@ class acp_extensions 'L_ACTION' => $this->user->lang('EXTENSION_' . $lang), 'L_ACTION_EXPLAIN' => (isset($this->user->lang['EXTENSION_' . $lang . '_EXPLAIN'])) ? $this->user->lang('EXTENSION_' . $lang . '_EXPLAIN') : '', 'U_ACTION' => $url, + 'ACTION_AJAX' => 'ext_' . strtolower($lang), ); if (isset($options['color'])) @@ -1026,7 +1074,11 @@ class acp_extensions } $this->template->assign_block_vars($block . '.actions', $vars); + + $vars_ary[] = $vars; } + + return $vars_ary; } /** diff --git a/phpBB/language/en/migrator.php b/phpBB/language/en/migrator.php index 8a82d40be5..03802549d2 100644 --- a/phpBB/language/en/migrator.php +++ b/phpBB/language/en/migrator.php @@ -46,7 +46,7 @@ $lang = array_merge($lang, array( 'MIGRATION_DATA_IN_PROGRESS' => 'Installing Data: %1$s; Time: %2$.2f seconds', 'MIGRATION_DATA_RUNNING' => 'Installing Data: %s.', 'MIGRATION_EFFECTIVELY_INSTALLED' => 'Migration already effectively installed (skipped): %s', - 'MIGRATION_EXCEPTION_ERROR' => 'Something went wrong during the request and an exception was thrown. The changes made before the error occurred were reversed to the best of our abilities, but you should check the board for errors.', + 'MIGRATION_EXCEPTION_ERROR' => 'Something went wrong during the request and an exception was thrown. The changes made before the error occurred were reversed to the best of our abilities, but you should check the board for errors:

%s', 'MIGRATION_NOT_FULFILLABLE' => 'The migration "%1$s" is not fulfillable, missing migration "%2$s".', 'MIGRATION_NOT_INSTALLED' => 'The migration "%s" is not installed.', 'MIGRATION_NOT_VALID' => '%s is not a valid migration.', diff --git a/tests/functional/extension_acp_test.php b/tests/functional/extension_acp_test.php index ce0f4911e3..68c33ae863 100644 --- a/tests/functional/extension_acp_test.php +++ b/tests/functional/extension_acp_test.php @@ -195,7 +195,7 @@ class phpbb_functional_extension_acp_test extends phpbb_functional_test_case $this->assertContainsLang('EXTENSION_ACTIONS', $crawler->filter('div.main thead')->text()); $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=delete_data_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); - $this->assertContains('Are you sure that you wish to delete the data associated with “phpBB Moo Extension”?', $crawler->filter('.errorbox')->text()); + $this->assertContains('Are you sure that you wish to delete the data associated with “phpBB Moo Extension”?', $crawler->filter('#main')->text()); } public function test_actions() @@ -206,7 +206,7 @@ class phpbb_functional_extension_acp_test extends phpbb_functional_test_case // Correctly submit the enable form $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); - $form = $crawler->selectButton('enable')->form(); + $form = $crawler->selectButton('confirm')->form(); $crawler = self::submit($form); $this->assertContainsLang('EXTENSION_ENABLE_SUCCESS', $crawler->filter('.successbox')->text()); @@ -216,7 +216,7 @@ class phpbb_functional_extension_acp_test extends phpbb_functional_test_case // Correctly submit the disable form $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); - $form = $crawler->selectButton('disable')->form(); + $form = $crawler->selectButton('confirm')->form(); $crawler = self::submit($form); $this->assertContainsLang('EXTENSION_DISABLE_SUCCESS', $crawler->filter('.successbox')->text()); @@ -226,7 +226,7 @@ class phpbb_functional_extension_acp_test extends phpbb_functional_test_case // Correctly submit the delete data form $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=delete_data_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); - $form = $crawler->selectButton('delete_data')->form(); + $form = $crawler->selectButton('confirm')->form(); $crawler = self::submit($form); $this->assertContainsLang('EXTENSION_DELETE_DATA_SUCCESS', $crawler->filter('.successbox')->text()); From 5cf88ea871afa9444f808bb9a2bb0e2b8e54056c Mon Sep 17 00:00:00 2001 From: Jakub Senko Date: Wed, 4 Jul 2018 14:56:36 +0200 Subject: [PATCH 2/2] [ticket/15664] Add support for multi step processes PHPBB3-15664 --- phpBB/adm/style/acp_ext_actions.html | 4 ++-- phpBB/adm/style/admin.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/adm/style/acp_ext_actions.html b/phpBB/adm/style/acp_ext_actions.html index b9c2febbe5..6f2341f381 100644 --- a/phpBB/adm/style/acp_ext_actions.html +++ b/phpBB/adm/style/acp_ext_actions.html @@ -1,6 +1,6 @@ {% for action in enabled.actions %} -{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} +{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} {% endfor %} {% for action in disabled.actions %} -{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} +{{ action.L_ACTION }}{% if not action.S_LAST_ROW %} | {% endif %} {% endfor %} \ No newline at end of file diff --git a/phpBB/adm/style/admin.js b/phpBB/adm/style/admin.js index b7c25b7c86..5fdd07356c 100644 --- a/phpBB/adm/style/admin.js +++ b/phpBB/adm/style/admin.js @@ -258,7 +258,7 @@ function set_actions(container, actions) { // ajaxify this action as well phpbb.ajaxify({ selector: a, - refresh: false, + refresh: true, callback: actions[i].ACTION_AJAX });