-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample.php
149 lines (130 loc) · 4.56 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace app\example;
use Yii;
use yii\web\Controller;
/**
* This controller handles five actions:
* - 3 generic ones to manipulate images
* - 2 specific ones for displaying content and saving it.
*/
class ExampleController extends Controller
{
/**
* Here is the setting for image handling ContentTools actions.
* Simple versions of these are provided by yii2-content-tools so let's just use them.
* Remember to configure widget to know where to find them.
* @return array
*/
public function actions()
{
return [
'image-upload' => \bizley\contenttools\actions\UploadAction::class,
'image-insert' => \bizley\contenttools\actions\InsertAction::class,
'image-rotate' => \bizley\contenttools\actions\RotateAction::class,
];
}
/**
* This action handles displaying the content.
* Here we fetch content stored for URL '/example/show' (or null if it's not saved yet) and pass it to the view.
* @return string
*/
public function actionShow()
{
$model = Content::findOne(['page' => '/example/show']);
return $this->render('show', ['model' => $model]);
}
/**
* This action handles saving the content.
* Actual data is saved in database and represented by Content model.
* @return string
*/
public function actionSave()
{
if (Yii::$app->request->isPost) {
// By default widget sends page key indicating the URL where the data was changed.
$page = Yii::$app->request->post('page');
// In case data has been previously saved let's find it for an update
$model = Content::findOne(['page' => $page]);
// In case it's not found because this is first time of saving it let's take new object instance
if ($model === null) {
$model = new Content();
}
// By default yii2-content-tools saves each editable region in 'contentToolsX' POST array key
// where X is the next number starting from 0. In this example we've got only one region.
$model->setAttributes([
'data' => Yii::$app->request->post('contentTools0'),
'page' => $page,
]);
if (!$model->save()) {
return $this->asJson(['errors' => $model->errors]);
}
return $this->asJson(true);
}
return $this->asJson(['errors' => ['No data sent in POST.']]);
}
}
use yii\db\ActiveRecord;
use yii\helpers\HtmlPurifier;
/**
* This model represents data stored in DB.
* In example let's use very simple table with only two columns.
*
* @property string $page Primary key of the table, identifies page URL
* @property string $data Stored content
*/
class Content extends ActiveRecord
{
/**
* @return string
*/
public static function tableName()
{
return 'content';
}
/**
* In this example validation rules are simple:
* - data and page are required,
* - data is filtered using HtmlPurifier to prevent malicious HTML being saved,
* - data must be a string with minimum 1 character length after purification,
* - page must one of the allowed URLs (in this example only one).
* @return array
*/
public function rules()
{
return [
[['page', 'data'], 'required'],
['data', 'filter', 'filter' => static function ($value) {
return HtmlPurifier::process($value);
}],
['data', 'string', 'min' => 1],
['page', 'in', 'range' => ['/example/show']],
];
}
}
/**
* ---------------------------------------------------------------------------------------------------------------------
* 'show' view file below
* ---------------------------------------------------------------------------------------------------------------------
*/
use bizley\contenttools\ContentTools;
/* @var $model Content */
?>
<div id="show-example">
<?php ContentTools::begin([
'imagesEngine' => [
'upload' => '/example/image-upload',
'rotate' => '/example/image-rotate',
'insert' => '/example/image-insert',
],
'saveEngine' => [
'save' => '/example/save',
]
]); ?>
<?php if ($model): ?>
<?= $model->data ?>
<?php else: ?>
<h2>This is heading example</h2>
<p>Here is the default text that can be changed using yii2-content-tools.</p>
<?php endif; ?>
<?php ContentTools::end(); ?>
</div>