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'];
 }
Exemplo n.º 2
0
 /**
  * Get name of measure by its type
  *
  * @param  $key
  * @return string
  */
 public function getMeasureWeightName($key)
 {
     $weight = new Zend_Measure_Weight(0);
     $conversionList = $weight->getConversionList();
     if (!empty($conversionList[$key]) && !empty($conversionList[$key][1])) {
         return $conversionList[$key][1];
     }
     return '';
 }
Exemplo n.º 3
0
 /**
  * test getConversionList
  * expected array
  */
 public function testWeightConversionList()
 {
     $value = new Zend_Measure_Weight('-100', Zend_Measure_Weight::STANDARD, 'de');
     $unit = $value->getConversionList();
     $this->assertTrue(is_array($unit), 'Array expected');
 }
Exemplo n.º 4
0
/**
 * Parse weight dimension
 * @param string $ps_value value to parse
 * @param null|array $pa_options options array
 * @return bool|null|Zend_Measure_Weight
 * @throws Exception
 */
function caParseWeightDimension($ps_value, $pa_options = null)
{
    global $g_ui_locale;
    $vs_locale = caGetOption('locale', $pa_options, $g_ui_locale);
    $pa_values = array(caConvertFractionalNumberToDecimal(trim($ps_value), $vs_locale));
    $vo_parsed_measurement = null;
    while ($vs_expression = array_shift($pa_values)) {
        // parse units of measurement
        if (preg_match("!^([\\d\\.\\,/ ]+)[ ]*([^\\d ]+)!", $vs_expression, $va_matches)) {
            $vs_value = trim($va_matches[1]);
            $va_values = explode(" ", $vs_value);
            if ($vs_expression = trim(str_replace($va_matches[0], '', $vs_expression))) {
                array_unshift($pa_values, $vs_expression);
            }
            $vs_value = 0;
            foreach ($va_values as $vs_v) {
                $vs_value += caConvertLocaleSpecificFloat(trim($vs_v), $vs_locale);
            }
            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:
                    throw new Exception(_t('Not a valid unit of weight [%2]', $ps_value));
                    break;
            }
            try {
                $o_tmp = new Zend_Measure_Weight($vs_value, $vs_units, $vs_locale);
            } catch (Exception $e) {
                throw new Exception(_t('Not a valid measurement'));
            }
            if ($o_tmp->getValue() < 0) {
                // weight can't be negative in our universe
                throw new Exception(_t('Must not be less than zero'));
            }
            if ($vo_parsed_measurement) {
                $vo_parsed_measurement = $vo_parsed_measurement->add($o_tmp);
            } else {
                $vo_parsed_measurement = $o_tmp;
            }
        }
    }
    if (!$vo_parsed_measurement) {
        throw new Exception(_t('Not a valid measurement [%1]', $ps_value));
    }
    return $vo_parsed_measurement;
}
Exemplo n.º 5
0
 /**
  * Compare if the value and type is equal
  *
  * @param  Zend_Measure_Weight  $object  Weight object to compare
  * @return boolean
  */
 public function equals($object)
 {
     if ($object->toString() == $this->toString()) {
         return true;
     }
     return false;
 }
Exemplo n.º 6
0
 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'));
 }
Exemplo n.º 7
0
 /**
  * @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;
 }