Skip to content

Instantly share code, notes, and snippets.

@lichtner
Created November 6, 2012 11:03
Show Gist options
  • Save lichtner/4024041 to your computer and use it in GitHub Desktop.
Save lichtner/4024041 to your computer and use it in GitHub Desktop.
Redactor.js nette presenter integration
<?php
class Article extends Base {
function getUploadDir() {
# e.g. something like this
return "upload/articles/article-$this[id]";
}
}
<?php
use Nette\Application\UI,
Nette\Forms\Controls\Button,
Nette\Application\Responses\JsonResponse;
class ArticlePresenter extends BasePresenter {
/** @var \Service\RedactorJsUpload */
private $redactorJsUpload;
public function injectRedactorJsUpload(\Service\RedactorJsUpload $upload) {
$this->redactorJsUpload = $upload;
}
public function actionEdit($id) {
$this->article = $this->createArticle()->find($id);
}
public function createComponentEdit() {
$form = new UI\Form;
$form->addTextArea('content', 'Content');
$form->addSubmit('update', 'Update')->onClick[] = $this->editFormUpdate;
return $form;
}
public function handleImageUpload() {
$imageJson = $this->redactorJsUpload->imageUpload(
$this->article->getUploadDir(),
$this->getHttpRequest()->getFile('file')
);
$this->sendResponse(new JsonResponse($imageJson));
}
public function handleImagesJson() {
$imagesJson = $this->redactorJsUpload->getImagesJson($this->article->getUploadDir());
$this->sendResponse(new JsonResponse($imagesJson));
}
public function handleFileUpload() {
$imageJson = $this->redactorJsUpload->fileUpload(
$this->article->getUploadDir(),
$this->getHttpRequest()->getFile('file')
);
$this->sendResponse(new JsonResponse($imageJson));
}
}
services:
redactorJsUpload: Service\RedactorJsUpload(%wwwDir%)
{block content}
{form edit}
{/block}
{block pageBottom}
<script src="{$basePath}/js/redactor/redactor.js"></script>
<script type="text/javascript">
$(document).ready(
function()
{
$('#frmedit-content').redactor({
imageUpload: {link imageUpload!},
imageGetJson: {link imagesJson!},
fileUpload: {link fileUpload!}
});
}
);
</script>
{/block}
<?php
namespace Service;
/**
* RedactorJsUpload manage upload images and files for redactor.js
*
* @author Marek Lichtner
*/
class RedactorJsUpload {
private $imagesDir = 'images';
private $thumbSubDir = 'thumbs';
private $filesDir = 'files';
private $wwwDir, $basePath;
function __construct($wwwDir, \Nette\Http\Request $request) {
$this->wwwDir = $wwwDir;
$this->basePath = substr($request->getUrl()->basePath, 0, -1);
}
function getImagesJson($uploadDir) {
$imagesDir = $uploadDir . '/' . $this->imagesDir;
$imagesJson = array();
$path = "$this->wwwDir/$imagesDir/$this->thumbSubDir";
foreach (glob("$path/*") as $file) {
$filename = basename($file);
$imagesJson[] = array(
'thumb' => "$this->basePath/$imagesDir/$this->thumbSubDir/$filename",
'image' => "$this->basePath/$imagesDir/$filename"
);
}
return $imagesJson;
}
function imageUpload($uploadDir, \Nette\Http\FileUpload $file) {
$imagesDir = $uploadDir . '/' . $this->imagesDir;
$fullImageDir = "$this->wwwDir/$imagesDir";
if (!file_exists($fullImageDir)) {
mkdir("$fullImageDir/$this->thumbSubDir", 0777, true);
}
# move image
$filename = strtolower($file->getSanitizedName());
$file->move("$fullImageDir/$filename");
$image = \Nette\Image::fromFile("$fullImageDir/$filename");
# set max size
$image->resize(1000, 700, $image::SHRINK_ONLY);
$image->save("$fullImageDir/$filename");
# create thumb
$image->resize(120, 120);
$image->save("$fullImageDir/$this->thumbSubDir/$filename");
return array('filelink' => "$this->basePath/$imagesDir/$filename");
}
function fileUpload($uploadDir, \Nette\Http\FileUpload $file) {
$filesDir = $uploadDir . '/' . $this->filesDir;
$fullFilesDir = "$this->wwwDir/$filesDir";
if (!file_exists($fullFilesDir)) {
mkdir("$fullFilesDir", 0777, true);
}
# move file
$filename = strtolower($file->getSanitizedName());
$file->move("$fullFilesDir/$filename");
return array('filelink' => "$this->basePath/$filesDir/$filename");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment