Skip to content

Commit

Permalink
Create new text files
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Oct 14, 2021
1 parent e526b2d commit 767ea8b
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
45 changes: 42 additions & 3 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}

}
7 changes: 7 additions & 0 deletions assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class Assets extends AssetBundle
'js/humhub.text_editor.js',
];

/**
* @inheritdoc
*/
public $css = [
'css/text_editor.css',
];

/**
* @inheritdoc
*/
Expand Down
52 changes: 52 additions & 0 deletions controllers/CreateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\components\Controller;
use humhub\modules\file\libs\FileHelper;
use humhub\modules\text_editor\models\CreateFile;
use humhub\modules\text_editor\Module;
use Yii;
use yii\helpers\Url;

class CreateController extends Controller
{

public function actionIndex()
{
/* @var Module $module */
$module = Yii::$app->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]);
}

}
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Changelog
0.0.1 - Unreleased
-------------------------
- Init: Initialize Module "Text editor"
- Fix #5: Position of "Edit" Handler
- Fix #5: Position of "Edit" Handler
- Enh #2: Create new text files
36 changes: 36 additions & 0 deletions filehandler/CreateFileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

/**
* CreateFileHandler provides the creating of a text file
*
* @author Luke
*/
class CreateFileHandler extends BaseFileHandler
{

/**
* @inheritdoc
*/
public function getLinkAttributes()
{
return [
'label' => Yii::t('TextEditorModule.base', 'Create file <small>(Text, Log, XML)</small>'),
'data-action-url' => Url::to(['/text-editor/create']),
'data-action-click' => 'ui.modal.load',
'data-modal-id' => 'texteditor-modal',
'data-modal-close' => ''
];
}

}
96 changes: 96 additions & 0 deletions models/CreateFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\models;

use humhub\modules\text_editor\Module;
use humhub\modules\file\models\File;
use Yii;
use yii\base\Exception;
use yii\base\Model;

/**
* CreateFile is a form for create a new text file
*
* @author Luke
*/
class CreateFile extends Model
{
/**
* @var string
*/
public $fileType;

/**
* @var string
*/
public $fileName;

/**
* @var bool
*/
public $openEditForm = true;

public function init()
{
/* @var Module $module */
$module = Yii::$app->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;
}

}
2 changes: 1 addition & 1 deletion models/FileUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function afterFind()
*/
public function beforeValidate()
{
if ($this->newFileContent && $this->size === null) {
if ($this->newFileContent) {
$this->setFileSize();
}

Expand Down
46 changes: 46 additions & 0 deletions resources/css/text_editor.css
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions resources/js/humhub.text_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
});

});
Loading

0 comments on commit 767ea8b

Please sign in to comment.