Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buttons 7: Backend classes for core view buttons #6545

Merged
merged 2 commits into from
Jul 24, 2024
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
79 changes: 79 additions & 0 deletions src/Panel/Ui/Buttons/LanguagesButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Toolkit\Str;

/**
* View button to switch content translation languages
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class LanguagesButton extends ViewButton
{
protected App $kirby;

public function __construct(
) {
$this->kirby = App::instance();

parent::__construct(
component: 'k-view-languages-button',
class: 'k-view-languages-button',
icon: 'translate',
options: $this->options(),
responsive: 'text',
text: Str::upper($this->kirby->language()?->code())
);
}

public function option(Language $language): array
{
return [
'text' => $language->name(),
'code' => $language->code(),
'current' => $language->code() === $this->kirby->language()?->code(),
];
}

public function options(): array
{
$languages = $this->kirby->languages();
$options = [];

if ($this->kirby->multilang() === false) {
return $options;
}

// add the primary/default language first
if ($default = $languages->default()) {
$options[] = $this->option($default);
$options[] = '-';
$languages = $languages->not($default);
}

// add all secondary languages after the separator
foreach ($languages as $language) {
$options[] = $this->option($language);
}

return $options;
}

public function render(): array|null
{
if ($this->kirby->multilang() === false) {
return null;
}

return parent::render();
}
}
48 changes: 48 additions & 0 deletions src/Panel/Ui/Buttons/PageStatusButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\Page;
use Kirby\Toolkit\I18n;

/**
* Status view button for pages
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class PageStatusButton extends ViewButton
{
public function __construct(
Page $page
) {
$status = $page->status();
$blueprint = $page->blueprint()->status()[$status] ?? null;
$disabled = $page->permissions()->cannot('changeStatus');
$title = I18n::translate('page.status') . ': ' . I18n::translate('page.status.' . $status);

if ($disabled === true) {
$title .= ' (' . I18n::translate('disabled') . ')';
}

parent::__construct(
class: 'k-view-status-button k-page-status-button',
dialog: $page->panel()->url(true) . '/changeStatus',
disabled: $disabled,
icon: 'status-' . $status,
style: '--icon-size: 15px',
text: $blueprint['label'] ?? $status,
title: $title,
theme: match($status) {
'draft' => 'negative-icon',
'unlisted' => 'info-icon',
'listed' => 'positive-icon'
}
);
}
}
31 changes: 31 additions & 0 deletions src/Panel/Ui/Buttons/PreviewButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Toolkit\I18n;

/**
* Preview view button
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class PreviewButton extends ViewButton
{
public function __construct(
public string|null $link
) {
parent::__construct(
class: 'k-view-preview-button',
icon: 'open',
link: $link,
target: '_blank',
title: I18n::translate('open')
);
}
}
32 changes: 32 additions & 0 deletions src/Panel/Ui/Buttons/SettingsButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\ModelWithContent;
use Kirby\Toolkit\I18n;

/**
* Settings view button for models
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class SettingsButton extends ViewButton
{
public function __construct(
ModelWithContent $model
) {
parent::__construct(
component: 'k-view-settings-button',
class: 'k-view-settings-button',
icon: 'cog',
options: $model->panel()->url(true),
title: I18n::translate('settings'),
);
}
}
93 changes: 93 additions & 0 deletions tests/Panel/Ui/Buttons/LanguagesButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\Language;
use Kirby\Panel\Areas\AreaTestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\LanguagesButton
* @covers ::__construct
*/
class LanguagesButtonTest extends AreaTestCase
{
/**
* @covers ::option
*/
public function testOption()
{
$language = new Language(['name' => 'Deutsch', 'code' => 'de']);
$button = new LanguagesButton();
$this->assertSame([
'text' => 'Deutsch',
'code' => 'de',
'current' => false
], $button->option($language));
}

/**
* @covers ::options
*/
public function testOptionsSingleLang()
{
$button = new LanguagesButton();
$this->assertSame([], $button->options());
}

/**
* @covers ::options
*/
public function testOptionsMultiLang()
{
$this->enableMultilang();
$this->installLanguages();

$button = new LanguagesButton();
$this->assertSame([
[
'text' => 'English',
'code' => 'en',
'current' => true
],
'-',
[
'text' => 'Deutsch',
'code' => 'de',
'current' => false
]
], $button->options());
}

/**
* @covers ::render
*/
public function testRenderSingleLang()
{
$button = new LanguagesButton();
$this->assertNull($button->render());
}

/**
* @covers ::props
* @covers ::render
*/
public function testRenderMultiLang()
{
$this->enableMultilang();
$this->installLanguages();

$button = new LanguagesButton();
$this->assertSame('k-view-languages-button', $button->component);
$this->assertSame('k-view-languages-button', $button->class);
$this->assertSame('translate', $button->icon);
$this->assertCount(3, $button->options);
$this->assertSame('text', $button->responsive);
$this->assertSame('EN', $button->text);

$render = $button->render();
$this->assertIsArray($render);
$this->assertSame('k-view-languages-button', $render['component']);
$this->assertIsArray($render['props']);
$this->assertIsArray($render['props']['options']);
}
}
50 changes: 50 additions & 0 deletions tests/Panel/Ui/Buttons/PageStatusButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Page;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PageStatusButton
*/
class PageStatusButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButtonDraftDisabled()
{

$page = new Page(['slug' => 'test', 'isDraft' => true]);
$button = new PageStatusButton($page);

$this->assertSame('k-view-button', $button->component);
$this->assertSame('k-view-status-button k-page-status-button', $button->class);
$this->assertSame('/pages/test/changeStatus', $button->dialog);
$this->assertTrue($button->disabled);
$this->assertSame('status-draft', $button->icon);
$this->assertTrue($button->responsive);
$this->assertSame('Draft', $button->text);
$this->assertSame('Status: Draft (Disabled)', $button->title);
$this->assertSame('negative-icon', $button->theme);
}

/**
* @covers ::__construct
*/
public function testButtonUnlisted()
{
App::instance()->impersonate('kirby');
$page = new Page(['slug' => 'test']);
$button = new PageStatusButton($page);

$this->assertFalse($button->disabled);
$this->assertSame('status-unlisted', $button->icon);
$this->assertTrue($button->responsive);
$this->assertSame('Unlisted', $button->text);
$this->assertSame('Status: Unlisted', $button->title);
$this->assertSame('info-icon', $button->theme);
}
}
27 changes: 27 additions & 0 deletions tests/Panel/Ui/Buttons/PreviewButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PreviewButton
*/
class PreviewButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButton()
{
$button = new PreviewButton(link: 'https://getkirby.com');

$this->assertSame('k-view-button', $button->component);
$this->assertSame('k-view-preview-button', $button->class);
$this->assertSame('open', $button->icon);
$this->assertSame('https://getkirby.com', $button->link);
$this->assertSame('_blank', $button->target);
$this->assertSame('Open', $button->title);
}

}
27 changes: 27 additions & 0 deletions tests/Panel/Ui/Buttons/SettingsButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\Page;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\SettingsButton
*/
class SettingsButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButton()
{
$page = new Page(['slug' => 'test']);
$button = new SettingsButton(model: $page);

$this->assertSame('k-view-settings-button', $button->component);
$this->assertSame('k-view-settings-button', $button->class);
$this->assertSame('cog', $button->icon);
$this->assertSame('/pages/test', $button->options);
$this->assertSame('Settings', $button->title);
}
}
Loading