diff --git a/README.md b/README.md index a424538..b292906 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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 diff --git a/composer.json b/composer.json index 4a76e87..41a809e 100644 --- a/composer.json +++ b/composer.json @@ -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": [ { @@ -17,4 +17,4 @@ } }, "minimum-stability": "dev" -} \ No newline at end of file +} diff --git a/src/Olsgreen/Auditable/Auditable.php b/src/Olsgreen/Auditable/Auditable.php index a490b57..0e94de0 100644 --- a/src/Olsgreen/Auditable/Auditable.php +++ b/src/Olsgreen/Auditable/Auditable.php @@ -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(); @@ -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 @@ -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; + } + } \ No newline at end of file diff --git a/src/Olsgreen/Auditable/Observer.php b/src/Olsgreen/Auditable/Observer.php index 7e97a46..0381369 100644 --- a/src/Olsgreen/Auditable/Observer.php +++ b/src/Olsgreen/Auditable/Observer.php @@ -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); } } \ No newline at end of file diff --git a/src/migrations/2014_01_31_142328_add_auditable_changesets_table.php b/src/migrations/2014_01_31_142328_add_auditable_changesets_table.php index 655d33e..cc13d7b 100644 --- a/src/migrations/2014_01_31_142328_add_auditable_changesets_table.php +++ b/src/migrations/2014_01_31_142328_add_auditable_changesets_table.php @@ -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')); });