public function actionPhoto() { Yii::beginProfile('action'); $data = Yii::$app->request->post(); if (empty($data['ParseImage']) || empty($data['id'])) { return false; } $parse = Parse::findOne($data['id']); if (empty($parse)) { return false; } //sleep(1); foreach ($data['ParseImage'] as $key => $url) { $url = @$url['url']; if (empty($key)) { $image = new ParseImage(); $image->parse_id = $parse->id; $image->url = $url; Yii::beginProfile('get'); if ($file = file_get_contents($url)) { Yii::endProfile('get'); $path = Yii::$app->params['uploadSalePath'] . DIRECTORY_SEPARATOR . $parse->sale_id; BaseFileHelper::createDirectory($path); $photo = new SalePhoto(); $photo->sale_id = $parse->sale_id; if ($photo->save()) { $photo->sort = $photo->id; $name = $photo->id . '.jpg'; if ($photo->save()) { if (!file_put_contents($path . DIRECTORY_SEPARATOR . $name, $file)) { $photo->delete(); } else { $photo->hash = md5_file($path . DIRECTORY_SEPARATOR . $name); $photo->save(); $image->photo_id = $photo->id; $image->save(); } } } } } elseif (empty($url)) { $image = ParseImage::findOne($key); if (!empty($image)) { if ($image->photo_id) { $photo = SalePhoto::findOne($image->photo_id); $photo->delete(); } $image->delete(); } } } Yii::endProfile('action'); return true; }
/** * @return \yii\db\ActiveQuery */ public function getParse() { return $this->hasOne(Parse::className(), ['id' => 'parse_id']); }
/** * Load XML file and saves the modified data in the database. * Returns the IDs of rows affected. * * @param $file * @param $scenario * @param bool $force * @return array $ids */ public static function saveXml($file, $user_id, $lang_id = null, $force = null) { $lang_id = $lang_id === null ? Lang::getCurrent()->id : $lang_id; $properties = simplexml_load_file($file); $property_ids = []; if ($user_id == Parse::SCENARIO_ARISTO) { foreach ($properties as $property) { $property_ids[] = (string) $property->propertyID; } } if ($user_id == Parse::SCENARIO_PAFILIA) { $properties = $properties->properties->property; foreach ($properties as $property) { $property_ids[] = str_replace('property-', '', (string) $property->attributes()->id); } } $parses = Parse::find()->where(['user_id' => $user_id, 'remote_id' => $property_ids])->indexBy('remote_id')->all(); $parse_ids = []; foreach ($parses as $p) { $parse_ids[] = $p->id; } $contents = ParseLang::find()->where(['id' => $parse_ids, 'lang_id' => $lang_id])->indexBy('id')->all(); $ids = []; foreach ($properties as $property) { $property_id = 0; if ($user_id == Parse::SCENARIO_ARISTO) { $property_id = (string) $property->propertyID; } if ($user_id == Parse::SCENARIO_PAFILIA) { $property_id = str_replace('property-', '', (string) $property->attributes()->id); } $content = false; if (!isset($parses[$property_id])) { $parse = new Parse(); $parse->user_id = $user_id; $parse->remote_id = $property_id; } else { $parse = $parses[$property_id]; if (isset($contents[$parse->id])) { $content = $contents[$parse->id]; } } if (empty($content)) { $content = new ParseLang(); $content->lang_id = $lang_id; } $content->data = Json::encode($property); $hash = md5($content->data); if ($hash != $content->hash || $force) { if ($parse->save()) { $content->hash = $hash; $content->id = $parse->id; if ($content->save()) { $ids[] = $parse->id; } } } elseif (!$parse->sale_id) { $ids[] = $parse->id; } } return $ids; }