From 1adb9bb6b643736706b746629b849df9cdf45e50 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 18 Feb 2021 11:54:22 +0300 Subject: [PATCH] profiles: use client dialog; move related methods to pref-prefs --- classes/pref/prefs.php | 165 ++++++++++++++++++++--------------------- classes/rpc.php | 75 +------------------ js/App.js | 6 ++ js/PrefHelpers.js | 62 ++++++++++++++-- 4 files changed, 144 insertions(+), 164 deletions(-) diff --git a/classes/pref/prefs.php b/classes/pref/prefs.php index 3bc8a9a9c..4a4aa45d6 100644 --- a/classes/pref/prefs.php +++ b/classes/pref/prefs.php @@ -1219,102 +1219,97 @@ class Pref_Prefs extends Handler_Protected { print json_encode(["value" => $value]); } - function editPrefProfiles() { - print "
"; + function activateprofile() { + $_SESSION["profile"] = (int) clean($_REQUEST["id"]); - print "
". - "" . __('Select').""; - print "
"; - print "
".__('All')."
"; - print "
".__('None')."
"; - print "
"; + // default value + if (!$_SESSION["profile"]) $_SESSION["profile"] = null; + } - print "
"; + function remprofiles() { + $ids = explode(",", clean($_REQUEST["ids"])); - print " -
"; + foreach ($ids as $id) { + if ($_SESSION["profile"] != $id) { + $sth = $this->pdo->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND + owner_uid = ?"); + $sth->execute([$id, $_SESSION['uid']]); + } + } + } - print "
"; + function addprofile() { + $title = clean($_REQUEST["title"]); + + if ($title) { + $this->pdo->beginTransaction(); + + $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles + WHERE title = ? AND owner_uid = ?"); + $sth->execute([$title, $_SESSION['uid']]); + + if (!$sth->fetch()) { + + $sth = $this->pdo->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid) + VALUES (?, ?)"); + + $sth->execute([$title, $_SESSION['uid']]); + + $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles WHERE + title = ? AND owner_uid = ?"); + $sth->execute([$title, $_SESSION['uid']]); + + if ($row = $sth->fetch()) { + $profile_id = $row['id']; + + if ($profile_id) { + Pref_Prefs::_init_user_prefs($_SESSION["uid"], $profile_id); + } + } + } + + $this->pdo->commit(); + } + } + + function saveprofile() { + $id = clean($_REQUEST["id"]); + $title = clean($_REQUEST["title"]); + + if ($id == 0) { + print __("Default profile"); + return; + } + + if ($title) { + $sth = $this->pdo->prepare("UPDATE ttrss_settings_profiles + SET title = ? WHERE id = ? AND + owner_uid = ?"); + + $sth->execute([$title, $id, $_SESSION['uid']]); + print $title; + } + } + + // TODO: this maybe needs to be unified with Public::getProfiles() + function getProfiles() { + $rv = []; $sth = $this->pdo->prepare("SELECT title,id FROM ttrss_settings_profiles WHERE owner_uid = ? ORDER BY title"); $sth->execute([$_SESSION['uid']]); - print "
"; + array_push($rv, ["title" => __("Default profile"), + "id" => 0, + "active" => empty($_SESSION["profile"]) + ]); - print "
"; - - print ""; - - print ""; # data-row-id='0' <-- no point, shouldn't be removed - - print ""; - - if (!isset($_SESSION["profile"])) { - $is_active = __("(active)"); - } else { - $is_active = ""; - } - - print ""; - - print ""; - - while ($line = $sth->fetch()) { - - $profile_id = $line["id"]; - - print ""; - - $edit_title = htmlspecialchars($line["title"]); - - print ""; - - if (isset($_SESSION["profile"]) && $_SESSION["profile"] == $line["id"]) { - $is_active = __("(active)"); - } else { - $is_active = ""; - } - - print ""; - - print ""; - } - - print "
" . __("Default profile") . " $is_active
" . $edit_title . - " - $is_active
"; - print "
"; - - print "
- - - "; - print "
"; - - print "
"; + while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { + $row["active"] = isset($_SESSION["profile"]) && $_SESSION["profile"] == $row["id"]; + array_push($rv, $row); + }; + print json_encode($rv); } private function _get_short_desc($pref_name) { diff --git a/classes/rpc.php b/classes/rpc.php index 6831d5667..0fd06da4d 100755 --- a/classes/rpc.php +++ b/classes/rpc.php @@ -2,84 +2,11 @@ class RPC extends Handler_Protected { function csrf_ignore($method) { - $csrf_ignored = array("completelabels", "saveprofile"); + $csrf_ignored = array("completelabels"); return array_search($method, $csrf_ignored) !== false; } - function setprofile() { - $_SESSION["profile"] = (int) clean($_REQUEST["id"]); - - // default value - if (!$_SESSION["profile"]) $_SESSION["profile"] = null; - } - - function remprofiles() { - $ids = explode(",", clean($_REQUEST["ids"])); - - foreach ($ids as $id) { - if ($_SESSION["profile"] != $id) { - $sth = $this->pdo->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND - owner_uid = ?"); - $sth->execute([$id, $_SESSION['uid']]); - } - } - } - - // Silent - function addprofile() { - $title = clean($_REQUEST["title"]); - - if ($title) { - $this->pdo->beginTransaction(); - - $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles - WHERE title = ? AND owner_uid = ?"); - $sth->execute([$title, $_SESSION['uid']]); - - if (!$sth->fetch()) { - - $sth = $this->pdo->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid) - VALUES (?, ?)"); - - $sth->execute([$title, $_SESSION['uid']]); - - $sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles WHERE - title = ? AND owner_uid = ?"); - $sth->execute([$title, $_SESSION['uid']]); - - if ($row = $sth->fetch()) { - $profile_id = $row['id']; - - if ($profile_id) { - Pref_Prefs::_init_user_prefs($_SESSION["uid"], $profile_id); - } - } - } - - $this->pdo->commit(); - } - } - - function saveprofile() { - $id = clean($_REQUEST["id"]); - $title = clean($_REQUEST["value"]); - - if ($id == 0) { - print __("Default profile"); - return; - } - - if ($title) { - $sth = $this->pdo->prepare("UPDATE ttrss_settings_profiles - SET title = ? WHERE id = ? AND - owner_uid = ?"); - - $sth->execute([$title, $id, $_SESSION['uid']]); - print $title; - } - } - function togglepref() { $key = clean($_REQUEST["key"]); set_pref($key, !get_pref($key)); diff --git a/js/App.js b/js/App.js index 9d8f6c275..ccfeb9024 100644 --- a/js/App.js +++ b/js/App.js @@ -42,6 +42,12 @@ const App = { cancel_dialog_tag: function(value, attributes = {}) { return this.button_tag(value, "", {...{onclick: "App.dialogOf(this).hide()"}, ...attributes}); }, + checkbox_tag: function(name, checked = false, value = "", attributes = {}, id = "") { + return `` + }, select_tag: function(name, value, values = [], attributes = {}, id = "") { return ` + ${App.FormFields.button_tag(__('Create profile'), "", {onclick: 'App.dialogOf(this).addProfile()'})} + + + +
+ +
+ + + ${reply.map((profile) => ` + + + + + `).join("")} +
+ ${App.FormFields.checkbox_tag("", false, "", {onclick: 'Tables.onRowChecked(this)'})} + + ${profile.id > 0 ? + `${profile.title} + + ` : `${profile.title}`} + ${profile.active ? __("(active)") : ""} +
+
+ +
+ `); }); }, execute: function () { @@ -143,7 +195,7 @@ const Helpers = { if (confirm(__("Activate selected profile?"))) { Notify.progress("Loading, please wait..."); - xhrPost("backend.php", {op: "rpc", method: "setprofile", id: sel_rows.toString()}, () => { + xhrPost("backend.php", {op: "pref-prefs", method: "activateprofile", id: sel_rows.toString()}, () => { window.location.reload(); }); }