Skip to content

Instantly share code, notes, and snippets.

@duellsy
Created August 23, 2013 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duellsy/6315222 to your computer and use it in GitHub Desktop.
Save duellsy/6315222 to your computer and use it in GitHub Desktop.
Displaying multiple revisions
<?php
class BaseController extends Controller
{
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if (!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
/**
* Display a listing of the clients.
*
* @param $model
* @param $item_id
*
* @return Response
*/
public function showRevisions($model, $item_id)
{
$item = $model::find($item_id);
$revisions = $item->revisionHistory;
$data = array(
'item' => $item,
'revisions' => $revisions,
'model' => Str::plural(substr($model, strpos($model, '\\') + 1)),
'model_id' => $item_id
);
// var_dump($data);exit;
$this->layout->nest('content', 'revisions/index', $data);
}
}
<h3>Revision History</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Field</th>
<th>Old value</th>
<th>New value</th>
<th>User</th>
<th>When</th>
<th>Tasks</th>
</tr>
</thead>
<tbody>
@foreach($revisions as $revision)
<?php $date = new ExpressiveDate($revision->created_at); ?>
<tr>
<td>{{ ucfirst($revision->fieldName()) }}</td>
<td>{{ $revision->oldValue() }}</td>
<td>{{ $revision->newValue() }}</td>
<td>{{ $revision->userResponsible()->first_name }}</td>
<td>{{ $date->getRelativeDate() }}</td>
<td><a class="btn tip" title="Revert this change" href="{{ URL::action('RevisionsController@revert', encryptId($revision->id, 'revisions')) }}"><i class="icon-signin"></i></a></td>
</tr>
@endforeach
</tbody>
</table>
<?php
class RevisionsController extends BaseController
{
public function revert($revision_id)
{
$revision_id = decryptId($revision_id, 'revisions');
$revision = \Venturecraft\Revisionable\Revision::find($revision_id);
$item = $revision->revisionable;
$field = $revision->key;
$item->$field = $revision->old_value;
$item->save();
Event::fire(strtolower($revision->revisionable_type) . '.updated', $item);
return Redirect::back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment