/** * 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 up() { $spaces = Space::find()->all(); $users = User::find()->all(); $containers = array_merge($users == null ? [] : $users, $spaces == null ? [] : $spaces); foreach ($containers as $container) { $created_by = $container instanceof User ? $container->id : $container instanceof Space ? $container->created_by : 1; $created_by = $created_by == null ? 1 : $created_by; if ($container->isModuleEnabled('cfiles')) { $this->insert('cfiles_folder', ['title' => Module::ROOT_TITLE, 'description' => Module::ROOT_DESCRIPTION, 'parent_folder_id' => 0, 'has_wall_entry' => false, 'type' => Folder::TYPE_FOLDER_ROOT]); $root_id = Yii::$app->db->getLastInsertID(); $this->insert('content', ['guid' => \humhub\libs\UUID::v4(), 'object_model' => Folder::className(), 'object_id' => $root_id, 'visibility' => 0, 'sticked' => 0, 'archived' => 0, 'created_at' => new \yii\db\Expression('NOW()'), 'created_by' => $created_by, 'updated_at' => new \yii\db\Expression('NOW()'), 'updated_by' => $created_by, 'contentcontainer_id' => $container->contentcontainer_id]); $this->insert('cfiles_folder', ['title' => Module::ALL_POSTED_FILES_TITLE, 'description' => Module::ALL_POSTED_FILES_DESCRIPTION, 'parent_folder_id' => $root_id, 'has_wall_entry' => false, 'type' => Folder::TYPE_FOLDER_POSTED]); $allpostedfiles_id = Yii::$app->db->getLastInsertID(); $this->insert('content', ['guid' => \humhub\libs\UUID::v4(), 'object_model' => Folder::className(), 'object_id' => $allpostedfiles_id, 'visibility' => 0, 'sticked' => 0, 'archived' => 0, 'created_at' => new \yii\db\Expression('NOW()'), 'created_by' => $created_by, 'updated_at' => new \yii\db\Expression('NOW()'), 'updated_by' => $created_by, 'contentcontainer_id' => $container->contentcontainer_id]); $posted_content_id = Yii::$app->db->getLastInsertID(); $filesQuery = File::find()->joinWith('baseFile')->contentContainer($container); $foldersQuery = Folder::find()->contentContainer($container); $filesQuery->andWhere(['cfiles_file.parent_folder_id' => 0]); // user maintained folders $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => 0]); // do not return any folders here that are root or allpostedfiles $foldersQuery->andWhere(['cfiles_folder.type' => null]); $rootsubfiles = $filesQuery->all(); $rootsubfolders = $foldersQuery->all(); foreach ($rootsubfiles as $file) { $this->update('cfiles_file', ['cfiles_file.parent_folder_id' => $root_id], ['id' => $file->id]); } foreach ($rootsubfolders as $folder) { $this->update('cfiles_folder', ['parent_folder_id' => $root_id], ['id' => $folder->id]); } } } }
public function disableContentContainer(ContentContainerActiveRecord $container) { foreach (Folder::find()->contentContainer($container)->all() as $folder) { $folder->delete(); } foreach (File::find()->contentContainer($container)->all() as $file) { $file->delete(); } }
public function getItemById($itemId) { list($type, $id) = explode('-', $itemId); if ($type == 'file') { return models\File::findOne(['id' => $id]); } elseif ($type == 'folder') { return models\Folder::findOne(['id' => $id]); } return null; }
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(); } }
public function getItemById($itemId) { $params = explode('_', $itemId); if (sizeof($params) < 2) { return null; } list($type, $id) = explode('_', $itemId); if ($type == 'file') { return models\File::findOne(['id' => $id]); } elseif ($type == 'folder') { return models\Folder::findOne(['id' => $id]); } elseif ($type == 'baseFile') { return \humhub\modules\file\models\File::findOne(['id' => $id]); } return null; }
/** * Add all posted files virtual directory content to zip file. * * @param ZipArchive $zipFile * The zip file to add the entries to * @param int $localPathPrefix * where we currently are in the zip file */ protected function archiveAllPostedFiles(&$zipFile, $localPathPrefix) { $files = $this->getAllPostedFilesList(); foreach ($files['postedFiles'] as $file) { $this->archiveFile($file, $zipFile, $localPathPrefix, \humhub\modules\cfiles\models\File::getUserById($file->created_by)->username . '_' . $file->created_at . '_'); } }
public static function getPathFromId($id, $parentFolderPath = false, $separator = '/') { if ($id == 0) { return $separator; } $item = File::findOne(['id' => $id]); if (empty($item)) { return null; } $tempFolder = $item->parentFolder; $path = $separator; if (!$parentFolderPath) { $path .= $item->title; } while (!empty($tempFolder)) { $path = $separator . $tempFolder->title . $path; } return $path; }
} ?> <?php foreach (array_key_exists('files', $items) ? $items['files'] : [] as $file) { ?> <?php echo FileSystemItem::widget(['parentFolderId' => $parentFolderId, 'type' => $file->getItemType(), 'id' => $file->getItemId(), 'downloadUrl' => $file->getUrl(true), 'url' => $file->getUrl(), 'wallUrl' => $file->getWallUrl(), 'iconClass' => $file->getIconClass(), 'title' => $file->getTitle(), 'size' => $file->getSize(), 'creator' => $file->creator, 'editor' => $file->editor, 'updatedAt' => $file->content->updated_at, 'contentObject' => $file]); ?> <?php } ?> <?php foreach (array_key_exists('postedFiles', $items) ? $items['postedFiles'] : [] as $file) { ?> <?php echo FileSystemItem::widget(['parentFolderId' => $parentFolderId, 'type' => \humhub\modules\cfiles\models\File::getItemTypeByExt($file->getExtension()), 'columns' => $itemsSelectable ? ['select', 'title', 'size', 'timestamp', 'creator'] : ['title', 'size', 'timestamp', 'creator'], 'id' => 'baseFile_' . $file->id, 'downloadUrl' => $file->getUrl() . '&' . http_build_query(['download' => true]), 'url' => $file->getUrl(), 'wallUrl' => \humhub\modules\cfiles\models\File::getBasePost($file)->getUrl(), 'iconClass' => \humhub\modules\cfiles\models\File::getIconClassByExt($file->getExtension()), 'title' => $file->file_name, 'size' => $file->size, 'creator' => \humhub\modules\cfiles\models\File::getUserById($file->created_by), 'editor' => \humhub\modules\cfiles\models\File::getUserById($file->updated_by), 'updatedAt' => $file->updated_at]); ?> <?php } ?> </table> </div> <?php } else { ?> <div class="folderEmptyMessage"> <div class="panel"> <div class="panel-body"> <p> <strong><?php echo Yii::t('CfilesModule.base', 'This folder is empty.');
/** * 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(); }
?> <?php echo Yii::$app->formatter->asShortSize($item['file']->size, 1); ?> <?php } ?> </td> <td class="text-right" data-sort-value="" title=""><a href="<?php echo File::getCreatorById($item['file']->created_by)->createUrl(); ?> "> <?php echo File::getCreatorById($item['file']->created_by)->username; ?> </a></td> </td> <td class="text-right" data-sort-value="" title=""> <?php echo \humhub\widgets\TimeAgo::widget(['timestamp' => $item['file']->updated_at]); ?> </td> </tr> <?php } } else { ?>
public function getFiles() { return $this->hasMany(File::className(), ['parent_folder_id' => 'id']); }
public static function getPathFromId($id, $parentFolderPath = false, $separator = '/', $withRoot = false) { if ($id == 0) { return $separator; } $item = File::findOne(['id' => $id]); if (empty($item)) { return null; } $tempFolder = $item->parentFolder; $path = $separator; if (!$parentFolderPath) { $path .= $item->title; } $counter = 0; // break at maxdepth 20 to avoid hangs while (!empty($tempFolder) && $counter++ <= 20) { $path = $separator . $tempFolder->title . $path; $tempFolder = $tempFolder->parentFolder; } return $path; }
/** * 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 fileToModel(&$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; } }
public function getFiles() { return $this->hasMany(File::className(), ['parent_folder_id' => 'id'])->joinWith('baseFile')->orderBy(['title' => SORT_ASC]); }
/** * Load all files and folders of the current folder from the database and get an array of them. * * @param array $filesOrder * orderBy array appended to the files query * @param array $foldersOrder * orderBy array appended to the folders query * @return Ambigous <multitype:, multitype:\yii\db\ActiveRecord > */ protected function getItemsList($filesOrder = NULL, $foldersOrder = NULL) { // set default value if (!$filesOrder) { $filesOrder = ['title' => SORT_ASC]; } if (!$foldersOrder) { $foldersOrder = ['title' => SORT_ASC]; } $filesQuery = File::find()->joinWith('baseFile')->contentContainer($this->contentContainer)->readable(); $foldersQuery = Folder::find()->contentContainer($this->contentContainer)->readable(); $specialFoldersQuery = Folder::find()->contentContainer($this->contentContainer)->readable(); $filesQuery->andWhere(['cfiles_file.parent_folder_id' => $this->getCurrentFolder()->id]); // user maintained folders $foldersQuery->andWhere(['cfiles_folder.parent_folder_id' => $this->getCurrentFolder()->id]); // do not return any folders here that are root or allpostedfiles $foldersQuery->andWhere(['or', ['cfiles_folder.type' => null], ['and', ['<>', 'cfiles_folder.type', Folder::TYPE_FOLDER_POSTED], ['<>', 'cfiles_folder.type', Folder::TYPE_FOLDER_ROOT]]]); // special default folders like the allposted files folder $specialFoldersQuery->andWhere(['cfiles_folder.parent_folder_id' => $this->getCurrentFolder()->id]); $specialFoldersQuery->andWhere(['is not', 'cfiles_folder.type', null]); $filesQuery->orderBy($filesOrder); $foldersQuery->orderBy($foldersOrder); return ['specialFolders' => $specialFoldersQuery->all(), 'folders' => $foldersQuery->all(), 'files' => $filesQuery->all()]; }