diff --git a/src/Venturecraft/Revisionable/Revision.php b/src/Venturecraft/Revisionable/Revision.php index acbc20cc..884541fe 100644 --- a/src/Venturecraft/Revisionable/Revision.php +++ b/src/Venturecraft/Revisionable/Revision.php @@ -20,6 +20,8 @@ class Revision extends Eloquent */ public $table = 'revisions'; + protected $dates = ['accepted_at']; + /** * @var array */ @@ -288,4 +290,14 @@ public function format($key, $value) return $value; } } + + public function scopeOnlyPending($q) + { + $q->whereNull('accepted_at'); + } + + public function scopeOnlyAccepted($q) + { + $q->whereNotNull('accepted_at'); + } } diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index f298dcf7..412b77c6 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -93,6 +93,17 @@ public function revisionHistory() return $this->morphMany('\Venturecraft\Revisionable\Revision', 'revisionable'); } + /** + * Restrict the result to include only records which has pending revision + * history which are not accepted yet. + */ + public function scopeHasPendingRevisionHistory($query) + { + $query->whereHas('revisionHistory', function($q){ + $q->whereNull('accepted_at'); + }); + } + /** * Generates a list of the last $limit revisions made to any objects of the class it is being called from. * @@ -117,7 +128,9 @@ public function preSave() // if there's no revisionEnabled. Or if there is, if it's true $this->originalData = $this->original; + $this->updatedData = $this->attributes; + $this->updating = $this->exists; // we can only safely compare basic items, // so for now we drop any object based items, like DateTime @@ -143,7 +156,17 @@ public function preSave() unset($this->attributes['keepRevisionOf']); $this->dirtyData = $this->getDirty(); - $this->updating = $this->exists; + + $changes_to_record = $this->changedRevisionableFields(); + foreach ($changes_to_record as $key => $value) { + if($this->updating && $this->autoAccept == false && in_array($key, $this->keepRevisionOf)){ + if(isset($this->originalData[$key])){ + \Log::debug('Changing value for key '.$key); + $this->attributes[$key] = $this->originalData[$key]; + } + } + } + } } @@ -184,12 +207,19 @@ public function postSave() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), + 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) ); } if (count($revisions) > 0) { if($LimitReached && $RevisionCleanup){ - $toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get(); + $columns = collect($revisions)->pluck('key'); + $toDelete = $this->revisionHistory() + ->whereIn('key', $columns) + ->orderBy('id','asc') + ->limit(count($revisions)) + ->get(); + foreach($toDelete as $delete){ $delete->delete(); } @@ -226,6 +256,8 @@ public function postCreate() 'user_id' => $this->getSystemUserId(), 'created_at' => new \DateTime(), 'updated_at' => new \DateTime(), + 'accepted_at' => (($this->autoAccept == false) ? null : new \DateTime()) + ); $revision = new Revision;