Skip to content
Open
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
4 changes: 2 additions & 2 deletions Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getConfirmationQuestion($row)
if (is_callable($this->confirmationQuestion)) {
return call_user_func($this->confirmationQuestion, $row);
} else {
return $this->confirmationQuestion;
return Grid::formatRecordString($row, $this->confirmationQuestion);
}
}

Expand Down Expand Up @@ -103,4 +103,4 @@ protected function createButton($row = null)
return $el;
}

}
}
114 changes: 110 additions & 4 deletions Column.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Gridito;
use Nette\Utils\Strings;
use Nette\Utils\Html;

/**
* Grid column
Expand All @@ -18,6 +20,12 @@ class Column extends \Nette\Application\UI\Control
/** @var callback */
private $renderer = null;

/** @var int */
private $maxlen = null;

/** @var string */
private $type = 'string';

/** @var bool */
private $sortable = false;

Expand All @@ -27,13 +35,16 @@ class Column extends \Nette\Application\UI\Control
/** @var string|callable */
private $cellClass = null;

/** @var string */
private $format = null;

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getters & setters">

public function setCellClass($class)
{
$this->cellClass = $class;
$this->cellClass = $class;
return $this;
}

Expand Down Expand Up @@ -98,7 +109,65 @@ public function setRenderer($cellRenderer)
return $this;
}

/**
* Set maximal length of cell
* @param $maxlen
* @return Column
*/
public function setLength($maxlen)
{
$this->maxlen = $maxlen;
return $this;
}

/**
* Get maximal length of cell
* @return int
*/
public function getLength()
{
return $this->maxlen;
}

/**
* Set the type of cell
* @param string type
* @return Column
*/
public function setType($type)
{
$this->type = $type;
return $this;
}

/**
* Get the type of cell
* @return string type
*/
public function getType($type)
{
return $this->type;
}

/**
* Set format of the cell
* @param mixed format
* @return Column
*/
public function setFormat($format)
{
$this->format = $format;
return $this;
}

/**
* Get the format
* @return mixed
*/
public function getFormat()
{
return $this->format;
}

/**
* Is sortable?
Expand Down Expand Up @@ -192,6 +261,36 @@ public static function renderDateTime($value, $format)
echo $value->format($format);
}

/**
* Render the text, takes care of length
* @param string $text text to render
* @param int $maxlen maximum length of text
*/
public static function renderText($text, $maxlen)
{
if (is_null($maxlen) || Strings::length($text) < $maxlen) {
echo htmlspecialchars($text, ENT_NOQUOTES);
} else {
echo Html::el('span')->title($text)
->setText(Strings::truncate($text, $maxlen));
}
}

/**
* Render the email address, takes care of length
* @param string $email email address
* @param int $maxlen maximum length of text
*/
public static function renderEmail($email, $maxlen)
{
$el = Html::el('a')->href('mailto:' . $email);
if (is_null($maxlen) || Strings::length($email) < $maxlen) {
echo $el->setText($email);
} else {
echo $el->title($email)
->setText(Strings::truncate($email, $maxlen));
}
}


/**
Expand All @@ -204,16 +303,23 @@ public function defaultCellRenderer($record, $column) {
$value = $record->$name;

// boolean
if (is_bool($value)) {
if (in_array($this->type, array('bool', 'boolean')) || is_bool($value)) {
self::renderBoolean($value);

// date
} elseif ($value instanceof \DateTime) {
self::renderDateTime($value, $this->dateTimeFormat);

// email
} elseif ($this->type == 'email') {
self::renderEmail($value, $this->maxlen);

// other
} else {
echo $value;
if (!is_null($this->format)) {
$value = Grid::formatRecordString($record, $this->format);
}
self::renderText($value, $this->maxlen);
}
}

Expand All @@ -227,4 +333,4 @@ public function renderCell($record) {
call_user_func($this->renderer ?: array($this, "defaultCellRenderer"), $record, $this);
}

}
}
12 changes: 11 additions & 1 deletion Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Gridito;

use Nette\ComponentModel\Container, Nette\Environment, Nette\Utils\Paginator;
use Nette\Utils\Strings;

/**
* Grid
Expand Down Expand Up @@ -66,6 +67,15 @@ public function __construct(\Nette\ComponentModel\IContainer $parent = null, $na
$this->paginator->setItemsPerPage($this->defaultItemsPerPage);
}

public static function formatRecordString($record, $formatString)
{
return Strings::replace($formatString, '#%[^%]*%#u',
function ($m) use ($record) {
$m = Strings::trim($m[0], '%');
return $m != '' ? $record[$m] : "%";
});
}

// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getters & setters">
Expand Down Expand Up @@ -401,4 +411,4 @@ protected function setOptions($object, $options)
}
}

}
}
57 changes: 57 additions & 0 deletions models/NetteModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Gridito;

use DibiFluent;
use Nette\Database\Table\Selection;

/**
* Nette\Database model
*
* @author Samuel Hapak
* @license MIT
*/
class NetteModel extends AbstractModel
{
/** @var Nette\Database\Table\Selection */
private $selection;

/**
* Constructor
* @param Selection $selection
*/
public function __construct(Selection $selection)
{
$this->selection = $selection;
}

public function getItemByUniqueId($uniqueId)
{
$select = clone $this->selection;
return $select->where($this->getPrimaryKey(), $uniqueId)
->fetch();
}

public function getItems()
{
$select = clone $this->selection;

list($sortColumn, $sortType) = $this->getSorting();
if ($sortColumn) {
$select->order("$sortColumn $sortType");
}
return $select->limit($this->getLimit(), $this->getOffset())
->fetchPairs($this->getPrimaryKey());
}


/**
* Item count
* @return int
*/
protected function _count()
{
return $this->selection->count('*');
}

}