Skip to content

Instantly share code, notes, and snippets.

@matula
Created November 21, 2013 22:58
Show Gist options
  • Save matula/7591382 to your computer and use it in GitHub Desktop.
Save matula/7591382 to your computer and use it in GitHub Desktop.
Adding in the Sir Trevor JS library into a PHP project using Laravel.
<!-- Not the FULL view -->
<link rel="stylesheet" href="{{ url('css/sir-trevor-icons.css')}}" type="text/css">
<link rel="stylesheet" href="{{ url('css/sir-trevor.css')}}" type="text/css">
<!-- Using some Bootstrap here -->
<div class="container">
<div class="row" id="post-form-container">
<h3>Create A Post</h3>
<form method="post" action="form" id="post-form" role="form">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" id="title" placeholder="post title">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea name="content" id="st-content" class="form-control" placeholder="post content"></textarea>
</div>
<button type="submit" class="btn btn-primary" id="submitbtn">Submit</button>
</form>
</div>
</div>
<script src="{{ url('js/jquery.js')}}"></script>
<script src="{{ url('js/underscore.js')}}" type="text/javascript"></script>
<script src="{{ url('js/eventable.js')}}" type="text/javascript"></script>
<script src="{{ url('js/sir-trevor.js')}}" type="text/javascript"></script>
<script src="{{ url('js/st.js')}}" type="text/javascript"></script>
<?php
Route::get('/', function()
{
return View::make('main');
});
Route::post('form', function()
{
$data['title'] = Input::get('title');
$contents = json_decode(Input::get('content'));
foreach ($contents->data as $content)
{
switch ($content->type) {
case 'text':
$data['post'][] = [
'type' => $content->type,
'data' => $content->data->text,
];
break;
case 'image':
$data['post'][] = [
'type' => $content->type,
// Sir Trevor splits each character of the image name into a new object for some reason
// This will concat them
'data' => implode('', (array)$content->data),
];
break;
case 'video':
$data['post'][] = [
'type' => $content->type,
// On the front-end, I'll explode this on #. I'm sure there are other ways this could get done
'data' => $content->data->source . '#' . $content->data->remote_id,
];
break;
default:
break;
}
}
// $post = new Post;
// $post->title = $data['title'];
// $post->save();
//
// // Adds each row with post_id
// $blocks = new PostBlocks;
// $blocks->save($post->id, $data['post']);
// Just to debug
dd($data);
});
Route::post('upload', function()
{
// Get the text
$upload = Input::get('attachment');
// Get the file
$file = Input::file('attachment');
// Save to array and create a new name for saving
$data['orig_name'] = $upload['name'];
$data['uid'] = $upload['uid'];
$data['new_name'] = $data['uid'] . '-' . $data['orig_name'];
// Upload it
$upload_file = $file['file']->move('uploads', $data['new_name']);
// Saving to DB
// $attachment = new Attachment;
// $attachment->save($data);
return $upload_file;
});
$(function() {
SirTrevor.DEBUG = true;
var form = $('#post-form');
new SirTrevor.Editor({
el: $('#st-content'),
blockTypes: [
"Text",
"Video",
"Image"
],
defaultType: "Text",
});
SirTrevor.setDefaults({
uploadUrl: "/upload"
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment