Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Run `composer update` to update your app or `composer install` to install.

After install / update you will need to run the packages migrations like so:

php artisan migrate --package="olsgreen/auditable"
php artisan migrate --package="olsgreen/laravel-auditable"

and also add `Auditable` as a service provider within your `app/config/app.php`:

Expand All @@ -48,7 +48,20 @@ Auditable offers two methods of implementation:
To come.

### Using Auditable via a model event observer
To come.
Import the Obeser in your model

use Olsgreen\Auditable\Observer;

Add observer to your model

User::observe(new Observer);

Provide representation to your model.

public $represent = 'first_name';
//or
public $represent = 'country_name';


## License
Copyright (c) 2014 Oliver Green
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "olsgreen/laravel-auditable",
"name": "cschand/laravel-auditable",
"description": "An easy to implement attribute audit log for Eloquent ORM in Laravel 4.X.",
"authors": [
{
Expand All @@ -17,4 +17,4 @@
}
},
"minimum-stability": "dev"
}
}
94 changes: 77 additions & 17 deletions src/Olsgreen/Auditable/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function get(Model $model)
* @param Illuminate\Database\Eloquent\Model $model
* @return boolean Whether there was a Changeset created
*/
public function record(Model $model)
public function recordChange(Model $model)
{
$dirty = $model->getDirty();

Expand All @@ -50,22 +50,8 @@ public function record(Model $model)

if ($model->exists && count($dirty) > 0) {

// Grab the models PK
$primarykey = (isset($model->primarykey))
? $model->primarykey : 'id';

/*
* -------------------------------------------------
* Create the Changeset
* -------------------------------------------------
* We have to improvise a little here as we
* don't want to depend on models having
* relationships with the Auditable Changeset model.
*/
$cs = new Changeset;
$cs->object_type = get_class($model); // Manual
$cs->object_id = $model->$primarykey; // Manual
$cs->user_id = (\Auth::user() instanceof \User) ? \Auth::user()->id : 0;
$cs = $this->saveChangeset($model);
$cs->action = 'edited';
$cs->save();

// Add the Changeset changes
Expand All @@ -83,4 +69,78 @@ public function record(Model $model)
return false;
}

/**
* Common function for saving the changeset
*
* @param Illuminate\Database\Eloquent\Model $model
* @return Olsgreen\Auditable\Changeset
*/
private function saveChangeset(Model $model) {

// Grab the models PK
$primarykey = (isset($model->primarykey))
? $model->primarykey : 'id';

/*
* -------------------------------------------------
* Create the Changeset
* -------------------------------------------------
* We have to improvise a little here as we
* don't want to depend on models having
* relationships with the Auditable Changeset model.
*/
$cs = new Changeset;
$cs->object_type = get_class($model); // Manual
$cs->object_id = $model->$primarykey; // Manual
$cs->user_id = (\Auth::user() instanceof \User) ? \Auth::user()->id : 0;
$cs->name = $this->getName($model);
$cs->save();
return $cs;
}

/**
* Get name from the representation of models
* Checks first the represent is added and if not check name field
*
* @param Illuminate\Database\Eloquent\Model $model
* @return mixed name
*/
private function getName(Model $model) {

if(isset($model->represent) && isset($model[$model->represent])) {
return $model[$model->represent];
}
if(isset($model['name'])) {
return $model['name'];
}
return null;

}

/**
* Records the value when created with data
*
* @param Illuminate\Database\Eloquent\Model $model
* @return boolean true on successful updation
*/
public function recordCreate(Model $model) {
$cs = $this->saveChangeset($model);
$cs->action = 'created';
$cs->save();
return true;
}

/**
* Records the value when deleting the data
*
* @param Illuminate\Database\Eloquent\Model $model
* @return boolean true on successful updation of changeset
*/
public function recordDelete(Model $model) {
$cs = $this->saveChangeset($model);
$cs->action = 'deleted';
$cs->save();
return true;
}

}
14 changes: 13 additions & 1 deletion src/Olsgreen/Auditable/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@ class Observer {
public function saving($model)
{
$a = new Auditable;
$a->record($model);
$a->recordChange($model);
}

public function created($model)
{
$a = new Auditable;
$a->recordCreate($model);
}

public function deleting($model)
{
$a = new Auditable;
$a->recordDelete($model);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function up()
$table->string('object_type');
$table->integer('object_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('action', 255)->nullable();
$table->string('name', 255)->nullable();
$table->timestamps();
$table->index(array('object_id', 'object_type'));
});
Expand Down