Skip to content

Commit

Permalink
Text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Sep 27, 2021
1 parent 3ef7760 commit d23a91b
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Events.php
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());
}
}

}
47 changes: 47 additions & 0 deletions Module.php
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());
}

}
41 changes: 41 additions & 0 deletions assets/Assets.php
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
];

}
40 changes: 40 additions & 0 deletions components/BaseFileController.php
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;
}

}
18 changes: 18 additions & 0 deletions config.php
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']],
],
];
51 changes: 51 additions & 0 deletions controllers/EditController.php
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,
]);
}

}
40 changes: 40 additions & 0 deletions controllers/ViewController.php
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,
]);
}

}
34 changes: 34 additions & 0 deletions filehandler/EditFileHandler.php
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',
];
}

}
39 changes: 39 additions & 0 deletions filehandler/ViewFileHandler.php
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',
];
}

}
Loading

0 comments on commit d23a91b

Please sign in to comment.