Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.10] Append wall entry controls to the file context menu #131

Merged
merged 15 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace humhub\modules\cfiles\controllers;

use humhub\modules\cfiles\models\File;
use humhub\modules\cfiles\models\Folder;
use humhub\modules\cfiles\models\rows\FileRow;
use humhub\modules\cfiles\widgets\FileSystemItem;
use Yii;
use humhub\modules\cfiles\widgets\FileList;
use yii\web\HttpException;
Expand Down Expand Up @@ -56,4 +60,46 @@ public function renderFileList($filesOrder = null, $foldersOrder = null)
]);
}

public function actionLoadEntry()
{
if ($file = $this->getFileById()) {
return $this->asJson([
'output' => $this->renderFileRow($file),
// Additional scripts may be generated here in order to display some messages in info footer bar
'scripts' => $this->renderAjaxContent(''),
]);
}

return $this->asJson([
'success' => false,
'error' => Yii::t('CfilesModule.base', 'No file found!')
]);
}

private function getFileById(): ?File
{
$fileId = Yii::$app->request->get('id');
$fileId = strpos($fileId, 'file_') === 0 ? substr($fileId, 5) : 0;

if (empty($fileId)) {
return null;
}

return File::find()->readable()->where(['cfiles_file.id' => $fileId])->one();
}

private function renderFileRow(File $file)
{
if ($file->parent_folder_id) {
$folder = Folder::findOne(['id' => $file->parent_folder_id]);
} else {
$folder = $this->getRootFolder();
}

return FileSystemItem::widget([
'folder' => $folder,
'row' => new FileRow(['item' => $file]),
]);
}

}
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.14.1 - Unreleased
-----------------------
- Enh #127: Improve context menu with items from wall stream entry


0.14.0 - November 26, 2021
--------------------------
- Enh #83: Enable paste/upload files from clipboard
Expand Down
36 changes: 8 additions & 28 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ class File extends FileSystemItem
*/
protected $_setFileContent = null;

/**
* @inheritdocs
*/
public $canMove = true;

/**
* @inheritdocs
*/
public $moduleId = 'cfiles';

/**
* @var array Content topics/tags
*/
Expand Down Expand Up @@ -210,24 +200,6 @@ public function afterSave($insert, $changedAttributes)
RichText::postProcess($this->description, $this);
}

/**
* @inheritdoc
*/
public function afterMove(ContentContainerActiveRecord $container = null) {
parent::afterMove($container);

/* @var $root Folder */
$root = Folder::find()
->contentContainer($container)
->andWhere(['type' => Folder::TYPE_FOLDER_ROOT])
->one();
if ($root) {
// Put the moved file into the root of new container:
$this->parent_folder_id = $root->id;
$this->save();
}
}

public function updateVisibility($visibility)
{
if ($visibility === null) {
Expand Down Expand Up @@ -267,6 +239,14 @@ public function getItemId()
return 'file_' . $this->id;
}

/**
* @inheritdoc
*/
public function getContentId()
{
return $this->content->id;
}

/**
* @return string
*/
Expand Down
42 changes: 42 additions & 0 deletions models/FileSystemItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace humhub\modules\cfiles\models;

use humhub\modules\cfiles\permissions\ManageFiles;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\user\models\User;
Expand All @@ -28,6 +29,16 @@ abstract class FileSystemItem extends ContentActiveRecord implements ItemInterfa
*/
public $managePermission = ManageFiles::class;

/**
* @inheritdocs
*/
public $canMove = true;

/**
* @inheritdocs
*/
public $moduleId = 'cfiles';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -115,6 +126,37 @@ public function afterSave($insert, $changedAttributes)
parent::afterSave($insert, $changedAttributes);
}

/**
* @inheritdoc
*/
public function afterMove(ContentContainerActiveRecord $container = null)
{
parent::afterMove($container);
$this->updateParentFolder();
}

/**
* Update parent Folder if it is from different Content Container(Space/User)
* This File/Folder will be moved into the root Folder of the current Content Container
*
* @param ContentContainerActiveRecord $container
* @return bool True on success moving or if parent Folder is already in the same Content Container
*/
public function updateParentFolder(): bool
{
$parentFolder = Folder::findOne(['id' => $this->parent_folder_id]);
if ($parentFolder && $parentFolder->content->contentcontainer_id == $this->content->contentcontainer_id) {
return true;
}

if (!($root = Folder::getOrInitRoot($this->content->getContainer()))) {
return false;
}

$this->parent_folder_id = $root->id;
return $this->save();
}

public function hasAttributeChanged($attributeName)
{
return $this->hasAttribute($attributeName) && ($this->isNewRecord || $this->getOldAttribute($attributeName) != $this->$attributeName);
Expand Down
74 changes: 70 additions & 4 deletions models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace humhub\modules\cfiles\models;

use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\file\libs\ImageHelper;
use humhub\modules\file\models\FileContent;
use Yii;
use yii\db\ActiveQuery;
use humhub\modules\file\libs\FileHelper;
use humhub\modules\user\models\User;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\search\events\SearchAddEvent;
use humhub\modules\space\models\Space;
use Yii;
use yii\db\ActiveQuery;
use yii\imagine\Image;
use yii\web\UploadedFile;

Expand Down Expand Up @@ -178,6 +178,51 @@ public function beforeSave($insert)
return parent::beforeSave($insert);
}

/**
* @inheritdoc
*/
public function afterMove(ContentContainerActiveRecord $container = null)
{
parent::afterMove($container);

// Move all sub folders and files into the same Container where this Folder has been moved to
$this->moveSubFoldersToContainer($container);
$this->moveSubFilesToContainer($container);
}

public function moveSubFoldersToContainer(ContentContainerActiveRecord $container = null)
{
if ($container === null) {
$container = $this->content->getContainer();
}

$folders = Folder::find()
->andWhere(['parent_folder_id' => $this->id])
->all();

foreach ($folders as $folder) {
/* @var Folder $folder */
$folder->move($container);
}
}

public function moveSubFilesToContainer(ContentContainerActiveRecord $container = null)
{
if ($container === null) {
$container = $this->content->getContainer();
}

$files = File::find()
->joinWith('baseFile')
->andWhere(['cfiles_file.parent_folder_id' => $this->id])
->all();

foreach ($files as $file) {
/* @var File $file */
$file->move($container);
}
}

/**
* @param $visibility
*/
Expand Down Expand Up @@ -376,6 +421,19 @@ private static function getContainerOwnerId(ContentContainerActiveRecord $conten
return null;
}

/**
* @param ContentContainerActiveRecord $contentContainer
* @return Folder the root folder of the given ContentContainerActiveRecord
*/
public static function getOrInitRoot(ContentContainerActiveRecord $contentContainer)
{
if ($root = Folder::getRoot($contentContainer)) {
return $root;
}

return Folder::initRoot($contentContainer);
}

/**
* @param ContentContainerActiveRecord $contentContainer
* @return Folder the root folder of the given ContentContainerActiveRecord
Expand Down Expand Up @@ -428,6 +486,14 @@ public function getItemId()
return $this->getItemType() . '_' . $this->id;
}

/**
* @inheritdoc
*/
public function getContentId()
{
return $this->content->id;
}

/**
* @return string
*/
Expand Down
2 changes: 2 additions & 0 deletions models/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface ItemInterface

public function getItemId();

public function getContentId();

public function getTitle();

public function getSize();
Expand Down
19 changes: 18 additions & 1 deletion models/forms/MoveForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use humhub\modules\cfiles\models\FileSystemItem;
use humhub\modules\cfiles\models\Folder;
use humhub\modules\cfiles\models\File;
use Yii;

/**
Expand Down Expand Up @@ -94,6 +93,24 @@ public function getMoveUrl()
return $this->sourceFolder->createUrl('/cfiles/move');
}

/**
* @return string URL to move Content to another Container
*/
public function getMoveToContainerUrl(): ?string
{
if (count($this->selection) !== 1) {
// Only single selected File/Folder can be moved to another Container
return null;
}

$item = FileSystemItem::getItemById($this->selection[0]);
if (!$item || !$item->content->container || !$item->content->checkMovePermission()) {
return null;
}

return $item->content->container->createUrl('/content/move/move', ['id' => $item->content->id]);
}

/**
* Executes the actual move of the selection files from source into target.
* @return bool
Expand Down
5 changes: 5 additions & 0 deletions models/rows/AbstractFileSystemItemRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public abstract function getType();
*/
public abstract function getItemId();

/**
* @return int
*/
public abstract function getContentId();

/**
* @return string
*/
Expand Down
8 changes: 8 additions & 0 deletions models/rows/BaseFileRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public function getItemId()
return 'baseFile_'.$this->baseFile->id;
}

/**
* @inheritdoc
*/
public function getContentId()
{
return null;
}

/**
* @return string
*/
Expand Down
11 changes: 10 additions & 1 deletion models/rows/FileRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace humhub\modules\cfiles\models\rows;

use Yii;
use humhub\modules\cfiles\widgets\WallEntryFile;
use humhub\modules\content\widgets\stream\WallStreamModuleEntryWidget;

/**
* Created by PhpStorm.
Expand Down Expand Up @@ -56,4 +57,12 @@ public function canEdit()
{
return $this->item->content->canEdit();
}

/**
* @inheritdoc
*/
public function getContext(): WallStreamModuleEntryWidget
{
return new WallEntryFile(['model' => $this->item]);
}
}
Loading