function testSorting() { $list = ListSorterTest_Person::get(); $options = array('Title', 'Oldest' => 'Age DESC', 'Youngest' => array("Age" => "ASC"), new ListSorter_Option("Age Title", "Age DESC, Title ASC", new ListSorter_Option("Age Title", array("Age" => "ASC", "Title" => "DESC")))); //title asc $request = new SS_HTTPRequest('GET', 'people', array("sort" => "title")); $sorter = new ListSorter($request, $options); $list = $sorter->sortList($list); $this->assertDOSEquals(array(array("Title" => "beth", "Age" => 20), array("Title" => "joe", "Age" => 30), array("Title" => "sam", "Age" => 10), array("Title" => "zoe", "Age" => 10)), $list); //age + title $request = new SS_HTTPRequest('GET', 'people', array("sort" => "age+title")); $sorter = new ListSorter($request, $options); $list = $sorter->sortList($list); $this->assertDOSEquals(array(array("Title" => "joe", "Age" => 30), array("Title" => "beth", "Age" => 20), array("Title" => "sam", "Age" => 10), array("Title" => "zoe", "Age" => 10)), $list); //age + title reverse $request = new SS_HTTPRequest('GET', 'people', array("sort" => "age+title_rev")); $sorter = new ListSorter($request, $options); $list = $sorter->sortList($list); $this->assertDOSEquals(array(array("Title" => "zoe", "Age" => 10), array("Title" => "sam", "Age" => 10), array("Title" => "beth", "Age" => 20), array("Title" => "joe", "Age" => 30)), $list); }
private function _prepare_sort() { global $Conf; $this->default_sort_column = PaperColumn::lookup("id"); $this->sorters[0]->field = null; if ($this->search->sorters) { foreach ($this->search->sorters as $sorter) { if ($sorter->type && ($field = PaperColumn::lookup($sorter->type)) && $field->prepare($this, PaperColumn::PREP_SORT) && $field->comparator) { $sorter->field = $field; } else { if ($sorter->type) { if ($this->contact->can_view_tags(null) && ($tagger = new Tagger()) && ($tag = $tagger->check($sorter->type)) && ($result = Dbl::qe("select paperId from PaperTag where tag=? limit 1", $tag)) && edb_nrows($result)) { $this->search->warn("Unrecognized sort “" . htmlspecialchars($sorter->type) . "”. Did you mean “sort:#" . htmlspecialchars($sorter->type) . "”?"); } else { $this->search->warn("Unrecognized sort “" . htmlspecialchars($sorter->type) . "”."); } continue; } } ListSorter::push($this->sorters, $sorter); } if (count($this->sorters) > 1 && $this->sorters[0]->empty) { array_shift($this->sorters); } } if (get($this->sorters[0], "field")) { /* all set */ } else { if ($this->sorters[0]->type && ($c = PaperColumn::lookup($this->sorters[0]->type)) && $c->prepare($this, PaperColumn::PREP_SORT)) { $this->sorters[0]->field = $c; } else { $this->sorters[0]->field = $this->default_sort_column; } } $this->sorters[0]->type = $this->sorters[0]->field->name; // set defaults foreach ($this->sorters as $s) { if ($s->reverse === null) { $s->reverse = false; } if ($s->score === null) { $s->score = ListSorter::default_score_sort(); } } }
/** * Sorting controls * * @return ListSorter sorter */ public function getSorter() { $options = array(); foreach (ProductCategory::config()->sort_options as $k => $v) { // make the label translatable $k = _t("ProductCategory.{$k}", $k); $options[$k] = $v; } $sorter = ListSorter::create($this->request, $options); $this->extend('updateSorter', $sorter); return $sorter; }
private function _add_sorters($qe, $thenmap) { foreach ($qe->get_float("sort", array()) as $s) { if ($s = ListSorter::parse_sorter($s)) { $s->thenmap = $thenmap; $this->sorters[] = $s; } } if (!$qe->get_float("sort") && $qe->type === "pn") { $pn = array_diff($qe->value[0], $qe->value[1]); $s = ListSorter::make_field(new NumericOrderPaperColumn(array_flip($pn))); $s->thenmap = $thenmap; $this->sorters[] = $s; } }