/** * Action to upload multiple files. * @return multitype:boolean multitype: */ public function actionIndex() { Yii::$app->response->format = 'json'; if (!$this->canWrite()) { throw new HttpException(401, Yii::t('CfilesModule.base', 'Insufficient rights to execute this action.')); } $response = []; foreach (UploadedFile::getInstancesByName('files') as $cFile) { $folder = $this->getCurrentFolder(); $currentFolderId = empty($folder) ? self::ROOT_ID : $folder->id; // check if the file already exists in this dir $filesQuery = File::find()->contentContainer($this->contentContainer)->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($cFile->name), 'parent_folder_id' => $currentFolderId]); $file = $filesQuery->one(); // if not, initialize new File if (empty($file)) { $file = new File(); $humhubFile = new \humhub\modules\file\models\File(); } else { $humhubFile = $file->baseFile; // logging file replacement $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]); $response['log'] = true; } $humhubFile->setUploadedFile($cFile); if ($humhubFile->validate()) { $file->content->container = $this->contentContainer; $folder = $this->getCurrentFolder(); if ($folder !== null) { $file->parent_folder_id = $folder->id; } if ($file->save()) { $humhubFile->object_model = $file->className(); $humhubFile->object_id = $file->id; $humhubFile->save(); $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]); } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($file->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages; $response['log'] = true; } } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($humhubFile->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages; $response['log'] = true; } } $response['files'] = $this->files; return $response; }
public function disableContentContainer(\humhub\modules\content\components\ContentContainerActiveRecord $container) { $folders = Content::findAll(['object_model' => Folder::className(), 'space_id' => $container->id]); foreach ($folders as $key => $folderContent) { $folder = Folder::findOne(['id' => $folderContent->object_id]); $folder->delete(); } $files = Content::findAll(['object_model' => File::className(), 'space_id' => $container->id]); foreach ($files as $key => $fileContent) { $file = File::findOne(['id' => $fileContent->object_id]); $file->delete(); } }
/** * Create a cfile model and create and connect it with its basefile File model from a given data file, connect it with its parent folder. * TODO: This method has a lot in common with BrowseController/actionUpload, common logic needs to be extracted and reused * * @param Response $response * the response errors will be parsed to. * @param int $parentFolderId * the files pid. * @param string $folderPath * the path of the folder the file data lies in. * @param string $filename * the files name. */ protected function generateModelFromFile(&$response, $parentFolderId, $folderPath, $filename) { $filepath = $folderPath . DIRECTORY_SEPARATOR . $filename; // check if the file already exists in this dir $filesQuery = File::find()->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($filename), 'parent_folder_id' => $parentFolderId]); $file = $filesQuery->one(); // if not, initialize new File if (empty($file)) { $file = new File(); $humhubFile = new \humhub\modules\file\models\File(); } else { // else replace the existing file $humhubFile = $file->baseFile; // logging file replacement $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]); $response['log'] = true; } // populate the file $humhubFile->mime_type = FileHelper::getMimeType($filepath); if ($humhubFile->mime_type == 'image/jpeg') { ImageConverter::TransformToJpeg($filepath, $filepath); } $humhubFile->size = filesize($filepath); $humhubFile->file_name = $filename; $humhubFile->newFileContent = stream_get_contents(fopen($filepath, 'r')); if ($humhubFile->save()) { $file->content->container = $this->contentContainer; $file->parent_folder_id = $parentFolderId; if ($file->save()) { $humhubFile->object_model = $file->className(); $humhubFile->object_id = $file->id; $humhubFile->save(); $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]); } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($file->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages; $response['log'] = true; } } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($humhubFile->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages; $response['log'] = true; } }
/** * Load all posted files from the database and get an array of them. * @return Ambigous <multitype:, multitype:\yii\db\ActiveRecord > */ protected function getAllPostedFiles() { // Get Posted Files $query = \humhub\modules\file\models\File::find(); $query->join('LEFT JOIN', 'comment', '(file.object_id=comment.id)'); $query->join('LEFT JOIN', 'content', '(comment.object_model=content.object_model AND comment.object_id=content.object_id) OR (file.object_model=content.object_model AND file.object_id=content.object_id)'); $query->andWhere(['content.space_id' => $this->contentContainer->id]); $query->andWhere(['<>', 'file.object_model', File::className()]); $query->orderBy(['file.updated_at' => SORT_DESC]); // Get Files from comments return $query->all(); }
public function getFiles() { return $this->hasMany(File::className(), ['parent_folder_id' => 'id']); }
public function getFiles() { return $this->hasMany(File::className(), ['parent_folder_id' => 'id'])->joinWith('baseFile')->orderBy(['title' => SORT_ASC]); }