Created
April 26, 2012 22:25
-
-
Save jedateach/2503707 to your computer and use it in GitHub Desktop.
SilverStripe Weight Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Weight extends Decimal{ | |
function Nice(){ | |
//return in format appropriate to value eg 2grams instead of 0.002kg | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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