Skip to content

Commit

Permalink
Merge pull request #171 from saimaz/enable_profile_with_link
Browse files Browse the repository at this point in the history
enable profile via link
  • Loading branch information
saimaz committed Sep 22, 2016
2 parents c9a5133 + ef7784d commit b3d424c
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 45 deletions.
24 changes: 24 additions & 0 deletions Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
23 changes: 19 additions & 4 deletions EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,6 +39,10 @@ public function onKernelRequest(GetResponseEvent $event)
return;
}

// ...
$profiles = $this->cookie->getValue();

if (is_array($profiles)) {
$this->settingsManager->appendActiveProfilesList($profiles);
}
}
}
6 changes: 6 additions & 0 deletions Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
53 changes: 47 additions & 6 deletions Resources/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ $(document).ready(function () {
});
});



//Profile section
var profileTable = $('#profiles').DataTable( {
ajax: {
Expand Down Expand Up @@ -252,8 +250,11 @@ $(document).ready(function () {
"targets": 3,
"data": null,
"orderable": false,
"defaultContent": '<a class="copy-link btn btn-primary btn-xs" data-toggle="modal" data-target="#setting-edit">Copy link</a>&nbsp;' +
''
// "render": function(data, type, row) {
// return '<a class="copy-link btn btn-primary btn-xs" data-toggle="modal">Copy link</a>&nbsp;';
// },
"defaultContent":
'<a class="copy-link btn btn-primary btn-xs" data-toggle="modal">Copy link</a>&nbsp;'
} ]
} );

Expand All @@ -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: '<span>'+link+'</span>',
confirmButton: 'Close',
});
}
} );

});
2 changes: 1 addition & 1 deletion Resources/public/script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Resources/public/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b3d424c

Please sign in to comment.