Skip to content

Commit

Permalink
AbstractPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
paveljanda committed May 15, 2019
1 parent 13071b5 commit a3b4ec1
Show file tree
Hide file tree
Showing 8 changed files with 1,236 additions and 1,073 deletions.
1 change: 1 addition & 0 deletions contributte-datagrid/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/_data
/vendor
/ftp-deployment
54 changes: 54 additions & 0 deletions contributte-datagrid/app/Presenters/AbstractPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Dibi\Connection;
use Dibi\Fluent;
use Dibi\Row;
use Nette\Application\UI\Presenter;
use Nette\Utils\ArrayHash;
use Ublaboo\DataGrid\DataGrid;

abstract class AbstractPresenter extends Presenter
{

/**
* @var Connection
* @inject
*/
public $dibiConnection;


/**
* @param mixed $id
*/
public function changeStatus($id, string $newStatus): void
{
$id = (int) $id;

if (in_array($newStatus, ['active', 'inactive', 'deleted'], true)) {
$data = ['status' => $newStatus];

$this->dibiConnection->update('users', $data)
->where('id = ?', $id)
->execute();
}

if ($this->isAjax()) {
$grid = $this['grid'];


if (!$grid instanceof DataGrid) {
throw new \UnexpectedValueException;
}

$grid->redrawItem($id);
$this->flashMessage('Status changed');
$this->redrawControl('flashes');
} else {
$this->redirect('this');
}
}
}
34 changes: 1 addition & 33 deletions contributte-datagrid/app/Presenters/ColumnsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Ublaboo\DataGrid\Column\ColumnStatus;
use Ublaboo\DataGrid\DataGrid;

final class ColumnsPresenter extends Presenter
final class ColumnsPresenter extends AbstractPresenter
{

/**
Expand Down Expand Up @@ -91,36 +91,4 @@ public function createComponentGrid(): DataGrid

return $grid;
}


/**
* @param mixed $id
*/
public function changeStatus($id, string $newStatus): void
{
$id = (int) $id;

if (in_array($newStatus, ['active', 'inactive', 'deleted'], true)) {
$data = ['status' => $newStatus];

$this->dibiConnection->update('users', $data)
->where('id = ?', $id)
->execute();
}

if ($this->isAjax()) {
$grid = $this['grid'];


if (!$grid instanceof DataGrid) {
throw new \UnexpectedValueException;
}

$grid->redrawItem($id);
$this->flashMessage('aaaa');
$this->redrawControl('flashes');
} else {
$this->redirect('this');
}
}
}
32 changes: 1 addition & 31 deletions contributte-datagrid/app/Presenters/FiltersPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Nette\Utils\ArrayHash;
use Ublaboo\DataGrid\DataGrid;

final class FiltersPresenter extends Presenter
final class FiltersPresenter extends AbstractPresenter
{

/**
Expand Down Expand Up @@ -71,34 +71,4 @@ public function createComponentGrid(): DataGrid

return $grid;
}


/**
* @param mixed $id
*/
public function changeStatus($id, string $newStatus): void
{
$id = (int) $id;

if (in_array($newStatus, ['active', 'inactive', 'deleted'], true)) {
$data = ['status' => $newStatus];

$this->dibiConnection->update('users', $data)
->where('id = ?', $id)
->execute();
}

if ($this->isAjax()) {
$grid = $this['grid'];


if (!$grid instanceof DataGrid) {
throw new \UnexpectedValueException;
}

$grid->redrawItem($id);
} else {
$this->redirect('this');
}
}
}
114 changes: 109 additions & 5 deletions contributte-datagrid/app/Presenters/TreeViewPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace App\Presenters;

use Dibi\Connection;
use Dibi\Fluent;
use Nette\Application\UI\Presenter;
use Ublaboo\DataGrid\Column\Action\Confirmation\StringConfirmation;
use Ublaboo\DataGrid\DataGrid;

final class TreeViewPresenter extends Presenter
final class TreeViewPresenter extends AbstractPresenter
{

/**
Expand All @@ -22,14 +24,116 @@ public function createComponentGrid(): DataGrid
{
$grid = new DataGrid;

$grid->setDataSource($this->dibiConnection->select('*')->from('users'));
$join = $this->dibiConnection->select('COUNT(id) AS count, parent_category_id')
->from('categories')
->groupBy('parent_category_id');

$grid->setItemsPerPageList([20, 50, 100]);
$fluent = $this->dibiConnection
->select('c.*, c_b.count as has_children')
->from('categories', 'c')
->leftJoin($join, 'c_b')
->on('c_b.parent_category_id = c.id')
->where('c.parent_category_id IS NULL');

$grid->addColumnNumber('id', 'Id')
->setAlign('left')
$grid->setDataSource($fluent);

$grid->setSortable();

$grid->setTreeView([$this, 'getChildren'], 'has_children');

$grid->addColumnText('name', 'Name');
$grid->addColumnText('name2', 'Name2', 'name');
$grid->addColumnText('id', 'Id')
->setAlign('center');

$columnStatus = $grid->addColumnStatus('status', 'Status');

$columnStatus
->addOption('active', 'Active')
->endOption()
->addOption('inactive', 'Inactive')
->setClass('btn-warning')
->endOption()
->addOption('deleted', 'Deleted')
->setClass('btn-danger')
->endOption()
->setSortable();
$columnStatus->onChange[] = [$this, 'changeStatus'];

$grid->addAction('edit', 'Edit', 'edit!')
->setIcon('pencil pencil-alt')
->setClass('btn btn-xs btn-default btn-secondary ajax')
->setTitle('Edit');

$grid->addAction('delete', '', 'delete!')
->setIcon('trash')
->setTitle('Delete')
->setClass('btn btn-xs btn-danger ajax')
->setConfirmation(
new StringConfirmation('Do you really want to delete example %s?', 'name')
);

return $grid;
}


public function getChildren($parentCategoryId): Fluent
{
$join = $this->dibiConnection->select('COUNT(id) AS count, parent_category_id')
->from('categories')
->groupBy('parent_category_id');

return $this->dibiConnection
->select('c.*, c_b.count as has_children')
->from('categories', 'c')
->leftJoin($join, 'c_b')
->on('c_b.parent_category_id = c.id')
->where('c.parent_category_id = ?', (int) $parentCategoryId);
}



public function handleSort($itemId, $prevId, $nextId, $parentId): void
{
$this->flashMessage(
sprintf(
'Item id: %s, Previous id: %s, Next id: %s, Parent id: %s (only in tree view)',
$itemId,
$prevId,
$nextId,
$parentId
),
'success'
);

if ($this->isAjax()) {
$this->redrawControl('flashes');
} else {
$this->redirect('this');
}
}


public function handleEdit(): void
{
$this->flashMessage('Edited!', 'success');

if ($this->isAjax()) {
$this->redrawControl('flashes');
} else {
$this->redirect('this');
}
}


public function handleDelete(): void
{
$this->flashMessage('Deleted!', 'info');

if ($this->isAjax()) {
$this->redrawControl('flashes');
} else {
$this->redirect('this');
}
}
}
8 changes: 7 additions & 1 deletion contributte-datagrid/app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

$configurator = new Configurator;

$configurator->setDebugMode(true);
$environment = getenv('DATAGRID_ENVIRONMENT');

$configurator->setDebugMode($environment === 'local');
$configurator->enableTracy(__DIR__ . '/../log');

$configurator->setTimeZone('Europe/Prague');
$configurator->setTempDirectory(__DIR__ . '/../temp');

$configurator->addConfig(__DIR__ . '/config/config.neon');

if (file_exists(__DIR__ . '/config/config.local.neon')) {
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
}

return $configurator->createContainer();
Loading

0 comments on commit a3b4ec1

Please sign in to comment.