Kotchasan Framework Documentation
CKEditor
CKEditor
\Kotchasan\CKEditor for WYSIWYG rich text editor.
Usage
use Kotchasan\CKEditor;
use Kotchasan\Html;
// Create form
$form = Html::create('form');
// Add CKEditor
$form->add('ckeditor', [
'id' => 'content',
'name' => 'content',
'label' => 'Content',
'value' => $existingContent,
'upload' => true // Enable image upload
]);
echo $form->render();Attributes
| Attribute | Type | Description |
|---|---|---|
id |
string | Editor ID |
name |
string | Field name |
label |
string | Label to display |
value |
string | Initial content |
upload |
bool | Enable file upload |
itemClass |
string | CSS class for container |
Methods
enabledUpload()
Check if upload is active.
if (CKEditor::enabledUpload()) {
// User can upload
}- Admin: Always allowed
- Member: Requires appropriate session
- Guest: Not allowed
render()
Render CKEditor as HTML.
$html = $editor->render();toTextarea()
Convert special characters for use in textarea.
$text = $editor->toTextarea($content);toDiv()
Convert {} to HTML entities for use in div.
$text = $editor->toDiv($content);Usage Examples
Basic Editor
$form = Html::create('form', [
'action' => 'save.php',
'method' => 'POST'
]);
$form->add('ckeditor', [
'id' => 'article_content',
'name' => 'content',
'label' => 'Article Content',
'value' => ''
]);
$form->add('button', [
'type' => 'submit',
'value' => 'Save'
]);
echo $form->render();Editor with Image Upload
$form = Html::create('form');
$form->add('ckeditor', [
'id' => 'article',
'name' => 'article',
'label' => 'Article',
'value' => $article->content,
'upload' => true,
'height' => 400,
'toolbar' => 'Full'
]);
echo $form->render();Use with CKFinder
If CKFinder is installed at ROOT_PATH/ckfinder, it will be used automatically:
// Requires ckfinder folder
// ROOT_PATH/ckfinder/ckfinder.html
$form->add('ckeditor', [
'id' => 'content',
'upload' => true // Will use CKFinder automatically
]);Configuration Options
CKEditor supports standard CKEditor options:
$form->add('ckeditor', [
'id' => 'editor',
'height' => 300,
'width' => '100%',
'toolbar' => 'Basic',
'language' => 'en',
'enterMode' => 2, // CKEDITOR.ENTER_BR
'shiftEnterMode' => 1 // CKEDITOR.ENTER_P
]);JavaScript Output
CKEditor automatically generates JavaScript initialization:
CKEDITOR.replace("content", {
height:300,
width:"100%",
toolbar:"Basic"
});Data Handling
Saving Data
// Get data from form
$content = $_POST['content'];
// Save to database
$db->insert('articles')->values([
'content' => $content
])->execute();Displaying Data
// Retrieve from database
$article = $db->select('*')->from('articles')->first();
// Display (Content is already HTML)
echo $article->content;Security
[!WARNING]
Content from CKEditor is HTML. Validate before saving.
// Use HTML Purifier or other filters
$content = strip_tags($content, '<p><a><strong><em><ul><li><ol>');