public function loadTypeSpecificValueFromRow($pa_value_array) { global $g_ui_locale; global $g_ui_units_pref; if ($pa_value_array['value_decimal1'] === '') { $this->ops_text_value = ''; return; } switch ($g_ui_units_pref) { case 'metric': $vo_measurement = new Zend_Measure_Weight((double) $pa_value_array['value_decimal1'], 'KILOGRAM', $g_ui_locale); $this->ops_text_value = $vo_measurement->convertTo(Zend_Measure_Weight::KILOGRAM, 4); break; case 'english': $vo_measurement = new Zend_Measure_Weight((double) $pa_value_array['value_decimal1'], 'KILOGRAM', $g_ui_locale); $this->ops_text_value = $vo_measurement->convertTo(Zend_Measure_Weight::POUND, 4); break; default: // show value in unit entered, but adjusted for the UI locale try { $vo_measurement = new Zend_Measure_Weight((double) $pa_value_array['value_decimal1'], 'KILOGRAM', $g_ui_locale); $this->ops_text_value = $vo_measurement->convertTo($pa_value_array['value_longtext2'], 4); } catch (Exception $e) { // derp $this->ops_text_value = $pa_value_array['value_longtext1']; } break; } $this->opn_decimal_value = $pa_value_array['value_decimal1']; }
public function parseValue($ps_value, $pa_element_info, $pa_options = null) { $ps_value = trim($ps_value); global $g_ui_locale; $va_settings = $this->getSettingValuesFromElementArray($pa_element_info, array('requireValue')); if (!$va_settings['requireValue'] && !trim($ps_value)) { return array('value_longtext1' => '', 'value_longtext2' => '', 'value_decimal1' => ''); } // parse units of measurement if (preg_match("!^([\\d\\.\\,/ ]+)[ ]*([^\\d ]+)!", $ps_value, $va_matches)) { $va_values = explode(" ", $va_matches[1]); $vs_value = 0; foreach ($va_values as $vs_v) { $vs_value += caConvertFractionalNumberToDecimal(trim($vs_v)); } switch (strtolower($va_matches[2])) { case "lbs": case 'lbs.': case 'lb': case 'lb.': case 'pound': case 'pounds': $vs_units = Zend_Measure_Weight::POUND; break; case 'kg': case 'kg.': case 'kilo': case 'kilos': case 'kilogram': case 'kilograms': $vs_units = Zend_Measure_Weight::KILOGRAM; break; case 'g': case 'g.': case 'gr': case 'gr.': case 'gram': case 'grams': $vs_units = Zend_Measure_Weight::GRAM; break; case 'mg': case 'mg.': case 'milligram': case 'milligrams': $vs_units = Zend_Measure_Weight::MILLIGRAM; break; case 'oz': case 'oz.': case 'ounce': case 'ounces': $vs_units = Zend_Measure_Weight::OUNCE; break; case 'ton': case 'tons': case 'tonne': case 'tonnes': case 't': case 't.': $vs_units = Zend_Measure_Weight::TON; break; case 'stone': $vs_units = Zend_Measure_Weight::STONE; break; default: $this->postError(1970, _t('%1 is not a valid unit of weight [%2]', $va_matches[2], $ps_value), 'WeightAttributeValue->parseValue()'); return false; break; } } else { $this->postError(1970, _t('%1 is not a valid measurement', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()'); return false; } try { $vo_parsed_measurement = new Zend_Measure_Weight($vs_value, $vs_units, $g_ui_locale); } catch (Exception $e) { $this->postError(1970, _t('%1 is not a valid measurement', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()'); return false; } if ($vo_parsed_measurement->getValue() < 0) { // Weight can't be negative in our universe // (at least I believe in *something*) $this->postError(1970, _t('%1 must not be less than zero', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()'); return false; } return array('value_longtext1' => $vo_parsed_measurement->toString(), 'value_longtext2' => $vs_units, 'value_decimal1' => $vo_parsed_measurement->convertTo('KILOGRAM', 6, 'en_US')); }
/** * @param $attributeType * @param $value * * @return int|string */ public function getAttributesMappingUnitConversion($attributeType, $value) { $this->_getConfigAttributeMapped($attributeType); if ($attributeType == 'weight') { //check if needs conversion if ($this->_mapping[$attributeType]['unit'] != self::ME_WEIGHT_UNIT) { $unit = new \Zend_Measure_Weight((double) $value); $unit->convertTo(\Zend_Measure_Weight::GRAM); return $unit->getValue(); } } elseif ($this->_mapping[$attributeType]['unit'] != self::ME_LENGTH_UNIT) { $unit = new \Zend_Measure_Length((double) $value); $unit->convertTo(\Zend_Measure_Length::CENTIMETER); return $unit->getValue(); } return $value; }