From 328af74ca6eafac6765e3517622c059be7cdaec5 Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Fri, 28 Sep 2012 16:24:47 +0200 Subject: [PATCH 1/7] Second order function --- Components/Buttonftp.php | 253 ++++++++++ DataSource/IDataSource.php | 6 + DataSource/NDataSource.php | 7 + Grid.php | 85 +++- Grid.php~ | 999 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1343 insertions(+), 7 deletions(-) create mode 100644 Components/Buttonftp.php create mode 100644 Grid.php~ diff --git a/Components/Buttonftp.php b/Components/Buttonftp.php new file mode 100644 index 0000000..f4ab602 --- /dev/null +++ b/Components/Buttonftp.php @@ -0,0 +1,253 @@ +label = $label; + + return $this; + } + + /** + * @param array $row + * @return string + */ + private function getLabel($row) + { + if(is_callable($this->label)){ + return call_user_func($this->label, $row); + } + return $this->label; + } + + /** + * @param callback|string $link + * @return Button + */ + public function setLink($link) + { + $this->link = $link; + + return $this; + } + + /** + * @param array $row + * @return string + */ + private function getLink($row) + { + if(is_callable($this->link)){ + return call_user_func($this->link, $row); + } + return $this->link; + } + + /** + * @param $text + * @return mixed + */ + public function setText($text) + { + $this->text = $text; + + return $this; + } + + /** + * @param array $row + * @return string + */ + private function getText($row) + { + if(is_callable($this->text)){ + return call_user_func($this->text, $row); + } + return $this->text; + } + + /** + * @param callback|string $target + * @return Button + */ + public function setTarget($target) + { + $this->target = $target; + + return $this; + } + + /** + * @param array $row + * @return callback|mixed|string + */ + private function getTarget($row) + { + if(is_callable($this->target)){ + return call_user_func($this->target, $row); + } + return $this->target; + } + + /** + * @param callback|string $class + * @return Button + */ + public function setClass($class) + { + $this->class = $class; + + return $this; + } + + /** + * @param array $row + * @return callback|mixed|string + */ + private function getClass($row) + { + if(is_callable($this->class)){ + return call_user_func($this->class, $row); + } + return $this->class; + } + + /** + * @param bool $ajax + * @return Button + */ + public function setAjax($ajax = TRUE) + { + $this->ajax = $ajax; + + return $this; + } + + /** + * @param callback|string $message + * @return Button + */ + public function setConfirmationDialog($message) + { + $this->dialog = $message; + + return $this; + } + + /** + * @param array $row + * @return callback|mixed|string + */ + public function getConfirmationDialog($row) + { + if(is_callable($this->dialog)){ + return call_user_func($this->dialog, $row); + } + return $this->dialog; + } + + /** + * @return bool + */ + private function hasConfirmationDialog() + { + return (!empty($this->dialog)) ? TRUE : FALSE; + } + + /** + * @param callback|string $show + * @return Button + */ + public function setShow($show) + { + $this->show = $show; + + return $this; + } + + /** + * @param array $row + * @return callback|mixed|string + */ + public function getShow($row) + { + if(is_callable($this->show)){ + return (boolean) call_user_func($this->show, $row); + } + return $this->show; + } + + /** + * @param array $row + */ + public function render($row) + { + if(!$this->getShow($row)){ + return false; + } + + $el = Html::el("a") + ->href($this->getLink($row)) + ->setText($this->getText($row)) + ->addClass("grid-button") + ->addClass($this->getClass($row)) + ->setTitle($this->getLabel($row)) + ->setTarget($this->getTarget($row)); + + if($this->getName() == Grid::ROW_FORM) { + $el->addClass("grid-editable"); + } + + if($this->hasConfirmationDialog()){ + $el->addClass("grid-confirm") + ->addData("grid-confirm", $this->getConfirmationDialog($row)); + } + + if($this->ajax){ + $el->addClass("grid-ajax"); + } + echo $el; + } + +} diff --git a/DataSource/IDataSource.php b/DataSource/IDataSource.php index 30d9aa2..0773cca 100644 --- a/DataSource/IDataSource.php +++ b/DataSource/IDataSource.php @@ -34,6 +34,12 @@ public function getCount($column = "*"); */ public function orderData($by, $way); + /** + * Sort data by given columns + * @param array $orders + */ + public function multipleOrderData($orders); + /** * Limit data to select * @param int $limit diff --git a/DataSource/NDataSource.php b/DataSource/NDataSource.php index 18035a7..c9a0e25 100644 --- a/DataSource/NDataSource.php +++ b/DataSource/NDataSource.php @@ -40,6 +40,13 @@ public function orderData($by, $way) $this->data->order($by." ".$way); } + public function multipleOrderData($orders) + { + foreach($orders as $order){ + $this->data->order($order[0]." ".$order[1]); + } + } + public function limitData($limit, $offset) { $this->data->limit($limit, $offset); diff --git a/Grid.php b/Grid.php index a1f058c..f55b54f 100644 --- a/Grid.php +++ b/Grid.php @@ -39,6 +39,9 @@ class Grid extends \Nette\Application\UI\Control /** @var string */ protected $defaultOrder; + /** @var string */ + protected $secondOrder; + /** @var IDataSource */ protected $dataSource; @@ -135,8 +138,7 @@ protected function attached($presenter) $this->orderData($this->order); } if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){ - $order = explode(" ", $this->defaultOrder); - $this->dataSource->orderData($order[0], $order[1]); + $this->orderData($this->defaultOrder); } $this->count = $this->getCount(); } @@ -380,9 +382,45 @@ public function setMessageNoRecords($messageNoRecords) */ public function setDefaultOrder($order) { + if(!is_array($order)){ + $order = explode(" ", trim($order)); + } + if($order[1] == 1 || strtoupper($order[1]) == "ASC"){ + $order[1] = "ASC"; + }else{ + $order[1] = "DESC"; + } + $order[2] = "default"; $this->defaultOrder = $order; } + /** + * @param mixed $order + * @param mixed $way + */ + public function setSecondOrder($order, $way = 0) + { + if(!is_array($order)){ + if($way == 1 || strtoupper($way) == "ASC"){ + $way = "ASC"; + }else{ + $way = "DESC"; + } + $this->secondOrder = array($order, $way); + }else{ + foreach($order as $key => $value){ + if(is_array($value)){ + if($value[1] == 1 || strtoupper($value[1]) == "ASC"){ + $order[$key][1] = "ASC"; + }else{ + $order[$key][1] = "DESC"; + } + } + } + $this->secondOrder = $order; + } + } + /** * @param array $values * @return array @@ -468,6 +506,14 @@ public function hasDefaultOrder() return !empty($this->defaultOrder) ? TRUE : FALSE; } + /** + * @return bool + */ + public function hasSecondOrder() + { + return !empty($this->secondOrder) ? TRUE : FALSE; + } + /** * @return bool */ @@ -578,17 +624,42 @@ protected function filterData() /** * @param string $order + * @param bool $second * @throws InvalidOrderException */ - protected function orderData($order) + protected function orderData($order, $second = false) { + $orders = array(); try{ - $order = explode(" ", $order); - if(in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ - $this->dataSource->orderData($order[0], $order[1]); - }else{ + if(!is_array($order)) { + $order = explode(" ", $order); + } + if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ + $orders[] = $order; + }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ + $orders[] = $order; + } + else{ throw new InvalidOrderException("Neplatné seřazení."); } + if($this->hasSecondOrder() && $second === false){ + if(is_array($this->secondOrder[0])){ + foreach($this->secondOrder[0] as $secondOrder){ + if($secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($secondOrder, true); + } + } + }else{ + if($this->secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($this->secondOrder, true); + } + } + $this->dataSource->multipleOrderData($orders); + }elseif($second === true){ + return $order; + }else{ + $this->dataSource->orderData($order[0], $order[1]); + } } catch(InvalidOrderException $e){ $this->flashMessage($e->getMessage(), "grid-error"); diff --git a/Grid.php~ b/Grid.php~ new file mode 100644 index 0000000..a5bb429 --- /dev/null +++ b/Grid.php~ @@ -0,0 +1,999 @@ + 20, 50 => 50, 100 => 100); + + /** @var bool */ + public $paginate = TRUE; + + /** @var string */ + protected $defaultOrder; + + /** @var string */ + protected $secondOrder; + + /** @var IDataSource */ + protected $dataSource; + + /** @var string */ + protected $primaryKey; + + /** @var int */ + protected $count; + + /** @var string */ + public $width; + + /** @var bool */ + public $enableSorting = TRUE; + + /** @var int */ + public $activeRowForm; + + /** @var callback */ + public $rowFormCallback; + + /** @var bool */ + public $showAddRow = FALSE; + + /** @var bool */ + public $isSubGrid = FALSE; + + /** @var array */ + public $subGrids = array(); + + /** @var callback */ + public $afterConfigureSettings; + + /** @var string */ + protected $templatePath; + + /** @var string */ + public $messageNoRecords = 'Žádné záznamy'; + + /** + * @param \Nette\Application\UI\Presenter $presenter + */ + protected function attached($presenter) + { + parent::attached($presenter); + + $this->addComponent(New \Nette\ComponentModel\Container(), "columns"); + $this->addComponent(New \Nette\ComponentModel\Container(), "buttons"); + $this->addComponent(New \Nette\ComponentModel\Container(), "globalButtons"); + $this->addComponent(New \Nette\ComponentModel\Container(), "actions"); + $this->addComponent(New \Nette\ComponentModel\Container(), "subGrids"); + + if($presenter->isAjax()){ + $this->invalidateControl(); + } + + $this->configure($presenter); + + if($this->isSubGrid && !empty($this->afterConfigureSettings)){ + call_user_func($this->afterConfigureSettings, $this); + } + + if($this->hasActiveSubGrid()){ + $subGrid = $this->addComponent($this['subGrids']->components[$this->activeSubGridName]->getGrid(), "subGrid".$this->activeSubGridName); + $subGrid->registerSubGrid("subGrid".$this->activeSubGridName); + } + + if($this->hasActionForm()){ + $actions = array(); + foreach($this['actions']->components as $name => $action){ + $actions[$name] = $action->getAction(); + } + $this['gridForm'][$this->name]['action']['action_name']->setItems($actions); + } + if($this->paginate){ + if($this->hasActiveItemPerPage()){ + if(in_array($this->perPage, $this['gridForm'][$this->name]['perPage']['perPage']->items)){ + $this['gridForm'][$this->name]['perPage']->setDefaults(array("perPage" => $this->perPage)); + }else{ + $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); + $this->perPage = reset($items); + } + }else{ + $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); + $this->perPage = reset($items); + } + $this->getPaginator()->itemsPerPage = $this->perPage; + } + if($this->hasActiveFilter()){ + $this->filterData(); + $this['gridForm'][$this->name]['filter']->setDefaults($this->filter); + } + if($this->hasActiveOrder() && $this->hasEnabledSorting()){ + $this->orderData($this->order); + } + if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){ + $this->orderData($this->defaultOrder); + } + $this->count = $this->getCount(); + } + + /** + * @param string $subGrid + */ + public function registerSubGrid($subGrid) + { + if(!$this->isSubGrid){ + $this->subGrids[] = $subGrid; + }else{ + $this->parent->registerSubGrid($this->name."-".$subGrid); + } + } + + /** + * @return array + */ + public function getSubGrids() + { + if($this->isSubGrid){ + return $this->parent->getSubGrids(); + }else{ + return $this->subGrids; + } + } + + /** + * @param null|string $gridName + * @return string + */ + public function getGridPath($gridName = NULL) + { + if(empty($gridName)){ + $gridName = $this->name; + }else{ + $gridName = $this->name."-".$gridName; + } + if($this->isSubGrid){ + return $this->parent->getGridPath($gridName); + }else{ + return $gridName; + } + } + + public function findSubGridPath($gridName) + { + foreach($this->subGrids as $subGrid){ + $path = explode("-", $subGrid); + if(end($path) == $gridName){ + return $subGrid; + } + } + } + + /** + * @param string $columnName + * @return \Nette\Forms\IControl + * @throws UnknownColumnException + */ + public function getColumnInput($columnName) + { + if(!$this->columnExists($columnName)){ + throw new UnknownColumnException("Column $columnName doesn't exists."); + } + return $this['gridForm'][$this->name]['rowForm'][$columnName]; + } + + /** + * @param string $name + * @param null|string $label + * @param null|string $width + * @param null|int $truncate + * @return Column + * @throws DuplicateColumnException + * @return \Nifty\Grid\Column + */ + protected function addColumn($name, $label = NULL, $width = NULL, $truncate = NULL) + { + if(!empty($this['columns']->components[$name])){ + throw new DuplicateColumnException("Column $name already exists."); + } + $column = new Column($this['columns'], $name); + $column->setName($name) + ->setLabel($label) + ->setWidth($width) + ->setTruncate($truncate) + ->injectParent($this); + + return $column; + } + + /** + * @param string $name + * @param null|string $label + * @return Button + * @throws DuplicateButtonException + */ + protected function addButton($name, $label = NULL) + { + if(!empty($this['buttons']->components[$name])){ + throw new DuplicateButtonException("Button $name already exists."); + } + $button = new Button($this['buttons'], $name); + if($name == self::ROW_FORM){ + $self = $this; + $primaryKey = $this->primaryKey; + $button->setLink(function($row) use($self, $primaryKey){ + return $self->link("showRowForm!", $row[$primaryKey]); + }); + } + $button->setLabel($label); + return $button; + } + + + /** + * @param string $name + * @param null|string $label + * @throws DuplicateGlobalButtonException + * @return GlobalButton + */ + public function addGlobalButton($name, $label = NULL) + { + if(!empty($this['globalButtons']->components[$name])){ + throw new DuplicateGlobalButtonException("Global button $name already exists."); + } + $globalButton = new GlobalButton($this['globalButtons'], $name); + if($name == self::ADD_ROW){ + $globalButton->setLink($this->link("addRow!")); + } + $globalButton->setLabel($label); + return $globalButton; + } + + /** + * @param string $name + * @param null|string $label + * @return Action + * @throws DuplicateActionException + */ + public function addAction($name, $label = NULL) + { + if(!empty($this['actions']->components[$name])){ + throw new DuplicateActionException("Action $name already exists."); + } + $action = new Action($this['actions'], $name); + $action->setName($name) + ->setLabel($label); + + return $action; + } + + /** + * @param string $name + * @param null|string $label + * @return SubGrid + * @throws DuplicateSubGridException + */ + public function addSubGrid($name, $label = NULL) + { + if(!empty($this['subGrids']->components[$name]) || in_array($name, $this->getSubGrids())){ + throw new DuplicateSubGridException("SubGrid $name already exists."); + } + $self = $this; + $primaryKey = $this->primaryKey; + $subGrid = new SubGrid($this['subGrids'], $name); + $subGrid->setName($name) + ->setLabel($label); + if($this->activeSubGridName == $name){ + $subGrid->setClass("grid-subgrid-close"); + $subGrid->setClass(function($row) use ($self, $primaryKey){ + return $row[$primaryKey] == $self->activeSubGridId ? "grid-subgrid-close" : "grid-subgrid-open"; + }); + $subGrid->setLink(function($row) use ($self, $name, $primaryKey){ + $link = $row[$primaryKey] == $self->activeSubGridId ? array("activeSubGridId" => NULL, "activeSubGridName" => NULL) : array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name); + return $self->link("this", $link); + }); + } + else{ + $subGrid->setClass("grid-subgrid-open") + ->setLink(function($row) use ($self, $name, $primaryKey){ + return $self->link("this", array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name)); + }); + } + return $subGrid; + } + + /** + * @return array + */ + public function getColumnNames() + { + foreach($this['columns']->components as $column){ + $columns[] = $column->name; + } + return $columns; + } + + /** + * @return int $count + */ + public function getColsCount() + { + $count = count($this['columns']->components); + $this->hasActionForm() ? $count++ : $count; + ($this->hasButtons() || $this->hasFilterForm()) ? $count++ : $count; + $count += count($this['subGrids']->components); + + return $count; + } + + /** + * @param IDataSource $dataSource + */ + protected function setDataSource(IDataSource $dataSource) + { + $this->dataSource = $dataSource; + $this->primaryKey = $this->dataSource->getPrimaryKey(); + } + + /** + * @param string $width + */ + public function setWidth($width) + { + $this->width = $width; + } + + /** + * @param string $messageNoRecords + */ + public function setMessageNoRecords($messageNoRecords) + { + $this->messageNoRecords = $messageNoRecords; + } + + /** + * @param string $order + */ + public function setDefaultOrder($order) + { + if(!is_array($order)){ + $order = explode(" ", trim($order)); + } + if($order[1] == 1 || strtoupper($order[1]) == "ASC"){ + $order[1] = "ASC"; + }else{ + $order[1] = "DESC"; + } + $order[2] = "default"; + $this->defaultOrder = $order; + } + + /** + * @param mixed $order + * @param mixed $way + */ + public function setSecondOrder($order, $way = 0) + { + if(!is_array($order)){ + if($way == 1 || strtoupper($way) == "ASC"){ + $way = "ASC"; + }else{ + $way = "DESC"; + } + $this->secondOrder = array($order, $way); + }else{ + foreach($order as $key => $value){ + if(is_array($value)){ + if($value[1] == 1 || strtoupper($value[1]) == "ASC"){ + $order[$key][1] = "ASC"; + }else{ + $order[$key][1] = "DESC"; + } + } + } + $this->secondOrder = $order; + } + } + + /** + * @param array $values + * @return array + */ + protected function setPerPageValues(array $values) + { + $perPageValues = array(); + foreach($values as $value){ + $perPageValues[$value] = $value; + } + $this->perPageValues = $perPageValues; + } + + /** + * @return bool + */ + public function hasButtons() + { + return count($this['buttons']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasGlobalButtons() + { + return count($this['globalButtons']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasFilterForm() + { + foreach($this['columns']->components as $column){ + if(!empty($column->filterType)) + return TRUE; + } + return FALSE; + } + + /** + * @return bool + */ + public function hasActionForm() + { + return count($this['actions']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasActiveFilter() + { + return count($this->filter) ? TRUE : FALSE; + } + + /** + * @param string $filter + * @return bool + */ + public function isSpecificFilterActive($filter) + { + if(isset($this->filter[$filter])){ + return ($this->filter[$filter] != '') ? TRUE : FALSE; + } + return false; + } + + /** + * @return bool + */ + public function hasActiveOrder() + { + return !empty($this->order) ? TRUE: FALSE; + } + + /** + * @return bool + */ + public function hasDefaultOrder() + { + return !empty($this->defaultOrder) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasSecondOrder() + { + return !empty($this->secondOrder) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasEnabledSorting() + { + return $this->enableSorting; + } + + /** + * @return bool + */ + public function hasActiveItemPerPage() + { + return !empty($this->perPage) ? TRUE : FALSE; + } + + public function hasActiveRowForm() + { + return !empty($this->activeRowForm) ? TRUE : FALSE; + } + + /** + * @param string $column + * @return bool + */ + public function columnExists($column) + { + return isset($this['columns']->components[$column]); + } + + /** + * @param string $subGrid + * @return bool + */ + public function subGridExists($subGrid) + { + return isset($this['subGrids']->components[$subGrid]); + } + + /** + * @return bool + */ + public function isEditable() + { + foreach($this['columns']->components as $component){ + if($component->editable) + return TRUE; + } + return FALSE; + } + + /** + * @return bool + */ + public function hasActiveSubGrid() + { + return (!empty($this->activeSubGridId) && !empty($this->activeSubGridName) && $this->subGridExists($this->activeSubGridName)) ? TRUE : FALSE; + } + + /** + * @return mixed + * @throws InvalidFilterException + * @throws UnknownColumnException + * @throws UnknownFilterException + */ + protected function filterData() + { + try{ + $filters = array(); + foreach($this->filter as $name => $value){ + if(!$this->columnExists($name)){ + throw new UnknownColumnException("Neexistující sloupec $name"); + + } + if(!$this['columns-'.$name]->hasFilter()){ + throw new UnknownFilterException("Neexistující filtr pro sloupec $name"); + } + + $type = $this['columns-'.$name]->getFilterType(); + $filter = FilterCondition::prepareFilter($value, $type); + + if(method_exists("\\NiftyGrid\\FilterCondition", $filter["condition"])){ + $filter = call_user_func("\\NiftyGrid\\FilterCondition::".$filter["condition"], $filter["value"]); + if(!empty($this['gridForm'][$this->name]['filter'][$name])){ + $filter["column"] = $name; + if(!empty($this['columns-'.$filter["column"]]->tableName)){ + $filter["column"] = $this['columns-'.$filter["column"]]->tableName; + } + $filters[] = $filter; + }else{ + throw new InvalidFilterException("Neplatný filtr"); + } + }else{ + throw new InvalidFilterException("Neplatný filtr"); + } + } + return $this->dataSource->filterData($filters); + } + catch(UnknownColumnException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("filter" => NULL)); + } + catch(UnknownFilterException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("filter" => NULL)); + } + } + + /** + * @param string $order + * @param bool $second + * @throws InvalidOrderException + */ + protected function orderData($order, $second = false) + { + $orders = array(); + try{ + if(!is_array($order)) { + $order = explode(" ", $order); + } + if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ + $orders[] = $order; + }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ + $orders[] = $order; + } + else{ + throw new InvalidOrderException("Neplatné seřazení."); + } + if($this->hasSecondOrder() && $second === false){ + if(is_array($this->secondOrder[0])){ + foreach($this->secondOrder[0] as $secondOrder){ + if($secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($secondOrder, true); + } + } + }else{ + if($this->secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($this->secondOrder, true); + } + } + $this->dataSource->multipleOrderData($orders); + }elseif($second === true){ + $return $order; + }else{ + $this->dataSource->orderData($order[0], $order[1]); + } + } + catch(InvalidOrderException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("order" => NULL)); + } + } + + /** + * @return int + */ + protected function getCount() + { + if($this->paginate){ + $count = $this->dataSource->getCount(); + $this->getPaginator()->itemCount = $count; + $this->dataSource->limitData($this->getPaginator()->itemsPerPage, $this->getPaginator()->offset); + return $count; + }else{ + $count = $this->dataSource->getCount(); + $this->getPaginator()->itemCount = $count; + return $count; + } + } + + /** + * @return GridPaginator + */ + protected function createComponentPaginator() + { + return new GridPaginator; + } + + /** + * @return \Nette\Utils\Paginator + */ + public function getPaginator() + { + return $this['paginator']->paginator; + } + + /** + * @param int $page + */ + public function handleChangeCurrentPage($page) + { + if($this->presenter->isAjax()){ + $this->redirect("this", array("paginator-page" => $page)); + } + } + + /** + * @param int $perPage + */ + public function handleChangePerPage($perPage) + { + if($this->presenter->isAjax()){ + $this->redirect("this", array("perPage" => $perPage)); + } + } + + /** + * @param string $column + * @param string $term + */ + public function handleAutocomplete($column, $term) + { + if($this->presenter->isAjax()){ + if(!empty($this['columns']->components[$column]) && $this['columns']->components[$column]->autocomplete){ + $this->filter[$column] = $term."%"; + $this->filterData(); + $this->dataSource->limitData($this['columns']->components[$column]->getAutocompleteResults(), NULL); + $data = $this->dataSource->getData(); + $results = array(); + foreach($data as $row){ + $value = $row[$column]; + if(!in_array($value, $results)){ + $results[] = $row[$column]; + } + } + $this->presenter->payload->payload = $results; + $this->presenter->sendPayload(); + } + } + } + + public function handleAddRow() + { + $this->showAddRow = TRUE; + } + + /** + * @param int $id + */ + public function handleShowRowForm($id) + { + $this->activeRowForm = $id; + } + + /** + * @param $callback + */ + public function setRowFormCallback($callback) + { + $this->rowFormCallback = $callback; + } + + /** + * @param int $id + * @return \Nette\Forms\Controls\Checkbox + */ + public function assignCheckboxToRow($id) + { + $this['gridForm'][$this->name]['action']->addCheckbox("row_".$id); + $this['gridForm'][$this->name]['action']["row_".$id]->getControlPrototype()->class[] = "grid-action-checkbox"; + return $this['gridForm'][$this->name]['action']["row_".$id]->getControl(); + } + + protected function createComponentGridForm() + { + $form = new \Nette\Application\UI\Form; + $form->method = "POST"; + $form->getElementPrototype()->class[] = "grid-gridForm"; + + $form->addContainer($this->name); + + $form[$this->name]->addContainer("rowForm"); + $form[$this->name]['rowForm']->addSubmit("send","Uložit"); + $form[$this->name]['rowForm']['send']->getControlPrototype()->addClass("grid-editable"); + + $form[$this->name]->addContainer("filter"); + $form[$this->name]['filter']->addSubmit("send","Filtrovat") + ->setValidationScope(FALSE); + + $form[$this->name]->addContainer("action"); + $form[$this->name]['action']->addSelect("action_name","Označené:"); + $form[$this->name]['action']->addSubmit("send","Potvrdit") + ->setValidationScope(FALSE) + ->getControlPrototype() + ->addData("select", $form[$this->name]["action"]["action_name"]->getControl()->name); + + $form[$this->name]->addContainer('perPage'); + $form[$this->name]['perPage']->addSelect("perPage","Záznamů na stranu:", $this->perPageValues) + ->getControlPrototype() + ->addClass("grid-changeperpage") + ->addData("gridname", $this->getGridPath()) + ->addData("link", $this->link("changePerPage!")); + $form[$this->name]['perPage']->addSubmit("send","Ok") + ->setValidationScope(FALSE) + ->getControlPrototype() + ->addClass("grid-perpagesubmit"); + + $form->onSuccess[] = callback($this, "processGridForm"); + + return $form; + } + + /** + * @param array $values + */ + public function processGridForm($values) + { + $values = $values->getHttpData(); + foreach($values as $gridName => $grid){ + foreach($grid as $section => $container){ + foreach($container as $key => $value){ + if($key == "send"){ + unset($container[$key]); + $subGrids = $this->subGrids; + foreach($subGrids as $subGrid){ + $path = explode("-", $subGrid); + if(end($path) == $gridName){ + $gridName = $subGrid; + break; + } + } + if($section == "filter"){ + $this->filterFormSubmitted($values); + } + $section = ($section == "rowForm") ? "row" : $section; + if(method_exists($this, $section."FormSubmitted")){ + call_user_func("self::".$section."FormSubmitted", $container, $gridName); + }else{ + $this->redirect("this"); + } + break 3; + } + } + } + } + } + + /** + * @param array $values + * @param string $gridName + */ + public function rowFormSubmitted($values, $gridName) + { + $subGrid = ($gridName == $this->name) ? FALSE : TRUE; + if($subGrid){ + call_user_func($this[$gridName]->rowFormCallback, (array) $values); + }else{ + call_user_func($this->rowFormCallback, (array) $values); + } + $this->redirect("this"); + } + + /** + * @param array $values + * @param string $gridName + */ + public function perPageFormSubmitted($values, $gridName) + { + $perPage = ($gridName == $this->name) ? "perPage" : $gridName."-perPage"; + + $this->redirect("this", array($perPage => $values["perPage"])); + } + + /** + * @param array $values + * @param string $gridName + * @throws NoRowSelectedException + */ + public function actionFormSubmitted($values, $gridName) + { + try{ + $rows = array(); + foreach($values as $name => $value){ + if(\Nette\Utils\Strings::startsWith($name, "row")){ + $vals = explode("_", $name); + if((boolean) $value){ + $rows[] = $vals[1]; + } + } + } + $subGrid = ($gridName == $this->name) ? FALSE : TRUE; + if(!count($rows)){ + throw new NoRowSelectedException("Nebyl vybrán žádný záznam."); + } + if($subGrid){ + call_user_func($this[$gridName]['actions']->components[$values['action_name']]->getCallback(), $rows); + }else{ + call_user_func($this['actions']->components[$values['action_name']]->getCallback(), $rows); + } + $this->redirect("this"); + } + catch(NoRowSelectedException $e){ + if($subGrid){ + $this[$gridName]->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); + }else{ + $this->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); + } + $this->redirect("this"); + } + } + + /** + * @param array $values + */ + public function filterFormSubmitted($values) + { + $filters = array(); + $paginators = array(); + foreach($values as $gridName => $grid){ + $isSubGrid = ($gridName == $this->name) ? FALSE : TRUE; + foreach($grid['filter'] as $name => $value){ + if($value != ''){ + if($name == "send"){ + continue; + } + if($isSubGrid){ + $gridName = $this->findSubGridPath($gridName); + $filters[$this->name."-".$gridName."-filter"][$name] = $value; + }else{ + $filters[$this->name."-filter"][$name] = $value; + } + } + } + if($isSubGrid){ + $paginators[$this->name."-".$gridName."-paginator-page"] = NULL; + if(empty($filters[$this->name."-".$gridName."-filter"])) $filters[$this->name."-".$gridName."-filter"] = array(); + }else{ + $paginators[$this->name."-paginator-page"] = NULL; + if(empty($filters[$this->name."-filter"])) $filters[$this->name."-filter"] = array(); + } + } + $this->presenter->redirect("this", array_merge($filters, $paginators)); + } + + /** + * @param string $templatePath + */ + protected function setTemplate($templatePath) + { + $this->templatePath = $templatePath; + } + + public function render() + { + $this->getPaginator()->itemCount = $this->count; + $this->template->results = $this->count; + $this->template->columns = $this['columns']->components; + $this->template->buttons = $this['buttons']->components; + $this->template->globalButtons = $this['globalButtons']->components; + $this->template->subGrids = $this['subGrids']->components; + $this->template->paginate = $this->paginate; + $this->template->colsCount = $this->getColsCount(); + $rows = $this->dataSource->getData(); + $this->template->rows = $rows; + $this->template->primaryKey = $this->primaryKey; + if($this->hasActiveRowForm()){ + $row = $rows[$this->activeRowForm]; + foreach($row as $name => $value){ + if($this->columnExists($name) && !empty($this['columns']->components[$name]->formRenderer)){ + $row[$name] = call_user_func($this['columns']->components[$name]->formRenderer, $row); + } + if(isset($this['gridForm'][$this->name]['rowForm'][$name])){ + $input = $this['gridForm'][$this->name]['rowForm'][$name]; + if($input instanceof \Nette\Forms\Controls\SelectBox){ + $items = $this['gridForm'][$this->name]['rowForm'][$name]->getItems(); + if(in_array($row[$name], $items)){ + $row[$name] = array_search($row[$name], $items); + } + } + } + } + $this['gridForm'][$this->name]['rowForm']->setDefaults($row); + $this['gridForm'][$this->name]['rowForm']->addHidden($this->primaryKey, $this->activeRowForm); + } + if($this->paginate){ + $this->template->viewedFrom = ((($this->getPaginator()->getPage()-1)*$this->perPage)+1); + $this->template->viewedTo = ($this->getPaginator()->getLength()+(($this->getPaginator()->getPage()-1)*$this->perPage)); + } + $templatePath = !empty($this->templatePath) ? $this->templatePath : __DIR__."/templates/grid.latte"; + $this->template->setFile($templatePath); + $this->template->render(); + } +} \ No newline at end of file From c2d66abf1b507c837cbb185659db47bdc3a6728d Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Fri, 28 Sep 2012 16:25:33 +0200 Subject: [PATCH 2/7] Remove backup file --- Grid.php~ | 999 ------------------------------------------------------ 1 file changed, 999 deletions(-) delete mode 100644 Grid.php~ diff --git a/Grid.php~ b/Grid.php~ deleted file mode 100644 index a5bb429..0000000 --- a/Grid.php~ +++ /dev/null @@ -1,999 +0,0 @@ - 20, 50 => 50, 100 => 100); - - /** @var bool */ - public $paginate = TRUE; - - /** @var string */ - protected $defaultOrder; - - /** @var string */ - protected $secondOrder; - - /** @var IDataSource */ - protected $dataSource; - - /** @var string */ - protected $primaryKey; - - /** @var int */ - protected $count; - - /** @var string */ - public $width; - - /** @var bool */ - public $enableSorting = TRUE; - - /** @var int */ - public $activeRowForm; - - /** @var callback */ - public $rowFormCallback; - - /** @var bool */ - public $showAddRow = FALSE; - - /** @var bool */ - public $isSubGrid = FALSE; - - /** @var array */ - public $subGrids = array(); - - /** @var callback */ - public $afterConfigureSettings; - - /** @var string */ - protected $templatePath; - - /** @var string */ - public $messageNoRecords = 'Žádné záznamy'; - - /** - * @param \Nette\Application\UI\Presenter $presenter - */ - protected function attached($presenter) - { - parent::attached($presenter); - - $this->addComponent(New \Nette\ComponentModel\Container(), "columns"); - $this->addComponent(New \Nette\ComponentModel\Container(), "buttons"); - $this->addComponent(New \Nette\ComponentModel\Container(), "globalButtons"); - $this->addComponent(New \Nette\ComponentModel\Container(), "actions"); - $this->addComponent(New \Nette\ComponentModel\Container(), "subGrids"); - - if($presenter->isAjax()){ - $this->invalidateControl(); - } - - $this->configure($presenter); - - if($this->isSubGrid && !empty($this->afterConfigureSettings)){ - call_user_func($this->afterConfigureSettings, $this); - } - - if($this->hasActiveSubGrid()){ - $subGrid = $this->addComponent($this['subGrids']->components[$this->activeSubGridName]->getGrid(), "subGrid".$this->activeSubGridName); - $subGrid->registerSubGrid("subGrid".$this->activeSubGridName); - } - - if($this->hasActionForm()){ - $actions = array(); - foreach($this['actions']->components as $name => $action){ - $actions[$name] = $action->getAction(); - } - $this['gridForm'][$this->name]['action']['action_name']->setItems($actions); - } - if($this->paginate){ - if($this->hasActiveItemPerPage()){ - if(in_array($this->perPage, $this['gridForm'][$this->name]['perPage']['perPage']->items)){ - $this['gridForm'][$this->name]['perPage']->setDefaults(array("perPage" => $this->perPage)); - }else{ - $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); - $this->perPage = reset($items); - } - }else{ - $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); - $this->perPage = reset($items); - } - $this->getPaginator()->itemsPerPage = $this->perPage; - } - if($this->hasActiveFilter()){ - $this->filterData(); - $this['gridForm'][$this->name]['filter']->setDefaults($this->filter); - } - if($this->hasActiveOrder() && $this->hasEnabledSorting()){ - $this->orderData($this->order); - } - if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){ - $this->orderData($this->defaultOrder); - } - $this->count = $this->getCount(); - } - - /** - * @param string $subGrid - */ - public function registerSubGrid($subGrid) - { - if(!$this->isSubGrid){ - $this->subGrids[] = $subGrid; - }else{ - $this->parent->registerSubGrid($this->name."-".$subGrid); - } - } - - /** - * @return array - */ - public function getSubGrids() - { - if($this->isSubGrid){ - return $this->parent->getSubGrids(); - }else{ - return $this->subGrids; - } - } - - /** - * @param null|string $gridName - * @return string - */ - public function getGridPath($gridName = NULL) - { - if(empty($gridName)){ - $gridName = $this->name; - }else{ - $gridName = $this->name."-".$gridName; - } - if($this->isSubGrid){ - return $this->parent->getGridPath($gridName); - }else{ - return $gridName; - } - } - - public function findSubGridPath($gridName) - { - foreach($this->subGrids as $subGrid){ - $path = explode("-", $subGrid); - if(end($path) == $gridName){ - return $subGrid; - } - } - } - - /** - * @param string $columnName - * @return \Nette\Forms\IControl - * @throws UnknownColumnException - */ - public function getColumnInput($columnName) - { - if(!$this->columnExists($columnName)){ - throw new UnknownColumnException("Column $columnName doesn't exists."); - } - return $this['gridForm'][$this->name]['rowForm'][$columnName]; - } - - /** - * @param string $name - * @param null|string $label - * @param null|string $width - * @param null|int $truncate - * @return Column - * @throws DuplicateColumnException - * @return \Nifty\Grid\Column - */ - protected function addColumn($name, $label = NULL, $width = NULL, $truncate = NULL) - { - if(!empty($this['columns']->components[$name])){ - throw new DuplicateColumnException("Column $name already exists."); - } - $column = new Column($this['columns'], $name); - $column->setName($name) - ->setLabel($label) - ->setWidth($width) - ->setTruncate($truncate) - ->injectParent($this); - - return $column; - } - - /** - * @param string $name - * @param null|string $label - * @return Button - * @throws DuplicateButtonException - */ - protected function addButton($name, $label = NULL) - { - if(!empty($this['buttons']->components[$name])){ - throw new DuplicateButtonException("Button $name already exists."); - } - $button = new Button($this['buttons'], $name); - if($name == self::ROW_FORM){ - $self = $this; - $primaryKey = $this->primaryKey; - $button->setLink(function($row) use($self, $primaryKey){ - return $self->link("showRowForm!", $row[$primaryKey]); - }); - } - $button->setLabel($label); - return $button; - } - - - /** - * @param string $name - * @param null|string $label - * @throws DuplicateGlobalButtonException - * @return GlobalButton - */ - public function addGlobalButton($name, $label = NULL) - { - if(!empty($this['globalButtons']->components[$name])){ - throw new DuplicateGlobalButtonException("Global button $name already exists."); - } - $globalButton = new GlobalButton($this['globalButtons'], $name); - if($name == self::ADD_ROW){ - $globalButton->setLink($this->link("addRow!")); - } - $globalButton->setLabel($label); - return $globalButton; - } - - /** - * @param string $name - * @param null|string $label - * @return Action - * @throws DuplicateActionException - */ - public function addAction($name, $label = NULL) - { - if(!empty($this['actions']->components[$name])){ - throw new DuplicateActionException("Action $name already exists."); - } - $action = new Action($this['actions'], $name); - $action->setName($name) - ->setLabel($label); - - return $action; - } - - /** - * @param string $name - * @param null|string $label - * @return SubGrid - * @throws DuplicateSubGridException - */ - public function addSubGrid($name, $label = NULL) - { - if(!empty($this['subGrids']->components[$name]) || in_array($name, $this->getSubGrids())){ - throw new DuplicateSubGridException("SubGrid $name already exists."); - } - $self = $this; - $primaryKey = $this->primaryKey; - $subGrid = new SubGrid($this['subGrids'], $name); - $subGrid->setName($name) - ->setLabel($label); - if($this->activeSubGridName == $name){ - $subGrid->setClass("grid-subgrid-close"); - $subGrid->setClass(function($row) use ($self, $primaryKey){ - return $row[$primaryKey] == $self->activeSubGridId ? "grid-subgrid-close" : "grid-subgrid-open"; - }); - $subGrid->setLink(function($row) use ($self, $name, $primaryKey){ - $link = $row[$primaryKey] == $self->activeSubGridId ? array("activeSubGridId" => NULL, "activeSubGridName" => NULL) : array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name); - return $self->link("this", $link); - }); - } - else{ - $subGrid->setClass("grid-subgrid-open") - ->setLink(function($row) use ($self, $name, $primaryKey){ - return $self->link("this", array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name)); - }); - } - return $subGrid; - } - - /** - * @return array - */ - public function getColumnNames() - { - foreach($this['columns']->components as $column){ - $columns[] = $column->name; - } - return $columns; - } - - /** - * @return int $count - */ - public function getColsCount() - { - $count = count($this['columns']->components); - $this->hasActionForm() ? $count++ : $count; - ($this->hasButtons() || $this->hasFilterForm()) ? $count++ : $count; - $count += count($this['subGrids']->components); - - return $count; - } - - /** - * @param IDataSource $dataSource - */ - protected function setDataSource(IDataSource $dataSource) - { - $this->dataSource = $dataSource; - $this->primaryKey = $this->dataSource->getPrimaryKey(); - } - - /** - * @param string $width - */ - public function setWidth($width) - { - $this->width = $width; - } - - /** - * @param string $messageNoRecords - */ - public function setMessageNoRecords($messageNoRecords) - { - $this->messageNoRecords = $messageNoRecords; - } - - /** - * @param string $order - */ - public function setDefaultOrder($order) - { - if(!is_array($order)){ - $order = explode(" ", trim($order)); - } - if($order[1] == 1 || strtoupper($order[1]) == "ASC"){ - $order[1] = "ASC"; - }else{ - $order[1] = "DESC"; - } - $order[2] = "default"; - $this->defaultOrder = $order; - } - - /** - * @param mixed $order - * @param mixed $way - */ - public function setSecondOrder($order, $way = 0) - { - if(!is_array($order)){ - if($way == 1 || strtoupper($way) == "ASC"){ - $way = "ASC"; - }else{ - $way = "DESC"; - } - $this->secondOrder = array($order, $way); - }else{ - foreach($order as $key => $value){ - if(is_array($value)){ - if($value[1] == 1 || strtoupper($value[1]) == "ASC"){ - $order[$key][1] = "ASC"; - }else{ - $order[$key][1] = "DESC"; - } - } - } - $this->secondOrder = $order; - } - } - - /** - * @param array $values - * @return array - */ - protected function setPerPageValues(array $values) - { - $perPageValues = array(); - foreach($values as $value){ - $perPageValues[$value] = $value; - } - $this->perPageValues = $perPageValues; - } - - /** - * @return bool - */ - public function hasButtons() - { - return count($this['buttons']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasGlobalButtons() - { - return count($this['globalButtons']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasFilterForm() - { - foreach($this['columns']->components as $column){ - if(!empty($column->filterType)) - return TRUE; - } - return FALSE; - } - - /** - * @return bool - */ - public function hasActionForm() - { - return count($this['actions']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasActiveFilter() - { - return count($this->filter) ? TRUE : FALSE; - } - - /** - * @param string $filter - * @return bool - */ - public function isSpecificFilterActive($filter) - { - if(isset($this->filter[$filter])){ - return ($this->filter[$filter] != '') ? TRUE : FALSE; - } - return false; - } - - /** - * @return bool - */ - public function hasActiveOrder() - { - return !empty($this->order) ? TRUE: FALSE; - } - - /** - * @return bool - */ - public function hasDefaultOrder() - { - return !empty($this->defaultOrder) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasSecondOrder() - { - return !empty($this->secondOrder) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasEnabledSorting() - { - return $this->enableSorting; - } - - /** - * @return bool - */ - public function hasActiveItemPerPage() - { - return !empty($this->perPage) ? TRUE : FALSE; - } - - public function hasActiveRowForm() - { - return !empty($this->activeRowForm) ? TRUE : FALSE; - } - - /** - * @param string $column - * @return bool - */ - public function columnExists($column) - { - return isset($this['columns']->components[$column]); - } - - /** - * @param string $subGrid - * @return bool - */ - public function subGridExists($subGrid) - { - return isset($this['subGrids']->components[$subGrid]); - } - - /** - * @return bool - */ - public function isEditable() - { - foreach($this['columns']->components as $component){ - if($component->editable) - return TRUE; - } - return FALSE; - } - - /** - * @return bool - */ - public function hasActiveSubGrid() - { - return (!empty($this->activeSubGridId) && !empty($this->activeSubGridName) && $this->subGridExists($this->activeSubGridName)) ? TRUE : FALSE; - } - - /** - * @return mixed - * @throws InvalidFilterException - * @throws UnknownColumnException - * @throws UnknownFilterException - */ - protected function filterData() - { - try{ - $filters = array(); - foreach($this->filter as $name => $value){ - if(!$this->columnExists($name)){ - throw new UnknownColumnException("Neexistující sloupec $name"); - - } - if(!$this['columns-'.$name]->hasFilter()){ - throw new UnknownFilterException("Neexistující filtr pro sloupec $name"); - } - - $type = $this['columns-'.$name]->getFilterType(); - $filter = FilterCondition::prepareFilter($value, $type); - - if(method_exists("\\NiftyGrid\\FilterCondition", $filter["condition"])){ - $filter = call_user_func("\\NiftyGrid\\FilterCondition::".$filter["condition"], $filter["value"]); - if(!empty($this['gridForm'][$this->name]['filter'][$name])){ - $filter["column"] = $name; - if(!empty($this['columns-'.$filter["column"]]->tableName)){ - $filter["column"] = $this['columns-'.$filter["column"]]->tableName; - } - $filters[] = $filter; - }else{ - throw new InvalidFilterException("Neplatný filtr"); - } - }else{ - throw new InvalidFilterException("Neplatný filtr"); - } - } - return $this->dataSource->filterData($filters); - } - catch(UnknownColumnException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("filter" => NULL)); - } - catch(UnknownFilterException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("filter" => NULL)); - } - } - - /** - * @param string $order - * @param bool $second - * @throws InvalidOrderException - */ - protected function orderData($order, $second = false) - { - $orders = array(); - try{ - if(!is_array($order)) { - $order = explode(" ", $order); - } - if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ - $orders[] = $order; - }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ - $orders[] = $order; - } - else{ - throw new InvalidOrderException("Neplatné seřazení."); - } - if($this->hasSecondOrder() && $second === false){ - if(is_array($this->secondOrder[0])){ - foreach($this->secondOrder[0] as $secondOrder){ - if($secondOrder[0] != $orders[0][0]){ - $orders[] = $this->orderData($secondOrder, true); - } - } - }else{ - if($this->secondOrder[0] != $orders[0][0]){ - $orders[] = $this->orderData($this->secondOrder, true); - } - } - $this->dataSource->multipleOrderData($orders); - }elseif($second === true){ - $return $order; - }else{ - $this->dataSource->orderData($order[0], $order[1]); - } - } - catch(InvalidOrderException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("order" => NULL)); - } - } - - /** - * @return int - */ - protected function getCount() - { - if($this->paginate){ - $count = $this->dataSource->getCount(); - $this->getPaginator()->itemCount = $count; - $this->dataSource->limitData($this->getPaginator()->itemsPerPage, $this->getPaginator()->offset); - return $count; - }else{ - $count = $this->dataSource->getCount(); - $this->getPaginator()->itemCount = $count; - return $count; - } - } - - /** - * @return GridPaginator - */ - protected function createComponentPaginator() - { - return new GridPaginator; - } - - /** - * @return \Nette\Utils\Paginator - */ - public function getPaginator() - { - return $this['paginator']->paginator; - } - - /** - * @param int $page - */ - public function handleChangeCurrentPage($page) - { - if($this->presenter->isAjax()){ - $this->redirect("this", array("paginator-page" => $page)); - } - } - - /** - * @param int $perPage - */ - public function handleChangePerPage($perPage) - { - if($this->presenter->isAjax()){ - $this->redirect("this", array("perPage" => $perPage)); - } - } - - /** - * @param string $column - * @param string $term - */ - public function handleAutocomplete($column, $term) - { - if($this->presenter->isAjax()){ - if(!empty($this['columns']->components[$column]) && $this['columns']->components[$column]->autocomplete){ - $this->filter[$column] = $term."%"; - $this->filterData(); - $this->dataSource->limitData($this['columns']->components[$column]->getAutocompleteResults(), NULL); - $data = $this->dataSource->getData(); - $results = array(); - foreach($data as $row){ - $value = $row[$column]; - if(!in_array($value, $results)){ - $results[] = $row[$column]; - } - } - $this->presenter->payload->payload = $results; - $this->presenter->sendPayload(); - } - } - } - - public function handleAddRow() - { - $this->showAddRow = TRUE; - } - - /** - * @param int $id - */ - public function handleShowRowForm($id) - { - $this->activeRowForm = $id; - } - - /** - * @param $callback - */ - public function setRowFormCallback($callback) - { - $this->rowFormCallback = $callback; - } - - /** - * @param int $id - * @return \Nette\Forms\Controls\Checkbox - */ - public function assignCheckboxToRow($id) - { - $this['gridForm'][$this->name]['action']->addCheckbox("row_".$id); - $this['gridForm'][$this->name]['action']["row_".$id]->getControlPrototype()->class[] = "grid-action-checkbox"; - return $this['gridForm'][$this->name]['action']["row_".$id]->getControl(); - } - - protected function createComponentGridForm() - { - $form = new \Nette\Application\UI\Form; - $form->method = "POST"; - $form->getElementPrototype()->class[] = "grid-gridForm"; - - $form->addContainer($this->name); - - $form[$this->name]->addContainer("rowForm"); - $form[$this->name]['rowForm']->addSubmit("send","Uložit"); - $form[$this->name]['rowForm']['send']->getControlPrototype()->addClass("grid-editable"); - - $form[$this->name]->addContainer("filter"); - $form[$this->name]['filter']->addSubmit("send","Filtrovat") - ->setValidationScope(FALSE); - - $form[$this->name]->addContainer("action"); - $form[$this->name]['action']->addSelect("action_name","Označené:"); - $form[$this->name]['action']->addSubmit("send","Potvrdit") - ->setValidationScope(FALSE) - ->getControlPrototype() - ->addData("select", $form[$this->name]["action"]["action_name"]->getControl()->name); - - $form[$this->name]->addContainer('perPage'); - $form[$this->name]['perPage']->addSelect("perPage","Záznamů na stranu:", $this->perPageValues) - ->getControlPrototype() - ->addClass("grid-changeperpage") - ->addData("gridname", $this->getGridPath()) - ->addData("link", $this->link("changePerPage!")); - $form[$this->name]['perPage']->addSubmit("send","Ok") - ->setValidationScope(FALSE) - ->getControlPrototype() - ->addClass("grid-perpagesubmit"); - - $form->onSuccess[] = callback($this, "processGridForm"); - - return $form; - } - - /** - * @param array $values - */ - public function processGridForm($values) - { - $values = $values->getHttpData(); - foreach($values as $gridName => $grid){ - foreach($grid as $section => $container){ - foreach($container as $key => $value){ - if($key == "send"){ - unset($container[$key]); - $subGrids = $this->subGrids; - foreach($subGrids as $subGrid){ - $path = explode("-", $subGrid); - if(end($path) == $gridName){ - $gridName = $subGrid; - break; - } - } - if($section == "filter"){ - $this->filterFormSubmitted($values); - } - $section = ($section == "rowForm") ? "row" : $section; - if(method_exists($this, $section."FormSubmitted")){ - call_user_func("self::".$section."FormSubmitted", $container, $gridName); - }else{ - $this->redirect("this"); - } - break 3; - } - } - } - } - } - - /** - * @param array $values - * @param string $gridName - */ - public function rowFormSubmitted($values, $gridName) - { - $subGrid = ($gridName == $this->name) ? FALSE : TRUE; - if($subGrid){ - call_user_func($this[$gridName]->rowFormCallback, (array) $values); - }else{ - call_user_func($this->rowFormCallback, (array) $values); - } - $this->redirect("this"); - } - - /** - * @param array $values - * @param string $gridName - */ - public function perPageFormSubmitted($values, $gridName) - { - $perPage = ($gridName == $this->name) ? "perPage" : $gridName."-perPage"; - - $this->redirect("this", array($perPage => $values["perPage"])); - } - - /** - * @param array $values - * @param string $gridName - * @throws NoRowSelectedException - */ - public function actionFormSubmitted($values, $gridName) - { - try{ - $rows = array(); - foreach($values as $name => $value){ - if(\Nette\Utils\Strings::startsWith($name, "row")){ - $vals = explode("_", $name); - if((boolean) $value){ - $rows[] = $vals[1]; - } - } - } - $subGrid = ($gridName == $this->name) ? FALSE : TRUE; - if(!count($rows)){ - throw new NoRowSelectedException("Nebyl vybrán žádný záznam."); - } - if($subGrid){ - call_user_func($this[$gridName]['actions']->components[$values['action_name']]->getCallback(), $rows); - }else{ - call_user_func($this['actions']->components[$values['action_name']]->getCallback(), $rows); - } - $this->redirect("this"); - } - catch(NoRowSelectedException $e){ - if($subGrid){ - $this[$gridName]->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); - }else{ - $this->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); - } - $this->redirect("this"); - } - } - - /** - * @param array $values - */ - public function filterFormSubmitted($values) - { - $filters = array(); - $paginators = array(); - foreach($values as $gridName => $grid){ - $isSubGrid = ($gridName == $this->name) ? FALSE : TRUE; - foreach($grid['filter'] as $name => $value){ - if($value != ''){ - if($name == "send"){ - continue; - } - if($isSubGrid){ - $gridName = $this->findSubGridPath($gridName); - $filters[$this->name."-".$gridName."-filter"][$name] = $value; - }else{ - $filters[$this->name."-filter"][$name] = $value; - } - } - } - if($isSubGrid){ - $paginators[$this->name."-".$gridName."-paginator-page"] = NULL; - if(empty($filters[$this->name."-".$gridName."-filter"])) $filters[$this->name."-".$gridName."-filter"] = array(); - }else{ - $paginators[$this->name."-paginator-page"] = NULL; - if(empty($filters[$this->name."-filter"])) $filters[$this->name."-filter"] = array(); - } - } - $this->presenter->redirect("this", array_merge($filters, $paginators)); - } - - /** - * @param string $templatePath - */ - protected function setTemplate($templatePath) - { - $this->templatePath = $templatePath; - } - - public function render() - { - $this->getPaginator()->itemCount = $this->count; - $this->template->results = $this->count; - $this->template->columns = $this['columns']->components; - $this->template->buttons = $this['buttons']->components; - $this->template->globalButtons = $this['globalButtons']->components; - $this->template->subGrids = $this['subGrids']->components; - $this->template->paginate = $this->paginate; - $this->template->colsCount = $this->getColsCount(); - $rows = $this->dataSource->getData(); - $this->template->rows = $rows; - $this->template->primaryKey = $this->primaryKey; - if($this->hasActiveRowForm()){ - $row = $rows[$this->activeRowForm]; - foreach($row as $name => $value){ - if($this->columnExists($name) && !empty($this['columns']->components[$name]->formRenderer)){ - $row[$name] = call_user_func($this['columns']->components[$name]->formRenderer, $row); - } - if(isset($this['gridForm'][$this->name]['rowForm'][$name])){ - $input = $this['gridForm'][$this->name]['rowForm'][$name]; - if($input instanceof \Nette\Forms\Controls\SelectBox){ - $items = $this['gridForm'][$this->name]['rowForm'][$name]->getItems(); - if(in_array($row[$name], $items)){ - $row[$name] = array_search($row[$name], $items); - } - } - } - } - $this['gridForm'][$this->name]['rowForm']->setDefaults($row); - $this['gridForm'][$this->name]['rowForm']->addHidden($this->primaryKey, $this->activeRowForm); - } - if($this->paginate){ - $this->template->viewedFrom = ((($this->getPaginator()->getPage()-1)*$this->perPage)+1); - $this->template->viewedTo = ($this->getPaginator()->getLength()+(($this->getPaginator()->getPage()-1)*$this->perPage)); - } - $templatePath = !empty($this->templatePath) ? $this->templatePath : __DIR__."/templates/grid.latte"; - $this->template->setFile($templatePath); - $this->template->render(); - } -} \ No newline at end of file From 790e7ba46e873f12d740aaeefc5bf62e542dc491 Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Fri, 28 Sep 2012 16:53:23 +0200 Subject: [PATCH 3/7] Removed my bad old file, some bugfixes --- Components/Buttonftp.php | 253 --------------------------------------- Grid.php | 10 ++ 2 files changed, 10 insertions(+), 253 deletions(-) delete mode 100644 Components/Buttonftp.php diff --git a/Components/Buttonftp.php b/Components/Buttonftp.php deleted file mode 100644 index f4ab602..0000000 --- a/Components/Buttonftp.php +++ /dev/null @@ -1,253 +0,0 @@ -label = $label; - - return $this; - } - - /** - * @param array $row - * @return string - */ - private function getLabel($row) - { - if(is_callable($this->label)){ - return call_user_func($this->label, $row); - } - return $this->label; - } - - /** - * @param callback|string $link - * @return Button - */ - public function setLink($link) - { - $this->link = $link; - - return $this; - } - - /** - * @param array $row - * @return string - */ - private function getLink($row) - { - if(is_callable($this->link)){ - return call_user_func($this->link, $row); - } - return $this->link; - } - - /** - * @param $text - * @return mixed - */ - public function setText($text) - { - $this->text = $text; - - return $this; - } - - /** - * @param array $row - * @return string - */ - private function getText($row) - { - if(is_callable($this->text)){ - return call_user_func($this->text, $row); - } - return $this->text; - } - - /** - * @param callback|string $target - * @return Button - */ - public function setTarget($target) - { - $this->target = $target; - - return $this; - } - - /** - * @param array $row - * @return callback|mixed|string - */ - private function getTarget($row) - { - if(is_callable($this->target)){ - return call_user_func($this->target, $row); - } - return $this->target; - } - - /** - * @param callback|string $class - * @return Button - */ - public function setClass($class) - { - $this->class = $class; - - return $this; - } - - /** - * @param array $row - * @return callback|mixed|string - */ - private function getClass($row) - { - if(is_callable($this->class)){ - return call_user_func($this->class, $row); - } - return $this->class; - } - - /** - * @param bool $ajax - * @return Button - */ - public function setAjax($ajax = TRUE) - { - $this->ajax = $ajax; - - return $this; - } - - /** - * @param callback|string $message - * @return Button - */ - public function setConfirmationDialog($message) - { - $this->dialog = $message; - - return $this; - } - - /** - * @param array $row - * @return callback|mixed|string - */ - public function getConfirmationDialog($row) - { - if(is_callable($this->dialog)){ - return call_user_func($this->dialog, $row); - } - return $this->dialog; - } - - /** - * @return bool - */ - private function hasConfirmationDialog() - { - return (!empty($this->dialog)) ? TRUE : FALSE; - } - - /** - * @param callback|string $show - * @return Button - */ - public function setShow($show) - { - $this->show = $show; - - return $this; - } - - /** - * @param array $row - * @return callback|mixed|string - */ - public function getShow($row) - { - if(is_callable($this->show)){ - return (boolean) call_user_func($this->show, $row); - } - return $this->show; - } - - /** - * @param array $row - */ - public function render($row) - { - if(!$this->getShow($row)){ - return false; - } - - $el = Html::el("a") - ->href($this->getLink($row)) - ->setText($this->getText($row)) - ->addClass("grid-button") - ->addClass($this->getClass($row)) - ->setTitle($this->getLabel($row)) - ->setTarget($this->getTarget($row)); - - if($this->getName() == Grid::ROW_FORM) { - $el->addClass("grid-editable"); - } - - if($this->hasConfirmationDialog()){ - $el->addClass("grid-confirm") - ->addData("grid-confirm", $this->getConfirmationDialog($row)); - } - - if($this->ajax){ - $el->addClass("grid-ajax"); - } - echo $el; - } - -} diff --git a/Grid.php b/Grid.php index f55b54f..a1ecf2e 100644 --- a/Grid.php +++ b/Grid.php @@ -401,6 +401,11 @@ public function setDefaultOrder($order) public function setSecondOrder($order, $way = 0) { if(!is_array($order)){ + $orderExp = explode(" ", $order); + if(count($orderExp > 1){ + $order = $orderExp[0]; + $way = $orderExp[1]; + } if($way == 1 || strtoupper($way) == "ASC"){ $way = "ASC"; }else{ @@ -415,6 +420,11 @@ public function setSecondOrder($order, $way = 0) }else{ $order[$key][1] = "DESC"; } + }else{ + $orderExp = explode(" ", $value); + if(count($orderExp > 1){ + $order[$key] = array($orderExp[0], $orderExp[1]); + } } } $this->secondOrder = $order; From 765b8eb65fb8c5b91434aee704843b755218c204 Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Fri, 28 Sep 2012 17:15:07 +0200 Subject: [PATCH 4/7] Fixed a typo --- Grid.php | 4 +- Grid.php~ | 1009 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1011 insertions(+), 2 deletions(-) create mode 100644 Grid.php~ diff --git a/Grid.php b/Grid.php index a1ecf2e..624dc40 100644 --- a/Grid.php +++ b/Grid.php @@ -402,7 +402,7 @@ public function setSecondOrder($order, $way = 0) { if(!is_array($order)){ $orderExp = explode(" ", $order); - if(count($orderExp > 1){ + if(count($orderExp) > 1){ $order = $orderExp[0]; $way = $orderExp[1]; } @@ -422,7 +422,7 @@ public function setSecondOrder($order, $way = 0) } }else{ $orderExp = explode(" ", $value); - if(count($orderExp > 1){ + if(count($orderExp) > 1){ $order[$key] = array($orderExp[0], $orderExp[1]); } } diff --git a/Grid.php~ b/Grid.php~ new file mode 100644 index 0000000..f13e33d --- /dev/null +++ b/Grid.php~ @@ -0,0 +1,1009 @@ + 20, 50 => 50, 100 => 100); + + /** @var bool */ + public $paginate = TRUE; + + /** @var string */ + protected $defaultOrder; + + /** @var string */ + protected $secondOrder; + + /** @var IDataSource */ + protected $dataSource; + + /** @var string */ + protected $primaryKey; + + /** @var int */ + protected $count; + + /** @var string */ + public $width; + + /** @var bool */ + public $enableSorting = TRUE; + + /** @var int */ + public $activeRowForm; + + /** @var callback */ + public $rowFormCallback; + + /** @var bool */ + public $showAddRow = FALSE; + + /** @var bool */ + public $isSubGrid = FALSE; + + /** @var array */ + public $subGrids = array(); + + /** @var callback */ + public $afterConfigureSettings; + + /** @var string */ + protected $templatePath; + + /** @var string */ + public $messageNoRecords = 'Žádné záznamy'; + + /** + * @param \Nette\Application\UI\Presenter $presenter + */ + protected function attached($presenter) + { + parent::attached($presenter); + + $this->addComponent(New \Nette\ComponentModel\Container(), "columns"); + $this->addComponent(New \Nette\ComponentModel\Container(), "buttons"); + $this->addComponent(New \Nette\ComponentModel\Container(), "globalButtons"); + $this->addComponent(New \Nette\ComponentModel\Container(), "actions"); + $this->addComponent(New \Nette\ComponentModel\Container(), "subGrids"); + + if($presenter->isAjax()){ + $this->invalidateControl(); + } + + $this->configure($presenter); + + if($this->isSubGrid && !empty($this->afterConfigureSettings)){ + call_user_func($this->afterConfigureSettings, $this); + } + + if($this->hasActiveSubGrid()){ + $subGrid = $this->addComponent($this['subGrids']->components[$this->activeSubGridName]->getGrid(), "subGrid".$this->activeSubGridName); + $subGrid->registerSubGrid("subGrid".$this->activeSubGridName); + } + + if($this->hasActionForm()){ + $actions = array(); + foreach($this['actions']->components as $name => $action){ + $actions[$name] = $action->getAction(); + } + $this['gridForm'][$this->name]['action']['action_name']->setItems($actions); + } + if($this->paginate){ + if($this->hasActiveItemPerPage()){ + if(in_array($this->perPage, $this['gridForm'][$this->name]['perPage']['perPage']->items)){ + $this['gridForm'][$this->name]['perPage']->setDefaults(array("perPage" => $this->perPage)); + }else{ + $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); + $this->perPage = reset($items); + } + }else{ + $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); + $this->perPage = reset($items); + } + $this->getPaginator()->itemsPerPage = $this->perPage; + } + if($this->hasActiveFilter()){ + $this->filterData(); + $this['gridForm'][$this->name]['filter']->setDefaults($this->filter); + } + if($this->hasActiveOrder() && $this->hasEnabledSorting()){ + $this->orderData($this->order); + } + if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){ + $this->orderData($this->defaultOrder); + } + $this->count = $this->getCount(); + } + + /** + * @param string $subGrid + */ + public function registerSubGrid($subGrid) + { + if(!$this->isSubGrid){ + $this->subGrids[] = $subGrid; + }else{ + $this->parent->registerSubGrid($this->name."-".$subGrid); + } + } + + /** + * @return array + */ + public function getSubGrids() + { + if($this->isSubGrid){ + return $this->parent->getSubGrids(); + }else{ + return $this->subGrids; + } + } + + /** + * @param null|string $gridName + * @return string + */ + public function getGridPath($gridName = NULL) + { + if(empty($gridName)){ + $gridName = $this->name; + }else{ + $gridName = $this->name."-".$gridName; + } + if($this->isSubGrid){ + return $this->parent->getGridPath($gridName); + }else{ + return $gridName; + } + } + + public function findSubGridPath($gridName) + { + foreach($this->subGrids as $subGrid){ + $path = explode("-", $subGrid); + if(end($path) == $gridName){ + return $subGrid; + } + } + } + + /** + * @param string $columnName + * @return \Nette\Forms\IControl + * @throws UnknownColumnException + */ + public function getColumnInput($columnName) + { + if(!$this->columnExists($columnName)){ + throw new UnknownColumnException("Column $columnName doesn't exists."); + } + return $this['gridForm'][$this->name]['rowForm'][$columnName]; + } + + /** + * @param string $name + * @param null|string $label + * @param null|string $width + * @param null|int $truncate + * @return Column + * @throws DuplicateColumnException + * @return \Nifty\Grid\Column + */ + protected function addColumn($name, $label = NULL, $width = NULL, $truncate = NULL) + { + if(!empty($this['columns']->components[$name])){ + throw new DuplicateColumnException("Column $name already exists."); + } + $column = new Column($this['columns'], $name); + $column->setName($name) + ->setLabel($label) + ->setWidth($width) + ->setTruncate($truncate) + ->injectParent($this); + + return $column; + } + + /** + * @param string $name + * @param null|string $label + * @return Button + * @throws DuplicateButtonException + */ + protected function addButton($name, $label = NULL) + { + if(!empty($this['buttons']->components[$name])){ + throw new DuplicateButtonException("Button $name already exists."); + } + $button = new Button($this['buttons'], $name); + if($name == self::ROW_FORM){ + $self = $this; + $primaryKey = $this->primaryKey; + $button->setLink(function($row) use($self, $primaryKey){ + return $self->link("showRowForm!", $row[$primaryKey]); + }); + } + $button->setLabel($label); + return $button; + } + + + /** + * @param string $name + * @param null|string $label + * @throws DuplicateGlobalButtonException + * @return GlobalButton + */ + public function addGlobalButton($name, $label = NULL) + { + if(!empty($this['globalButtons']->components[$name])){ + throw new DuplicateGlobalButtonException("Global button $name already exists."); + } + $globalButton = new GlobalButton($this['globalButtons'], $name); + if($name == self::ADD_ROW){ + $globalButton->setLink($this->link("addRow!")); + } + $globalButton->setLabel($label); + return $globalButton; + } + + /** + * @param string $name + * @param null|string $label + * @return Action + * @throws DuplicateActionException + */ + public function addAction($name, $label = NULL) + { + if(!empty($this['actions']->components[$name])){ + throw new DuplicateActionException("Action $name already exists."); + } + $action = new Action($this['actions'], $name); + $action->setName($name) + ->setLabel($label); + + return $action; + } + + /** + * @param string $name + * @param null|string $label + * @return SubGrid + * @throws DuplicateSubGridException + */ + public function addSubGrid($name, $label = NULL) + { + if(!empty($this['subGrids']->components[$name]) || in_array($name, $this->getSubGrids())){ + throw new DuplicateSubGridException("SubGrid $name already exists."); + } + $self = $this; + $primaryKey = $this->primaryKey; + $subGrid = new SubGrid($this['subGrids'], $name); + $subGrid->setName($name) + ->setLabel($label); + if($this->activeSubGridName == $name){ + $subGrid->setClass("grid-subgrid-close"); + $subGrid->setClass(function($row) use ($self, $primaryKey){ + return $row[$primaryKey] == $self->activeSubGridId ? "grid-subgrid-close" : "grid-subgrid-open"; + }); + $subGrid->setLink(function($row) use ($self, $name, $primaryKey){ + $link = $row[$primaryKey] == $self->activeSubGridId ? array("activeSubGridId" => NULL, "activeSubGridName" => NULL) : array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name); + return $self->link("this", $link); + }); + } + else{ + $subGrid->setClass("grid-subgrid-open") + ->setLink(function($row) use ($self, $name, $primaryKey){ + return $self->link("this", array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name)); + }); + } + return $subGrid; + } + + /** + * @return array + */ + public function getColumnNames() + { + foreach($this['columns']->components as $column){ + $columns[] = $column->name; + } + return $columns; + } + + /** + * @return int $count + */ + public function getColsCount() + { + $count = count($this['columns']->components); + $this->hasActionForm() ? $count++ : $count; + ($this->hasButtons() || $this->hasFilterForm()) ? $count++ : $count; + $count += count($this['subGrids']->components); + + return $count; + } + + /** + * @param IDataSource $dataSource + */ + protected function setDataSource(IDataSource $dataSource) + { + $this->dataSource = $dataSource; + $this->primaryKey = $this->dataSource->getPrimaryKey(); + } + + /** + * @param string $width + */ + public function setWidth($width) + { + $this->width = $width; + } + + /** + * @param string $messageNoRecords + */ + public function setMessageNoRecords($messageNoRecords) + { + $this->messageNoRecords = $messageNoRecords; + } + + /** + * @param string $order + */ + public function setDefaultOrder($order) + { + if(!is_array($order)){ + $order = explode(" ", trim($order)); + } + if($order[1] == 1 || strtoupper($order[1]) == "ASC"){ + $order[1] = "ASC"; + }else{ + $order[1] = "DESC"; + } + $order[2] = "default"; + $this->defaultOrder = $order; + } + + /** + * @param mixed $order + * @param mixed $way + */ + public function setSecondOrder($order, $way = 0) + { + if(!is_array($order)){ + $orderExp = explode(" ", $order); + if(count($orderExp) > 1){ + $order = $orderExp[0]; + $way = $orderExp[1]; + } + if($way == 1 || strtoupper($way) == "ASC"){ + $way = "ASC"; + }else{ + $way = "DESC"; + } + $this->secondOrder = array($order, $way); + }else{ + foreach($order as $key => $value){ + if(is_array($value)){ + if($value[1] == 1 || strtoupper($value[1]) == "ASC"){ + $order[$key][1] = "ASC"; + }else{ + $order[$key][1] = "DESC"; + } + }else{ + $orderExp = explode(" ", $value);echo count($orderExp); + if(count($orderExp) > 1){ + $order[$key] = array($orderExp[0], $orderExp[1]); + } + } + } + $this->secondOrder = $order; + } + } + + /** + * @param array $values + * @return array + */ + protected function setPerPageValues(array $values) + { + $perPageValues = array(); + foreach($values as $value){ + $perPageValues[$value] = $value; + } + $this->perPageValues = $perPageValues; + } + + /** + * @return bool + */ + public function hasButtons() + { + return count($this['buttons']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasGlobalButtons() + { + return count($this['globalButtons']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasFilterForm() + { + foreach($this['columns']->components as $column){ + if(!empty($column->filterType)) + return TRUE; + } + return FALSE; + } + + /** + * @return bool + */ + public function hasActionForm() + { + return count($this['actions']->components) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasActiveFilter() + { + return count($this->filter) ? TRUE : FALSE; + } + + /** + * @param string $filter + * @return bool + */ + public function isSpecificFilterActive($filter) + { + if(isset($this->filter[$filter])){ + return ($this->filter[$filter] != '') ? TRUE : FALSE; + } + return false; + } + + /** + * @return bool + */ + public function hasActiveOrder() + { + return !empty($this->order) ? TRUE: FALSE; + } + + /** + * @return bool + */ + public function hasDefaultOrder() + { + return !empty($this->defaultOrder) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasSecondOrder() + { + return !empty($this->secondOrder) ? TRUE : FALSE; + } + + /** + * @return bool + */ + public function hasEnabledSorting() + { + return $this->enableSorting; + } + + /** + * @return bool + */ + public function hasActiveItemPerPage() + { + return !empty($this->perPage) ? TRUE : FALSE; + } + + public function hasActiveRowForm() + { + return !empty($this->activeRowForm) ? TRUE : FALSE; + } + + /** + * @param string $column + * @return bool + */ + public function columnExists($column) + { + return isset($this['columns']->components[$column]); + } + + /** + * @param string $subGrid + * @return bool + */ + public function subGridExists($subGrid) + { + return isset($this['subGrids']->components[$subGrid]); + } + + /** + * @return bool + */ + public function isEditable() + { + foreach($this['columns']->components as $component){ + if($component->editable) + return TRUE; + } + return FALSE; + } + + /** + * @return bool + */ + public function hasActiveSubGrid() + { + return (!empty($this->activeSubGridId) && !empty($this->activeSubGridName) && $this->subGridExists($this->activeSubGridName)) ? TRUE : FALSE; + } + + /** + * @return mixed + * @throws InvalidFilterException + * @throws UnknownColumnException + * @throws UnknownFilterException + */ + protected function filterData() + { + try{ + $filters = array(); + foreach($this->filter as $name => $value){ + if(!$this->columnExists($name)){ + throw new UnknownColumnException("Neexistující sloupec $name"); + + } + if(!$this['columns-'.$name]->hasFilter()){ + throw new UnknownFilterException("Neexistující filtr pro sloupec $name"); + } + + $type = $this['columns-'.$name]->getFilterType(); + $filter = FilterCondition::prepareFilter($value, $type); + + if(method_exists("\\NiftyGrid\\FilterCondition", $filter["condition"])){ + $filter = call_user_func("\\NiftyGrid\\FilterCondition::".$filter["condition"], $filter["value"]); + if(!empty($this['gridForm'][$this->name]['filter'][$name])){ + $filter["column"] = $name; + if(!empty($this['columns-'.$filter["column"]]->tableName)){ + $filter["column"] = $this['columns-'.$filter["column"]]->tableName; + } + $filters[] = $filter; + }else{ + throw new InvalidFilterException("Neplatný filtr"); + } + }else{ + throw new InvalidFilterException("Neplatný filtr"); + } + } + return $this->dataSource->filterData($filters); + } + catch(UnknownColumnException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("filter" => NULL)); + } + catch(UnknownFilterException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("filter" => NULL)); + } + } + + /** + * @param string $order + * @param bool $second + * @throws InvalidOrderException + */ + protected function orderData($order, $second = false) + { + $orders = array(); + try{ + if(!is_array($order)) { + $order = explode(" ", $order); + } + if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ + $orders[] = $order; + }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ + $orders[] = $order; + } + else{ + throw new InvalidOrderException("Neplatné seřazení."); + } + if($this->hasSecondOrder() && $second === false){ + if(is_array($this->secondOrder[0])){ + foreach($this->secondOrder[0] as $secondOrder){ + if($secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($secondOrder, true); + } + } + }else{ + if($this->secondOrder[0] != $orders[0][0]){ + $orders[] = $this->orderData($this->secondOrder, true); + } + } + $this->dataSource->multipleOrderData($orders); + }elseif($second === true){ + return $order; + }else{ + $this->dataSource->orderData($order[0], $order[1]); + } + } + catch(InvalidOrderException $e){ + $this->flashMessage($e->getMessage(), "grid-error"); + $this->redirect("this", array("order" => NULL)); + } + } + + /** + * @return int + */ + protected function getCount() + { + if($this->paginate){ + $count = $this->dataSource->getCount(); + $this->getPaginator()->itemCount = $count; + $this->dataSource->limitData($this->getPaginator()->itemsPerPage, $this->getPaginator()->offset); + return $count; + }else{ + $count = $this->dataSource->getCount(); + $this->getPaginator()->itemCount = $count; + return $count; + } + } + + /** + * @return GridPaginator + */ + protected function createComponentPaginator() + { + return new GridPaginator; + } + + /** + * @return \Nette\Utils\Paginator + */ + public function getPaginator() + { + return $this['paginator']->paginator; + } + + /** + * @param int $page + */ + public function handleChangeCurrentPage($page) + { + if($this->presenter->isAjax()){ + $this->redirect("this", array("paginator-page" => $page)); + } + } + + /** + * @param int $perPage + */ + public function handleChangePerPage($perPage) + { + if($this->presenter->isAjax()){ + $this->redirect("this", array("perPage" => $perPage)); + } + } + + /** + * @param string $column + * @param string $term + */ + public function handleAutocomplete($column, $term) + { + if($this->presenter->isAjax()){ + if(!empty($this['columns']->components[$column]) && $this['columns']->components[$column]->autocomplete){ + $this->filter[$column] = $term."%"; + $this->filterData(); + $this->dataSource->limitData($this['columns']->components[$column]->getAutocompleteResults(), NULL); + $data = $this->dataSource->getData(); + $results = array(); + foreach($data as $row){ + $value = $row[$column]; + if(!in_array($value, $results)){ + $results[] = $row[$column]; + } + } + $this->presenter->payload->payload = $results; + $this->presenter->sendPayload(); + } + } + } + + public function handleAddRow() + { + $this->showAddRow = TRUE; + } + + /** + * @param int $id + */ + public function handleShowRowForm($id) + { + $this->activeRowForm = $id; + } + + /** + * @param $callback + */ + public function setRowFormCallback($callback) + { + $this->rowFormCallback = $callback; + } + + /** + * @param int $id + * @return \Nette\Forms\Controls\Checkbox + */ + public function assignCheckboxToRow($id) + { + $this['gridForm'][$this->name]['action']->addCheckbox("row_".$id); + $this['gridForm'][$this->name]['action']["row_".$id]->getControlPrototype()->class[] = "grid-action-checkbox"; + return $this['gridForm'][$this->name]['action']["row_".$id]->getControl(); + } + + protected function createComponentGridForm() + { + $form = new \Nette\Application\UI\Form; + $form->method = "POST"; + $form->getElementPrototype()->class[] = "grid-gridForm"; + + $form->addContainer($this->name); + + $form[$this->name]->addContainer("rowForm"); + $form[$this->name]['rowForm']->addSubmit("send","Uložit"); + $form[$this->name]['rowForm']['send']->getControlPrototype()->addClass("grid-editable"); + + $form[$this->name]->addContainer("filter"); + $form[$this->name]['filter']->addSubmit("send","Filtrovat") + ->setValidationScope(FALSE); + + $form[$this->name]->addContainer("action"); + $form[$this->name]['action']->addSelect("action_name","Označené:"); + $form[$this->name]['action']->addSubmit("send","Potvrdit") + ->setValidationScope(FALSE) + ->getControlPrototype() + ->addData("select", $form[$this->name]["action"]["action_name"]->getControl()->name); + + $form[$this->name]->addContainer('perPage'); + $form[$this->name]['perPage']->addSelect("perPage","Záznamů na stranu:", $this->perPageValues) + ->getControlPrototype() + ->addClass("grid-changeperpage") + ->addData("gridname", $this->getGridPath()) + ->addData("link", $this->link("changePerPage!")); + $form[$this->name]['perPage']->addSubmit("send","Ok") + ->setValidationScope(FALSE) + ->getControlPrototype() + ->addClass("grid-perpagesubmit"); + + $form->onSuccess[] = callback($this, "processGridForm"); + + return $form; + } + + /** + * @param array $values + */ + public function processGridForm($values) + { + $values = $values->getHttpData(); + foreach($values as $gridName => $grid){ + foreach($grid as $section => $container){ + foreach($container as $key => $value){ + if($key == "send"){ + unset($container[$key]); + $subGrids = $this->subGrids; + foreach($subGrids as $subGrid){ + $path = explode("-", $subGrid); + if(end($path) == $gridName){ + $gridName = $subGrid; + break; + } + } + if($section == "filter"){ + $this->filterFormSubmitted($values); + } + $section = ($section == "rowForm") ? "row" : $section; + if(method_exists($this, $section."FormSubmitted")){ + call_user_func("self::".$section."FormSubmitted", $container, $gridName); + }else{ + $this->redirect("this"); + } + break 3; + } + } + } + } + } + + /** + * @param array $values + * @param string $gridName + */ + public function rowFormSubmitted($values, $gridName) + { + $subGrid = ($gridName == $this->name) ? FALSE : TRUE; + if($subGrid){ + call_user_func($this[$gridName]->rowFormCallback, (array) $values); + }else{ + call_user_func($this->rowFormCallback, (array) $values); + } + $this->redirect("this"); + } + + /** + * @param array $values + * @param string $gridName + */ + public function perPageFormSubmitted($values, $gridName) + { + $perPage = ($gridName == $this->name) ? "perPage" : $gridName."-perPage"; + + $this->redirect("this", array($perPage => $values["perPage"])); + } + + /** + * @param array $values + * @param string $gridName + * @throws NoRowSelectedException + */ + public function actionFormSubmitted($values, $gridName) + { + try{ + $rows = array(); + foreach($values as $name => $value){ + if(\Nette\Utils\Strings::startsWith($name, "row")){ + $vals = explode("_", $name); + if((boolean) $value){ + $rows[] = $vals[1]; + } + } + } + $subGrid = ($gridName == $this->name) ? FALSE : TRUE; + if(!count($rows)){ + throw new NoRowSelectedException("Nebyl vybrán žádný záznam."); + } + if($subGrid){ + call_user_func($this[$gridName]['actions']->components[$values['action_name']]->getCallback(), $rows); + }else{ + call_user_func($this['actions']->components[$values['action_name']]->getCallback(), $rows); + } + $this->redirect("this"); + } + catch(NoRowSelectedException $e){ + if($subGrid){ + $this[$gridName]->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); + }else{ + $this->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); + } + $this->redirect("this"); + } + } + + /** + * @param array $values + */ + public function filterFormSubmitted($values) + { + $filters = array(); + $paginators = array(); + foreach($values as $gridName => $grid){ + $isSubGrid = ($gridName == $this->name) ? FALSE : TRUE; + foreach($grid['filter'] as $name => $value){ + if($value != ''){ + if($name == "send"){ + continue; + } + if($isSubGrid){ + $gridName = $this->findSubGridPath($gridName); + $filters[$this->name."-".$gridName."-filter"][$name] = $value; + }else{ + $filters[$this->name."-filter"][$name] = $value; + } + } + } + if($isSubGrid){ + $paginators[$this->name."-".$gridName."-paginator-page"] = NULL; + if(empty($filters[$this->name."-".$gridName."-filter"])) $filters[$this->name."-".$gridName."-filter"] = array(); + }else{ + $paginators[$this->name."-paginator-page"] = NULL; + if(empty($filters[$this->name."-filter"])) $filters[$this->name."-filter"] = array(); + } + } + $this->presenter->redirect("this", array_merge($filters, $paginators)); + } + + /** + * @param string $templatePath + */ + protected function setTemplate($templatePath) + { + $this->templatePath = $templatePath; + } + + public function render() + { + $this->getPaginator()->itemCount = $this->count; + $this->template->results = $this->count; + $this->template->columns = $this['columns']->components; + $this->template->buttons = $this['buttons']->components; + $this->template->globalButtons = $this['globalButtons']->components; + $this->template->subGrids = $this['subGrids']->components; + $this->template->paginate = $this->paginate; + $this->template->colsCount = $this->getColsCount(); + $rows = $this->dataSource->getData(); + $this->template->rows = $rows; + $this->template->primaryKey = $this->primaryKey; + if($this->hasActiveRowForm()){ + $row = $rows[$this->activeRowForm]; + foreach($row as $name => $value){ + if($this->columnExists($name) && !empty($this['columns']->components[$name]->formRenderer)){ + $row[$name] = call_user_func($this['columns']->components[$name]->formRenderer, $row); + } + if(isset($this['gridForm'][$this->name]['rowForm'][$name])){ + $input = $this['gridForm'][$this->name]['rowForm'][$name]; + if($input instanceof \Nette\Forms\Controls\SelectBox){ + $items = $this['gridForm'][$this->name]['rowForm'][$name]->getItems(); + if(in_array($row[$name], $items)){ + $row[$name] = array_search($row[$name], $items); + } + } + } + } + $this['gridForm'][$this->name]['rowForm']->setDefaults($row); + $this['gridForm'][$this->name]['rowForm']->addHidden($this->primaryKey, $this->activeRowForm); + } + if($this->paginate){ + $this->template->viewedFrom = ((($this->getPaginator()->getPage()-1)*$this->perPage)+1); + $this->template->viewedTo = ($this->getPaginator()->getLength()+(($this->getPaginator()->getPage()-1)*$this->perPage)); + } + $templatePath = !empty($this->templatePath) ? $this->templatePath : __DIR__."/templates/grid.latte"; + $this->template->setFile($templatePath); + $this->template->render(); + } +} \ No newline at end of file From 48965ee223746afb3f1f0f016484e881c453fc45 Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Fri, 28 Sep 2012 17:15:20 +0200 Subject: [PATCH 5/7] Fixed a typo --- Grid.php~ | 1009 ----------------------------------------------------- 1 file changed, 1009 deletions(-) delete mode 100644 Grid.php~ diff --git a/Grid.php~ b/Grid.php~ deleted file mode 100644 index f13e33d..0000000 --- a/Grid.php~ +++ /dev/null @@ -1,1009 +0,0 @@ - 20, 50 => 50, 100 => 100); - - /** @var bool */ - public $paginate = TRUE; - - /** @var string */ - protected $defaultOrder; - - /** @var string */ - protected $secondOrder; - - /** @var IDataSource */ - protected $dataSource; - - /** @var string */ - protected $primaryKey; - - /** @var int */ - protected $count; - - /** @var string */ - public $width; - - /** @var bool */ - public $enableSorting = TRUE; - - /** @var int */ - public $activeRowForm; - - /** @var callback */ - public $rowFormCallback; - - /** @var bool */ - public $showAddRow = FALSE; - - /** @var bool */ - public $isSubGrid = FALSE; - - /** @var array */ - public $subGrids = array(); - - /** @var callback */ - public $afterConfigureSettings; - - /** @var string */ - protected $templatePath; - - /** @var string */ - public $messageNoRecords = 'Žádné záznamy'; - - /** - * @param \Nette\Application\UI\Presenter $presenter - */ - protected function attached($presenter) - { - parent::attached($presenter); - - $this->addComponent(New \Nette\ComponentModel\Container(), "columns"); - $this->addComponent(New \Nette\ComponentModel\Container(), "buttons"); - $this->addComponent(New \Nette\ComponentModel\Container(), "globalButtons"); - $this->addComponent(New \Nette\ComponentModel\Container(), "actions"); - $this->addComponent(New \Nette\ComponentModel\Container(), "subGrids"); - - if($presenter->isAjax()){ - $this->invalidateControl(); - } - - $this->configure($presenter); - - if($this->isSubGrid && !empty($this->afterConfigureSettings)){ - call_user_func($this->afterConfigureSettings, $this); - } - - if($this->hasActiveSubGrid()){ - $subGrid = $this->addComponent($this['subGrids']->components[$this->activeSubGridName]->getGrid(), "subGrid".$this->activeSubGridName); - $subGrid->registerSubGrid("subGrid".$this->activeSubGridName); - } - - if($this->hasActionForm()){ - $actions = array(); - foreach($this['actions']->components as $name => $action){ - $actions[$name] = $action->getAction(); - } - $this['gridForm'][$this->name]['action']['action_name']->setItems($actions); - } - if($this->paginate){ - if($this->hasActiveItemPerPage()){ - if(in_array($this->perPage, $this['gridForm'][$this->name]['perPage']['perPage']->items)){ - $this['gridForm'][$this->name]['perPage']->setDefaults(array("perPage" => $this->perPage)); - }else{ - $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); - $this->perPage = reset($items); - } - }else{ - $items = $this['gridForm'][$this->name]['perPage']['perPage']->getItems(); - $this->perPage = reset($items); - } - $this->getPaginator()->itemsPerPage = $this->perPage; - } - if($this->hasActiveFilter()){ - $this->filterData(); - $this['gridForm'][$this->name]['filter']->setDefaults($this->filter); - } - if($this->hasActiveOrder() && $this->hasEnabledSorting()){ - $this->orderData($this->order); - } - if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){ - $this->orderData($this->defaultOrder); - } - $this->count = $this->getCount(); - } - - /** - * @param string $subGrid - */ - public function registerSubGrid($subGrid) - { - if(!$this->isSubGrid){ - $this->subGrids[] = $subGrid; - }else{ - $this->parent->registerSubGrid($this->name."-".$subGrid); - } - } - - /** - * @return array - */ - public function getSubGrids() - { - if($this->isSubGrid){ - return $this->parent->getSubGrids(); - }else{ - return $this->subGrids; - } - } - - /** - * @param null|string $gridName - * @return string - */ - public function getGridPath($gridName = NULL) - { - if(empty($gridName)){ - $gridName = $this->name; - }else{ - $gridName = $this->name."-".$gridName; - } - if($this->isSubGrid){ - return $this->parent->getGridPath($gridName); - }else{ - return $gridName; - } - } - - public function findSubGridPath($gridName) - { - foreach($this->subGrids as $subGrid){ - $path = explode("-", $subGrid); - if(end($path) == $gridName){ - return $subGrid; - } - } - } - - /** - * @param string $columnName - * @return \Nette\Forms\IControl - * @throws UnknownColumnException - */ - public function getColumnInput($columnName) - { - if(!$this->columnExists($columnName)){ - throw new UnknownColumnException("Column $columnName doesn't exists."); - } - return $this['gridForm'][$this->name]['rowForm'][$columnName]; - } - - /** - * @param string $name - * @param null|string $label - * @param null|string $width - * @param null|int $truncate - * @return Column - * @throws DuplicateColumnException - * @return \Nifty\Grid\Column - */ - protected function addColumn($name, $label = NULL, $width = NULL, $truncate = NULL) - { - if(!empty($this['columns']->components[$name])){ - throw new DuplicateColumnException("Column $name already exists."); - } - $column = new Column($this['columns'], $name); - $column->setName($name) - ->setLabel($label) - ->setWidth($width) - ->setTruncate($truncate) - ->injectParent($this); - - return $column; - } - - /** - * @param string $name - * @param null|string $label - * @return Button - * @throws DuplicateButtonException - */ - protected function addButton($name, $label = NULL) - { - if(!empty($this['buttons']->components[$name])){ - throw new DuplicateButtonException("Button $name already exists."); - } - $button = new Button($this['buttons'], $name); - if($name == self::ROW_FORM){ - $self = $this; - $primaryKey = $this->primaryKey; - $button->setLink(function($row) use($self, $primaryKey){ - return $self->link("showRowForm!", $row[$primaryKey]); - }); - } - $button->setLabel($label); - return $button; - } - - - /** - * @param string $name - * @param null|string $label - * @throws DuplicateGlobalButtonException - * @return GlobalButton - */ - public function addGlobalButton($name, $label = NULL) - { - if(!empty($this['globalButtons']->components[$name])){ - throw new DuplicateGlobalButtonException("Global button $name already exists."); - } - $globalButton = new GlobalButton($this['globalButtons'], $name); - if($name == self::ADD_ROW){ - $globalButton->setLink($this->link("addRow!")); - } - $globalButton->setLabel($label); - return $globalButton; - } - - /** - * @param string $name - * @param null|string $label - * @return Action - * @throws DuplicateActionException - */ - public function addAction($name, $label = NULL) - { - if(!empty($this['actions']->components[$name])){ - throw new DuplicateActionException("Action $name already exists."); - } - $action = new Action($this['actions'], $name); - $action->setName($name) - ->setLabel($label); - - return $action; - } - - /** - * @param string $name - * @param null|string $label - * @return SubGrid - * @throws DuplicateSubGridException - */ - public function addSubGrid($name, $label = NULL) - { - if(!empty($this['subGrids']->components[$name]) || in_array($name, $this->getSubGrids())){ - throw new DuplicateSubGridException("SubGrid $name already exists."); - } - $self = $this; - $primaryKey = $this->primaryKey; - $subGrid = new SubGrid($this['subGrids'], $name); - $subGrid->setName($name) - ->setLabel($label); - if($this->activeSubGridName == $name){ - $subGrid->setClass("grid-subgrid-close"); - $subGrid->setClass(function($row) use ($self, $primaryKey){ - return $row[$primaryKey] == $self->activeSubGridId ? "grid-subgrid-close" : "grid-subgrid-open"; - }); - $subGrid->setLink(function($row) use ($self, $name, $primaryKey){ - $link = $row[$primaryKey] == $self->activeSubGridId ? array("activeSubGridId" => NULL, "activeSubGridName" => NULL) : array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name); - return $self->link("this", $link); - }); - } - else{ - $subGrid->setClass("grid-subgrid-open") - ->setLink(function($row) use ($self, $name, $primaryKey){ - return $self->link("this", array("activeSubGridId" => $row[$primaryKey], "activeSubGridName" => $name)); - }); - } - return $subGrid; - } - - /** - * @return array - */ - public function getColumnNames() - { - foreach($this['columns']->components as $column){ - $columns[] = $column->name; - } - return $columns; - } - - /** - * @return int $count - */ - public function getColsCount() - { - $count = count($this['columns']->components); - $this->hasActionForm() ? $count++ : $count; - ($this->hasButtons() || $this->hasFilterForm()) ? $count++ : $count; - $count += count($this['subGrids']->components); - - return $count; - } - - /** - * @param IDataSource $dataSource - */ - protected function setDataSource(IDataSource $dataSource) - { - $this->dataSource = $dataSource; - $this->primaryKey = $this->dataSource->getPrimaryKey(); - } - - /** - * @param string $width - */ - public function setWidth($width) - { - $this->width = $width; - } - - /** - * @param string $messageNoRecords - */ - public function setMessageNoRecords($messageNoRecords) - { - $this->messageNoRecords = $messageNoRecords; - } - - /** - * @param string $order - */ - public function setDefaultOrder($order) - { - if(!is_array($order)){ - $order = explode(" ", trim($order)); - } - if($order[1] == 1 || strtoupper($order[1]) == "ASC"){ - $order[1] = "ASC"; - }else{ - $order[1] = "DESC"; - } - $order[2] = "default"; - $this->defaultOrder = $order; - } - - /** - * @param mixed $order - * @param mixed $way - */ - public function setSecondOrder($order, $way = 0) - { - if(!is_array($order)){ - $orderExp = explode(" ", $order); - if(count($orderExp) > 1){ - $order = $orderExp[0]; - $way = $orderExp[1]; - } - if($way == 1 || strtoupper($way) == "ASC"){ - $way = "ASC"; - }else{ - $way = "DESC"; - } - $this->secondOrder = array($order, $way); - }else{ - foreach($order as $key => $value){ - if(is_array($value)){ - if($value[1] == 1 || strtoupper($value[1]) == "ASC"){ - $order[$key][1] = "ASC"; - }else{ - $order[$key][1] = "DESC"; - } - }else{ - $orderExp = explode(" ", $value);echo count($orderExp); - if(count($orderExp) > 1){ - $order[$key] = array($orderExp[0], $orderExp[1]); - } - } - } - $this->secondOrder = $order; - } - } - - /** - * @param array $values - * @return array - */ - protected function setPerPageValues(array $values) - { - $perPageValues = array(); - foreach($values as $value){ - $perPageValues[$value] = $value; - } - $this->perPageValues = $perPageValues; - } - - /** - * @return bool - */ - public function hasButtons() - { - return count($this['buttons']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasGlobalButtons() - { - return count($this['globalButtons']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasFilterForm() - { - foreach($this['columns']->components as $column){ - if(!empty($column->filterType)) - return TRUE; - } - return FALSE; - } - - /** - * @return bool - */ - public function hasActionForm() - { - return count($this['actions']->components) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasActiveFilter() - { - return count($this->filter) ? TRUE : FALSE; - } - - /** - * @param string $filter - * @return bool - */ - public function isSpecificFilterActive($filter) - { - if(isset($this->filter[$filter])){ - return ($this->filter[$filter] != '') ? TRUE : FALSE; - } - return false; - } - - /** - * @return bool - */ - public function hasActiveOrder() - { - return !empty($this->order) ? TRUE: FALSE; - } - - /** - * @return bool - */ - public function hasDefaultOrder() - { - return !empty($this->defaultOrder) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasSecondOrder() - { - return !empty($this->secondOrder) ? TRUE : FALSE; - } - - /** - * @return bool - */ - public function hasEnabledSorting() - { - return $this->enableSorting; - } - - /** - * @return bool - */ - public function hasActiveItemPerPage() - { - return !empty($this->perPage) ? TRUE : FALSE; - } - - public function hasActiveRowForm() - { - return !empty($this->activeRowForm) ? TRUE : FALSE; - } - - /** - * @param string $column - * @return bool - */ - public function columnExists($column) - { - return isset($this['columns']->components[$column]); - } - - /** - * @param string $subGrid - * @return bool - */ - public function subGridExists($subGrid) - { - return isset($this['subGrids']->components[$subGrid]); - } - - /** - * @return bool - */ - public function isEditable() - { - foreach($this['columns']->components as $component){ - if($component->editable) - return TRUE; - } - return FALSE; - } - - /** - * @return bool - */ - public function hasActiveSubGrid() - { - return (!empty($this->activeSubGridId) && !empty($this->activeSubGridName) && $this->subGridExists($this->activeSubGridName)) ? TRUE : FALSE; - } - - /** - * @return mixed - * @throws InvalidFilterException - * @throws UnknownColumnException - * @throws UnknownFilterException - */ - protected function filterData() - { - try{ - $filters = array(); - foreach($this->filter as $name => $value){ - if(!$this->columnExists($name)){ - throw new UnknownColumnException("Neexistující sloupec $name"); - - } - if(!$this['columns-'.$name]->hasFilter()){ - throw new UnknownFilterException("Neexistující filtr pro sloupec $name"); - } - - $type = $this['columns-'.$name]->getFilterType(); - $filter = FilterCondition::prepareFilter($value, $type); - - if(method_exists("\\NiftyGrid\\FilterCondition", $filter["condition"])){ - $filter = call_user_func("\\NiftyGrid\\FilterCondition::".$filter["condition"], $filter["value"]); - if(!empty($this['gridForm'][$this->name]['filter'][$name])){ - $filter["column"] = $name; - if(!empty($this['columns-'.$filter["column"]]->tableName)){ - $filter["column"] = $this['columns-'.$filter["column"]]->tableName; - } - $filters[] = $filter; - }else{ - throw new InvalidFilterException("Neplatný filtr"); - } - }else{ - throw new InvalidFilterException("Neplatný filtr"); - } - } - return $this->dataSource->filterData($filters); - } - catch(UnknownColumnException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("filter" => NULL)); - } - catch(UnknownFilterException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("filter" => NULL)); - } - } - - /** - * @param string $order - * @param bool $second - * @throws InvalidOrderException - */ - protected function orderData($order, $second = false) - { - $orders = array(); - try{ - if(!is_array($order)) { - $order = explode(" ", $order); - } - if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ - $orders[] = $order; - }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ - $orders[] = $order; - } - else{ - throw new InvalidOrderException("Neplatné seřazení."); - } - if($this->hasSecondOrder() && $second === false){ - if(is_array($this->secondOrder[0])){ - foreach($this->secondOrder[0] as $secondOrder){ - if($secondOrder[0] != $orders[0][0]){ - $orders[] = $this->orderData($secondOrder, true); - } - } - }else{ - if($this->secondOrder[0] != $orders[0][0]){ - $orders[] = $this->orderData($this->secondOrder, true); - } - } - $this->dataSource->multipleOrderData($orders); - }elseif($second === true){ - return $order; - }else{ - $this->dataSource->orderData($order[0], $order[1]); - } - } - catch(InvalidOrderException $e){ - $this->flashMessage($e->getMessage(), "grid-error"); - $this->redirect("this", array("order" => NULL)); - } - } - - /** - * @return int - */ - protected function getCount() - { - if($this->paginate){ - $count = $this->dataSource->getCount(); - $this->getPaginator()->itemCount = $count; - $this->dataSource->limitData($this->getPaginator()->itemsPerPage, $this->getPaginator()->offset); - return $count; - }else{ - $count = $this->dataSource->getCount(); - $this->getPaginator()->itemCount = $count; - return $count; - } - } - - /** - * @return GridPaginator - */ - protected function createComponentPaginator() - { - return new GridPaginator; - } - - /** - * @return \Nette\Utils\Paginator - */ - public function getPaginator() - { - return $this['paginator']->paginator; - } - - /** - * @param int $page - */ - public function handleChangeCurrentPage($page) - { - if($this->presenter->isAjax()){ - $this->redirect("this", array("paginator-page" => $page)); - } - } - - /** - * @param int $perPage - */ - public function handleChangePerPage($perPage) - { - if($this->presenter->isAjax()){ - $this->redirect("this", array("perPage" => $perPage)); - } - } - - /** - * @param string $column - * @param string $term - */ - public function handleAutocomplete($column, $term) - { - if($this->presenter->isAjax()){ - if(!empty($this['columns']->components[$column]) && $this['columns']->components[$column]->autocomplete){ - $this->filter[$column] = $term."%"; - $this->filterData(); - $this->dataSource->limitData($this['columns']->components[$column]->getAutocompleteResults(), NULL); - $data = $this->dataSource->getData(); - $results = array(); - foreach($data as $row){ - $value = $row[$column]; - if(!in_array($value, $results)){ - $results[] = $row[$column]; - } - } - $this->presenter->payload->payload = $results; - $this->presenter->sendPayload(); - } - } - } - - public function handleAddRow() - { - $this->showAddRow = TRUE; - } - - /** - * @param int $id - */ - public function handleShowRowForm($id) - { - $this->activeRowForm = $id; - } - - /** - * @param $callback - */ - public function setRowFormCallback($callback) - { - $this->rowFormCallback = $callback; - } - - /** - * @param int $id - * @return \Nette\Forms\Controls\Checkbox - */ - public function assignCheckboxToRow($id) - { - $this['gridForm'][$this->name]['action']->addCheckbox("row_".$id); - $this['gridForm'][$this->name]['action']["row_".$id]->getControlPrototype()->class[] = "grid-action-checkbox"; - return $this['gridForm'][$this->name]['action']["row_".$id]->getControl(); - } - - protected function createComponentGridForm() - { - $form = new \Nette\Application\UI\Form; - $form->method = "POST"; - $form->getElementPrototype()->class[] = "grid-gridForm"; - - $form->addContainer($this->name); - - $form[$this->name]->addContainer("rowForm"); - $form[$this->name]['rowForm']->addSubmit("send","Uložit"); - $form[$this->name]['rowForm']['send']->getControlPrototype()->addClass("grid-editable"); - - $form[$this->name]->addContainer("filter"); - $form[$this->name]['filter']->addSubmit("send","Filtrovat") - ->setValidationScope(FALSE); - - $form[$this->name]->addContainer("action"); - $form[$this->name]['action']->addSelect("action_name","Označené:"); - $form[$this->name]['action']->addSubmit("send","Potvrdit") - ->setValidationScope(FALSE) - ->getControlPrototype() - ->addData("select", $form[$this->name]["action"]["action_name"]->getControl()->name); - - $form[$this->name]->addContainer('perPage'); - $form[$this->name]['perPage']->addSelect("perPage","Záznamů na stranu:", $this->perPageValues) - ->getControlPrototype() - ->addClass("grid-changeperpage") - ->addData("gridname", $this->getGridPath()) - ->addData("link", $this->link("changePerPage!")); - $form[$this->name]['perPage']->addSubmit("send","Ok") - ->setValidationScope(FALSE) - ->getControlPrototype() - ->addClass("grid-perpagesubmit"); - - $form->onSuccess[] = callback($this, "processGridForm"); - - return $form; - } - - /** - * @param array $values - */ - public function processGridForm($values) - { - $values = $values->getHttpData(); - foreach($values as $gridName => $grid){ - foreach($grid as $section => $container){ - foreach($container as $key => $value){ - if($key == "send"){ - unset($container[$key]); - $subGrids = $this->subGrids; - foreach($subGrids as $subGrid){ - $path = explode("-", $subGrid); - if(end($path) == $gridName){ - $gridName = $subGrid; - break; - } - } - if($section == "filter"){ - $this->filterFormSubmitted($values); - } - $section = ($section == "rowForm") ? "row" : $section; - if(method_exists($this, $section."FormSubmitted")){ - call_user_func("self::".$section."FormSubmitted", $container, $gridName); - }else{ - $this->redirect("this"); - } - break 3; - } - } - } - } - } - - /** - * @param array $values - * @param string $gridName - */ - public function rowFormSubmitted($values, $gridName) - { - $subGrid = ($gridName == $this->name) ? FALSE : TRUE; - if($subGrid){ - call_user_func($this[$gridName]->rowFormCallback, (array) $values); - }else{ - call_user_func($this->rowFormCallback, (array) $values); - } - $this->redirect("this"); - } - - /** - * @param array $values - * @param string $gridName - */ - public function perPageFormSubmitted($values, $gridName) - { - $perPage = ($gridName == $this->name) ? "perPage" : $gridName."-perPage"; - - $this->redirect("this", array($perPage => $values["perPage"])); - } - - /** - * @param array $values - * @param string $gridName - * @throws NoRowSelectedException - */ - public function actionFormSubmitted($values, $gridName) - { - try{ - $rows = array(); - foreach($values as $name => $value){ - if(\Nette\Utils\Strings::startsWith($name, "row")){ - $vals = explode("_", $name); - if((boolean) $value){ - $rows[] = $vals[1]; - } - } - } - $subGrid = ($gridName == $this->name) ? FALSE : TRUE; - if(!count($rows)){ - throw new NoRowSelectedException("Nebyl vybrán žádný záznam."); - } - if($subGrid){ - call_user_func($this[$gridName]['actions']->components[$values['action_name']]->getCallback(), $rows); - }else{ - call_user_func($this['actions']->components[$values['action_name']]->getCallback(), $rows); - } - $this->redirect("this"); - } - catch(NoRowSelectedException $e){ - if($subGrid){ - $this[$gridName]->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); - }else{ - $this->flashMessage("Nebyl vybrán žádný záznam.","grid-error"); - } - $this->redirect("this"); - } - } - - /** - * @param array $values - */ - public function filterFormSubmitted($values) - { - $filters = array(); - $paginators = array(); - foreach($values as $gridName => $grid){ - $isSubGrid = ($gridName == $this->name) ? FALSE : TRUE; - foreach($grid['filter'] as $name => $value){ - if($value != ''){ - if($name == "send"){ - continue; - } - if($isSubGrid){ - $gridName = $this->findSubGridPath($gridName); - $filters[$this->name."-".$gridName."-filter"][$name] = $value; - }else{ - $filters[$this->name."-filter"][$name] = $value; - } - } - } - if($isSubGrid){ - $paginators[$this->name."-".$gridName."-paginator-page"] = NULL; - if(empty($filters[$this->name."-".$gridName."-filter"])) $filters[$this->name."-".$gridName."-filter"] = array(); - }else{ - $paginators[$this->name."-paginator-page"] = NULL; - if(empty($filters[$this->name."-filter"])) $filters[$this->name."-filter"] = array(); - } - } - $this->presenter->redirect("this", array_merge($filters, $paginators)); - } - - /** - * @param string $templatePath - */ - protected function setTemplate($templatePath) - { - $this->templatePath = $templatePath; - } - - public function render() - { - $this->getPaginator()->itemCount = $this->count; - $this->template->results = $this->count; - $this->template->columns = $this['columns']->components; - $this->template->buttons = $this['buttons']->components; - $this->template->globalButtons = $this['globalButtons']->components; - $this->template->subGrids = $this['subGrids']->components; - $this->template->paginate = $this->paginate; - $this->template->colsCount = $this->getColsCount(); - $rows = $this->dataSource->getData(); - $this->template->rows = $rows; - $this->template->primaryKey = $this->primaryKey; - if($this->hasActiveRowForm()){ - $row = $rows[$this->activeRowForm]; - foreach($row as $name => $value){ - if($this->columnExists($name) && !empty($this['columns']->components[$name]->formRenderer)){ - $row[$name] = call_user_func($this['columns']->components[$name]->formRenderer, $row); - } - if(isset($this['gridForm'][$this->name]['rowForm'][$name])){ - $input = $this['gridForm'][$this->name]['rowForm'][$name]; - if($input instanceof \Nette\Forms\Controls\SelectBox){ - $items = $this['gridForm'][$this->name]['rowForm'][$name]->getItems(); - if(in_array($row[$name], $items)){ - $row[$name] = array_search($row[$name], $items); - } - } - } - } - $this['gridForm'][$this->name]['rowForm']->setDefaults($row); - $this['gridForm'][$this->name]['rowForm']->addHidden($this->primaryKey, $this->activeRowForm); - } - if($this->paginate){ - $this->template->viewedFrom = ((($this->getPaginator()->getPage()-1)*$this->perPage)+1); - $this->template->viewedTo = ($this->getPaginator()->getLength()+(($this->getPaginator()->getPage()-1)*$this->perPage)); - } - $templatePath = !empty($this->templatePath) ? $this->templatePath : __DIR__."/templates/grid.latte"; - $this->template->setFile($templatePath); - $this->template->render(); - } -} \ No newline at end of file From d51698e16b862e68aeda40abde6c711c6b511777 Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Sat, 29 Sep 2012 21:50:37 +0200 Subject: [PATCH 6/7] Fixed multiple ordering by arrays --- Grid.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Grid.php b/Grid.php index 624dc40..da9391b 100644 --- a/Grid.php +++ b/Grid.php @@ -644,17 +644,22 @@ protected function orderData($order, $second = false) if(!is_array($order)) { $order = explode(" ", $order); } - if(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ + if(is_array($order) && is_array($order[0])){ + foreach($order as $value){ + $orders[] = $this->orderData($value, true); + } + }elseif(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ $orders[] = $order; - }elseif(isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ + }elseif(is_array($order) && isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ $orders[] = $order; - } - else{ + }elseif(is_array($order) && in_array($order[1], array("ASC", "DESC"))){ + $orders[] = $order; + }else{ throw new InvalidOrderException("Neplatné seřazení."); } if($this->hasSecondOrder() && $second === false){ if(is_array($this->secondOrder[0])){ - foreach($this->secondOrder[0] as $secondOrder){ + foreach($this->secondOrder as $secondOrder){ if($secondOrder[0] != $orders[0][0]){ $orders[] = $this->orderData($secondOrder, true); } From 49bbec6162df86ed67bf29b5df164434c5937cbc Mon Sep 17 00:00:00 2001 From: PingusPepan Date: Sun, 30 Sep 2012 00:13:42 +0200 Subject: [PATCH 7/7] Some bugfixes --- Grid.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Grid.php b/Grid.php index da9391b..2af1ce3 100644 --- a/Grid.php +++ b/Grid.php @@ -644,11 +644,15 @@ protected function orderData($order, $second = false) if(!is_array($order)) { $order = explode(" ", $order); } + $component = $order[0]; + if(isset($this['columns']->components[$order[0]]) && isset($this['columns']->components[$order[0]]->tableName)){ + $order[0] = $this['columns']->components[$order[0]]->tableName; + } if(is_array($order) && is_array($order[0])){ foreach($order as $value){ $orders[] = $this->orderData($value, true); } - }elseif(is_array($order) && in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){ + }elseif(is_array($order) && in_array($component, $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$component]->isSortable()){ $orders[] = $order; }elseif(is_array($order) && isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){ $orders[] = $order; @@ -660,12 +664,12 @@ protected function orderData($order, $second = false) if($this->hasSecondOrder() && $second === false){ if(is_array($this->secondOrder[0])){ foreach($this->secondOrder as $secondOrder){ - if($secondOrder[0] != $orders[0][0]){ + if(!in_array($secondOrder, $orders) || empty($orders)){ $orders[] = $this->orderData($secondOrder, true); } } }else{ - if($this->secondOrder[0] != $orders[0][0]){ + if(!in_array($this->secondOrder, $orders) || empty($orders)){ $orders[] = $this->orderData($this->secondOrder, true); } }