Ejemplo n.º 1
0
 public static function getDefaultAttributeOptions($attributes)
 {
     if (is_array($attributes) == false || empty($attributes)) {
         return array();
     }
     $pid = $attributes[0]->product_id;
     $map = CitruscartHelperProduct::getAttributeQuantityMap($pid);
     $default = array();
     foreach ($attributes as $attribute) {
         $default[$attribute->productattribute_id] = 0;
     }
     JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
     if (is_array($map) && $map) {
         // we figure out what should be the default attribute options combination by recursively searching the product quantity map
         $answer = array();
         // array with final combination
         $not_finished = true;
         $act_pos = -1;
         // actual attribute
         $attr_order = array();
         // order of attribute IDs
         $cq = count(@$map['q']);
         $prev = array();
         foreach ($map['pa'] as $key => $value) {
             $attr_order[$map['ord'][$key]] = $key;
         }
         $c_answers = count($attr_order);
         // number of elements that should be in the result
         do {
             $ca = count($answer);
             // order of attribution combination
             $attr_id = $attr_order[$ca];
             $attribs = $map['pa'][$attr_id];
             $c_attribs = count($attribs);
             $pos = $map['ord'][$attr_id];
             $found = false;
             // found option for this attribute
             for ($i = 0; !$found && $i < $c_attribs; $i++) {
                 // first we find combination with this
                 $pao_id = $attribs[$i]->productattributeoption_id;
                 for ($j = 0; !$found && $j < $cq; $j++) {
                     if ($map['q'][$j][$pos] == $pao_id) {
                         // found a combination with this option -> now we check it
                         $wrong = false;
                         for ($k = 0; !$wrong && $k < $ca; $k++) {
                             $ord_a = $map['ord'][$attr_order[$k]];
                             $wrong = $map['q'][$j][$ord_a] == $answer[$attr_order[$k]];
                         }
                         if (!$wrong) {
                             $found = true;
                             $answer[$attr_id] = $pao_id;
                         }
                     }
                 }
             }
             if ($i == $c_attribs || $c_answers == count($answer)) {
                 $not_finished = false;
             }
         } while ($not_finished);
         foreach ($answer as $key => $val) {
             $default[$key] = $val;
         }
         return $default;
     } else {
         // no product quantities -> use the old way
         foreach ($attributes as $attribute) {
             $model = JModelLegacy::getInstance('ProductAttributeOptions', 'CitruscartModel');
             $model->setState('filter_attribute', $attribute->productattribute_id);
             $model->setState('order', 'tbl.ordering');
             $items = $model->getList();
             if (count($items)) {
                 $default[$attribute->productattribute_id] = $items[0]->productattributeoption_id;
             }
         }
     }
     return $default;
 }