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

Yii2 Froala Plugin, Update in accordance with Froala PHP SDK #20

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
78 changes: 19 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

## Installation

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). yii2-froala editor depends on Froala PHP SDK
To use and Install the editor, one must install PHP SDK also.

Either run

```
php composer.phar require --prefer-dist froala/yii2-froala-editor
php composer.phar require --prefer-dist froala/wysiwyg-editor-php-sdk
php composer.phar require --prefer-dist bower-asset/froala-wysiwyg-editor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a composer package as well: https://packagist.org/packages/froala/wysiwyg-editor.


```

or add

```
"froala/yii2-froala-editor": "^2.6.0"
"froala/yii2-froala-editor": "^2.6.0",
"froala/wysiwyg-editor-php-sdk" : "*",
"bower-asset/froala-wysiwyg-editor": "^2.6.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, there is a compose package: https://packagist.org/packages/froala/wysiwyg-editor.


```

to the require section of your `composer.json` file.
Expand Down Expand Up @@ -63,65 +70,19 @@ or use with a model:

## Upload example

Using the basic Yii template make a new folder under /web/ called uploads.

For controler:

```php
public function actionUpload() {
$base_path = Yii::getAlias('@app');
$web_path = Yii::getAlias('@web');
$model = new UploadForm();

if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstanceByName('file');

if ($model->validate()) {
$model->file->saveAs($base_path . '/web/uploads/' . $model->file->baseName . '.' . $model->file->extension);
}
}

// Get file link
$res = [
'link' => $web_path . '/uploads/' . $model->file->baseName . '.' . $model->file->extension,
];

// Response data
Yii::$app->response->format = Yii::$app->response->format = Response::FORMAT_JSON;
return $res;
}
```

For model:
Using the Froala PHP SDK with Froala Editor widget, the first step would be to add the configuration in web.php config file, make an entry for Froala Module
in the Config Array.

```php
namespace app\models;
use yii\base\Model;
use yii\web\UploadedFile;

/**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
/**
* @var UploadedFile|Null file attribute
*/
public $file;

/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file']
];
}
}
'modules' => [
'froala' => [
'class' => '\froala\froalaeditor\Module',
'uploadFolder' => '/uploads/'
]
],
```

For the view:
Make sure you have a folder called "uploads" in your web root directory,Now to use the Froala Widget on any view just use the following code in the view:

```php
<?= \froala\froalaeditor\FroalaEditorWidget::widget([
Expand All @@ -132,8 +93,7 @@ For the view:
'theme' => 'royal',//optional: dark, red, gray, royal
'language' => 'en_gb' ,
'toolbarButtons' => ['fullscreen', 'bold', 'italic', 'underline', '|', 'paragraphFormat', 'insertImage'],
'imageUploadParam' => 'file',
'imageUploadURL' => \yii\helpers\Url::to(['site/upload/'])
'imageUploadParam' => 'file'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be required.

],
'clientPlugins'=> ['fullscreen', 'paragraph_format', 'image']
]); ?>
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
],
"require": {
"yiisoft/yii2": "^2.0",
"froala/wysiwyg-editor": "^2.6.0",
"rmrevin/yii2-fontawesome": "^2.0"
"rmrevin/yii2-fontawesome": "^2.0",
"froala/wysiwyg-editor-php-sdk": "*",
"bower-asset/froala-wysiwyg-editor": "^2.6.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the composer package.

},
"autoload": {
"psr-4": {
Expand Down
10 changes: 10 additions & 0 deletions src/FroalaEditorWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ public function registerClientScript()

$view->registerJs("\$('#$id').froalaEditor($jsOptions);");
}

public static function widget($config = [])
{
if (!isset($config['clientOptions']['imageUploadURL']))
{
$config['clientOptions']['imageUploadURL'] = \yii\helpers\Url::to(['froala/default/upload']);
}

return parent::widget($config);
}
}
13 changes: 13 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace froala\froalaeditor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's user Froala\Editor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand this change, do you want something like this?

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be better to use Froala\Editor instead of froala\froalaeditor.


class Module extends \yii\base\Module
{
public $controllerNamespace = 'froala\froalaeditor\controllers';

// Without false it will give "Bad Request (#400) Unable to verify your data submission."
public $enableCsrfValidation = false;

public $uploadFolder;
}
26 changes: 26 additions & 0 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace froala\froalaeditor\controllers;

use yii\web\Controller;

class DefaultController extends Controller
{
// Without false it will give "Bad Request (#400) Unable to verify your data submission."
public $enableCsrfValidation = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set CSRF and pass it along with imageUploadParams?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or through the requestHeaders option?


public function actionUpload()
{
// Store the image.
try {
$uploadFolder = $this->module->uploadFolder;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use 4 spaces indentation.


$response = \FroalaEditor_Image::upload($uploadFolder);
echo stripslashes(json_encode($response));
}
catch (Exception $e) {
http_response_code(404);
}

}
}