/**
  * Get product attributes
  * 
  * @param $product  obj Model_Product
  * @param $active   true, false, all
  * @return $out     array(array($obj))
  */
 public static function get_product_attributes($product = false, $active = true)
 {
     $out = array();
     if (!empty($product->attributes)) {
         // Decode all product atributes
         $product_attributes = array();
         foreach ($product->attributes as $k => $attribute) {
             $decoded = json_decode($attribute->attributes, true);
             if ($decoded) {
                 $product_attributes[$k] = $decoded;
                 $product_attributes_id[$k] = $attribute->id;
             }
         }
         // Create array with attributes id
         $attributes = array();
         if (!empty($product_attributes)) {
             foreach ($product_attributes as $v) {
                 if (is_array($v)) {
                     foreach ($v as $kk => $vv) {
                         $attributes[] = $kk;
                     }
                 }
             }
         }
         // Select used attributes for this product
         $used_attributes = array();
         if (!empty($attributes)) {
             // Find used attributes
             $used_attributes = \Attribute\Model_Attribute::find(array('where' => array(array('id', 'in', array_unique($attributes))), 'order_by' => array('sort' => 'asc')), 'id');
         }
         if (!empty($used_attributes) && !empty($product_attributes)) {
             foreach ($product_attributes as $k => $v) {
                 if (is_array($v)) {
                     foreach ($v as $kk => $vv) {
                         if (isset($used_attributes[$kk])) {
                             $options = $used_attributes[$kk]->options;
                             if (!empty($options) && isset($options[$vv])) {
                                 if ($active !== 'all') {
                                     if ($active) {
                                         if ($product->attributes[$k]->active != 1) {
                                             continue;
                                         }
                                     } else {
                                         if ($product->attributes[$k]->active != 1) {
                                             continue;
                                         }
                                     }
                                 }
                                 $tmp_obj = new \stdClass();
                                 $tmp_obj->sort = $used_attributes[$kk]->sort;
                                 $tmp_obj->attribute = $used_attributes[$kk];
                                 $tmp_obj->option = $options[$vv];
                                 $tmp_obj->product_attribute = $product->attributes[$k];
                                 $out[$product_attributes_id[$k]][] = $tmp_obj;
                             }
                         }
                     }
                     if (isset($out[$product_attributes_id[$k]])) {
                         usort($out[$product_attributes_id[$k]], array('\\Product\\Model_Product', 'sort_product_attributes'));
                     }
                 }
             }
         }
     }
     return $out;
 }
 public static function update_stock_quantity($combination, $quantity)
 {
     $ids = array_keys($combination);
     $ids = $ids == null ? array(null) : array_keys($ids);
     $attributes = \Attribute\Model_Attribute::find(array('where' => array(array('id', 'in', $ids)), 'order_by' => array('sort' => 'asc')));
     if ($attributes) {
         foreach ($attributes as $attribute) {
             if ($attribute->options) {
                 foreach ($attribute->options as $option) {
                     if (isset($combination[$attribute->id]) && $combination[$attribute->id] == $option->id) {
                         //$out[] = $attribute->title . ': ' . $option->title;
                         // $out[$attribute->title] = $option->title;
                         \Product\Model_Attribute::delete($option->id);
                     }
                 }
             }
         }
     }
 }