mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Renamed attribute to tags & continued interface
Also fixed page create route broken in last commit
This commit is contained in:
@ -55,12 +55,12 @@ class Entity extends Ownable
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Attribute models that have been user assigned to this entity.
|
||||
* Get the Tag models that have been user assigned to this entity.
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
||||
*/
|
||||
public function attributes()
|
||||
public function tags()
|
||||
{
|
||||
return $this->morphMany(Attribute::class, 'entity');
|
||||
return $this->morphMany(Tag::class, 'entity');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,64 +0,0 @@
|
||||
<?php namespace BookStack\Http\Controllers;
|
||||
|
||||
use BookStack\Repos\AttributeRepo;
|
||||
use Illuminate\Http\Request;
|
||||
use BookStack\Http\Requests;
|
||||
|
||||
class AttributeController extends Controller
|
||||
{
|
||||
|
||||
protected $attributeRepo;
|
||||
|
||||
/**
|
||||
* AttributeController constructor.
|
||||
* @param $attributeRepo
|
||||
*/
|
||||
public function __construct(AttributeRepo $attributeRepo)
|
||||
{
|
||||
$this->attributeRepo = $attributeRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the Attributes for a particular entity
|
||||
* @param $entityType
|
||||
* @param $entityId
|
||||
*/
|
||||
public function getForEntity($entityType, $entityId)
|
||||
{
|
||||
$attributes = $this->attributeRepo->getForEntity($entityType, $entityId);
|
||||
return response()->json($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the attributes for a particular entity.
|
||||
* @param $entityType
|
||||
* @param $entityId
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateForEntity($entityType, $entityId, Request $request)
|
||||
{
|
||||
$entity = $this->attributeRepo->getEntity($entityType, $entityId, 'update');
|
||||
if ($entity === null) return $this->jsonError("Entity not found", 404);
|
||||
|
||||
$inputAttributes = $request->input('attributes');
|
||||
$attributes = $this->attributeRepo->saveAttributesToEntity($entity, $inputAttributes);
|
||||
return response()->json([
|
||||
'attributes' => $attributes,
|
||||
'message' => 'Attributes successfully updated'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute name suggestions from a given search term.
|
||||
* @param Request $request
|
||||
*/
|
||||
public function getNameSuggestions(Request $request)
|
||||
{
|
||||
$searchTerm = $request->get('search');
|
||||
$suggestions = $this->attributeRepo->getNameSuggestions($searchTerm);
|
||||
return response()->json($suggestions);
|
||||
}
|
||||
|
||||
|
||||
}
|
64
app/Http/Controllers/TagController.php
Normal file
64
app/Http/Controllers/TagController.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php namespace BookStack\Http\Controllers;
|
||||
|
||||
use BookStack\Repos\TagRepo;
|
||||
use Illuminate\Http\Request;
|
||||
use BookStack\Http\Requests;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
|
||||
protected $tagRepo;
|
||||
|
||||
/**
|
||||
* TagController constructor.
|
||||
* @param $tagRepo
|
||||
*/
|
||||
public function __construct(TagRepo $tagRepo)
|
||||
{
|
||||
$this->tagRepo = $tagRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the Tags for a particular entity
|
||||
* @param $entityType
|
||||
* @param $entityId
|
||||
*/
|
||||
public function getForEntity($entityType, $entityId)
|
||||
{
|
||||
$tags = $this->tagRepo->getForEntity($entityType, $entityId);
|
||||
return response()->json($tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the tags for a particular entity.
|
||||
* @param $entityType
|
||||
* @param $entityId
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateForEntity($entityType, $entityId, Request $request)
|
||||
{
|
||||
$entity = $this->tagRepo->getEntity($entityType, $entityId, 'update');
|
||||
if ($entity === null) return $this->jsonError("Entity not found", 404);
|
||||
|
||||
$inputTags = $request->input('tags');
|
||||
$tags = $this->tagRepo->saveTagsToEntity($entity, $inputTags);
|
||||
return response()->json([
|
||||
'tags' => $tags,
|
||||
'message' => 'Tags successfully updated'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tag name suggestions from a given search term.
|
||||
* @param Request $request
|
||||
*/
|
||||
public function getNameSuggestions(Request $request)
|
||||
{
|
||||
$searchTerm = $request->get('search');
|
||||
$suggestions = $this->tagRepo->getNameSuggestions($searchTerm);
|
||||
return response()->json($suggestions);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -28,7 +28,7 @@ Route::group(['middleware' => 'auth'], function () {
|
||||
// Pages
|
||||
Route::get('/{bookSlug}/page/create', 'PageController@create');
|
||||
Route::get('/{bookSlug}/draft/{pageId}', 'PageController@editDraft');
|
||||
Route::post('/{bookSlug}/page/{pageId}', 'PageController@store');
|
||||
Route::post('/{bookSlug}/draft/{pageId}', 'PageController@store');
|
||||
Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
|
||||
Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
|
||||
Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageController@exportHtml');
|
||||
@ -85,11 +85,11 @@ Route::group(['middleware' => 'auth'], function () {
|
||||
Route::get('/ajax/page/{id}', 'PageController@getPageAjax');
|
||||
Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy');
|
||||
|
||||
// Attribute routes (AJAX)
|
||||
Route::group(['prefix' => 'ajax/attributes'], function() {
|
||||
Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity');
|
||||
Route::get('/suggest', 'AttributeController@getNameSuggestions');
|
||||
Route::post('/update/{entityType}/{entityId}', 'AttributeController@updateForEntity');
|
||||
// Tag routes (AJAX)
|
||||
Route::group(['prefix' => 'ajax/tags'], function() {
|
||||
Route::get('/get/{entityType}/{entityId}', 'TagController@getForEntity');
|
||||
Route::get('/suggest', 'TagController@getNameSuggestions');
|
||||
Route::post('/update/{entityType}/{entityId}', 'TagController@updateForEntity');
|
||||
});
|
||||
|
||||
// Links
|
||||
|
@ -582,7 +582,7 @@ class PageRepo extends EntityRepo
|
||||
{
|
||||
Activity::removeEntity($page);
|
||||
$page->views()->delete();
|
||||
$page->attributes()->delete();
|
||||
$page->tags()->delete();
|
||||
$page->revisions()->delete();
|
||||
$page->permissions()->delete();
|
||||
$this->permissionService->deleteJointPermissionsForEntity($page);
|
||||
|
@ -1,29 +1,29 @@
|
||||
<?php namespace BookStack\Repos;
|
||||
|
||||
use BookStack\Attribute;
|
||||
use BookStack\Tag;
|
||||
use BookStack\Entity;
|
||||
use BookStack\Services\PermissionService;
|
||||
|
||||
/**
|
||||
* Class AttributeRepo
|
||||
* Class TagRepo
|
||||
* @package BookStack\Repos
|
||||
*/
|
||||
class AttributeRepo
|
||||
class TagRepo
|
||||
{
|
||||
|
||||
protected $attribute;
|
||||
protected $tag;
|
||||
protected $entity;
|
||||
protected $permissionService;
|
||||
|
||||
/**
|
||||
* AttributeRepo constructor.
|
||||
* @param Attribute $attr
|
||||
* TagRepo constructor.
|
||||
* @param Tag $attr
|
||||
* @param Entity $ent
|
||||
* @param PermissionService $ps
|
||||
*/
|
||||
public function __construct(Attribute $attr, Entity $ent, PermissionService $ps)
|
||||
public function __construct(Tag $attr, Entity $ent, PermissionService $ps)
|
||||
{
|
||||
$this->attribute = $attr;
|
||||
$this->tag = $attr;
|
||||
$this->entity = $ent;
|
||||
$this->permissionService = $ps;
|
||||
}
|
||||
@ -37,13 +37,13 @@ class AttributeRepo
|
||||
public function getEntity($entityType, $entityId, $action = 'view')
|
||||
{
|
||||
$entityInstance = $this->entity->getEntityInstance($entityType);
|
||||
$searchQuery = $entityInstance->where('id', '=', $entityId)->with('attributes');
|
||||
$searchQuery = $entityInstance->where('id', '=', $entityId)->with('tags');
|
||||
$searchQuery = $this->permissionService->enforceEntityRestrictions($searchQuery, $action);
|
||||
return $searchQuery->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attributes for a particular entity.
|
||||
* Get all tags for a particular entity.
|
||||
* @param string $entityType
|
||||
* @param int $entityId
|
||||
* @return mixed
|
||||
@ -53,42 +53,42 @@ class AttributeRepo
|
||||
$entity = $this->getEntity($entityType, $entityId);
|
||||
if ($entity === null) return collect();
|
||||
|
||||
return $entity->attributes;
|
||||
return $entity->tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute name suggestions from scanning existing attribute names.
|
||||
* Get tag name suggestions from scanning existing tag names.
|
||||
* @param $searchTerm
|
||||
* @return array
|
||||
*/
|
||||
public function getNameSuggestions($searchTerm)
|
||||
{
|
||||
if ($searchTerm === '') return [];
|
||||
$query = $this->attribute->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
|
||||
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'attributes', 'entity_id', 'entity_type');
|
||||
$query = $this->tag->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
|
||||
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
|
||||
return $query->get(['name'])->pluck('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save an array of attributes to an entity
|
||||
* Save an array of tags to an entity
|
||||
* @param Entity $entity
|
||||
* @param array $attributes
|
||||
* @param array $tags
|
||||
* @return array|\Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function saveAttributesToEntity(Entity $entity, $attributes = [])
|
||||
public function saveTagsToEntity(Entity $entity, $tags = [])
|
||||
{
|
||||
$entity->attributes()->delete();
|
||||
$newAttributes = [];
|
||||
foreach ($attributes as $attribute) {
|
||||
if (trim($attribute['name']) === '') continue;
|
||||
$newAttributes[] = $this->newInstanceFromInput($attribute);
|
||||
$entity->tags()->delete();
|
||||
$newTags = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (trim($tag['name']) === '') continue;
|
||||
$newTags[] = $this->newInstanceFromInput($tag);
|
||||
}
|
||||
|
||||
return $entity->attributes()->saveMany($newAttributes);
|
||||
return $entity->tags()->saveMany($newTags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Attribute instance from user input.
|
||||
* Create a new Tag instance from user input.
|
||||
* @param $input
|
||||
* @return static
|
||||
*/
|
||||
@ -98,7 +98,7 @@ class AttributeRepo
|
||||
$value = isset($input['value']) ? trim($input['value']) : '';
|
||||
// Any other modification or cleanup required can go here
|
||||
$values = ['name' => $name, 'value' => $value];
|
||||
return $this->attribute->newInstance($values);
|
||||
return $this->tag->newInstance($values);
|
||||
}
|
||||
|
||||
}
|
@ -4,12 +4,12 @@
|
||||
* Class Attribute
|
||||
* @package BookStack
|
||||
*/
|
||||
class Attribute extends Model
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = ['name', 'value', 'order'];
|
||||
|
||||
/**
|
||||
* Get the entity that this attribute belongs to
|
||||
* Get the entity that this tag belongs to
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
*/
|
||||
public function entity()
|
Reference in New Issue
Block a user