FEATURE: move bussiness logic out of models

This commit is contained in:
Michal Kváček
2016-10-17 01:05:20 +02:00
parent 2ded2924a1
commit e26e6fd460
46 changed files with 1069 additions and 1039 deletions
+1 -76
View File
@@ -2,9 +2,6 @@
namespace App\Models\Content;
use App\CMS\Handlers\Contracts\AppHandler;
use App\CMS\Handlers\Contracts\HistoryHandler;
use App\CMS\Helpers\Differ;
use Illuminate\Database\Eloquent\Model;
/**
@@ -12,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
*/
class ContentHistory extends Model
{
use ModelTraits\ContentHistory;
protected $table = 'content_history';
// this table does not have created_at and updated_at fields
public $timestamps = false;
@@ -24,77 +22,4 @@ class ContentHistory extends Model
'old_value'
];
protected $guarded = [];
/**
* Returns history grouped by revisions (edit_batches)
*
* @param Content $content
*
* @return array
*/
public function getStructuredHistoryOf(Content $content)
{
$data = $content->history()
->select('content_history.*', 'contents.module_handler', 'users.email AS user_email', 'users.name AS user_name')
->join('users', 'users.id', '=', 'content_history.user_id')
->join('contents', 'contents.id', '=', 'content_history.content_id')
->orderBy('changed_at', 'DESC')
->get();
$info = [];
$history = [];
foreach ($data as $change) {
$info[$change->edit_batch] = [
'changed_at' => $change->changed_at,
'user' => [
'id' => $change->user_id,
'name' => $change->user_name,
'email' => $change->user_email
]
];
$change = $this->handleChangeset($change);
$history[$change->edit_batch][] = $change;
}
return [
'changesets' => $history,
'info' => $info
];
}
/**
* Getter for history handler of given module
*
* @param $change
*
* @return HistoryHandler
*/
protected function getHistoryHandler($change)
{
/**
* @var $handler AppHandler
*/
// get handler of given content page
$handler = $change->module_handler;
return $handler::getHistoryHandler();
}
public function handleChangeset($change)
{
$historyHandler = $this->getHistoryHandler($change);
$change->column_trans = $historyHandler::parseColumnName($change->column);
$change->diff = $historyHandler::parseChangedValue($change);
return $change;
}
public function getDiff($change)
{
return Differ::htmlDiff(e($change->old_value), e($change->new_value));
}
}