/** * @param array $field * @param \cs\base\BaseForm $model * * @return array поля для обновления в БД */ public static function onUpdate($field, $model) { $fieldName = $field[BaseForm::POS_DB_NAME]; $v = $model->{$fieldName}; $isDelete = false; $hasFile = false; $hasUrl = false; if (ArrayHelper::getValue(Yii::$app->request->post(), $model->formName() . '.' . $fieldName . '-is_delete', 0) == 1) { $isDelete = true; } $fileModel = $v['file']; if (!is_null($fileModel)) { if ($fileModel->error == 0) { $hasFile = true; } } if ($v['url'] != '') { $hasUrl = true; } $choose = ''; if ($isDelete == false && $hasUrl == false && $hasFile == true) { $choose = 'file'; } if ($isDelete == false && $hasUrl == true && $hasFile == false) { $choose = 'url'; } if ($isDelete == false && $hasUrl == true && $hasFile == true) { $choose = 'file'; } if ($isDelete == true && $hasUrl == false && $hasFile == false) { $choose = 'delete'; } if ($isDelete == true && $hasUrl == false && $hasFile == true) { $choose = 'file'; } if ($isDelete == true && $hasUrl == true && $hasFile == false) { $choose = 'url'; } if ($isDelete == true && $hasUrl == true && $hasFile == true) { $choose = 'file'; } if ($isDelete == false && $hasUrl == false && $hasFile == true) { $choose = 'file'; } if ($isDelete == false && $hasUrl == true && $hasFile == false) { $choose = 'url'; } if ($isDelete == false && $hasUrl == true && $hasFile == true) { $choose = 'file'; } if ($choose == 'delete') { $row = $model->getRow(); $dbValue = ArrayHelper::getValue($row, $fieldName, ''); if ($dbValue != '') { // удалить старые файлы $f = new SitePath($dbValue); $f->deleteFile(); } return [$fieldName => '']; } if ($choose == 'url') { try { $data = file_get_contents($v['url']); } catch (\Exception $e) { $data = ''; } if ($data == '') { return []; } $file = File::content($data); $url = new \cs\services\Url($v['url']); $extension = $url->getExtension('jpg'); return self::save($file, $extension, $field, $model); } if ($choose == 'file') { $file = File::path($fileModel->tempName); $extension = $fileModel->extension; return self::save($file, $extension, $field, $model); } return []; }