Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from echo511/master
Browse files Browse the repository at this point in the history
Doctrine2 DataSource
  • Loading branch information
holubj committed Jun 16, 2012
2 parents a7550aa + 484cf72 commit 8a5243f
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Components/Button.php
Expand Up @@ -10,6 +10,7 @@
namespace NiftyGrid;

use Nette\Utils\Html;
use NiftyGrid\Grid; // For constant only

class Button extends \Nette\Application\UI\PresenterComponent
{
Expand Down Expand Up @@ -150,6 +151,10 @@ public function render($row)
->addClass("grid-button")
->setTitle($this->getLabel($row));

if($this->getName() == Grid::ROW_FORM) {
$el->addClass("grid-editable");
}

if($this->hasConfirmationDialog()){
$el->addClass("grid-confirm")
->addData("grid-confirm", $this->getConfirmationDialog($row));
Expand Down
166 changes: 166 additions & 0 deletions DataSource/DoctrineDataSource.php
@@ -0,0 +1,166 @@
<?php
/**
* Doctrine DataSource for NiftyGrid - DataGrid for Nette
*
* @author Nikolas Tsiongas
* @copyright Copyright (c) 2012 Nikolas Tsiongas
* @license New BSD Licence
* @link http://addons.nette.org/cs/niftygrid
*/
namespace NiftyGrid;

use NiftyGrid\FilterCondition,
Nette\Utils\Strings;

use Doctrine\ORM\Tools\Pagination\Paginator;

class DoctrineDataSource implements IDataSource
{
private $qb;

private $primary;

public function __construct($qb, $primary)
{
// Query builder
$this->qb = $qb;

// Primary id
$this->primary = $primary;
}

public function getQuery()
{
return $this->qb->getQuery();
}


public function getData()
{
$result = array();
foreach($this->getQuery()->getScalarResult() as $values) {
$id = $result[$values[$this->primary]]['id'] = $values[$this->primary];

foreach($values as $column => $value) {
$result[$id][$column] = $value;
}
}

return $result;
}

public function getCount($column = "*")
{
return $this->getSelectedRowsCount();
}

public function getSelectedRowsCount()
{
$paginator = new Paginator($this->getQuery());

return $paginator->count();
}

public function orderData($by, $way)
{
$this->qb->orderBy($this->columnName($by), $way);
}

public function limitData($limit, $offset)
{
$this->qb->setFirstResult($offset)
->setMaxResults($limit);
}

public function filterData(array $filters)
{
foreach($filters as $filter){
if($filter["type"] == FilterCondition::WHERE){

$column = $this->columnName($filter['column']);
$value = $filter["value"];
$expr = $this->qb->expr();
$cond = false;

switch($filter['cond']) {
case ' LIKE ?':
$cond = $expr->like($column, $expr->literal($value));
break;

case ' = ?':
$cond = $expr->eq($column, $expr->literal($value));
break;

case ' > ?':
$cond = $expr->gt($column, $expr->literal($value));
break;

case ' >= ?':
$cond = $expr->gte($column, $expr->literal($value));
break;

case ' < ?':
$cond = $expr->lt($column, $expr->literal($value));
break;

case ' <= ?':
$cond = $expr->lte($column, $expr->literal($value));
break;

case ' <> ?':
$cond = $expr->neq($column, $expr->literal($value));
break;
}

if(!$cond) {
try {
$datetime = new \DateTime($value);
$value = $datetime->format('Y-m-d H:i:s');
} catch(\Exception $e) {}

if(isset($datetime)) {
switch($filter['cond']) {
/** Dates */
case ' = ':
$cond = $expr->like($column, $expr->literal($datetime->format('Y-m-d') . '%'));
break;

case ' > ':
$cond = $expr->gt($column, $expr->literal($value));
break;

case ' >= ':
$cond = $expr->gte($column, $expr->literal($value));
break;

case ' < ':
$cond = $expr->lt($column, $expr->literal($value));
break;

case ' <= ':
$cond = $expr->lte($column, $expr->literal($value));
break;

case ' <> ':
$cond = $expr->neq($column, $expr->literal($value));
break;
}
}
}

if($cond) {
$this->qb->andWhere($cond);
}

}
}
}

private function columnName($full)
{
$name = explode("_", $full);
$entity = $name[0];
unset($name[0]);
return $entity.".".implode("_", $name);
}
}
11 changes: 11 additions & 0 deletions js/grid.js
Expand Up @@ -133,4 +133,15 @@ $(function(){
setDatepicker();
hidePerPageSubmit();
});

$("input.grid-editable").live("keypress", function(e) {
if (e.keyCode == '13') {
e.preventDefault();
$("input[type=submit].grid-editable").click();
}
});

$("table.grid tbody tr:not(.grid-subgrid-row) td.grid-data-cell").live("dblclick", function(e) {
$(this).parent().find("a.grid-editable:first").click();
});
});
2 changes: 1 addition & 1 deletion templates/grid.latte
Expand Up @@ -60,7 +60,7 @@
<td n:foreach="$subGrids as $subgrid" n:attr="class => array(grid-row-cell, $control->isEditable() && $control->activeRowForm == $row['id'] ? 'grid-edited-cell')">
{control $subgrid $row}
</td>
<td n:foreach="$columns as $column" n:attr="class => array(grid-row-cell, $control->isEditable() && $control->activeRowForm == $row['id'] ? 'grid-edited-cell'), style => $column->hasCellRenderer() ? $column->getCellRenderer($row)">
<td n:foreach="$columns as $column" n:attr="class => array(grid-row-cell, grid-data-cell, $control->isEditable() && $control->activeRowForm == $row['id'] ? 'grid-edited-cell'), style => $column->hasCellRenderer() ? $column->getCellRenderer($row)">
{if $control->isEditable() && $column->editable && $control->activeRowForm == $row['id']}
{$control['gridForm'][$control->name]['rowForm'][$column->name]->getControl()}
{else}
Expand Down

0 comments on commit 8a5243f

Please sign in to comment.