Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public function dash(Request $request): View
} elseif ($treat_tags_as == 'tags') {
$data['apps'] = Item::with('parents')->where('type', 0)->pinned()->orderBy('order', 'asc')->get();
$data['all_apps'] = Item::where('type', 0)->orderBy('order', 'asc')->get();
$data['taglist'] = Item::where('id', 0)->orWhere(function ($query) {
$query->where('type', 1)->pinned();
})->orderBy('order', 'asc')->get();
$data['taglist'] = Item::taglist()->orderBy('order', 'asc')->get();
} else {
$data['apps'] = Item::whereHas('parents', function ($query) {
$query->where('id', 0);
Expand All @@ -70,9 +68,7 @@ public function dash(Request $request): View
} elseif ($treat_tags_as == 'tags') {
$data['apps'] = Item::with('parents')->where('type', 0)->pinned()->orderBy('order', 'asc')->get();
$data['all_apps'] = Item::where('type', 0)->orderBy('order', 'asc')->get();
$data['taglist'] = Item::where('id', 0)->orWhere(function ($query) {
$query->where('type', 1)->pinned();
})->orderBy('order', 'asc')->get();
$data['taglist'] = Item::taglist()->orderBy('order', 'asc')->get();
} else {
$data['apps'] = Item::whereHas('parents', function ($query) {
$query->where('id', 0);
Expand Down
11 changes: 11 additions & 0 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public function scopePinned(Builder $query): Builder
return $query->where('pinned', 1);
}

/**
* Scope a query to the tags shown in the dashboard taglist: the home
* dashboard (root tag, id 0) plus every pinned tag.
*/
public function scopeTaglist(Builder $query): Builder
{
return $query->where('id', 0)->orWhere(function (Builder $query) {
$query->where('type', 1)->pinned();
});
}

public static function checkConfig($config)
{
// die(print_r($config));
Expand Down
27 changes: 17 additions & 10 deletions app/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ public function getListValueAttribute()
if ($this->key === 'search_provider') {
$options = Search::providers()->pluck('name', 'id')->toArray();
} elseif ($this->key === 'default_tag') {
$options = [];
$tags = Item::where('type', 1)->where('id', '>', 0)->pinned()->orderBy('title', 'asc')->get();
foreach ($tags as $tag) {
$options[$tag->tag_url] = $tag->title;
}
$options = self::defaultTagOptions();
}
$value = (array_key_exists($this->value, $options))
? __($options[$this->value])
Expand Down Expand Up @@ -197,11 +193,7 @@ public function getEditValueAttribute()
if ($this->key === 'search_provider') {
$options = Search::providers()->pluck('name', 'id');
} elseif ($this->key === 'default_tag') {
$options = ['' => 'app.options.none'];
$tags = Item::where('type', 1)->where('id', '>', 0)->pinned()->orderBy('title', 'asc')->get();
foreach ($tags as $tag) {
$options[$tag->tag_url] = $tag->title;
}
$options = ['' => 'app.options.none'] + self::defaultTagOptions();
}
$value = '<select name="value" class="form-control">';
foreach ($options as $key => $opt) {
Expand Down Expand Up @@ -311,4 +303,19 @@ public static function user()
{
return User::currentUser();
}

/**
* Tags offered by the default_tag setting, keyed by their taglist slug:
* the home dashboard first, then the pinned tags by title.
*
* @return array<string, string>
*/
public static function defaultTagOptions(): array
{
return Item::taglist()
->get()
->sortBy(fn (Item $tag) => $tag->id === 0 ? '' : $tag->title)
->pluck('title', 'tag_url')
->toArray();
}
}
18 changes: 18 additions & 0 deletions tests/Feature/DashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public function test_dash_exposes_the_configured_default_tag(): void
$response->assertSee('data-default-tag="home-dashboard"', false);
}

public function test_dash_can_default_to_the_home_dashboard_tag(): void
{
$this->seed();

Setting::where('key', 'treat_tags_as')->update(['value' => 'tags']);
Setting::where('key', 'default_tag')->update(['value' => '0-dash']);

$this->addPinnedItemWithTitleToDB('Home Item');

$response = $this->get('/');

$response->assertStatus(200);
// The stored slug matches the chip and the tile class the JS filters on.
$response->assertSee('data-default-tag="0-dash"', false);
$response->assertSee('data-tag="tag-0-dash"', false);
$response->assertSee('class="item-container tag-0-dash"', false);
}

public function test_categories_mode_renders_sortable_category_and_item_markup(): void
{
$this->seed();
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/database/seeders/SettingsSeederTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,26 @@ public function test_default_tag_edit_value_lists_all_tags_and_a_none_option():

// The unpinned tag is excluded.
$this->assertStringNotContainsString('value="archive"', $editValue);

// The root tag (id 0) is what the taglist renders as the "Home
// Dashboard" chip, under the "0-dash" slug; its title is a
// translation key and is rendered translated, right after "none".
$this->assertStringContainsString('value="0-dash"', $editValue);
$this->assertStringContainsString('>' . __('app.dashboard') . '</option>', $editValue);
$this->assertLessThan(
strpos($editValue, 'value="home-dashboard"'),
strpos($editValue, 'value="0-dash"')
);
}

public function test_default_tag_list_value_shows_the_home_dashboard_when_selected(): void
{
$this->seed();

// list_value re-reads the stored value, so it has to be persisted.
Setting::where('key', 'default_tag')->update(['value' => '0-dash']);
$setting = Setting::where('key', 'default_tag')->first();

$this->assertSame(__('app.dashboard'), $setting->list_value);
}
}
Loading