From 58f0eb8da79a914cc452f732c4a9cb6c571a88ac Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:29:47 +0200 Subject: [PATCH 1/8] Pridany Nette Model --- models/NetteModel.php | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 models/NetteModel.php diff --git a/models/NetteModel.php b/models/NetteModel.php new file mode 100644 index 0000000..075e7ef --- /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(); + } + +} From 4849972dcbe2e937d4718eabd27aa3701ba1bf1d Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:37:59 +0200 Subject: [PATCH 2/8] FIX: XSS --- Column.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Column.php b/Column.php index 03874bf..ad55a6b 100644 --- a/Column.php +++ b/Column.php @@ -213,7 +213,7 @@ public function defaultCellRenderer($record, $column) { // other } else { - echo $value; + echo htmlspecialchars($value, ENT_NOQUOTES); } } @@ -227,4 +227,4 @@ public function renderCell($record) { call_user_func($this->renderer ?: array($this, "defaultCellRenderer"), $record, $this); } -} \ No newline at end of file +} From 18451c21db8708b5e5a8c126da2549d02d9f7d5f Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:44:41 +0200 Subject: [PATCH 3/8] Add property maxlen --- Column.php | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Column.php b/Column.php index ad55a6b..b2fcffd 100644 --- a/Column.php +++ b/Column.php @@ -18,6 +18,8 @@ class Column extends \Nette\Application\UI\Control /** @var callback */ private $renderer = null; + /** @var int */ + private $maxlen = null; /** @var bool */ private $sortable = false; @@ -33,7 +35,7 @@ class Column extends \Nette\Application\UI\Control public function setCellClass($class) { - $this->cellClass = $class; + $this->cellClass = $class; return $this; } @@ -98,6 +100,25 @@ 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; + } /** @@ -192,6 +213,21 @@ 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)); + } + } + /** @@ -213,7 +249,7 @@ public function defaultCellRenderer($record, $column) { // other } else { - echo htmlspecialchars($value, ENT_NOQUOTES); + self::renderText($value, $this->maxlen); } } From 159d81bbe17b1ae7cc91aa4d68c9efdeadcc2741 Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:47:05 +0200 Subject: [PATCH 4/8] Add property type --- Column.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Column.php b/Column.php index b2fcffd..9670b43 100644 --- a/Column.php +++ b/Column.php @@ -20,6 +20,10 @@ class Column extends \Nette\Application\UI\Control /** @var int */ private $maxlen = null; + + /** @var string */ + private $type = 'string'; + /** @var bool */ private $sortable = false; @@ -120,6 +124,25 @@ 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; + } /** * Is sortable? @@ -240,7 +263,7 @@ 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 From cd99c438c6eef2606364d4be5404cd2d517cd9de Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:48:33 +0200 Subject: [PATCH 5/8] Add email renderer --- Column.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Column.php b/Column.php index 9670b43..76e90a7 100644 --- a/Column.php +++ b/Column.php @@ -251,6 +251,21 @@ public static function renderText($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)); + } + } /** @@ -270,6 +285,10 @@ public function defaultCellRenderer($record, $column) { } elseif ($value instanceof \DateTime) { self::renderDateTime($value, $this->dateTimeFormat); + // email + } elseif ($this->type == 'email') { + self::renderEmail($value, $this->maxlen); + // other } else { self::renderText($value, $this->maxlen); From d3904e7a76e62da20d5e3a4f25a307e8c7bdb8d2 Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Fri, 1 Jul 2011 13:49:18 +0200 Subject: [PATCH 6/8] Fix: forgotten use ... --- Column.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Column.php b/Column.php index 76e90a7..c23f330 100644 --- a/Column.php +++ b/Column.php @@ -1,6 +1,8 @@ Date: Fri, 1 Jul 2011 13:55:42 +0200 Subject: [PATCH 7/8] Add formating --- Button.php | 4 ++-- Column.php | 26 ++++++++++++++++++++++++++ Grid.php | 12 +++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) 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 c23f330..11de479 100644 --- a/Column.php +++ b/Column.php @@ -35,6 +35,9 @@ class Column extends \Nette\Application\UI\Control /** @var string|callable */ private $cellClass = null; + /** @var string */ + private $format = null; + // // @@ -146,6 +149,26 @@ 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? * @return bool @@ -293,6 +316,9 @@ public function defaultCellRenderer($record, $column) { // other } else { + if (!is_null($this->format)) { + $value = Grid::formatRecordString($record, $this->format); + } self::renderText($value, $this->maxlen); } } 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 +} From 9eb53ff3ce1ef0f4d61d4e395eb3f98b64b4aa97 Mon Sep 17 00:00:00 2001 From: Samuel Hapak Date: Thu, 7 Jul 2011 23:09:57 +0200 Subject: [PATCH 8/8] Z nejakeho divneho dovodu count() najskor vsetky data vytiahne z databazy a potom zisti ich pocet. Narozdiel od count('*'), ktory sa databazy iba spyta na pocet, co je vyrazne rychlejsie. --- models/NetteModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/NetteModel.php b/models/NetteModel.php index 075e7ef..e3d52c2 100644 --- a/models/NetteModel.php +++ b/models/NetteModel.php @@ -51,7 +51,7 @@ public function getItems() */ protected function _count() { - return $this->selection->count(); + return $this->selection->count('*'); } }