function __construct($filename) { if (is_file($filename)) { $this->filename = $filename; $xml = new \XMLReader(); if (false !== $xml->open($filename)) { $this->xml = $xml; $rootCategory = Category::findOne(['parent_id' => 0]); if (empty($rootCategory)) { if (null === ($rootCategory = Category::createEmptyCategory(0, null, 'Каталог'))) { $this->xml->close(); $this->xml = null; } $this->rootCategoryCache = $rootCategory->id; } else { $this->rootCategoryCache = $rootCategory->id; } if (empty(static::$propertiesCache)) { static::$propertiesCache = array_reduce(CommercemlGuid::find([['>', 'model_id', 0], ['type' => 'PROPERTY']])->all(), function ($result, $item) { $result[$item['guid']] = $item->property; return $result; }, []); } $this->objectProduct = Object::getForClass(Product::className()); } } }
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]); }