diff --git a/Button.php b/Button.php index 248de05..a9f86e0 100644 --- a/Button.php +++ b/Button.php @@ -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); } } @@ -103,4 +103,4 @@ protected function createButton($row = null) return $el; } -} \ No newline at end of file +} diff --git a/Column.php b/Column.php index 03874bf..11de479 100644 --- a/Column.php +++ b/Column.php @@ -1,6 +1,8 @@ // public function setCellClass($class) { - $this->cellClass = $class; + $this->cellClass = $class; return $this; } @@ -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? @@ -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)); + } + } /** @@ -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); } } @@ -227,4 +333,4 @@ public function renderCell($record) { call_user_func($this->renderer ?: array($this, "defaultCellRenderer"), $record, $this); } -} \ No newline at end of file +} diff --git a/Grid.php b/Grid.php index cc7b52e..00ec0e3 100644 --- a/Grid.php +++ b/Grid.php @@ -3,6 +3,7 @@ namespace Gridito; use Nette\ComponentModel\Container, Nette\Environment, Nette\Utils\Paginator; +use Nette\Utils\Strings; /** * Grid @@ -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] : "%"; + }); + } + // // @@ -401,4 +411,4 @@ protected function setOptions($object, $options) } } -} \ No newline at end of file +} diff --git a/models/NetteModel.php b/models/NetteModel.php new file mode 100644 index 0000000..e3d52c2 --- /dev/null +++ b/models/NetteModel.php @@ -0,0 +1,57 @@ +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('*'); + } + +}