-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ef7760
commit d23a91b
Showing
13 changed files
with
527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor; | ||
|
||
use humhub\modules\file\handler\FileHandlerCollection; | ||
use Yii; | ||
|
||
class Events | ||
{ | ||
|
||
public static function onFileHandlerCollection($event) | ||
{ | ||
/* @var $collection FileHandlerCollection */ | ||
$collection = $event->sender; | ||
|
||
/* @var $module Module */ | ||
$module = Yii::$app->getModule('text-editor'); | ||
|
||
if (!$module->isSupportedType($collection->file)) { | ||
return; | ||
} | ||
|
||
if ($collection->type == FileHandlerCollection::TYPE_EDIT && $module->canEdit($collection->file)) { | ||
$collection->register(new filehandler\EditFileHandler()); | ||
} | ||
|
||
if ($collection->type == FileHandlerCollection::TYPE_VIEW && $module->canView($collection->file)) { | ||
$collection->register(new filehandler\ViewFileHandler()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor; | ||
|
||
use humhub\modules\file\libs\FileHelper; | ||
use humhub\modules\file\models\File; | ||
|
||
class Module extends \humhub\components\Module | ||
{ | ||
/** | ||
* @var string[] allowed text extensions | ||
*/ | ||
public $textExtensions = ['txt', 'log', 'xml']; | ||
|
||
/** | ||
* Check the file type is supported by this module | ||
* | ||
* @param $file | ||
* @return bool | ||
*/ | ||
public function isSupportedType($file): bool | ||
{ | ||
$fileExtension = FileHelper::getExtension($file); | ||
|
||
return in_array($fileExtension, $this->textExtensions); | ||
} | ||
|
||
public function canEdit(File $file): bool | ||
{ | ||
return $this->isSupportedType($file) && | ||
$file->canDelete() && | ||
is_writable($file->getStore()->get()); | ||
} | ||
|
||
public function canView(File $file): bool | ||
{ | ||
return $this->isSupportedType($file) && | ||
$file->canRead() && | ||
is_readable($file->getStore()->get()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\assets; | ||
|
||
use yii\web\AssetBundle; | ||
use yii\web\View; | ||
|
||
class Assets extends AssetBundle | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public $sourcePath = '@text-editor/resources'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $js = [ | ||
'js/humhub.text_editor.js', | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $jsOptions = [ | ||
'position' => View::POS_END | ||
]; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $publishOptions = [ | ||
'forceCopy' => true | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\components; | ||
|
||
use humhub\components\access\ControllerAccess; | ||
use Yii; | ||
use yii\web\HttpException; | ||
use humhub\modules\file\models\File; | ||
use humhub\components\Controller; | ||
|
||
/** | ||
* BaseFileController | ||
* | ||
* @author Luke | ||
*/ | ||
class BaseFileController extends Controller | ||
{ | ||
/** | ||
* @inheritdoc | ||
* Allow access to this controller without any authentication (guest access) | ||
*/ | ||
public $access = ControllerAccess::class; | ||
|
||
protected function getFile(string $class = File::class): File | ||
{ | ||
$guid = Yii::$app->request->get('guid', Yii::$app->request->post('guid')); | ||
$file = $class::findOne(['guid' => $guid]); | ||
if (empty($file)) { | ||
throw new HttpException(404, Yii::t('TextEditorModule.base', 'Could not find requested file!')); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
use humhub\modules\file\handler\FileHandlerCollection; | ||
use humhub\modules\text_editor\Events; | ||
|
||
return [ | ||
'id' => 'text-editor', | ||
'class' => 'humhub\modules\text_editor\Module', | ||
'namespace' => 'humhub\modules\text_editor', | ||
'events' => [ | ||
[FileHandlerCollection::class, FileHandlerCollection::EVENT_INIT, [Events::class, 'onFileHandlerCollection']], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\controllers; | ||
|
||
use humhub\modules\file\models\FileContent; | ||
use humhub\modules\text_editor\models\FileUpdate; | ||
use Yii; | ||
use yii\web\HttpException; | ||
use humhub\modules\text_editor\components\BaseFileController; | ||
|
||
class EditController extends BaseFileController | ||
{ | ||
|
||
/** | ||
* Edit the text file in modal | ||
* | ||
* @return string | ||
* @throws HttpException | ||
*/ | ||
public function actionIndex() | ||
{ | ||
/* @var $file FileUpdate */ | ||
$file = $this->getFile(FileUpdate::class); | ||
|
||
if (!$file->canDelete()) { | ||
throw new HttpException(401, Yii::t('TextEditorModule.base', 'Insufficient permissions!')); | ||
} | ||
|
||
if (!is_writable($file->getStore()->get())) { | ||
throw new HttpException(403, Yii::t('TextEditorModule.base', 'File is not writable!')); | ||
} | ||
|
||
if ($file->load(Yii::$app->request->post())) { | ||
if ($file->save()) { | ||
return $this->asJson(['result' => Yii::t('TextEditorModule.base', 'Content of the file :fileName has been updated.', [':fileName' => '"' . $file->file_name . '"'])]); | ||
} else { | ||
return $this->asJson(['error' => Yii::t('TextEditorModule.base', 'File :fileName could not be updated.', [':fileName' => '"' . $file->file_name . '"'])]); | ||
} | ||
} | ||
|
||
return $this->renderAjax('index', [ | ||
'file' => $file, | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\controllers; | ||
|
||
use Yii; | ||
use yii\web\HttpException; | ||
use humhub\modules\text_editor\components\BaseFileController; | ||
|
||
class ViewController extends BaseFileController | ||
{ | ||
|
||
/** | ||
* View the text file in modal | ||
* | ||
* @return string | ||
* @throws HttpException | ||
*/ | ||
public function actionIndex() | ||
{ | ||
$file = $this->getFile(); | ||
|
||
if (!$file->canRead()) { | ||
throw new HttpException(401, Yii::t('TextEditorModule.base', 'Insufficient permissions!')); | ||
} | ||
|
||
if (!is_readable($file->getStore()->get())) { | ||
throw new HttpException(403, Yii::t('TextEditorModule.base', 'File is not readable!')); | ||
} | ||
|
||
return $this->renderAjax('index', [ | ||
'file' => $file, | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\filehandler; | ||
|
||
use humhub\modules\file\handler\BaseFileHandler; | ||
use Yii; | ||
use yii\helpers\Url; | ||
|
||
/** | ||
* EditFileHandler provides the edit of a text file | ||
* | ||
* @author Luke | ||
*/ | ||
class EditFileHandler extends BaseFileHandler | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLinkAttributes() | ||
{ | ||
return [ | ||
'label' => Yii::t('TextEditorModule.base', 'Edit'), | ||
'data-action-url' => Url::to(['/text-editor/edit', 'guid' => $this->file->guid]), | ||
'data-action-click' => 'ui.modal.load', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* @link https://www.humhub.org/ | ||
* @copyright Copyright (c) 2021 HumHub GmbH & Co. KG | ||
* @license https://www.humhub.com/licences | ||
*/ | ||
|
||
namespace humhub\modules\text_editor\filehandler; | ||
|
||
use humhub\modules\file\handler\BaseFileHandler; | ||
use Yii; | ||
use yii\helpers\Url; | ||
|
||
/** | ||
* ViewFileHandler provides the view of a text file | ||
* | ||
* @author Luke | ||
*/ | ||
class ViewFileHandler extends BaseFileHandler | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $position = self::POSITION_TOP; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getLinkAttributes() | ||
{ | ||
return [ | ||
'label' => Yii::t('TextEditorModule.base', 'View'), | ||
'data-action-url' => Url::to(['/text-editor/view', 'guid' => $this->file->guid]), | ||
'data-action-click' => 'ui.modal.load', | ||
]; | ||
} | ||
|
||
} |
Oops, something went wrong.