Skip to content

Commit

Permalink
Add renderRowButtons() into abstract DataTables provider
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed Jul 4, 2019
1 parent 9779e6f commit d1902cb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 39 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
CHANGELOG
=========

### master
### [3.4.0](https://github.com/webeweb/jquery-datatables-bundle/tree/v3.4.0) (2019-07-04)

- Add jQuery DataTables events
- Add renderRowButtons() into abstract DataTables provider

### [3.3.0](https://github.com/webeweb/jquery-datatables-bundle/tree/v3.3.0) (2019-06-14)

Expand Down
91 changes: 69 additions & 22 deletions Provider/AbstractDataTablesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function __construct(RouterInterface $router, TranslatorInterface $transl
$this->setTranslator($translator);
}

/**
* {@inheritDoc}
*/
public function getCSVExporter() {
return $this;
}

/**
* {@inheritDoc}
*/
public function getEditor() {
return $this;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -79,31 +93,13 @@ public function getOptions() {
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
* @deprecated since 3.4.0 use "WBW\Bundle\JQuery\DataTablesBundle\Provider\AbstractDataTablesProvider::renderRowButtons()" instead
*/
protected function renderButtons($entity, $editRoute, $deleteRoute = null, $enableDelete = true) {

$titles = [];
$titles[] = $this->getTranslator()->trans("label.edit", [], "WBWCoreBundle");
$titles[] = $this->getTranslator()->trans("label.delete", [], "WBWCoreBundle");

$actions = [];
$actions[] = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "pencil", "title" => $titles[0], "size" => "xs"]);
$actions[] = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "trash", "title" => $titles[1], "size" => "xs"]);

$routes = [];
$routes[] = $this->getRouter()->generate($editRoute, ["id" => $entity->getId()]);
$routes[] = $this->getRouter()->generate("wbw_jquery_datatables_delete", ["name" => $this->getName(), "id" => $entity->getId()]);
if (null !== $deleteRoute) {
$routes[1] = $this->getRouter()->generate($deleteRoute, ["id" => $entity->getId()]);
if (null === $deleteRoute && true === $enableDelete) {
$deleteRoute = "wbw_jquery_datatables_delete";
}

$links = [];
$links[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($actions[0], $routes[0]);
if (true === $enableDelete) {
$links[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($actions[1], $routes[1]);
}

return implode(" ", $links);
return $this->renderRowButtons($entity, $editRoute, $deleteRoute, null);
}

/**
Expand Down Expand Up @@ -144,6 +140,57 @@ protected function renderFloat($number, $decimals = 2, $decPoint = ".", $thousan
return number_format($number, $decimals, $decPoint, $thousandsSep);
}

/**
* Render the DataTables row buttons.
*
* @param object $entity The entity.
* @param string|null $editRoute The edit route.
* @param string|null $deleteRoute The delete route.
* @param string|null $showRoute The show route.
* @return string Returns the DataTables row buttons.
* @throws InvalidParameterException Throws an invalid parameter exception if a parameter is invalid.
* @throws RouteNotFoundException Throws a route not found exception if the route doesn't exist.
* @throws MissingMandatoryParametersException Throws a missing mandatory parameter exception if a mandatory exception is missing.
*/
protected function renderRowButtons($entity, $editRoute = null, $deleteRoute = null, $showRoute = null) {

$output = [];

$titles = [];
$titles[] = $this->getTranslator()->trans("label.edit", [], "WBWJQueryDataTablesBundle");
$titles[] = $this->getTranslator()->trans("label.delete", [], "WBWJQueryDataTablesBundle");
$titles[] = $this->getTranslator()->trans("label.show", [], "WBWJQueryDataTablesBundle");

$buttons = [];
$buttons[] = $this->getButtonTwigExtension()->bootstrapButtonDefaultFunction(["icon" => "fa:pen", "title" => $titles[0], "size" => "xs"]);
$buttons[] = $this->getButtonTwigExtension()->bootstrapButtonDangerFunction(["icon" => "fa:trash", "title" => $titles[1], "size" => "xs"]);
$buttons[] = $this->getButtonTwigExtension()->bootstrapButtonInfoFunction(["icon" => "fa:eye", "title" => $titles[2], "size" => "xs"]);

$urls = [];

if (null !== $editRoute) {

$urls[] = $this->getRouter()->generate($editRoute, ["id" => $entity->getId()]);
$output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[0], $urls[0]);
}

if (null !== $deleteRoute) {

$args = "wbw_jquery_datatables_delete" === $deleteRoute ? ["name" => $this->getName()] : [];

$urls[] = $this->getRouter()->generate($deleteRoute, array_merge($args, ["id" => $entity->getId()]));
$output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[1], $urls[1]);
}

if (null !== $showRoute) {

$urls[] = $this->getRouter()->generate($showRoute, ["id" => $entity->getId()]);
$output[] = $this->getButtonTwigExtension()->bootstrapButtonLinkFilter($buttons[2], $urls[2]);
}

return implode(" ", $output);
}

/**
* Wrap a content.
*
Expand Down
5 changes: 5 additions & 0 deletions Resources/translations/WBWJQueryDataTablesBundle.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ DataTablesController:
danger: "Record not found"
success: "Successful editing"
warning: "Failed editing"

label:
delete: "Delete"
edit: "Edit"
show: "Show"
5 changes: 5 additions & 0 deletions Resources/translations/WBWJQueryDataTablesBundle.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ DataTablesController:
danger: "Enregistrement non trouvé"
success: "Edition réussie"
warning: "Edition échouée"

label:
delete: "Supprimer"
edit: "Editer"
show: "Voir"
21 changes: 7 additions & 14 deletions Tests/Fixtures/Provider/TestDataTablesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,13 @@
*/
class TestDataTablesProvider extends AbstractDataTablesProvider {

/**
*{@inheritDoc}
*/
public function getCSVExporter() {
return null;
}

/**
*{@inheritDoc}
*/
public function getColumns() {
return null;
}

/**
*{@inheritDoc}
*/
public function getEditor() {
return null;
}

/**
*{@inheritDoc}
*/
Expand Down Expand Up @@ -114,6 +100,13 @@ public function renderRow($dtRow, $entity, $rowNumber) {
return null;
}

/**
*{@inheritDoc}
*/
public function renderRowButtons($entity, $editRoute = null, $deleteRoute = null, $showRoute = null) {
return parent::renderRowButtons($entity, $editRoute, $deleteRoute, $showRoute);
}

/**
*{@inheritDoc}
*/
Expand Down
48 changes: 46 additions & 2 deletions Tests/Provider/AbstractDataTablesProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ public function testConstructor() {
$this->assertSame($this->translator, $obj->getTranslator());
}

/**
* Tests the getCSVExporter() method.
*
* @return void
*/
public function testGetCSVExporter() {

$obj = new TestDataTablesProvider($this->router, $this->translator, $this->buttonTwigExtension);

$this->assertSame($obj, $obj->getCSVExporter());
}

/**
* Tests the getEditor() method.
*
* @return void
*/
public function testGetEditor() {

$obj = new TestDataTablesProvider($this->router, $this->translator, $this->buttonTwigExtension);

$this->assertSame($obj, $obj->getEditor());
}

/**
* Tests the getMethod() method.
*
Expand Down Expand Up @@ -106,9 +130,9 @@ public function testRenderButtons() {
$obj = new TestDataTablesProvider($this->router, $this->translator, $this->buttonTwigExtension);

$res = <<< EOT
<a class="btn btn-default btn-xs" title="label.edit" href="editRoute" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a> <a class="btn btn-danger btn-xs" title="label.delete" href="deleteRoute" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
<a class="btn btn-default btn-xs" title="label.edit" href="editRoute" data-toggle="tooltip" data-placement="top"><i class="fa fa-pen"></i></a> <a class="btn btn-danger btn-xs" title="label.delete" href="wbw_jquery_datatables_delete" data-toggle="tooltip" data-placement="top"><i class="fa fa-trash"></i></a>
EOT;
$this->assertEquals($res, $obj->renderButtons(new Employee(), "editRoute", "deleteRoute"));
$this->assertEquals($res, $obj->renderButtons(new Employee(), "editRoute"));
}

/**
Expand Down Expand Up @@ -156,6 +180,26 @@ public function testRenderFloat() {
$this->assertEquals("1 000,000", $obj->renderFloat(1000, 3, ",", " "));
}

/**
* Tests the renderRowButtons() method.
*
* @return void
*/
public function testRenderRowButtons() {

// Set the Router mock.
$this->router->expects($this->any())->method("generate")->willReturnCallback(function($name, $parameters = [], $referenceType = RouterInterface::ABSOLUTE_PATH) {
return $name;
});

$obj = new TestDataTablesProvider($this->router, $this->translator, $this->buttonTwigExtension);

$res = <<< EOT
<a class="btn btn-default btn-xs" title="label.edit" href="editRoute" data-toggle="tooltip" data-placement="top"><i class="fa fa-pen"></i></a> <a class="btn btn-danger btn-xs" title="label.delete" href="deleteRoute" data-toggle="tooltip" data-placement="top"><i class="fa fa-trash"></i></a> <a class="btn btn-info btn-xs" title="label.show" href="showRoute" data-toggle="tooltip" data-placement="top"><i class="fa fa-eye"></i></a>
EOT;
$this->assertEquals($res, $obj->renderRowButtons(new Employee(), "editRoute", "deleteRoute", "showRoute"));
}

/**
* Tests the wrapContent() method.
*
Expand Down

0 comments on commit d1902cb

Please sign in to comment.