Index: branches/5.1.x/units/shipping_quote_engines/shipping_quote_engine_event_handler.php =================================================================== diff -u -N -r13465 -r13813 --- branches/5.1.x/units/shipping_quote_engines/shipping_quote_engine_event_handler.php (.../shipping_quote_engine_event_handler.php) (revision 13465) +++ branches/5.1.x/units/shipping_quote_engines/shipping_quote_engine_event_handler.php (.../shipping_quote_engine_event_handler.php) (revision 13813) @@ -1,6 +1,6 @@ getObject(); - if($object->GetDBField('AccountPassword') == '') - { - $sql = 'SELECT Properties FROM '.$object->TableName.' - WHERE EngineId = '.$object->GetDBField('EngineId'); - $properties = unserialize( $this->Conn->GetOne($sql) ); - $object->SetDBField('AccountPassword', $properties['AccountPassword']); + /* @var $object kDBItem */ + + $engine =& $this->Application->recallObject( $object->GetDBField('ClassName') ); + /* @var $engine ShippingQuoteEngine */ + + $engine_fields = $engine->GetEngineFields(); + $properties = $object->GetDBField('Properties'); + $properties = $properties ? unserialize($properties) : Array (); + + // common fields for all shipping quote engines + if ($object->GetDBField('AccountPassword') != '') { + // don't erase password by accident + $engine_fields[] = 'AccountPassword'; } - $properties = Array( - 'AccountLogin' => $object->GetDBField('AccountLogin'), - 'AccountPassword' => $object->GetDBField('AccountPassword'), - 'UPSEnabled' => $object->GetDBField('UPSEnabled'), - 'UPSAccount' => $object->GetDBField('UPSAccount'), - 'UPSInvoiced' => $object->GetDBField('UPSInvoiced'), - 'FDXEnabled' => $object->GetDBField('FDXEnabled'), - 'FDXAccount' => $object->GetDBField('FDXAccount'), - 'DHLEnabled' => $object->GetDBField('DHLEnabled'), - 'DHLAccount' => $object->GetDBField('DHLAccount'), - 'DHLInvoiced' => $object->GetDBField('DHLInvoiced'), - 'USPEnabled' => $object->GetDBField('USPEnabled'), - 'USPAccount' => $object->GetDBField('USPAccount'), - 'USPInvoiced' => $object->GetDBField('USPInvoiced'), - 'ARBEnabled' => $object->GetDBField('ARBEnabled'), - 'ARBAccount' => $object->GetDBField('ARBAccount'), - 'ARBInvoiced' => $object->GetDBField('ARBInvoiced'), - '1DYEnabled' => $object->GetDBField('1DYEnabled'), - '2DYEnabled' => $object->GetDBField('2DYEnabled'), - '3DYEnabled' => $object->GetDBField('3DYEnabled'), - 'GNDEnabled' => $object->GetDBField('GNDEnabled'), - 'ShipMethod' => $object->GetDBField('ShipMethod'), - ); - $properties = serialize($properties); - $object->SetDBField('Properties', $properties); + // save shipping quote specific fields + foreach ($engine_fields as $engine_field) { + $properties[$engine_field] = $object->GetDBField($engine_field); + } + + $object->SetDBField('Properties', serialize($properties)); + + $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); + /* @var $cs_helper kCountryStatesHelper */ + $from_country = $this->Application->ConfigValue('Comm_Shipping_Country'); - if (strlen($from_country) == 3) { - $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); - /* @var $cs_helper kCountryStatesHelper */ + $has_states = strlen($from_country) == 3 ? $cs_helper->CountryHasStates($from_country) : false; - // get 2symbol ISO code from 3symbol ISO code - $from_country = $cs_helper->getCountryIso($from_country); - } + $valid_address = $from_country && ($has_states && $this->Application->ConfigValue('Comm_Shipping_State') || !$has_states) && + $this->Application->ConfigValue('Comm_Shipping_City') && $this->Application->ConfigValue('Comm_Shipping_ZIP'); - if( !function_exists('curl_init') ) - { - $object->FieldErrors['Status']['pseudo'] = 'curl_not_present'; - $object->ErrorMsgs['curl_not_present'] = $this->Application->Phrase('la_error_EnableCurlFirst'); + if (!function_exists('curl_init')) { + $object->SetError('Status', 'curl_not_present'); } - elseif( $object->GetDBField('Status') == 1 && (!$this->Application->ConfigValue('Comm_Shipping_City') || !$from_country || - ( ($from_country == 'US' || $from_country == 'CA') && !$this->Application->ConfigValue('Comm_Shipping_State') ) || - !$this->Application->ConfigValue('Comm_Shipping_ZIP') ) ) - { - $object->FieldErrors['Status']['pseudo'] = 'from_info_not_filled_in'; - $object->ErrorMsgs['from_info_not_filled_in'] = $this->Application->Phrase('la_error_FillInShippingFromAddress'); + elseif (($object->GetDBField('Status') == STATUS_ACTIVE) && !$valid_address) { + $object->SetError('Status', 'from_info_not_filled_in'); } - } /** @@ -97,35 +81,68 @@ } /** - * Enter description here... + * Sets virtual fields from serialized properties array * * @param kEvent $event */ function OnAfterItemLoad(&$event) { + parent::OnAfterItemLoad($event); + $object =& $event->getObject(); - $properties = unserialize( $object->GetDBField('Properties') ); - $object->SetDBFieldsFromHash($properties); + /* @var $object kDBItem */ + + $properties = $object->GetDBField('Properties'); + + if ($properties) { + $object->SetDBFieldsFromHash( unserialize($properties) ); + } } + /** + * Deletes cached shipping quotes on any setting change + * + * @param kEvent $event + */ function OnAfterItemCreate(&$event) { - $event->CallSubEvent('OnAnyChange'); + parent::OnAfterItemCreate($event); + + $this->_deleteQuoteCache(); } + /** + * Deletes cached shipping quotes on any setting change + * + * @param kEvent $event + */ function OnAfterItemUpdate(&$event) { - $event->CallSubEvent('OnAnyChange'); + parent::OnAfterItemUpdate($event); + + $this->_deleteQuoteCache(); } + /** + * Deletes cached shipping quotes on any setting change + * + * @param kEvent $event + */ function OnAfterItemDelete(&$event) { - $event->CallSubEvent('OnAnyChange'); + parent::OnAfterItemDelete($event); + + $this->_deleteQuoteCache(); } - function OnAnyChange(&$event) + /** + * Deletes cached shipping quotes + * + */ + function _deleteQuoteCache() { - $sql = 'DELETE FROM ' . TABLE_PREFIX . 'Cache WHERE VarName LIKE "ShippingQuotes%"'; + $sql = 'DELETE FROM ' . TABLE_PREFIX . 'Cache + WHERE VarName LIKE "ShippingQuotes%"'; $this->Conn->Query($sql); } } \ No newline at end of file