From ef7784de06bad6de2794b107fb389ec2b2676f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= Date: Wed, 21 Sep 2016 17:18:40 +0300 Subject: [PATCH] enable profile via link --- Controller/SettingsController.php | 24 ++++++ EventListener/RequestListener.php | 23 ++++- Resources/config/routing.yml | 6 ++ Resources/config/services.yml | 2 +- Resources/js/script.js | 53 ++++++++++-- Resources/public/script.js | 2 +- Resources/public/style.css | 2 +- Resources/public/style.css.map | 2 +- Resources/sass/style.scss | 6 ++ Resources/views/Profiles/list.html.twig | 3 +- .../views/Settings/enableProfile.html.twig | 70 +++++++++++++++ Resources/views/Settings/list.html.twig | 2 +- Resources/views/base.html.twig | 4 +- Service/SettingsManager.php | 86 +++++++++++++------ Tests/Unit/Service/SettingsManagerTest.php | 9 +- 15 files changed, 249 insertions(+), 45 deletions(-) create mode 100644 Resources/views/Settings/enableProfile.html.twig diff --git a/Controller/SettingsController.php b/Controller/SettingsController.php index 417e041..ae5bee3 100644 --- a/Controller/SettingsController.php +++ b/Controller/SettingsController.php @@ -21,6 +21,30 @@ */ class SettingsController extends Controller { + + public function enableProfileAction($key) + { + $profileName = $key; + + $cookie = $this->get('ongr_settings.cookie.active_profiles'); + $currentActiveProfiles = $cookie->getValue(); + + if (is_array($currentActiveProfiles) && !array_intersect($currentActiveProfiles, [$profileName])) { + $currentActiveProfiles[] = $profileName; + $cookie->setValue($currentActiveProfiles); + } else { + $cookie->setValue([$profileName]); + } + + $settingsManager = $this->get('ongr_settings.settings_manager'); + $settings = $settingsManager->getProfileSettings($profileName); + + return $this->render('ONGRSettingsBundle:Settings:enableProfile.html.twig', [ + 'profile' => $profileName, + 'profiles' => $settings, + ]); + } + /** * Renders settings list page. * diff --git a/EventListener/RequestListener.php b/EventListener/RequestListener.php index 25b1b0c..fc06d3c 100644 --- a/EventListener/RequestListener.php +++ b/EventListener/RequestListener.php @@ -11,15 +11,26 @@ namespace ONGR\SettingsBundle\EventListener; +use ONGR\CookiesBundle\Cookie\Model\GenericCookie; +use ONGR\SettingsBundle\Service\SettingsManager; use Symfony\Component\HttpKernel\Event\GetResponseEvent; class RequestListener { - private $cache; + /** + * @var GenericCookie + */ + private $cookie; - public function __construct($cache) + /** + * @var SettingsManager + */ + private $settingsManager; + + public function __construct($cookie, $settingsManager) { - $this->cache = $cache; + $this->cookie = $cookie; + $this->settingsManager = $settingsManager; } public function onKernelRequest(GetResponseEvent $event) @@ -28,6 +39,10 @@ public function onKernelRequest(GetResponseEvent $event) return; } - // ... + $profiles = $this->cookie->getValue(); + + if (is_array($profiles)) { + $this->settingsManager->appendActiveProfilesList($profiles); + } } } diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 08108b3..a39ab30 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -5,6 +5,12 @@ ongr_settings_home: route: ongr_settings_settings permanent: true +ongr_settings_enable_profile: + path: /enable/{key} + defaults: { _controller: ONGRSettingsBundle:Settings:enableProfile } + options: + expose: true + ongr_settings_search_page: path: /settings/search methods: [GET] diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 6debc98..ad5c21d 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -35,6 +35,6 @@ services: ongr_settings.request_listener: class: ONGR\SettingsBundle\EventListener\RequestListener - arguments: ["@ong_settings.cache_provider"] + arguments: ["@ongr_settings.cookie.active_profiles", "@ongr_settings.settings_manager"] tags: - { name: kernel.event_listener, event: kernel.request } \ No newline at end of file diff --git a/Resources/js/script.js b/Resources/js/script.js index 80f6cd6..a195af9 100644 --- a/Resources/js/script.js +++ b/Resources/js/script.js @@ -206,8 +206,6 @@ $(document).ready(function () { }); }); - - //Profile section var profileTable = $('#profiles').DataTable( { ajax: { @@ -252,8 +250,11 @@ $(document).ready(function () { "targets": 3, "data": null, "orderable": false, - "defaultContent": 'Copy link ' + - '' + // "render": function(data, type, row) { + // return 'Copy link '; + // }, + "defaultContent": + 'Copy link ' } ] } ); @@ -266,8 +267,48 @@ $(document).ready(function () { $('#profiles tbody').on( 'click', 'a.copy-link', function (e) { e.preventDefault(); - $('#profile-list-error-message').html('Enabling profiles by link is not yet implemented.'); - $('#profile-list-error').show(); + var data = profileTable.row( $(this).parents('tr') ).data(); + var link = Routing.generate('ongr_settings_enable_profile', {key:data.name},true); + $('#copy-placeholder').text(link); + var range = document.createRange(); + var field = document.querySelector('#copy-placeholder'); + range.selectNode(field); + window.getSelection().addRange(range); + try { + var success = document.execCommand('copy'); + if (success) { + noty({ + text: 'Link successfully copied to the clipboard.', + type: 'success', + layout : 'topRight', + theme: 'bootstrapTheme', + timeout: 10, + animation: { + open: 'animated fadeIn', + close: 'animated fadeOut', + } + }); + } else { + throw new Error("Cannot copy"); + } + } catch(err) { + noty({ + text: 'Something went wrong..', + type: 'error', + layout : 'topRight', + theme: 'bootstrapTheme', + timeout: 10, + animation: { + open: 'animated fadeIn', + close: 'animated fadeOut', + } + }); + $.alert({ + title: 'Here\'s the link:', + content: ''+link+'', + confirmButton: 'Close', + }); + } } ); }); \ No newline at end of file diff --git a/Resources/public/script.js b/Resources/public/script.js index ae69d45..b8f9a16 100644 --- a/Resources/public/script.js +++ b/Resources/public/script.js @@ -1 +1 @@ -$(document).ready(function(){function t(t,e){var a="";e&&(a='checked="checked"');var n='";$("#profiles-container .checkbox").append(n)}function e(e){$("#profiles-loader").show(),$("#profiles-container .checkbox").html(""),$.post(Routing.generate("ongr_settings_profiles_get_all"),function(a){$("#profiles-loader").hide(),a.forEach(function(a){$.inArray(a,e)>-1?t(a,!0):t(a,!1)})})}var a=$("#settings").DataTable({ajax:{url:Routing.generate("ongr_settings_search_page"),dataSrc:"documents"},stateSave:!0,columns:[{data:"name"},{data:"value"},{data:"description"},{data:"profile"},{}],columnDefs:[{targets:1,render:function(t,e,a){if("bool"==a.type){var n=$("