Piwik\DataTable\Filter\Sort::filter PHP Method

filter() public method

See {@link Sort}.
public filter ( DataTable $table ) : mixed
$table Piwik\DataTable
return mixed
    public function filter($table)
    {
        if ($table instanceof Simple) {
            return;
        }
        if (empty($this->columnToSort)) {
            return;
        }
        if (!$table->getRowsCountWithoutSummaryRow()) {
            return;
        }
        $row = $table->getFirstRow();
        if ($row === false) {
            return;
        }
        $config = new Sorter\Config();
        $sorter = new Sorter($config);
        $config->naturalSort = $this->naturalSort;
        $config->primaryColumnToSort = $sorter->getPrimaryColumnToSort($table, $this->columnToSort);
        $config->primarySortOrder = $sorter->getPrimarySortOrder($this->order);
        $config->primarySortFlags = $sorter->getBestSortFlags($table, $config->primaryColumnToSort);
        $config->secondaryColumnToSort = $sorter->getSecondaryColumnToSort($row, $config->primaryColumnToSort);
        $config->secondarySortOrder = $sorter->getSecondarySortOrder($this->order, $config->secondaryColumnToSort);
        $config->secondarySortFlags = $sorter->getBestSortFlags($table, $config->secondaryColumnToSort);
        // secondary sort should not be needed for all other sort flags (eg string/natural sort) as label is unique and would make it slower
        $isSecondaryColumnSortNeeded = $config->primarySortFlags === SORT_NUMERIC;
        $config->isSecondaryColumnSortEnabled = $this->isSecondaryColumnSortEnabled && $isSecondaryColumnSortNeeded;
        $this->sort($sorter, $table);
    }

Usage Example

コード例 #1
0
ファイル: SortTest.php プロジェクト: carriercomm/piwik
 /**
  * Test to sort by visit
  *
  * @group Core
  */
 public function testFilterSortNumeric()
 {
     $idcol = Row::COLUMNS;
     $table = new DataTable();
     $rows = array(array($idcol => array('label' => 'google', 'nb_visits' => 897)), array($idcol => array('label' => 'ask', 'nb_visits' => -152)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30)), array($idcol => array('label' => '238949', 'nb_visits' => 0)), array($idcol => array('label' => 'Q*(%&*', 'nb_visits' => 1)));
     $table->addRowsFromArray($rows);
     $expectedtable = new DataTable();
     $rows = array(array($idcol => array('label' => 'ask', 'nb_visits' => -152)), array($idcol => array('label' => '238949', 'nb_visits' => 0)), array($idcol => array('label' => 'Q*(%&*', 'nb_visits' => 1)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'google', 'nb_visits' => 897)));
     $expectedtable->addRowsFromArray($rows);
     $expectedtableReverse = new DataTable();
     $expectedtableReverse->addRowsFromArray(array_reverse($rows));
     $filter = new Sort($table, 'nb_visits', 'asc');
     $filter->filter($table);
     $this->assertTrue(DataTable::isEqual($table, $expectedtable));
     $filter = new Sort($table, 'nb_visits', 'desc');
     $filter->filter($table);
     $this->assertTrue(DataTable::isEqual($table, $expectedtableReverse));
 }
All Usage Examples Of Piwik\DataTable\Filter\Sort::filter