Skip to content

Instantly share code, notes, and snippets.

@jedateach
Created April 26, 2012 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedateach/2503707 to your computer and use it in GitHub Desktop.
Save jedateach/2503707 to your computer and use it in GitHub Desktop.
SilverStripe Weight Field
<?php
class Weight extends Decimal{
function Nice(){
//return in format appropriate to value eg 2grams instead of 0.002kg
}
}
<?php
class WeightField extends TextField{
static $baseunit = 'kilogram';
//rounding?
static $unittable = array(
'kilogram' => array(
'convert' => 1,
'abbreviation' => 'kg'
),
'gram' => array(
'convert' => 0.001,
'abbreviation' => 'g'
),
'pound' => array(
'convert' => 0.45359237,
'abbreviation' => 'lb'
),
'ounce' => array(
'convert' => 0.0283495231,
'abbreviation' => 'oz'
)
);
function __construct($name, $title = null, $value = "", $form = null) {
// naming with underscores to prevent values from actually being saved somewhere
$this->fieldValue = parent::Field($name."[Amount]",$title,$value,$form);
$this->fieldUnit = $this->UnitField($name);
parent::__construct($name, $title, $value, $form);
}
function Field() {
return "<div class=\"fieldgroup\">" .
"<div class=\"fieldgroupField\">" . $this->fieldValue . "</div>" .
"<div class=\"fieldgroupField\">" . $this->fieldUnit . "</div>" .
"</div>";
}
function UnitField($name){
$unittypes = array(
'kilogram'
);
return new DropdownField("{$name}[Unit]",_t('WeightField.FIELDLABELUNIT', 'Unit'),$unittypes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment