Files
opensem/app/Repositories/Core/Comments.php
Ludovic CANDELLIER 9e2226a776 Fix on preview mode
2021-08-23 23:56:46 +02:00

99 lines
2.3 KiB
PHP

<?php
namespace App\Repositories\Core;
use BeyondCode\Comments\Comment;
use App\Models\Core\Comment as rawComment;
use App\Repositories\Core\Auth\Users;
use App\Datatables\Admin\Core\CommentsDataTable;
class Comments
{
public static function get($id)
{
return rawComment::find($id);
}
public static function getDatatable()
{
$model = new CommentsDataTable();
return $model->html();
}
public static function getCommentsByModel($model, $model_id)
{
$class = self::getClass($model);
return self::getCommentsByClass($class, $model_id);
}
public static function getCommentsByClass($class, $id)
{
return self::getByModel(self::getModel($class, $id));
}
public static function getModel($class, $id)
{
return $class::find($id);
}
public static function getClass($model)
{
return 'App\Models\\' . str_replace('.','\\', $model);
}
public static function getByModel($model)
{
return $model ? $model->comments->sortByDesc('updated_at')->toArray() : false;
}
public static function storeComments($model, $comments)
{
foreach (($comments ?? []) as $comment) {
self::storeComment($model, $comment);
}
}
public static function storeComment($model, $comment)
{
return $model->comment($comment);
}
public static function store($data)
{
$id = $data['id'] ?? false;
unset($data['_token']);
$data['commentable_type'] = Comments::getClass($data['commentable_type']);
$data['commentable_id'] = (int) $data['commentable_id'];
return $id ? self::update($data, $id) : self::create($data);
}
public static function create($data)
{
unset($data['id']);
$data['is_approved'] = true;
$data['user_id'] = Users::getId();
return rawComment::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$model = self::get($id);
$model->update($data);
return $model;
}
public static function deleteComments($model)
{
return true;
}
public static function deleteComment($model, $index)
{
return true;
}
}