Skip to content

Commit

Permalink
Update abstract DataTables provider:
Browse files Browse the repository at this point in the history
- add renderActionButton()
- use Image renderer trait
  • Loading branch information
webeweb committed Mar 23, 2022
1 parent 2eb46f4 commit 26ae8bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions Provider/DataTables/AbstractDataTablesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use WBW\Bundle\EDMBundle\Provider\DocumentIconProviderTrait;
use WBW\Bundle\EDMBundle\Translation\TranslatorInterface;
use WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider as BaseDataTablesProvider;
use WBW\Library\Symfony\Renderer\Assets\ImageRendererTrait;
use WBW\Library\Types\Helper\StringHelper;

/**
Expand All @@ -34,6 +35,28 @@ abstract class AbstractDataTablesProvider extends BaseDataTablesProvider {
use KernelEventListenerTrait {
setKernelEventListener as public;
}
use ImageRendererTrait;

/**
* Render an action button.
*
* @param DocumentInterface $document The document.
* @param string $route The route.
* @param string $icon The icon.
* @param string $label The label.
* @param string $type The type.
* @return string Returns the rendered action button.
*/
private function renderActionButton(DocumentInterface $document, string $route, string $icon, string $label, string $type): string {

$method = sprintf("bootstrapButton%sFunction", $type);

$title = $this->translate($label);
$button = $this->getButtonTwigExtension()->$method(["icon" => $icon, "title" => $title, "size" => "xs"]);
$href = $this->getRouter()->generate($route, ["id" => $document->getId()]);

return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $href);
}

/**
* Render an action button "download".
Expand All @@ -42,12 +65,7 @@ abstract class AbstractDataTablesProvider extends BaseDataTablesProvider {
* @return string Returns the rendered action button "download".
*/
protected function renderActionButtonDownload(DocumentInterface $document): string {

$title = $this->translate("label.download");
$button = $this->getButtonTwigExtension()->bootstrapButtonInfoFunction(["icon" => "fa:download", "title" => $title, "size" => "xs"]);
$url = $this->getRouter()->generate("wbw_edm_document_download", ["id" => $document->getId()]);

return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
return $this->renderActionButton($document, "wbw_edm_document_download", "fa:download", "label.download", "Info");
}

/**
Expand All @@ -57,12 +75,7 @@ protected function renderActionButtonDownload(DocumentInterface $document): stri
* @return string Returns the rendered action button "index".
*/
protected function renderActionButtonIndex(DocumentInterface $document): string {

$title = $this->translate("label.index");
$button = $this->getButtonTwigExtension()->bootstrapButtonPrimaryFunction(["icon" => "fa:folder-open", "title" => $title, "size" => "xs"]);
$url = $this->getRouter()->generate("wbw_edm_document_index", ["id" => $document->getId()]);

return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
return $this->renderActionButton($document, "wbw_edm_document_index", "fa:folder-open", "label.index", "Primary");
}

/**
Expand All @@ -72,12 +85,7 @@ protected function renderActionButtonIndex(DocumentInterface $document): string
* @return string Returns the rendered action button "move".
*/
protected function renderActionButtonMove(DocumentInterface $document): string {

$title = $this->translate("label.move");
$button = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:arrows-alt", "title" => $title, "size" => "xs"]);
$url = $this->getRouter()->generate("wbw_edm_document_move", ["id" => $document->getId()]);

return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
return $this->renderActionButton($document, "wbw_edm_document_move", "fa:arrows-alt", "label.move", "Default");
}

/**
Expand All @@ -87,12 +95,7 @@ protected function renderActionButtonMove(DocumentInterface $document): string {
* @return string Returns the rendered action button "upload".
*/
protected function renderActionButtonUpload(DocumentInterface $document): string {

$title = $this->translate("label.upload");
$button = $this->getButtonTwigExtension()->bootstrapButtonSuccessFunction(["icon" => "fa:upload", "title" => $title, "size" => "xs"]);
$url = $this->getRouter()->generate("wbw_edm_dropzone_upload", ["id" => $document->getId()]);

return $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($button, $url);
return $this->renderActionButton($document, "wbw_edm_dropzone_upload", "fa:upload", "label.upload", "Success");
}

/**
Expand Down Expand Up @@ -126,8 +129,7 @@ protected function renderColumnActions(DocumentInterface $document): string {
*/
protected function renderColumnIcon(DocumentInterface $document): string {

$format = '<img src="%s" height="32px" />';
$output = sprintf($format, $this->getDocumentIconProvider()->getIconAsset($document));
$output = $this->renderImage($this->getDocumentIconProvider()->getIconAsset($document), null, null, "32px");

return AbstractTwigExtension::coreHtmlElement("span", $output, ["class" => "pull-left"]);
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Provider/DataTables/DocumentDataTablesProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function testRenderColumn(): void {

$col = $obj->getColumns();

$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/application-octet-stream.svg" height="32px" /></span>document.php', $obj->renderColumn($col[0], $document));
$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/application-octet-stream.svg" height="32px"/></span>document.php', $obj->renderColumn($col[0], $document));
$this->assertRegExp('/^<span class="pull-right">1[\.,]00 Kio<\/span>$/', $obj->renderColumn($col[1], $document));
$this->assertRegExp("/^[0-9\- :]{16}$/", $obj->renderColumn($col[2], $document));
$this->assertEquals("application/octet-stream", $obj->renderColumn($col[3], $document));
Expand Down Expand Up @@ -242,7 +242,7 @@ public function testRenderColumnWithDirectory(): void {

$col = $obj->getColumns();

$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/folder.svg" height="32px" /></span>document<br/><span class="font-italic">label.items_count</span>', $obj->renderColumn($col[0], $document));
$this->assertEquals('<span class="pull-left"><img src="/bundles/wbwedm/img/folder.svg" height="32px"/></span>document<br/><span class="font-italic">label.items_count</span>', $obj->renderColumn($col[0], $document));
$this->assertRegExp('/^<span class="pull-right">1[\.,]00 Kio<\/span>$/', $obj->renderColumn($col[1], $document));
$this->assertRegExp("/^[0-9\- :]{16}$/", $obj->renderColumn($col[2], $document));
$this->assertEquals("label.directory", $obj->renderColumn($col[3], $document));
Expand Down

0 comments on commit 26ae8bc

Please sign in to comment.