Ejemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $this->object = Object::getForClass(get_class($this->model));
     $cacheKey = 'PropertiesWidget: ' . get_class($this->model) . ':' . $this->model->id;
     $data = Yii::$app->cache->get($cacheKey);
     if ($data === false) {
         $this->objectPropertyGroups = ObjectPropertyGroup::getForModel($this->model);
         $addedPropertyGroupsIds = [];
         foreach ($this->objectPropertyGroups as $opg) {
             $addedPropertyGroupsIds[] = $opg->property_group_id;
         }
         $restPg = (new Query())->select('id, name')->from(PropertyGroup::tableName())->where(['object_id' => $this->object->id])->andWhere(['not in', 'id', $addedPropertyGroupsIds])->orderBy('sort_order')->all();
         $this->propertyGroupsToAdd = ArrayHelper::map($restPg, 'id', 'name');
         Yii::$app->cache->set($cacheKey, ['objectPropertyGroups' => $this->objectPropertyGroups, 'propertyGroupsToAdd' => $this->propertyGroupsToAdd], 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(get_class($this->model)), ActiveRecordHelper::getCommonTag(PropertyGroup::className()), ActiveRecordHelper::getCommonTag(Property::className())]]));
     } else {
         $this->objectPropertyGroups = $data['objectPropertyGroups'];
         $this->propertyGroupsToAdd = $data['propertyGroupsToAdd'];
     }
     return $this->render($this->viewFile, ['model' => $this->model, 'object' => $this->object, 'object_property_groups' => $this->objectPropertyGroups, 'property_groups_to_add' => $this->propertyGroupsToAdd, 'form' => $this->form, 'widget_id' => $this->getId()]);
 }
Ejemplo n.º 2
0
 protected function createProduct($item = [], $createNotExists = true)
 {
     if (empty($item) || !isset($item[static::ELEMENT_ID]) || !isset($item[static::ELEMENT_NAIMENOVANIE])) {
         return false;
     }
     $guid = CommercemlGuid::findOne(['guid' => $item[static::ELEMENT_ID], 'type' => 'PRODUCT']);
     if (empty($guid) && $createNotExists) {
         $category = !empty($item['categories']) ? array_shift($item['categories']) : null;
         $product = new Product();
         $product->name = $product->title = $product->h1 = $item[static::ELEMENT_NAIMENOVANIE];
         $product->slug = Helper::createSlug($product->name);
         $product->main_category_id = isset($this->categoryCache[$category]) ? $this->categoryCache[$category] : $this->rootCategoryCache;
         $product->content = empty($item[static::ELEMENT_OPISANIE]) ? '' : $item[static::ELEMENT_OPISANIE];
         if ($product->validate() && $product->save()) {
             $product->refresh();
             $product->linkToCategory($this->rootCategoryCache);
             $guid = new CommercemlGuid();
             $guid->guid = $item[static::ELEMENT_ID];
             $guid->name = $item[static::ELEMENT_NAIMENOVANIE];
             $guid->model_id = $product->id;
             $guid->type = 'PRODUCT';
             $guid->save();
             return true;
         }
     }
     if (!empty($guid)) {
         /** @var Product $product */
         $product = isset($product) ? $product : $guid->product;
         if (!empty($product)) {
             $product->price = empty($item['price']) ?: array_shift($item['price']);
             $product->content = empty($item[static::ELEMENT_OPISANIE]) ?: $item[static::ELEMENT_OPISANIE];
             $product->name = empty($item[static::ELEMENT_NAIMENOVANIE]) ?: $item[static::ELEMENT_NAIMENOVANIE];
             if (!empty($item['properties'])) {
                 AbstractPropertyEavModel::setTableName($this->objectProduct->eav_table_name);
                 $product_eav = array_reduce(AbstractPropertyEavModel::findByModelId($product->id), function ($result, $item) {
                     $key = $item->property_group_id . ':' . $item->key;
                     $result[$key] = $item;
                     return $result;
                 }, []);
                 $product_groups = array_reduce(ObjectPropertyGroup::getForModel($product), function ($result, $item) {
                     $result[] = $item->property_group_id;
                     return $result;
                 }, []);
                 $product_osv = array_reduce(ObjectStaticValues::findAll(['object_id' => $this->objectProduct->id, 'object_model_id' => $product->id]), function ($result, $item) {
                     $result[] = $item->property_static_value_id;
                     return $result;
                 }, []);
                 foreach ($item['properties'] as $key => $value) {
                     if (isset(static::$propertiesCache[$key])) {
                         /** @var Property $prop */
                         $prop = static::$propertiesCache[$key];
                         if (!in_array($prop->property_group_id, $product_groups)) {
                             $objectGroup = new ObjectPropertyGroup();
                             $objectGroup->object_id = $this->objectProduct->id;
                             $objectGroup->object_model_id = $product->id;
                             $objectGroup->property_group_id = $prop->property_group_id;
                             $objectGroup->save();
                         }
                         if ($prop->has_static_values) {
                             $psv = PropertyStaticValues::findOne(['value' => $value]);
                             if (empty($psv)) {
                                 $psv = new PropertyStaticValues();
                                 $psv->name = $psv->value = $value;
                                 $psv->property_id = $prop->id;
                                 $psv->slug = Helper::createSlug($value);
                                 if ($psv->validate() && $psv->save()) {
                                     $psv->refresh();
                                 } else {
                                     $psv = null;
                                 }
                             }
                             if (!empty($psv) && !in_array($psv->id, $product_osv)) {
                                 $osv = new ObjectStaticValues();
                                 $osv->object_id = $this->objectProduct->id;
                                 $osv->object_model_id = $product->id;
                                 $osv->property_static_value_id = $psv->id;
                                 $osv->save();
                             }
                         } elseif ($prop->is_eav) {
                             $_key = $prop->property_group_id . ':' . $prop->key;
                             if (isset($product_eav[$_key])) {
                                 /** @var AbstractPropertyEavModel $eav */
                                 $eav = $product_eav[$_key];
                                 $eav->value = $value;
                                 $eav->save();
                             } else {
                                 $eav = new AbstractPropertyEavModel();
                                 $eav->object_model_id = $product->id;
                                 $eav->property_group_id = $prop->property_group_id;
                                 $eav->key = $prop->key;
                                 $eav->value = $value;
                                 $eav->save();
                             }
                         }
                     }
                 }
             }
             $product->save();
             return true;
         }
     }
     return false;
 }