/** * Создает папку для загрузки * * @param array $field * @param \cs\base\BaseForm $model * * @return \cs\services\SitePath */ protected static function getFolderPath($field, $model) { $folder = ArrayHelper::getValue($field, 'type.1.folder', $model->getTableName()); return UploadFolderDispatcher::createFolder('FileUpload2', $folder, $model->id); }
/** * @param array $field * @param \cs\base\BaseForm $model * * @return array поля для обновления в БД */ public static function onUpdate($field, $model) { $fieldName = $field[BaseForm::POS_DB_NAME]; $content = $model->{$fieldName}; $destination = UploadFolderDispatcher::createFolder('HtmlContent', $model->getTableName(), $fieldName, $model->id); $content = self::process($content, $destination); return [$fieldName => $content]; }
/** * @param array $field * @param \cs\base\BaseForm $model * * @return array поля для обновления в БД */ public static function onUpdate($field, $model) { $fieldName = $field[BaseForm::POS_DB_NAME]; $files = $model->{$fieldName}; $tableName = $model->getTableName(); $fieldDb = ModelFields::insertOne(['table_name' => $tableName, 'field_name' => $fieldName]); ModelFiles::deleteByCondition(['row_id' => $fieldDb->getId()]); $folder = \cs\services\UploadFolderDispatcher::createFolder('FileUploadMany', $fieldDb->getId(), $model->id); $allFiles = []; foreach ($files as $file) { $allFiles[] = $file[0]; } $currentFiles = (new Query())->select('w.file_path, w.id')->from('widget_uploader_many w')->innerJoin('widget_uploader_many_fields wf', 'w.field_id = wf.id')->where(['w.row_id' => $model->id, 'wf.table_name' => $model->getTableName(), 'wf.field_name' => $fieldName])->all(); foreach ($currentFiles as $currentFile) { if (!in_array($currentFile['file_path'], $allFiles)) { (new SitePath($currentFile['file_path']))->deleteFile(); $id = (int) $currentFile['id']; ModelFiles::deleteByCondition(['id' => $id]); } } foreach ($files as $file) { if (StringHelper::startsWith($file[0], '/')) { continue; } $sourcePathFull = Yii::getAlias(self::$uploadDirectory . '/' . $file[0]); $destinationFile = $folder->cloneObject($file[0]); copy($sourcePathFull, $destinationFile->getPathFull()); ModelFiles::insert(['file_path' => $destinationFile->getPath(), 'file_name' => $file[1], 'row_id' => $model->id, 'field_id' => $fieldDb->getId(), 'datetime' => gmdate('YmdHis')]); } return [$fieldName => count($files)]; }