Skip to content
This repository was archived by the owner on Mar 10, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions DataSource/IDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions DataSource/NDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
102 changes: 96 additions & 6 deletions Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Grid extends \Nette\Application\UI\Control
/** @var string */
protected $defaultOrder;

/** @var string */
protected $secondOrder;

/** @var IDataSource */
protected $dataSource;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -380,9 +382,55 @@ 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)){
$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);
if(count($orderExp) > 1){
$order[$key] = array($orderExp[0], $orderExp[1]);
}
}
}
$this->secondOrder = $order;
}
}

/**
* @param array $values
* @return array
Expand Down Expand Up @@ -468,6 +516,14 @@ public function hasDefaultOrder()
return !empty($this->defaultOrder) ? TRUE : FALSE;
}

/**
* @return bool
*/
public function hasSecondOrder()
{
return !empty($this->secondOrder) ? TRUE : FALSE;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -578,17 +634,51 @@ 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]);
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($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;
}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 as $secondOrder){
if(!in_array($secondOrder, $orders) || empty($orders)){
$orders[] = $this->orderData($secondOrder, true);
}
}
}else{
if(!in_array($this->secondOrder, $orders) || empty($orders)){
$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");
Expand Down