From 23fc89c30c1fc6bd3089bf2f5ca228c9e483952b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 14 Oct 2013 00:25:40 +0200 Subject: [PATCH] User safety settings --- public_html/media/css/core.css | 51 +++++++++++++++++++++++------- public_html/media/js/core.js | 26 +++++++++++++++ src/Controllers/PostController.php | 15 ++++++--- src/Controllers/UserController.php | 19 +++++++++++ src/Models/Model_User.php | 43 ++++++++++++++++++++++++- src/Views/layout-normal.phtml | 23 ++++++++++++-- src/Views/post-view.phtml | 2 +- 7 files changed, 157 insertions(+), 22 deletions(-) diff --git a/public_html/media/css/core.css b/public_html/media/css/core.css index 2c6e4d98..6b61ab38 100644 --- a/public_html/media/css/core.css +++ b/public_html/media/css/core.css @@ -31,7 +31,7 @@ body { color: black; } -#top-nav ul { +#top-nav ul.main-nav { margin: 0 -0.75em; padding: 0; list-style-type: none; @@ -41,36 +41,63 @@ body { margin: 0 1.5em; } -#top-nav li { +#top-nav li.main-nav-item { display: inline-block; } #top-nav li input, -#top-nav li a { +#top-nav li.main-nav-item a { color: black; display: inline-block; margin-bottom: 3px; text-decoration: none; } -#top-nav li a { +#top-nav li.main-nav-item a { padding: 0.2em 0.75em; outline: 0; } -#top-nav li.search { - background: white; - margin: 0 0.25em; - padding: 0.2em 0.5em; -} - -#top-nav li a:focus, -#top-nav li a:hover { +#top-nav li.main-nav-item a:focus, +#top-nav li.main-nav-item a:hover { color: firebrick; border-bottom: 3px solid firebrick; margin-bottom: 0; } +#top-nav li.safety { + float: right; +} +#top-nav li.safety ul { + list-style-type: none; + margin: 0; + padding: 0; +} +#top-nav li.safety li { + display: inline-block; +} +#top-nav li.safety a { + display: inline-block; + width: 20px; + height: 20px; + border: 1px solid black; +} +#top-nav li.safety span { + display: none; +} +#top-nav li.safety .safety-safe .enabled { background: lime; } +#top-nav li.safety .safety-safe .disabled { background: green; } +#top-nav li.safety .safety-sketchy .enabled { background: yellow; } +#top-nav li.safety .safety-sketchy .disabled { background: olive; } +#top-nav li.safety .safety-unsafe .enabled { background: red; } +#top-nav li.safety .safety-unsafe .disabled { background: maroon; } + +#top-nav li.search { + float: right; + background: white; + margin: 0 0.25em; + padding: 0.2em 0.5em; +} #top-nav li.search input { border: 0; } diff --git a/public_html/media/js/core.js b/public_html/media/js/core.js index 9ee9dd23..0dd503ee 100644 --- a/public_html/media/js/core.js +++ b/public_html/media/js/core.js @@ -14,3 +14,29 @@ if ($.when.all === undefined) return deferred; } } + +$(function() +{ + $('.safety a').click(function(e) + { + e.preventDefault(); + + var aDom = $(this); + if (aDom.hasClass('inactive')) + return; + aDom.addClass('inactive'); + + var url = $(this).attr('href') + '?json'; + $.get(url, function(data) + { + if (data['success']) + { + window.location.reload(); + } + else + { + alert(data['errorMessage']); + } + }); + }); +}); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 0c493721..0820ff32 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -79,13 +79,13 @@ class PostController if ($this->config->browsing->endlessScrolling) $this->context->scripts []= 'paginator-endless.js'; - #redirect requests in form of /posts/?query=... to canonical address + //redirect requests in form of /posts/?query=... to canonical address $formQuery = InputHelper::get('query'); if ($formQuery !== null) { $this->context->transport->searchQuery = $formQuery; if (strpos($formQuery, '/') !== false) - throw new SimpleException('Search query contains invalid characters.'); + throw new SimpleException('Search query contains invalid characters'); $url = \Chibi\UrlHelper::route('post', 'list', ['query' => urlencode($formQuery)]); \Chibi\UrlHelper::forward($url); return; @@ -102,19 +102,24 @@ class PostController { $dbQuery->from('post'); + + /* safety */ $allowedSafety = array_filter(PostSafety::getAll(), function($safety) { - return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)); + return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)) and + $this->context->user->hasEnabledSafety($safety); }); - //todo safety [user choice] - $dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')'); foreach ($allowedSafety as $s) $dbQuery->put($s); + + /* hidden */ if (!PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, 'hidden')) $dbQuery->andNot('hidden'); + + /* search tokens */ $tokens = array_filter(array_unique(explode(' ', $query)), function($x) { return $x != ''; }); if (count($tokens) > $this->config->browsing->maxSearchTokens) throw new SimpleException('Too many search tokens (maximum: ' . $this->config->browsing->maxSearchTokens . ')'); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 541daa85..407e1e53 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -19,4 +19,23 @@ class UserController $this->context->subTitle = $name; throw new SimpleException('Not implemented'); } + + /** + * @route /user/toggle-safety/{safety} + */ + public function toggleSafetyAction($safety) + { + if (!$this->context->loggedIn) + throw new SimpleException('Not logged in'); + + if (!in_array($safety, PostSafety::getAll())) + throw new SimpleExcetpion('Invalid safety'); + + $this->context->user->enableSafety($safety, + !$this->context->user->hasEnabledSafety($safety)); + + R::store($this->context->user); + + $this->context->transport->success = true; + } } diff --git a/src/Models/Model_User.php b/src/Models/Model_User.php index 1deb59ae..dad84910 100644 --- a/src/Models/Model_User.php +++ b/src/Models/Model_User.php @@ -1,7 +1,7 @@ email) ? $this->email @@ -10,4 +10,45 @@ class Model_User extends RedBean_SimpleModel $url = 'http://www.gravatar.com/avatar/' . $hash . '?s=' . $size . '&d=retro'; return $url; } + + public function getSetting($key) + { + $settings = json_decode($this->settings, true); + return isset($settings[$key]) + ? $settings[$key] + : null; + } + + public function setSetting($key, $value) + { + $settings = json_decode($this->settings, true); + $settings[$key] = $value; + $settings = json_encode($settings); + if (strlen($settings) > 200) + throw new SimpleException('Too much data'); + $this->settings = $settings; + } + + public function hasEnabledSafety($safety) + { + return $this->getSetting('safety-' . $safety) !== false; + } + + public function enableSafety($safety, $enabled) + { + if (!$enabled) + { + $this->setSetting('safety-' . $safety, false); + $anythingEnabled = false; + foreach (PostSafety::getAll() as $safety) + if (self::hasEnabledSafety($safety)) + $anythingEnabled = true; + if (!$anythingEnabled) + $this->setSetting('safety-' . PostSafety::Safe, true); + } + else + { + $this->setSetting('safety-' . $safety, true); + } + } } diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index 49c96d2f..9bca5f2b 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -18,7 +18,7 @@