public function updateSequence($event) { $type_id = $this->owner->getPrimaryKey(); $hashes = Yii::$app->request->post('fp_hash'); foreach ($hashes as $hash) { // fetch one record to determine `type` of upload $uploadExample = Uploads::find()->select(['type'])->where(['hash' => $hash])->one(); if (count($uploadExample) > 0) { $type = $uploadExample->getAttribute('type'); $acl = VariationHelper::getAclOfType($type); if (AccessControl::checkAccess($acl, $type_id)) { // all right, attach uploads Uploads::updateAll(['type_id' => $type_id], 'hash=:hash', [':hash' => $hash]); } else { // no access, delete uploaded files Uploads::deleteAll('hash=:hash', [':hash' => $hash]); } } } $this->updateFileIdInOwnerModel($event); }
private function fetchFiles($files, &$images, $name = 'file') { $errors = []; $type = Yii::$app->request->post('type'); $type_id = Yii::$app->request->post('type_id'); $hash = Yii::$app->request->post('hash'); // Check access. if `$type_id` is null, then access check must be only in ConnectFileSequence behaviour if (!is_null($type_id)) { $acl = VariationHelper::getAclOfType($type); if (!AccessControl::checkAccess($acl, $type_id)) { Yii::warning('Someone trying to upload file with no access.', 'file-processor'); return ['You have no access to perform this upload']; } } if (isset($files['tmp_name'])) { $file_temp_name = $files['tmp_name']; $file_real_name = basename($files['name']); if (is_uploaded_file($file_temp_name)) { $mime = FileHelper::getMimeType($file_temp_name); if (is_null($mime)) { $mime = FileHelper::getMimeTypeByExtension($file_real_name); } if (strpos($mime, 'image') !== false) { $file_dimensions = getimagesize($file_temp_name); } else { $file_dimensions = [null, null]; } // insert into db $model = new Uploads(); $model->type = $type; $model->type_id = $type_id; $model->hash = $hash; $model->ord = Uploads::getMaxOrderValue($type, $type_id, $hash) + 1; $model->filename = Uploads::generateBaseFileName($file_real_name); $model->original = $file_real_name; $model->mime = $mime; $model->size = filesize($file_temp_name); $model->width = $file_dimensions[0]; $model->height = $file_dimensions[1]; // save model, save file and fill response array if ($model->save()) { // load configuration $config = VariationHelper::getConfigOfType($model->type); $errors = array_merge($errors, $model->process($file_temp_name, $config)); // insert id of uploaded file into attribute in model (if needed) Uploads::updateConnectedModelAttribute($config, $model->type_id, $model->id); if (empty($errors)) { $images[$name] = ['width' => $model->width, 'height' => $model->height, 'mime' => $model->mime, 'size' => $model->size, 'id' => $model->id, 'type' => $model->type, 'type_id' => $model->type_id, 'hash' => $model->hash, 'errors' => null]; } else { $model->removeFile(); } } else { Yii::warning('file was unable to be saved. Errors: ' . VarDumper::dumpAsString($model->getErrors()), 'file-processor'); array_push($errors, 'File was unable to be saved.'); } } else { array_push($errors, 'File was unable to be uploaded.'); } } else { foreach ($files as $name => $file) { $errors = array_merge($errors, $this->fetchFiles($file, $images, $name)); } } return $errors; }