From 767ea8b38086732d7e565e54e9ca13a853eb7b1b Mon Sep 17 00:00:00 2001 From: yurabakhtin Date: Thu, 14 Oct 2021 17:01:44 +0300 Subject: [PATCH] Create new text files --- Events.php | 5 ++ Module.php | 45 +++++++++++++- assets/Assets.php | 7 +++ controllers/CreateController.php | 52 ++++++++++++++++ docs/CHANGELOG.md | 3 +- filehandler/CreateFileHandler.php | 36 +++++++++++ models/CreateFile.php | 96 ++++++++++++++++++++++++++++++ models/FileUpdate.php | 2 +- resources/css/text_editor.css | 46 ++++++++++++++ resources/js/humhub.text_editor.js | 21 +++++++ views/create/file.php | 34 +++++++++++ views/create/index.php | 33 ++++++++++ 12 files changed, 375 insertions(+), 5 deletions(-) create mode 100644 controllers/CreateController.php create mode 100644 filehandler/CreateFileHandler.php create mode 100644 models/CreateFile.php create mode 100644 resources/css/text_editor.css create mode 100644 views/create/file.php create mode 100644 views/create/index.php diff --git a/Events.php b/Events.php index 3550cce..0d256f2 100644 --- a/Events.php +++ b/Events.php @@ -18,6 +18,11 @@ public static function onFileHandlerCollection($event) /* @var $collection FileHandlerCollection */ $collection = $event->sender; + if ($collection->type === FileHandlerCollection::TYPE_CREATE) { + $collection->register(new filehandler\CreateFileHandler()); + return; + } + /* @var $module Module */ $module = Yii::$app->getModule('text-editor'); diff --git a/Module.php b/Module.php index a1b8bff..9102c67 100644 --- a/Module.php +++ b/Module.php @@ -9,23 +9,35 @@ use humhub\modules\file\libs\FileHelper; use humhub\modules\file\models\File; +use Yii; class Module extends \humhub\components\Module { + /** + * Allowed file types + */ + const FILE_TYPE_TXT = 'txt'; + const FILE_TYPE_LOG = 'log'; + const FILE_TYPE_XML = 'xml'; + /** * @var string[] allowed text extensions */ - public $textExtensions = ['txt', 'log', 'xml']; + public $textExtensions = [self::FILE_TYPE_TXT, self::FILE_TYPE_LOG, self::FILE_TYPE_XML]; /** * Check the file type is supported by this module * - * @param $file + * @param string|File $file File name or extension * @return bool */ public function isSupportedType($file): bool { - $fileExtension = FileHelper::getExtension($file); + if (is_string($file) && strpos($file, '.') === false) { + $fileExtension = $file; + } else { + $fileExtension = FileHelper::getExtension($file); + } return in_array($fileExtension, $this->textExtensions); } @@ -44,4 +56,31 @@ public function canView(File $file): bool is_readable($file->getStore()->get()); } + public function getTypesData(): array + { + return [ + self::FILE_TYPE_TXT => [ + 'title' => Yii::t('TextEditorModule.base', 'Text'), + 'icon' => 'fa-file-text-o', + 'mimeType' => 'text/plain', + ], + self::FILE_TYPE_LOG => [ + 'title' => Yii::t('TextEditorModule.base', 'Log'), + 'icon' => 'fa-file-o', + 'mimeType' => 'text/plain', + ], + self::FILE_TYPE_XML => [ + 'title' => Yii::t('TextEditorModule.base', 'XML'), + 'icon' => 'fa-file-code-o', + 'mimeType' => 'text/xml', + ], + ]; + } + + public function getTypeInfo(string $type, string $field): ?string + { + $types = $this->getTypesData(); + return isset($types[$type][$field]) ? $types[$type][$field] : null; + } + } diff --git a/assets/Assets.php b/assets/Assets.php index cc832ab..2508671 100644 --- a/assets/Assets.php +++ b/assets/Assets.php @@ -24,6 +24,13 @@ class Assets extends AssetBundle 'js/humhub.text_editor.js', ]; + /** + * @inheritdoc + */ + public $css = [ + 'css/text_editor.css', + ]; + /** * @inheritdoc */ diff --git a/controllers/CreateController.php b/controllers/CreateController.php new file mode 100644 index 0000000..cbc8cfa --- /dev/null +++ b/controllers/CreateController.php @@ -0,0 +1,52 @@ +getModule('text-editor'); + + return $this->renderAjax('index', [ + 'types' => $module->getTypesData(), + ]); + } + + public function actionFile() + { + $model = new CreateFile(['fileType' => Yii::$app->request->get('type')]); + + if ($model->load(Yii::$app->request->post())) { + if ($file = $model->save()) { + return $this->asJson([ + 'success' => true, + 'file' => FileHelper::getFileInfos($file), + 'editFormUrl' => $model->openEditForm ? Url::to(['/text-editor/edit', 'guid' => $file->guid]) : false, + ]); + } else { + return $this->asJson([ + 'success' => false, + 'output' => $this->renderAjax('file', ['model' => $model]) + ]); + } + } + + return $this->renderAjax('file', ['model' => $model]); + } + +} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 59dbbd4..977f121 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,4 +4,5 @@ Changelog 0.0.1 - Unreleased ------------------------- - Init: Initialize Module "Text editor" -- Fix #5: Position of "Edit" Handler \ No newline at end of file +- Fix #5: Position of "Edit" Handler +- Enh #2: Create new text files \ No newline at end of file diff --git a/filehandler/CreateFileHandler.php b/filehandler/CreateFileHandler.php new file mode 100644 index 0000000..49f55f1 --- /dev/null +++ b/filehandler/CreateFileHandler.php @@ -0,0 +1,36 @@ + Yii::t('TextEditorModule.base', 'Create file (Text, Log, XML)'), + 'data-action-url' => Url::to(['/text-editor/create']), + 'data-action-click' => 'ui.modal.load', + 'data-modal-id' => 'texteditor-modal', + 'data-modal-close' => '' + ]; + } + +} diff --git a/models/CreateFile.php b/models/CreateFile.php new file mode 100644 index 0000000..d7e1399 --- /dev/null +++ b/models/CreateFile.php @@ -0,0 +1,96 @@ +getModule('text-editor'); + + if (!$module->isSupportedType($this->fileType)) { + throw new Exception('Invalid file type!'); + } + + parent::init(); + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + ['fileName', 'required'], + ['openEditForm', 'boolean'], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'openEditForm' => Yii::t('TextEditorModule.base', 'Edit the new file in the next step') + ]; + } + + /** + * @return false|File + */ + public function save() + { + /* @var $module Module */ + $module = Yii::$app->getModule('text-editor'); + + if (!$this->validate()) { + return false; + } + + $file = new File(); + $file->file_name = $this->fileName . '.' . $this->fileType; + $file->size = 0; + $file->mime_type = $module->getTypeInfo($this->fileType, 'mimeType'); + if (!$file->save()) { + return false; + } + + $file->store->setContent(''); + + return $file; + } + +} diff --git a/models/FileUpdate.php b/models/FileUpdate.php index f4a127f..e21102c 100644 --- a/models/FileUpdate.php +++ b/models/FileUpdate.php @@ -37,7 +37,7 @@ public function afterFind() */ public function beforeValidate() { - if ($this->newFileContent && $this->size === null) { + if ($this->newFileContent) { $this->setFileSize(); } diff --git a/resources/css/text_editor.css b/resources/css/text_editor.css new file mode 100644 index 0000000..7f6bb36 --- /dev/null +++ b/resources/css/text_editor.css @@ -0,0 +1,46 @@ +ul.text-editor-types { + list-style: none; + margin: 0; + padding: 0; +} +ul.text-editor-types li { + float: left; + cursor: pointer; + margin: 15px 25px; + width: 135px; +} + +a.text-editor-type { + display: block; + border: 1px solid #EEE; + border-radius: 5px; + padding: 12px; + font-size: 14px; + font-weight: bold; + text-align: center; + text-decoration: none; +} +a.text-editor-type .fa { + display: block; + font-size: 100px; + margin-bottom: 10px; +} + +a.text-editor-type.txt .fa { + color: #333; +} +a.text-editor-type.txt:hover { + background-color: #ddd; +} +a.text-editor-type.log .fa { + color: #582F0E; +} +a.text-editor-type.log:hover { + background-color: #ede0d4; +} +a.text-editor-type.xml .fa { + color: #003049; +} +a.text-editor-type.xml:hover { + background-color: #caf0f8; +} \ No newline at end of file diff --git a/resources/js/humhub.text_editor.js b/resources/js/humhub.text_editor.js index 279b22c..0e186ba 100644 --- a/resources/js/humhub.text_editor.js +++ b/resources/js/humhub.text_editor.js @@ -3,6 +3,7 @@ humhub.module('text_editor', function (module, require, $) { var modal = require('ui.modal'); var object = require('util').object; var Widget = require('ui.widget').Widget; + var event = require('event'); var Editor = function (node, options) { Widget.call(this, node, options); @@ -38,8 +39,28 @@ humhub.module('text_editor', function (module, require, $) { evt.finish(); } + var createSubmit = function (evt) { + client.submit(evt).then(function (response) { + var modalWindow = modal.get('#texteditor-modal'); + if (response.success) { + event.trigger('humhub:file:created', [response.file]); + if (response.editFormUrl) { + modalWindow.load(response.editFormUrl); + modalWindow.show(); + } else { + modalWindow.close(); + } + } else { + modalWindow.setContent(response.output); + } + }).catch(function (e) { + module.log.error(e, true); + }); + }; + module.export({ Editor: Editor, + createSubmit: createSubmit, }); }); \ No newline at end of file diff --git a/views/create/file.php b/views/create/file.php new file mode 100644 index 0000000..aa2613c --- /dev/null +++ b/views/create/file.php @@ -0,0 +1,34 @@ + + + Yii::t('TextEditorModule.base', 'Create file')]) ?> + + + + + + + + + + \ No newline at end of file diff --git a/views/create/index.php b/views/create/index.php new file mode 100644 index 0000000..e660262 --- /dev/null +++ b/views/create/index.php @@ -0,0 +1,33 @@ + + + Yii::t('TextEditorModule.base', 'Create file')]); ?> + +