Allow cloning the default profile.

This commit is contained in:
supahgreg 2025-11-13 00:10:21 +00:00
parent 1c7477c601
commit 36bbba2eb0
No known key found for this signature in database
2 changed files with 57 additions and 27 deletions

View File

@ -1403,33 +1403,51 @@ class Pref_Prefs extends Handler_Protected {
}
}
/**
* @todo this should result in an error on failures
*/
function cloneprofile(): void {
$old_profile = $_REQUEST["old_profile"] ?? 0;
$new_title = clean($_REQUEST["new_title"]);
$old_profile_id = $_REQUEST['old_profile'] ?? '';
if ($old_profile && $new_title) {
$new_profile = ORM::for_table('ttrss_settings_profiles')->create();
$new_profile->title = $new_title;
$new_profile->owner_uid = $_SESSION['uid'];
if (ctype_digit($old_profile_id))
$old_profile_id = (int) $old_profile_id;
else
return;
if ($new_profile->save()) {
$sth = $this->pdo->prepare("INSERT INTO ttrss_user_prefs2
(owner_uid, pref_name, profile, value)
SELECT
:uid,
pref_name,
:new_profile,
value
FROM ttrss_user_prefs2
WHERE owner_uid = :uid AND profile = :old_profile");
$new_title = clean($_REQUEST['new_title']);
$sth->execute([
"uid" => $_SESSION["uid"],
"new_profile" => $new_profile->id,
"old_profile" => $old_profile,
]);
}
}
if (!$new_title)
return;
$new_profile = ORM::for_table('ttrss_settings_profiles')->create();
$new_profile->title = $new_title;
$new_profile->owner_uid = $_SESSION['uid'];
if (!$new_profile->save())
return;
// NOTE: In 'ttrss_user_prefs2' the default profile is represented by 'profile' being null,
// but 0 is what gets used as its representative ID on the frontend.
$sth = $this->pdo->prepare('INSERT INTO ttrss_user_prefs2
(owner_uid, pref_name, profile, value)
SELECT
:uid,
pref_name,
:new_profile,
value
FROM ttrss_user_prefs2
WHERE owner_uid = :uid
AND ' . ($old_profile_id === 0 ? 'profile IS NULL' : 'profile = :old_profile_id'));
$params = [
'uid' => $_SESSION['uid'],
'new_profile' => $new_profile->id,
];
if ($old_profile_id !== 0)
$params['old_profile_id'] = $old_profile_id;
$sth->execute($params);
}
function remprofiles(): void {

View File

@ -216,7 +216,10 @@ class Prefs {
}
private function cache_all(int $owner_uid, ?int $profile_id): void {
if (!$profile_id) $profile_id = null;
// If explicitly null, 0 (e.g. from the frontend), or otherwise falsy,
// normalize to null to represent the default profile.
if (!$profile_id)
$profile_id = null;
// fill cache with defaults
$ref = new ReflectionClass(static::class);
@ -303,7 +306,10 @@ class Prefs {
}
private function _set(string $pref_name, bool|int|string $value, int $owner_uid, ?int $profile_id, bool $strip_tags = true): bool {
if (!$profile_id) $profile_id = null;
// If explicitly null, 0 (e.g. from the frontend), or otherwise falsy,
// normalize to null to represent the default profile.
if (!$profile_id)
$profile_id = null;
if ($profile_id && in_array($pref_name, self::_PROFILE_BLACKLIST))
return false;
@ -355,7 +361,10 @@ class Prefs {
if (Config::get_schema_version() < 141)
return;
if (!$profile_id) $profile_id = null;
// If explicitly null, 0 (e.g. from the frontend), or otherwise falsy,
// normalize to null to represent the default profile.
if (!$profile_id)
$profile_id = null;
if (!$this->_get(Prefs::_PREFS_MIGRATED, $owner_uid, $profile_id)) {
@ -394,7 +403,10 @@ class Prefs {
}
static function reset(int $owner_uid, ?int $profile_id): void {
if (!$profile_id) $profile_id = null;
// If explicitly null, 0 (e.g. from the frontend), or otherwise falsy,
// normalize to null to represent the default profile.
if (!$profile_id)
$profile_id = null;
$sth = Db::pdo()->prepare("DELETE FROM ttrss_user_prefs2
WHERE owner_uid = :uid AND pref_name != :mig_key AND