You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-07-03 23:50:33 +02:00
Simplified default folder concept
This commit is contained in:
@ -52,21 +52,9 @@ class Folder extends BaseItem {
|
|||||||
public function save(Array $options = array()) {
|
public function save(Array $options = array()) {
|
||||||
$dirty = $this->getDirty();
|
$dirty = $this->getDirty();
|
||||||
|
|
||||||
// Handling of default folder is done in several steps:
|
|
||||||
// - If changing is_default to false and this is the only default folder - throw an exception.
|
|
||||||
// - Then save the folder
|
|
||||||
// - Then, if the folder was set to be the new default, set all the other folders to non-default.
|
|
||||||
|
|
||||||
if (isset($dirty['is_default'])) {
|
|
||||||
$defaultFolders = self::where('owner_id', '=', $this->owner_id)->where('is_default', '=', 1);
|
|
||||||
|
|
||||||
if (!$dirty['is_default'] && $defaultFolders->count() == 1 && self::defaultFolder($this->owner_id)->id == $this->id) {
|
|
||||||
throw new \Exception(sprintf('Cannot make folder %s non-default - there should be at least one default folder', BaseModel::hex($this->id)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$output = parent::save($options);
|
$output = parent::save($options);
|
||||||
|
|
||||||
|
// If the folder was set to be the new default, set all the other folders to non-default.
|
||||||
if ($output && isset($dirty['is_default'])) {
|
if ($output && isset($dirty['is_default'])) {
|
||||||
if (!!$dirty['is_default']) {
|
if (!!$dirty['is_default']) {
|
||||||
self::where('owner_id', '=', $this->owner_id)->where('id', '!=', $this->id)->update(array('is_default' => 0));
|
self::where('owner_id', '=', $this->owner_id)->where('id', '!=', $this->id)->update(array('is_default' => 0));
|
||||||
|
@ -12,7 +12,7 @@ class FolderTest extends BaseTestCase {
|
|||||||
Folder::truncate();
|
Folder::truncate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefault() {
|
public function testNewFolderBecomesDefault() {
|
||||||
$f1 = new Folder();
|
$f1 = new Folder();
|
||||||
$f1->is_default = true;
|
$f1->is_default = true;
|
||||||
$f1->owner_id = TestUtils::userId();
|
$f1->owner_id = TestUtils::userId();
|
||||||
@ -30,17 +30,4 @@ class FolderTest extends BaseTestCase {
|
|||||||
$this->assertTrue(!!$f2->is_default);
|
$this->assertTrue(!!$f2->is_default);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefaultException() {
|
|
||||||
$this->expectException(Exception::class);
|
|
||||||
|
|
||||||
$f1 = new Folder();
|
|
||||||
$f1->is_default = true;
|
|
||||||
$f1->owner_id = TestUtils::userId();
|
|
||||||
$f1->save();
|
|
||||||
|
|
||||||
$f1 = Folder::find($f1->id);
|
|
||||||
$f1->is_default = false;
|
|
||||||
$f1->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user