/** * Return main attribute values for the good * @return GoodAttrVal[] main good attribute values */ public function GetMainAttributes() { $features = GoodAttrVal::model()->with(array('attr' => array('select' => 'name, type, template'), 'attrValue' => array('select' => 'value')))->findAll(array('condition' => 'good_id=' . $this->id . " AND attr.is_main = 1 ", 'order' => 'attr.global_pos', 'select' => 'attr_value_id,attr_id, value')); foreach ($features as $feature) { if ($feature->attr->type == 1) { $feature->value = $feature->attrValue->value; } } return $features; }
$this->menu = array(array('label' => 'Список товаров', 'url' => array('index')), array('label' => 'Добавить товар', 'url' => array('create')), array('label' => 'Изменить товар', 'url' => array('update', 'id' => $model->id)), array('label' => 'Удалить товар', 'url' => '#', 'linkOptions' => array('submit' => array('delete', 'id' => $model->id), 'confirm' => 'Are you sure you want to delete this item?')), array('label' => 'Управление товарами', 'url' => array('admin'))); ?> <h1>Посмотреть товар #<?php echo $model->id; ?> </h1> <?php $this->widget('zii.widgets.CDetailView', array('data' => $model, 'attributes' => array('id', 'name', 'category.name', 'brand.name', 'price', array('label' => 'Photo', 'type' => 'raw', 'value' => $model->GetGoodImages('s'))))); ?> <br> <h3>Характеристики</h3> <?php $features = GoodAttrVal::model()->findAllByAttributes(array('good_id' => $model->id)); foreach ($features as $feature) { if (isset($feature->attrValue)) { if ($feature->attrValue->attr->type == '1') { echo $feature->attrValue->attr->name . ' : ' . $feature->attrValue->value; } } elseif (isset($feature->attr)) { if ($feature->attr->type == '2') { if ($feature->value == '1') { echo $feature->attr->name . ' : ' . 'yes'; } elseif ($feature->value == '0') { echo $feature->attr->name . ' : ' . 'no'; } } elseif ($feature->attr->type == '3') { echo $feature->attr->name . ' : ' . $feature->value; }
/** * Save good attribute values from array * @param $data array with good attribute values */ public function SaveFeatures($data) { //save good features if (!isset($data)) { return; } //check exist good features first $featureValues = GoodAttrVal::model()->findAllByAttributes(array('good_id' => $this->id)); foreach ($featureValues as $featureValue) { if (isset($featureValue->attrValue)) { if ($featureValue->attrValue->attr->type == '1') { //if feature is string type if (empty($data['normal'][$featureValue->attrValue->attr->id])) { $featureValue->delete(); } elseif ($data['normal'][$featureValue->attrValue->attr->id] != $featureValue->attr_value_id) { if ($data['normal'][$featureValue->attrValue->attr->id] == '0') { $featureValue->attr_value_id = $data['new'][$featureValue->attrValue->attr->id]; } else { $featureValue->attr_value_id = $data['normal'][$featureValue->attrValue->attr->id]; } $featureValue->save(); } } } elseif (isset($featureValue->attr)) { if ($featureValue->attr->type == '2') { //if feature is boolean type if ($data['normal'][$featureValue->attr->id] == '0') { $featureValue->delete(); } elseif ($data['normal'][$featureValue->attr->id] != $featureValue->value) { $featureValue->value = $data['normal'][$featureValue->attr->id]; $featureValue->save(); } } elseif ($featureValue->attr->type == '3') { //if feature is numeric type if (empty($data['normal'][$featureValue->attr->id])) { $featureValue->delete(); } elseif ($data['normal'][$featureValue->attr->id] != $featureValue->value) { $featureValue->value = $data['normal'][$featureValue->attr->id]; $featureValue->save(); } } } } $newFeature = TRUE; foreach ($data['normal'] as $featureId => $feature) { foreach ($featureValues as $featureValue) { if ($featureValue->attrValue !== null && $featureValue->attrValue->attr_id == $featureId) { $newFeature = FALSE; } if ($featureValue->attr->id == $featureId) { $newFeature = FALSE; } } if ($newFeature) { $attr = Attr::model()->findByPk($featureId); $newGoodFeature = new GoodAttrVal(); $newGoodFeature->good_id = $this->id; if ($attr->type == '1') { if ($feature == '0' && !empty($data['new'][$featureId])) { //add new attribute value $newAttrValue = new AttrValue(); $newAttrValue->attr_id = $attr->id; $newAttrValue->value = $data['new'][$featureId]; $newAttrValue->save(); $newAttrValue->refresh(); $newGoodFeature->attr_value_id = $newAttrValue->id; $newGoodFeature->save(); } elseif ($feature != '0') { $newGoodFeature->attr_value_id = $feature; $newGoodFeature->save(); } } elseif ($attr->type == '2' && $feature != '0') { $newGoodFeature->value = $feature; $newGoodFeature->attr_id = $featureId; $newGoodFeature->save(); } elseif ($attr->type == '3' && !empty($feature)) { $newGoodFeature->value = $feature; $newGoodFeature->attr_id = $featureId; $newGoodFeature->save(); } } $newFeature = TRUE; } }
/** * Create good attribute value for boolean type attribute * @param $good * @param integer $attr_id * @param string $val */ public function SetBoolFeatureValue($good, $attr_id, $val) { if (strpos($val, "да") !== false || strpos($val, 'есть') !== false) { $val = 1; } else { $val = 0; } $attr_val = new GoodAttrVal(); $attr_val->attr_id = $attr_id; $attr_val->good_id = $good->id; $attr_val->value = $val; $attr_val->save(); }