Index: branches/5.1.x/units/shipping_quote_engines/shipping_quote_collector.php =================================================================== diff -u -N -r13164 -r13813 --- branches/5.1.x/units/shipping_quote_engines/shipping_quote_collector.php (.../shipping_quote_collector.php) (revision 13164) +++ branches/5.1.x/units/shipping_quote_engines/shipping_quote_collector.php (.../shipping_quote_collector.php) (revision 13813) @@ -1,6 +1,6 @@ Application->recallObject('CountryStatesHelper'); + /* @var $cs_helper kCountryStatesHelper */ - $db =& $this->Application->GetADODBConnection(); + $has_states = $cs_helper->CountryHasStates( $cs_helper->getCountryIso($params['dest_country'], true) ); - $cached_var_name = 'ShippingQuotes'.crc32(serialize($params)); + if ( + !$params['dest_city'] || !$params['dest_country'] || + ($has_states && !$params['dest_state']) || + !$params['dest_postal'] || !$params['packages'] + ) { + return Array (); + } + $cached_var_name = 'ShippingQuotes' . crc32(serialize($params)); + $shipping_types = $this->Application->getDBCache($cached_var_name); if ($shipping_types) { return unserialize($shipping_types); } - $sql = 'SELECT Classname FROM '.$this->Application->getUnitOption('sqe', 'TableName').' WHERE Status = 1'; - $classes = $db->GetCol($sql); - $classes[] = 'CustomShippingQuoteEngine'; // always persists $shipping_types = Array(); - foreach($classes as $class) - { + $classes = $this->getEngineClasses(); + + foreach ($classes as $class) { $object =& $this->Application->recallObject($class); + /* @var $object ShippingQuoteEngine */ + $new_shipping_types = $object->GetShippingQuotes($params); $shipping_types = array_merge($shipping_types, $new_shipping_types); } + uasort($shipping_types, Array(&$this, 'price_sort')); - //exclude not available shipping quotes by products + // exclude not available shipping quotes by products $limit_types = unserialize($params['limit_types']); + if (is_array($limit_types) && !in_array('ANY', $limit_types)) { - if (count($limit_types) == 0) break; - $available_types = array(); - foreach ($shipping_types as $a_type) - { - $include = false; //exclude by default - foreach ($limit_types as $limit) - { + if (count($limit_types) == 0) { + break; + } + + $available_types = Array (); + foreach ($shipping_types as $a_type) { + $include = false; // exclude by default + + foreach ($limit_types as $limit) { $include = $include || preg_match("/^$limit/", $a_type['ShippingId']); - if ($include) break; + if ($include) { + break; + } } - if (!$include) continue; - $available_types[$a_type['ShippingId']] = $a_type; + + if (!$include) { + continue; + } + + $available_types[ $a_type['ShippingId'] ] = $a_type; } + $shipping_types = $available_types; } - //exclude Selected Products Only shipping types, not matching products - $available_types = array(); + // exclude Selected Products Only shipping types, not matching products + $available_types = Array(); + foreach ($shipping_types as $a_type) { if (getArrayValue($a_type, 'SelectedOnly')) { - if (!is_array($limit_types) || !in_array($a_type['ShippingId'], $limit_types)) continue; + if (!is_array($limit_types) || !in_array($a_type['ShippingId'], $limit_types)) { + continue; + } } - $available_types[$a_type['ShippingId']] = $a_type; + + $available_types[ $a_type['ShippingId'] ] = $a_type; } + $shipping_types = $available_types; $this->Application->setDBCache($cached_var_name, serialize($shipping_types), 24 * 3600); @@ -82,52 +102,55 @@ function GetAvailableShippingTypes() { - $db =& $this->Application->GetADODBConnection(); - $sql = 'SELECT Classname FROM '.$this->Application->getUnitOption('sqe', 'TableName').' WHERE Status = 1'; - $classes = $db->GetCol($sql); - $classes[] = 'CustomShippingQuoteEngine'; // always persists - $shipping_types = Array(); - foreach($classes as $class) - { + $shipping_types = Array (); + $classes = $this->getEngineClasses(); + + foreach ($classes as $class) { $object =& $this->Application->recallObject($class); + /* @var $object ShippingQuoteEngine */ + $new_shipping_types = $object->GetAvailableTypes(); $shipping_types = array_merge($shipping_types, $new_shipping_types); } + uasort($shipping_types, Array(&$this, 'SortShippingTypes')); return $shipping_types; } + /** + * Returns all enabled shipping quote engine classes + * + * @return Array + */ + function getEngineClasses() + { + $sql = 'SELECT Classname + FROM ' . $this->Application->getUnitOption('sqe', 'TableName') . ' + WHERE Status = ' . STATUS_ACTIVE; + $classes = $this->Conn->GetCol($sql); + + // always persists + $classes[] = 'CustomShippingQuoteEngine'; + + return $classes; + } + function SortShippingTypes($elem1, $elem2) { - if($elem1['_Name'] < $elem2['_Name']) - { - return -1; - } - elseif($elem1['_Name'] > $elem2['_Name']) - { - return 1; - } - else - { + if ($elem1['_Name'] == $elem2['_Name']) { return 0; } + + return $elem1['_Name'] < $elem2['_Name'] ? -1 : 1; } function price_sort($elem1, $elem2) { - if($elem1['TotalCost'] < $elem2['TotalCost']) - { - return -1; - } - elseif($elem1['TotalCost'] > $elem2['TotalCost']) - { - return 1; - } - else - { + if ($elem1['TotalCost'] == $elem2['TotalCost']) { return 0; } + return $elem1['TotalCost'] < $elem2['TotalCost'] ? -1 : 1; } } \ No newline at end of file