2015-08-16 18:59:23 +01:00
|
|
|
<?php
|
|
|
|
|
2018-09-25 12:30:50 +01:00
|
|
|
namespace BookStack\Actions;
|
|
|
|
|
2023-01-24 14:55:34 +00:00
|
|
|
use BookStack\Auth\Permissions\JointPermission;
|
2018-09-25 12:30:50 +01:00
|
|
|
use BookStack\Auth\User;
|
2020-11-22 00:17:45 +00:00
|
|
|
use BookStack\Entities\Models\Entity;
|
2018-09-25 12:30:50 +01:00
|
|
|
use BookStack\Model;
|
2020-11-08 00:03:19 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2023-01-24 14:55:34 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2021-03-07 22:24:05 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
2020-11-20 19:33:11 +00:00
|
|
|
use Illuminate\Support\Str;
|
2015-08-16 18:59:23 +01:00
|
|
|
|
|
|
|
/**
|
2020-11-08 00:03:19 +00:00
|
|
|
* @property string $type
|
2021-06-26 15:23:15 +00:00
|
|
|
* @property User $user
|
2019-09-19 18:03:17 +01:00
|
|
|
* @property Entity $entity
|
2020-11-08 00:03:19 +00:00
|
|
|
* @property string $detail
|
2019-09-19 18:03:17 +01:00
|
|
|
* @property string $entity_type
|
2021-06-26 15:23:15 +00:00
|
|
|
* @property int $entity_id
|
|
|
|
* @property int $user_id
|
2015-08-16 18:59:23 +01:00
|
|
|
*/
|
|
|
|
class Activity extends Model
|
|
|
|
{
|
2015-08-30 11:47:58 +01:00
|
|
|
/**
|
|
|
|
* Get the entity for this activity.
|
|
|
|
*/
|
2021-03-07 22:24:05 +00:00
|
|
|
public function entity(): MorphTo
|
2015-08-16 18:59:23 +01:00
|
|
|
{
|
2018-01-28 16:58:52 +00:00
|
|
|
if ($this->entity_type === '') {
|
|
|
|
$this->entity_type = null;
|
|
|
|
}
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2016-02-28 19:03:04 +00:00
|
|
|
return $this->morphTo('entity');
|
2015-08-16 18:59:23 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 11:47:58 +01:00
|
|
|
/**
|
|
|
|
* Get the user this activity relates to.
|
|
|
|
*/
|
2020-11-08 00:03:19 +00:00
|
|
|
public function user(): BelongsTo
|
2015-08-16 18:59:23 +01:00
|
|
|
{
|
2016-05-01 21:20:50 +01:00
|
|
|
return $this->belongsTo(User::class);
|
2015-08-16 18:59:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 14:55:34 +00:00
|
|
|
public function jointPermissions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(JointPermission::class, 'entity_id', 'entity_id')
|
|
|
|
->whereColumn('activities.entity_type', '=', 'joint_permissions.entity_type');
|
|
|
|
}
|
|
|
|
|
2015-08-16 18:59:23 +01:00
|
|
|
/**
|
2020-11-08 00:03:19 +00:00
|
|
|
* Returns text from the language files, Looks up by using the activity key.
|
2015-08-16 18:59:23 +01:00
|
|
|
*/
|
2020-11-08 00:03:19 +00:00
|
|
|
public function getText(): string
|
2015-08-16 18:59:23 +01:00
|
|
|
{
|
2020-11-08 00:03:19 +00:00
|
|
|
return trans('activities.' . $this->type);
|
2015-08-16 18:59:23 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:33:11 +00:00
|
|
|
/**
|
|
|
|
* Check if this activity is intended to be for an entity.
|
|
|
|
*/
|
|
|
|
public function isForEntity(): bool
|
|
|
|
{
|
|
|
|
return Str::startsWith($this->type, [
|
2021-06-26 15:23:15 +00:00
|
|
|
'page_', 'chapter_', 'book_', 'bookshelf_',
|
2020-11-20 19:33:11 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-08-30 11:47:58 +01:00
|
|
|
/**
|
|
|
|
* Checks if another Activity matches the general information of another.
|
|
|
|
*/
|
2021-10-26 22:04:18 +01:00
|
|
|
public function isSimilarTo(self $activityB): bool
|
2018-01-28 16:58:52 +00:00
|
|
|
{
|
2020-11-08 00:03:19 +00:00
|
|
|
return [$this->type, $this->entity_type, $this->entity_id] === [$activityB->type, $activityB->entity_type, $activityB->entity_id];
|
2015-08-30 11:47:58 +01:00
|
|
|
}
|
2015-08-16 18:59:23 +01:00
|
|
|
}
|