From a356bf9181428a4a1326d1a4eaf01201a3296abc Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 24 Jan 2008 00:12:02 +0000 Subject: [PATCH] Merged revisions 47454 via svnmerge from http://svn.silverstripe.com/open/modules/sapphire/branches/2.2.1asfonz ........ r47454 | jshipman | 2007-12-21 13:36:38 +1300 (Fri, 21 Dec 2007) | 1 line ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@48536 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/DataObjectSet.php | 50 +++++++++++++++++++++++++++++++++++ core/model/fieldtypes/Int.php | 3 +++ 2 files changed, 53 insertions(+) diff --git a/core/model/DataObjectSet.php b/core/model/DataObjectSet.php index 1ed0c176e..72f1186f7 100644 --- a/core/model/DataObjectSet.php +++ b/core/model/DataObjectSet.php @@ -832,6 +832,56 @@ class DataObjectSet extends ViewableData implements Iterator { column_sort($this->items, $fieldname, $direction, false); } } + + + /** + * Adds ranking field to the dataobjects according to current ordering of dataobjects + * @param string $duplicatevalue the field being compared (used to check for equal ranks). Equals are excluded if null. + * @param boolean $skipduplicates skip the next number if there are equal ranks eg 1st equal, 1st equal, 3rd, 4th + */ + public function addRankings($field = 'ID', $skipduplicates = false){ + //do a sort by duplicate value? + + //suffixes of number values + + $count = 1; + if($this->items){ + $previous = null; + foreach($this->items as $key => $item){ + //check if current item field value equals the next + if($item->$field == $previous->$field){ + + } + + $item->Ranking = $this->convertIntToRank($count); + $count++; + } + } + } + + /** + * Helperfunction for converting ints to rank values. + * Should we move this to Int or somthing and just use in the template? The problem this poses is having "equal" values eg "1st equal" + * @param + */ + private function convertIntToRank($value){ + $rankconv = array( + 11 => "th", + 12 => "th", + 13 => "th", + 1=> "st", + 2 => "nd", + 3 => "rd", + ); + //if the value ends with any of $rankconv, then add that suffix + foreach($rankconv as $key => $val){ + if(ereg($key."$",$value)){ + return $value.$val; + } + } + return $value."th"; + } + /** * Remove duplicates from this set based on the dataobjects ID. diff --git a/core/model/fieldtypes/Int.php b/core/model/fieldtypes/Int.php index e39ea78ba..a502e52df 100644 --- a/core/model/fieldtypes/Int.php +++ b/core/model/fieldtypes/Int.php @@ -43,6 +43,9 @@ class Int extends DBField { function Nice() { return sprintf( '%d', $this->value ); } + + + } ?>