use orm for settings profiles stuff

This commit is contained in:
Andrew Dolgov 2021-03-04 12:30:45 +03:00
parent 31b29e0a56
commit 30765805fd
2 changed files with 43 additions and 52 deletions

View File

@ -1427,66 +1427,61 @@ class Pref_Prefs extends Handler_Protected {
} }
function activateprofile() { function activateprofile() {
$_SESSION["profile"] = (int) clean($_REQUEST["id"]); $id = (int) $_REQUEST['id'] ?? 0;
// default value $profile = ORM::for_table('ttrss_settings_profiles')
if (!$_SESSION["profile"]) $_SESSION["profile"] = null; ->where('owner_uid', $_SESSION['uid'])
->find_one($id);
if ($profile) {
$_SESSION["profile"] = $id;
} else {
$_SESSION["profile"] = null;
}
} }
function remprofiles() { function remprofiles() {
$ids = explode(",", clean($_REQUEST["ids"])); $ids = $_REQUEST["ids"] ?? [];
foreach ($ids as $id) { ORM::for_table('ttrss_settings_profiles')
if ($_SESSION["profile"] != $id) { ->where('owner_uid', $_SESSION['uid'])
$sth = $this->pdo->prepare("DELETE FROM ttrss_settings_profiles WHERE id = ? AND ->where_in('id', $ids)
owner_uid = ?"); ->where_not_equal('id', $_SESSION['profile'] ?? 0)
$sth->execute([$id, $_SESSION['uid']]); ->delete_many();
}
}
} }
function addprofile() { function addprofile() {
$title = clean($_REQUEST["title"]); $title = clean($_REQUEST["title"]);
if ($title) { if ($title) {
$this->pdo->beginTransaction(); $profile = ORM::for_table('ttrss_settings_profiles')
->where('owner_uid', $_SESSION['uid'])
->where('title', $title)
->find_one();
$sth = $this->pdo->prepare("SELECT id FROM ttrss_settings_profiles if (!$profile) {
WHERE title = ? AND owner_uid = ?"); $profile = ORM::for_table('ttrss_settings_profiles')->create();
$sth->execute([$title, $_SESSION['uid']]);
if (!$sth->fetch()) { $profile->title = $title;
$profile->owner_uid = $_SESSION['uid'];
$sth = $this->pdo->prepare("INSERT INTO ttrss_settings_profiles (title, owner_uid) $profile->save();
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']]);
} }
$this->pdo->commit();
} }
} }
function saveprofile() { function saveprofile() {
$id = clean($_REQUEST["id"]); $id = (int)$_REQUEST["id"];
$title = clean($_REQUEST["title"]); $title = clean($_REQUEST["value"]);
if ($id == 0) { if ($title && $id) {
print __("Default profile"); $profile = ORM::for_table('ttrss_settings_profiles')
return; ->where('owner_uid', $_SESSION['uid'])
} ->find_one($id);
if ($title) { if ($profile) {
$sth = $this->pdo->prepare("UPDATE ttrss_settings_profiles $profile->title = $title;
SET title = ? WHERE id = ? AND $profile->save();
owner_uid = ?"); }
$sth->execute([$title, $id, $_SESSION['uid']]);
print $title;
} }
} }
@ -1494,18 +1489,19 @@ class Pref_Prefs extends Handler_Protected {
function getProfiles() { function getProfiles() {
$rv = []; $rv = [];
$sth = $this->pdo->prepare("SELECT title,id FROM ttrss_settings_profiles $profiles = ORM::for_table('ttrss_settings_profiles')
WHERE owner_uid = ? ORDER BY title"); ->where('owner_uid', $_SESSION['uid'])
$sth->execute([$_SESSION['uid']]); ->order_by_expr('title')
->find_many();
array_push($rv, ["title" => __("Default profile"), array_push($rv, ["title" => __("Default profile"),
"id" => 0, "id" => 0,
"active" => empty($_SESSION["profile"]) "active" => empty($_SESSION["profile"])
]); ]);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { foreach ($profiles as $profile) {
$row["active"] = isset($_SESSION["profile"]) && $_SESSION["profile"] == $row["id"]; $profile['active'] = ($_SESSION["profile"] ?? 0) == $profile->id;
array_push($rv, $row); array_push($rv, $profile->as_array());
}; };
print json_encode($rv); print json_encode($rv);

View File

@ -135,12 +135,7 @@ const Helpers = {
if (confirm(__("Remove selected profiles? Active and default profiles will not be removed."))) { if (confirm(__("Remove selected profiles? Active and default profiles will not be removed."))) {
Notify.progress("Removing selected profiles...", true); Notify.progress("Removing selected profiles...", true);
const query = { xhr.post("backend.php", {op: "pref-prefs", method: "remprofiles", "ids[]": sel_rows}, () => {
op: "pref-prefs", method: "remprofiles",
ids: sel_rows.toString()
};
xhr.post("backend.php", query, () => {
Notify.close(); Notify.close();
dialog.refresh(); dialog.refresh();
}); });