Example #1
0
 /**
  * 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;
     }
 }
 /**
  * Set good attribute value for string type attribute
  * @param $good
  * @param Attr $attr attribute
  * @param string $val value
  * @return bool
  */
 public function SetStringFeatureValue($good, $attr, $val)
 {
     foreach ($attr->attrValues as $exist_value) {
         if (strtolower($exist_value->value) == strtolower($val)) {
             $this->SetGoodAttribute($good, $attr->id, $exist_value->id);
             return;
         }
     }
     //if value not exist, create it
     $attrVal = new AttrValue();
     $attrVal->attr_id = $attr->id;
     $attrVal->value = $val;
     $attrVal->save();
     $this->SetGoodAttribute($good, $attr->id, $attrVal->id);
     return;
 }