Skip to content
Open
12 changes: 12 additions & 0 deletions src/Venturecraft/Revisionable/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Revision extends Eloquent
*/
public $table = 'revisions';

protected $dates = ['accepted_at'];

/**
* @var array
*/
Expand Down Expand Up @@ -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');
}
}
36 changes: 34 additions & 2 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand All @@ -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];
}
}
}

}
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down