From 3c4bfb6c78d19451b8132f109e20149260f1b3ca Mon Sep 17 00:00:00 2001 From: sysadminstory Date: Sat, 22 Jan 2022 00:53:03 +0100 Subject: [PATCH 1/5] Initial fix for File Association preference This is a initial fix to permit user to change the file association preferences again. --- appinfo/info.xml | 4 ++ appinfo/routes.php | 3 +- js/settings.js | 2 +- lib/Controller/SettingsController.php | 74 ++++++++++++++++++++++ lib/Settings/Personal.php | 89 +++++++++++++++++++++++++++ lib/Settings/PersonalSection.php | 70 +++++++++++++++++++++ 6 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 lib/Controller/SettingsController.php create mode 100644 lib/Settings/Personal.php create mode 100644 lib/Settings/PersonalSection.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 3cfe617..bffaa21 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -48,4 +48,8 @@ See [README] for more exhaustive information on features and potential misfeatur + + OCA\Epubreader\Settings\Personal + OCA\Epubreader\Settings\PersonalSection + diff --git a/appinfo/routes.php b/appinfo/routes.php index b3fde19..eda152f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -10,7 +10,7 @@ * later. */ -$this->create('reader_personal_settings', 'ajax/personal.php')->actionInclude('epubreader/ajax/personal.php'); +//$this->create('reader_personal_settings', 'ajax/personal.php')->actionInclude('epubreader/ajax/personal.php'); return ['routes' => [ // Page @@ -36,5 +36,6 @@ return ['routes' => [ ['name' => 'preference#get', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'GET', 'defaults' => ['name' => '']], ['name' => 'preference#set', 'url' => '/preference', 'verb' => 'POST'], ['name' => 'preference#delete', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'DELETE'], + ['name' => 'settings#setPreference', 'url' => '/ajax/personal.php', 'verb' => 'POST'], ]]; diff --git a/js/settings.js b/js/settings.js index 92f0a82..c296899 100644 --- a/js/settings.js +++ b/js/settings.js @@ -9,7 +9,7 @@ window.addEventListener('DOMContentLoaded', function () { }; OC.msg.startSaving('#reader-personal .msg'); - $.post(OC.filePath('epubreader', 'ajax', 'personal.php'), data, readerSettings.afterSave); + $.post(OC.generateUrl('apps/epubreader/ajax/personal.php'), data, readerSettings.afterSave); }, afterSave : function(data){ OC.msg.finishedSaving('#reader-personal .msg', data); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php new file mode 100644 index 0000000..9564d3f --- /dev/null +++ b/lib/Controller/SettingsController.php @@ -0,0 +1,74 @@ +urlGenerator = $urlGenerator; + $this->preferenceService = $preferenceService; + } + + /** + * @brief return preference for $fileId + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @param string $scope + * @param int $fileId + * @param string $name if null, return all preferences for $scope + $fileId + * + * @return array|\OCP\AppFramework\Http\JSONResponse + */ + public function setPreference($scope, $fileId, $name) { + /*\OC_JSON::callCheck(); + \OC_JSON::checkLoggedIn();*/ + + $l = \OC::$server->getL10N('epubreader'); + + $EpubEnable = isset($_POST['EpubEnable']) ? $_POST['EpubEnable'] : 'false'; + $PdfEnable = isset($_POST['PdfEnable']) ? $_POST['PdfEnable'] : 'false'; + $CbxEnable = isset($_POST['CbxEnable']) ? $_POST['CbxEnable'] : 'false'; + + Config::set('epub_enable', $EpubEnable); + Config::set('pdf_enable', $PdfEnable); + Config::set('cbx_enable', $CbxEnable); + + \OC_JSON::success( + array( + 'data' => array('message'=> $l->t('Settings updated successfully.')) + ) + ); + } +} \ No newline at end of file diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php new file mode 100644 index 0000000..dbfccb1 --- /dev/null +++ b/lib/Settings/Personal.php @@ -0,0 +1,89 @@ +userId = $userId; + $this->configManager = $configManager; + } + + /** + * @return TemplateResponse returns the instance with all parameters set, ready to be rendered + * @since 9.1 + */ + public function getForm() + { + + $parameters = [ + 'EpubEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'epub_enable'), + 'PdfEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'pdf_enable'), + 'CbxEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'cbx_enable'), + ]; + return new TemplateResponse('epubreader', 'settings-personal', $parameters, ''); + } + + /** + * Print config section (ownCloud 10) + * + * @return TemplateResponse + */ + public function getPanel() + { + return $this->getForm(); + } + + /** + * @return string the section ID, e.g. 'sharing' + * @since 9.1 + */ + public function getSection() + { + return 'epubreader'; + } + + /** + * Get section ID (ownCloud 10) + * + * @return string + */ + public function getSectionID() + { + return 'epubreader'; + } + + /** + * @return int whether the form should be rather on the top or bottom of + * the admin section. The forms are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * + * E.g.: 70 + * @since 9.1 + */ + public function getPriority() + { + return 10; + } +} diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php new file mode 100644 index 0000000..0ce26e9 --- /dev/null +++ b/lib/Settings/PersonalSection.php @@ -0,0 +1,70 @@ +urlGenerator = $urlGenerator; + $this->l = $l; + } + + /** + * returns the relative path to an 16*16 icon describing the section. + * + * @returns string + */ + public function getIcon() + { + return $this->urlGenerator->imagePath('epubreader', 'app.svg'); + } + + /** + * returns the ID of the section. It is supposed to be a lower case string, + * + * @returns string + */ + public function getID() + { + return 'epubreader'; + } + + /** + * returns the translated name as it should be displayed + * + * @return string + */ + public function getName() + { + return $this->l->t('EPUB/CBZ/PDF ebook reader'); + } + + /** + * returns priority for positioning + * + * @return int + */ + public function getPriority() + { + return 100; + } +} \ No newline at end of file From 13d4946e19be9c3d047879db7c97001014508fb4 Mon Sep 17 00:00:00 2001 From: sysadminstory Date: Sat, 22 Jan 2022 01:55:57 +0100 Subject: [PATCH 2/5] Cleanup old files and update comments Change the settings url to be mor consistant with the others urls of the app. Better feedback if the saving of settings did work or not. Use the Framework to get the settings value, instead of using the $_POST array. --- ajax/personal.php | 35 --------------------------- appinfo/routes.php | 6 ++--- js/settings.js | 2 +- lib/Controller/SettingsController.php | 29 +++++++++------------- lib/Settings/PersonalSection.php | 2 +- 5 files changed, 17 insertions(+), 57 deletions(-) delete mode 100644 ajax/personal.php diff --git a/ajax/personal.php b/ajax/personal.php deleted file mode 100644 index 801090b..0000000 --- a/ajax/personal.php +++ /dev/null @@ -1,35 +0,0 @@ -getL10N('epubreader'); - -$EpubEnable = isset($_POST['EpubEnable']) ? $_POST['EpubEnable'] : 'false'; -$PdfEnable = isset($_POST['PdfEnable']) ? $_POST['PdfEnable'] : 'false'; -$CbxEnable = isset($_POST['CbxEnable']) ? $_POST['CbxEnable'] : 'false'; - -Config::set('epub_enable', $EpubEnable); -Config::set('pdf_enable', $PdfEnable); -Config::set('cbx_enable', $CbxEnable); - -\OC_JSON::success( - array( - 'data' => array('message'=> $l->t('Settings updated successfully.')) - ) -); - -exit(); - diff --git a/appinfo/routes.php b/appinfo/routes.php index eda152f..f70c426 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -10,8 +10,6 @@ * later. */ -//$this->create('reader_personal_settings', 'ajax/personal.php')->actionInclude('epubreader/ajax/personal.php'); - return ['routes' => [ // Page ['name' => 'page#showReader', 'url' => '/', 'verb' => 'GET'], @@ -36,6 +34,8 @@ return ['routes' => [ ['name' => 'preference#get', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'GET', 'defaults' => ['name' => '']], ['name' => 'preference#set', 'url' => '/preference', 'verb' => 'POST'], ['name' => 'preference#delete', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'DELETE'], - ['name' => 'settings#setPreference', 'url' => '/ajax/personal.php', 'verb' => 'POST'], + + // User Settings + ['name' => 'settings#setPreference', 'url' => '/settings/set', 'verb' => 'POST'], ]]; diff --git a/js/settings.js b/js/settings.js index c296899..171e675 100644 --- a/js/settings.js +++ b/js/settings.js @@ -9,7 +9,7 @@ window.addEventListener('DOMContentLoaded', function () { }; OC.msg.startSaving('#reader-personal .msg'); - $.post(OC.generateUrl('apps/epubreader/ajax/personal.php'), data, readerSettings.afterSave); + $.post(OC.generateUrl('apps/epubreader/settings/set'), data, readerSettings.afterSave); }, afterSave : function(data){ OC.msg.finishedSaving('#reader-personal .msg', data); diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 9564d3f..acf3520 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -17,6 +17,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Controller; use OCA\Epubreader\Service\PreferenceService; use OCA\Epubreader\Config; +use OCP\AppFramework\Http\JSONResponse; class SettingsController extends Controller { @@ -40,35 +41,29 @@ class SettingsController extends Controller { } /** - * @brief return preference for $fileId + * @brief set preference for file type association * * @NoAdminRequired - * @NoCSRFRequired * - * @param string $scope - * @param int $fileId - * @param string $name if null, return all preferences for $scope + $fileId + * @param int $EpubEnable + * @param int $EpubEnable + * @param int $CbxEnable * * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function setPreference($scope, $fileId, $name) { - /*\OC_JSON::callCheck(); - \OC_JSON::checkLoggedIn();*/ + public function setPreference($EpubEnable, $PdfEnable, $CbxEnable) { $l = \OC::$server->getL10N('epubreader'); - $EpubEnable = isset($_POST['EpubEnable']) ? $_POST['EpubEnable'] : 'false'; - $PdfEnable = isset($_POST['PdfEnable']) ? $_POST['PdfEnable'] : 'false'; - $CbxEnable = isset($_POST['CbxEnable']) ? $_POST['CbxEnable'] : 'false'; - Config::set('epub_enable', $EpubEnable); Config::set('pdf_enable', $PdfEnable); Config::set('cbx_enable', $CbxEnable); - \OC_JSON::success( - array( - 'data' => array('message'=> $l->t('Settings updated successfully.')) - ) - ); + $response = array( + 'data' => array('message'=> $l->t('Settings updated successfully.')), + 'status' => 'success' + ); + + return new JSONResponse($response); } } \ No newline at end of file diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php index 0ce26e9..17d8d64 100644 --- a/lib/Settings/PersonalSection.php +++ b/lib/Settings/PersonalSection.php @@ -65,6 +65,6 @@ class PersonalSection implements IIconSection */ public function getPriority() { - return 100; + return 20; } } \ No newline at end of file From 9f1250afa243e203f313bb985061291934ebaeb8 Mon Sep 17 00:00:00 2001 From: sysadminstory Date: Mon, 24 Jan 2022 00:40:57 +0100 Subject: [PATCH 3/5] Fix the settings controller parameters The Settings controller parameters were did not have a type, and prevented them from being automatically injected --- lib/Controller/SettingsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index acf3520..7b26a34 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -51,7 +51,7 @@ class SettingsController extends Controller { * * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function setPreference($EpubEnable, $PdfEnable, $CbxEnable) { + public function setPreference(string $EpubEnable, string $PdfEnable, string $CbxEnable) { $l = \OC::$server->getL10N('epubreader'); From 69d4859b717b193f4461aa6aa9ad666e637f9eb4 Mon Sep 17 00:00:00 2001 From: sysadminstory Date: Mon, 24 Jan 2022 22:53:10 +0100 Subject: [PATCH 4/5] Remove old settings file Remove the 'personal.php' file to remove the duplicate personal settings page --- appinfo/app.php | 1 - personal.php | 25 ------------------------- 2 files changed, 26 deletions(-) delete mode 100644 personal.php diff --git a/appinfo/app.php b/appinfo/app.php index 5d2e294..cf3b4ee 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -19,4 +19,3 @@ $l = \OC::$server->getL10N('epubreader'); \OCA\Epubreader\Hooks::register(); Util::addscript('epubreader', 'plugin'); -\OCP\App::registerPersonal('epubreader', 'personal'); diff --git a/personal.php b/personal.php deleted file mode 100644 index 262b8e6..0000000 --- a/personal.php +++ /dev/null @@ -1,25 +0,0 @@ -getL10N('epubreader'); - -$tmpl = new \OCP\Template('epubreader', 'settings-personal'); -$EpubEnable = Config::get('epub_enable', 'true'); -$PdfEnable = Config::get('pdf_enable', 'true'); -$CbxEnable = Config::get('cbx_enable', 'true'); -$tmpl->assign('EpubEnable', $EpubEnable); -$tmpl->assign('PdfEnable', $PdfEnable); -$tmpl->assign('CbxEnable', $CbxEnable); - -return $tmpl->fetchPage(); From 6eadc60b36ca3156eeb3c5f62670e735b1b4adbb Mon Sep 17 00:00:00 2001 From: sysadminstory Date: Tue, 22 Mar 2022 12:23:51 +0100 Subject: [PATCH 5/5] Update Route in personal.js It seems personal.js is used in the settings too, so i changed the URL there too. --- js/personal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/personal.js b/js/personal.js index a59a3be..171e675 100644 --- a/js/personal.js +++ b/js/personal.js @@ -9,7 +9,7 @@ window.addEventListener('DOMContentLoaded', function () { }; OC.msg.startSaving('#reader-personal .msg'); - $.post(OC.filePath('epubreader', 'lib', 'personal-back.php'), data, readerSettings.afterSave); + $.post(OC.generateUrl('apps/epubreader/settings/set'), data, readerSettings.afterSave); }, afterSave : function(data){ OC.msg.finishedSaving('#reader-personal .msg', data);