public function actionConfigure()
 {
     if (\Yii::$app->request->isPost) {
         foreach (\Yii::$app->request->post('guidSelect', []) as $key => $value) {
             $item = CommercemlGuid::findOne(['id' => $key, 'type' => 'PROPERTY']);
             if (!empty($item)) {
                 $item->model_id = $value;
                 $item->save();
             }
         }
         if (null !== ($file = UploadedFile::getInstanceByName('cmlFile'))) {
             $xmlReader = new XmlFileReader($file->tempName);
             foreach ($xmlReader->getProperties() as $item) {
                 $model = CommercemlGuid::findOne(['guid' => $item[XmlFileReader::ELEMENT_ID]]);
                 if (empty($model)) {
                     $guid = new CommercemlGuid();
                     $guid->type = 'PROPERTY';
                     $guid->guid = $item[XmlFileReader::ELEMENT_ID];
                     $guid->name = $item[XmlFileReader::ELEMENT_NAIMENOVANIE];
                     $guid->model_id = 0;
                     $guid->save();
                 }
             }
         }
         return $this->redirect('', 301);
     }
     $properties = array_reduce(CommercemlGuid::find()->where(['type' => 'PROPERTY'])->asArray()->all(), function ($result, $item) {
         $result[$item['guid']] = $item;
         return $result;
     }, []);
     $propertiesGroups = array_reduce(PropertyGroup::findAll(['object_id' => 3]), function ($result, $item) {
         return array_reduce($item->properties, function ($result, $item) {
             if ($item->is_eav || $item->has_static_values) {
                 $result[] = ['id' => $item->id, 'name' => $item->name];
             }
             return $result;
         }, $result);
     }, [['id' => 0, 'name' => '']]);
     return $this->render('configure', ['props' => $properties, 'propsGroups' => $propertiesGroups]);
 }
Exemplo 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;
 }