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
85 changes: 23 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@

## 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.

php composer.phar require --prefer-dist 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.

"froala/wysiwyg-editor": "^2.6.5"

```

to the require section of your `composer.json` file.
Expand All @@ -29,7 +38,7 @@ to the require section of your `composer.json` file.
Once the extension is installed, simply use it in your code by :

```php
<?php echo froala\froalaeditor\FroalaEditorWidget::widget([
<?php echo Froala\Editor\FroalaEditorWidget::widget([
'name' => 'content',
'options' => [
// html attributes
Expand All @@ -46,7 +55,7 @@ Once the extension is installed, simply use it in your code by :
or use with a model:

```php
<?php echo froala\froalaeditor\FroalaEditorWidget::widget([
<?php echo Froala\Editor\FroalaEditorWidget::widget([
'model' => $model,
'attribute' => 'content',
'options' => [
Expand All @@ -63,77 +72,29 @@ 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\Editor\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([
<?= \Froala\Editor\FroalaEditorWidget::widget([
'name' => 'body',
'clientOptions' => [
'toolbarInline'=> false,
'height' => 200,
'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/'])
],
'clientPlugins'=> ['fullscreen', 'paragraph_format', 'image']
]); ?>
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
],
"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",
"froala/wysiwyg-editor": "^2.6.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/FroalaEditorAsset.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace froala\froalaeditor;
namespace Froala\Editor;

use yii\base\Exception;
use yii\helpers\ArrayHelper;
Expand Down
17 changes: 14 additions & 3 deletions src/FroalaEditorWidget.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace froala\froalaeditor;
namespace Froala\Editor;

use yii;
use yii\helpers\Html;
Expand Down Expand Up @@ -85,12 +85,12 @@ public function registerClientScript()
//theme
$themeType = isset($this->clientOptions['theme']) ? $this->clientOptions['theme'] : 'default';
if ($themeType != 'default') {
$view->registerCssFile("{$asset->baseUrl}/css/themes/{$themeType}.css", ['depends' => '\froala\froalaeditor\FroalaEditorAsset']);
$view->registerCssFile("{$asset->baseUrl}/css/themes/{$themeType}.css", ['depends' => '\Froala\Editor\FroalaEditorAsset']);
}
//language
$langType = isset($this->clientOptions['language']) ? $this->clientOptions['language'] : 'en_gb';
if ($langType != 'es_gb') {
$view->registerJsFile("{$asset->baseUrl}/js/languages/{$langType}.js", ['depends' => '\froala\froalaeditor\FroalaEditorAsset']);
$view->registerJsFile("{$asset->baseUrl}/js/languages/{$langType}.js", ['depends' => '\Froala\Editor\FroalaEditorAsset']);
}

$id = $this->options['id'];
Expand All @@ -110,4 +110,15 @@ 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']);
$config['clientOptions']['imageUploadParam'] = 'file';
}

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\Editor;

class Module extends \yii\base\Module
{
public $controllerNamespace = 'Froala\Editor\controllers';

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

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

namespace Froala\Editor\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;

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

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