Renamed attribute to tags & continued interface
Also fixed page create route broken in last commit
This commit is contained in:
		
							parent
							
								
									1fa079b466
								
							
						
					
					
						commit
						b80184cd93
					
				| 
						 | 
					@ -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
 | 
					     * @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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -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
 | 
					        // Pages
 | 
				
			||||||
        Route::get('/{bookSlug}/page/create', 'PageController@create');
 | 
					        Route::get('/{bookSlug}/page/create', 'PageController@create');
 | 
				
			||||||
        Route::get('/{bookSlug}/draft/{pageId}', 'PageController@editDraft');
 | 
					        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}', 'PageController@show');
 | 
				
			||||||
        Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
 | 
					        Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
 | 
				
			||||||
        Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageController@exportHtml');
 | 
					        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::get('/ajax/page/{id}', 'PageController@getPageAjax');
 | 
				
			||||||
    Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy');
 | 
					    Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Attribute routes (AJAX)
 | 
					    // Tag routes (AJAX)
 | 
				
			||||||
    Route::group(['prefix' => 'ajax/attributes'], function() {
 | 
					    Route::group(['prefix' => 'ajax/tags'], function() {
 | 
				
			||||||
        Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity');
 | 
					        Route::get('/get/{entityType}/{entityId}', 'TagController@getForEntity');
 | 
				
			||||||
        Route::get('/suggest', 'AttributeController@getNameSuggestions');
 | 
					        Route::get('/suggest', 'TagController@getNameSuggestions');
 | 
				
			||||||
        Route::post('/update/{entityType}/{entityId}', 'AttributeController@updateForEntity');
 | 
					        Route::post('/update/{entityType}/{entityId}', 'TagController@updateForEntity');
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Links
 | 
					    // Links
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -582,7 +582,7 @@ class PageRepo extends EntityRepo
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Activity::removeEntity($page);
 | 
					        Activity::removeEntity($page);
 | 
				
			||||||
        $page->views()->delete();
 | 
					        $page->views()->delete();
 | 
				
			||||||
        $page->attributes()->delete();
 | 
					        $page->tags()->delete();
 | 
				
			||||||
        $page->revisions()->delete();
 | 
					        $page->revisions()->delete();
 | 
				
			||||||
        $page->permissions()->delete();
 | 
					        $page->permissions()->delete();
 | 
				
			||||||
        $this->permissionService->deleteJointPermissionsForEntity($page);
 | 
					        $this->permissionService->deleteJointPermissionsForEntity($page);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,29 +1,29 @@
 | 
				
			||||||
<?php namespace BookStack\Repos;
 | 
					<?php namespace BookStack\Repos;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use BookStack\Attribute;
 | 
					use BookStack\Tag;
 | 
				
			||||||
use BookStack\Entity;
 | 
					use BookStack\Entity;
 | 
				
			||||||
use BookStack\Services\PermissionService;
 | 
					use BookStack\Services\PermissionService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Class AttributeRepo
 | 
					 * Class TagRepo
 | 
				
			||||||
 * @package BookStack\Repos
 | 
					 * @package BookStack\Repos
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class AttributeRepo
 | 
					class TagRepo
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected $attribute;
 | 
					    protected $tag;
 | 
				
			||||||
    protected $entity;
 | 
					    protected $entity;
 | 
				
			||||||
    protected $permissionService;
 | 
					    protected $permissionService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * AttributeRepo constructor.
 | 
					     * TagRepo constructor.
 | 
				
			||||||
     * @param Attribute $attr
 | 
					     * @param Tag $attr
 | 
				
			||||||
     * @param Entity $ent
 | 
					     * @param Entity $ent
 | 
				
			||||||
     * @param PermissionService $ps
 | 
					     * @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->entity = $ent;
 | 
				
			||||||
        $this->permissionService = $ps;
 | 
					        $this->permissionService = $ps;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -37,13 +37,13 @@ class AttributeRepo
 | 
				
			||||||
    public function getEntity($entityType, $entityId, $action = 'view')
 | 
					    public function getEntity($entityType, $entityId, $action = 'view')
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $entityInstance = $this->entity->getEntityInstance($entityType);
 | 
					        $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);
 | 
					        $searchQuery = $this->permissionService->enforceEntityRestrictions($searchQuery, $action);
 | 
				
			||||||
        return $searchQuery->first();
 | 
					        return $searchQuery->first();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Get all attributes for a particular entity.
 | 
					     * Get all tags for a particular entity.
 | 
				
			||||||
     * @param string $entityType
 | 
					     * @param string $entityType
 | 
				
			||||||
     * @param int $entityId
 | 
					     * @param int $entityId
 | 
				
			||||||
     * @return mixed
 | 
					     * @return mixed
 | 
				
			||||||
| 
						 | 
					@ -53,42 +53,42 @@ class AttributeRepo
 | 
				
			||||||
        $entity = $this->getEntity($entityType, $entityId);
 | 
					        $entity = $this->getEntity($entityType, $entityId);
 | 
				
			||||||
        if ($entity === null) return collect();
 | 
					        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
 | 
					     * @param $searchTerm
 | 
				
			||||||
     * @return array
 | 
					     * @return array
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function getNameSuggestions($searchTerm)
 | 
					    public function getNameSuggestions($searchTerm)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if ($searchTerm === '') return [];
 | 
					        if ($searchTerm === '') return [];
 | 
				
			||||||
        $query = $this->attribute->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
 | 
					        $query = $this->tag->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
 | 
				
			||||||
        $query = $this->permissionService->filterRestrictedEntityRelations($query, 'attributes', 'entity_id', 'entity_type');
 | 
					        $query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
 | 
				
			||||||
        return $query->get(['name'])->pluck('name');
 | 
					        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 Entity $entity
 | 
				
			||||||
     * @param array $attributes
 | 
					     * @param array $tags
 | 
				
			||||||
     * @return array|\Illuminate\Database\Eloquent\Collection
 | 
					     * @return array|\Illuminate\Database\Eloquent\Collection
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function saveAttributesToEntity(Entity $entity, $attributes = [])
 | 
					    public function saveTagsToEntity(Entity $entity, $tags = [])
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $entity->attributes()->delete();
 | 
					        $entity->tags()->delete();
 | 
				
			||||||
        $newAttributes = [];
 | 
					        $newTags = [];
 | 
				
			||||||
        foreach ($attributes as $attribute) {
 | 
					        foreach ($tags as $tag) {
 | 
				
			||||||
            if (trim($attribute['name']) === '') continue;
 | 
					            if (trim($tag['name']) === '') continue;
 | 
				
			||||||
            $newAttributes[] = $this->newInstanceFromInput($attribute);
 | 
					            $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
 | 
					     * @param $input
 | 
				
			||||||
     * @return static
 | 
					     * @return static
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
| 
						 | 
					@ -98,7 +98,7 @@ class AttributeRepo
 | 
				
			||||||
        $value = isset($input['value']) ? trim($input['value']) : '';
 | 
					        $value = isset($input['value']) ? trim($input['value']) : '';
 | 
				
			||||||
        // Any other modification or cleanup required can go here
 | 
					        // Any other modification or cleanup required can go here
 | 
				
			||||||
        $values = ['name' => $name, 'value' => $value];
 | 
					        $values = ['name' => $name, 'value' => $value];
 | 
				
			||||||
        return $this->attribute->newInstance($values);
 | 
					        return $this->tag->newInstance($values);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -4,12 +4,12 @@
 | 
				
			||||||
 * Class Attribute
 | 
					 * Class Attribute
 | 
				
			||||||
 * @package BookStack
 | 
					 * @package BookStack
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class Attribute extends Model
 | 
					class Tag extends Model
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    protected $fillable = ['name', 'value', 'order'];
 | 
					    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
 | 
					     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function entity()
 | 
					    public function entity()
 | 
				
			||||||
| 
						 | 
					@ -54,7 +54,7 @@ $factory->define(BookStack\Role::class, function ($faker) {
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$factory->define(BookStack\Attribute::class, function ($faker) {
 | 
					$factory->define(BookStack\Tag::class, function ($faker) {
 | 
				
			||||||
    return [
 | 
					    return [
 | 
				
			||||||
        'name' => $faker->city,
 | 
					        'name' => $faker->city,
 | 
				
			||||||
        'value' => $faker->sentence(3)
 | 
					        'value' => $faker->sentence(3)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,7 +3,7 @@
 | 
				
			||||||
use Illuminate\Database\Schema\Blueprint;
 | 
					use Illuminate\Database\Schema\Blueprint;
 | 
				
			||||||
use Illuminate\Database\Migrations\Migration;
 | 
					use Illuminate\Database\Migrations\Migration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CreateAttributesTable extends Migration
 | 
					class CreateTagsTable extends Migration
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Run the migrations.
 | 
					     * Run the migrations.
 | 
				
			||||||
| 
						 | 
					@ -12,7 +12,7 @@ class CreateAttributesTable extends Migration
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function up()
 | 
					    public function up()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Schema::create('attributes', function (Blueprint $table) {
 | 
					        Schema::create('tags', function (Blueprint $table) {
 | 
				
			||||||
            $table->increments('id');
 | 
					            $table->increments('id');
 | 
				
			||||||
            $table->integer('entity_id');
 | 
					            $table->integer('entity_id');
 | 
				
			||||||
            $table->string('entity_type', 100);
 | 
					            $table->string('entity_type', 100);
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,6 @@ class CreateAttributesTable extends Migration
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function down()
 | 
					    public function down()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        Schema::drop('attributes');
 | 
					        Schema::drop('tags');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -400,75 +400,75 @@ module.exports = function (ngApp, events) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }]);
 | 
					    }]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ngApp.controller('PageAttributeController', ['$scope', '$http', '$attrs',
 | 
					    ngApp.controller('PageTagController', ['$scope', '$http', '$attrs',
 | 
				
			||||||
        function ($scope, $http, $attrs) {
 | 
					        function ($scope, $http, $attrs) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const pageId = Number($attrs.pageId);
 | 
					            const pageId = Number($attrs.pageId);
 | 
				
			||||||
            $scope.attributes = [];
 | 
					            $scope.tags = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * Push an empty attribute to the end of the scope attributes.
 | 
					             * Push an empty tag to the end of the scope tags.
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
            function addEmptyAttribute() {
 | 
					            function addEmptyTag() {
 | 
				
			||||||
                $scope.attributes.push({
 | 
					                $scope.tags.push({
 | 
				
			||||||
                    name: '',
 | 
					                    name: '',
 | 
				
			||||||
                    value: ''
 | 
					                    value: ''
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * Get all attributes for the current book and add into scope.
 | 
					             * Get all tags for the current book and add into scope.
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
            function getAttributes() {
 | 
					            function getTags() {
 | 
				
			||||||
                $http.get('/ajax/attributes/get/page/' + pageId).then((responseData) => {
 | 
					                $http.get('/ajax/tags/get/page/' + pageId).then((responseData) => {
 | 
				
			||||||
                    $scope.attributes = responseData.data;
 | 
					                    $scope.tags = responseData.data;
 | 
				
			||||||
                    addEmptyAttribute();
 | 
					                    addEmptyTag();
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            getAttributes();
 | 
					            getTags();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * Set the order property on all attributes.
 | 
					             * Set the order property on all tags.
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
            function setAttributeOrder() {
 | 
					            function setTagOrder() {
 | 
				
			||||||
                for (let i = 0; i < $scope.attributes.length; i++) {
 | 
					                for (let i = 0; i < $scope.tags.length; i++) {
 | 
				
			||||||
                    $scope.attributes[i].order = i;
 | 
					                    $scope.tags[i].order = i;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * When an attribute changes check if another empty editable
 | 
					             * When an tag changes check if another empty editable
 | 
				
			||||||
             * field needs to be added onto the end.
 | 
					             * field needs to be added onto the end.
 | 
				
			||||||
             * @param attribute
 | 
					             * @param tag
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
            $scope.attributeChange = function(attribute) {
 | 
					            $scope.tagChange = function(tag) {
 | 
				
			||||||
                let cPos = $scope.attributes.indexOf(attribute);
 | 
					                let cPos = $scope.tags.indexOf(tag);
 | 
				
			||||||
                if (cPos !== $scope.attributes.length-1) return;
 | 
					                if (cPos !== $scope.tags.length-1) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (attribute.name !== '' || attribute.value !== '') {
 | 
					                if (tag.name !== '' || tag.value !== '') {
 | 
				
			||||||
                    addEmptyAttribute();
 | 
					                    addEmptyTag();
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /**
 | 
					            /**
 | 
				
			||||||
             * When an attribute field loses focus check the attribute to see if its
 | 
					             * When an tag field loses focus check the tag to see if its
 | 
				
			||||||
             * empty and therefore could be removed from the list.
 | 
					             * empty and therefore could be removed from the list.
 | 
				
			||||||
             * @param attribute
 | 
					             * @param tag
 | 
				
			||||||
             */
 | 
					             */
 | 
				
			||||||
            $scope.attributeBlur = function(attribute) {
 | 
					            $scope.tagBlur = function(tag) {
 | 
				
			||||||
                let isLast = $scope.attributes.length - 1 === $scope.attributes.indexOf(attribute);
 | 
					                let isLast = $scope.tags.length - 1 === $scope.tags.indexOf(tag);
 | 
				
			||||||
                if (attribute.name === '' && attribute.value === '' && !isLast) {
 | 
					                if (tag.name === '' && tag.value === '' && !isLast) {
 | 
				
			||||||
                    let cPos = $scope.attributes.indexOf(attribute);
 | 
					                    let cPos = $scope.tags.indexOf(tag);
 | 
				
			||||||
                    $scope.attributes.splice(cPos, 1);
 | 
					                    $scope.tags.splice(cPos, 1);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $scope.saveAttributes = function() {
 | 
					            $scope.saveTags = function() {
 | 
				
			||||||
                setAttributeOrder();
 | 
					                setTagOrder();
 | 
				
			||||||
                let postData = {attributes: $scope.attributes};
 | 
					                let postData = {tags: $scope.tags};
 | 
				
			||||||
                $http.post('/ajax/attributes/update/page/' + pageId, postData).then((responseData) => {
 | 
					                $http.post('/ajax/tags/update/page/' + pageId, postData).then((responseData) => {
 | 
				
			||||||
                    $scope.attributes = responseData.data.attributes;
 | 
					                    $scope.tags = responseData.data.tags;
 | 
				
			||||||
                    addEmptyAttribute();
 | 
					                    addEmptyTag();
 | 
				
			||||||
                    events.emit('success', responseData.data.message);
 | 
					                    events.emit('success', responseData.data.message);
 | 
				
			||||||
                })
 | 
					                })
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -239,6 +239,17 @@ div[editor-type="markdown"] .title-input.page-title input[type="text"] {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					input.outline {
 | 
				
			||||||
 | 
					  border: 0;
 | 
				
			||||||
 | 
					  border-bottom: 2px solid #DDD;
 | 
				
			||||||
 | 
					  border-radius: 0;
 | 
				
			||||||
 | 
					  &:focus, &:active {
 | 
				
			||||||
 | 
					    border: 0;
 | 
				
			||||||
 | 
					    border-bottom: 2px solid #AAA;
 | 
				
			||||||
 | 
					    outline: 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#login-form label[for="remember"] {
 | 
					#login-form label[for="remember"] {
 | 
				
			||||||
  margin: 0;
 | 
					  margin: 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +26,13 @@ table {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					table.no-style {
 | 
				
			||||||
 | 
					  td {
 | 
				
			||||||
 | 
					    border: 0;
 | 
				
			||||||
 | 
					    padding: 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
table.list-table {
 | 
					table.list-table {
 | 
				
			||||||
  margin: 0 -$-xs;
 | 
					  margin: 0 -$-xs;
 | 
				
			||||||
  td {
 | 
					  td {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -208,11 +208,70 @@ $btt-size: 40px;
 | 
				
			||||||
  background-color: #FFF;
 | 
					  background-color: #FFF;
 | 
				
			||||||
  border: 1px solid #BBB;
 | 
					  border: 1px solid #BBB;
 | 
				
			||||||
  border-radius: 3px;
 | 
					  border-radius: 3px;
 | 
				
			||||||
  padding: $-l;
 | 
					 | 
				
			||||||
  position: fixed;
 | 
					  position: fixed;
 | 
				
			||||||
  right: $-xl*2;
 | 
					  right: $-xl*2;
 | 
				
			||||||
  top: 100px;
 | 
					  top: 100px;
 | 
				
			||||||
  z-index: 99;
 | 
					  z-index: 99;
 | 
				
			||||||
  height: 800px;
 | 
					  height: 800px;
 | 
				
			||||||
 | 
					  width: 480px;
 | 
				
			||||||
  overflow-y: scroll;
 | 
					  overflow-y: scroll;
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  align-items: stretch;
 | 
				
			||||||
 | 
					  flex-direction: row;
 | 
				
			||||||
 | 
					  > div {
 | 
				
			||||||
 | 
					    flex: 1;
 | 
				
			||||||
 | 
					    position: relative;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  .tabs {
 | 
				
			||||||
 | 
					    display: block;
 | 
				
			||||||
 | 
					    border-right: 1px solid #DDD;
 | 
				
			||||||
 | 
					    width: 54px;
 | 
				
			||||||
 | 
					    flex: 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  .tabs i {
 | 
				
			||||||
 | 
					    padding: 0;
 | 
				
			||||||
 | 
					    margin: 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  .tabs [tab-button] {
 | 
				
			||||||
 | 
					    display: block;
 | 
				
			||||||
 | 
					    cursor: pointer;
 | 
				
			||||||
 | 
					    color: #666;
 | 
				
			||||||
 | 
					    padding: $-m;
 | 
				
			||||||
 | 
					    border-bottom: 1px solid rgba(255, 255, 255, 0.3);
 | 
				
			||||||
 | 
					    &.active {
 | 
				
			||||||
 | 
					      color: #444;
 | 
				
			||||||
 | 
					      background-color: rgba(0, 0, 0, 0.1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  div[tab-content] .padded {
 | 
				
			||||||
 | 
					    padding: 0 $-m;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  h4 {
 | 
				
			||||||
 | 
					    font-size: 24px;
 | 
				
			||||||
 | 
					    margin: $-m 0 0 0;
 | 
				
			||||||
 | 
					    padding: 0 $-m;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  .tags input {
 | 
				
			||||||
 | 
					    max-width: 100%;
 | 
				
			||||||
 | 
					    width: 100%;
 | 
				
			||||||
 | 
					    min-width: 50px;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  .tags td {
 | 
				
			||||||
 | 
					    padding-right: $-s;
 | 
				
			||||||
 | 
					    padding-top: $-s;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  button.pos {
 | 
				
			||||||
 | 
					    position: absolute;
 | 
				
			||||||
 | 
					    bottom: 0;
 | 
				
			||||||
 | 
					    display: block;
 | 
				
			||||||
 | 
					    width: 100%;
 | 
				
			||||||
 | 
					    padding: $-s;
 | 
				
			||||||
 | 
					    border: 0;
 | 
				
			||||||
 | 
					    margin: 0;
 | 
				
			||||||
 | 
					    box-shadow: none;
 | 
				
			||||||
 | 
					    border-radius: 0;
 | 
				
			||||||
 | 
					    &:hover{
 | 
				
			||||||
 | 
					      box-shadow: none;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -16,17 +16,7 @@
 | 
				
			||||||
            @include('pages/form', ['model' => $page])
 | 
					            @include('pages/form', ['model' => $page])
 | 
				
			||||||
        </form>
 | 
					        </form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <div class="floating-toolbox" ng-controller="PageAttributeController" page-id="{{ $page->id or 0 }}">
 | 
					        @include('pages/form-toolbox')
 | 
				
			||||||
            <form ng-submit="saveAttributes()">
 | 
					 | 
				
			||||||
                <table>
 | 
					 | 
				
			||||||
                    <tr ng-repeat="attribute in attributes">
 | 
					 | 
				
			||||||
                        <td><input type="text" ng-model="attribute.name" ng-change="attributeChange(attribute)" ng-blur="attributeBlur(attribute)" placeholder="Attribute Name"></td>
 | 
					 | 
				
			||||||
                        <td><input type="text" ng-model="attribute.value" ng-change="attributeChange(attribute)" ng-blur="attributeBlur(attribute)" placeholder="Value"></td>
 | 
					 | 
				
			||||||
                    </tr>
 | 
					 | 
				
			||||||
                </table>
 | 
					 | 
				
			||||||
                <button class="button pos" type="submit">Save attributes</button>
 | 
					 | 
				
			||||||
            </form>
 | 
					 | 
				
			||||||
        </div>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    @include('partials/image-manager', ['imageType' => 'gallery', 'uploaded_to' => $page->id])
 | 
					    @include('partials/image-manager', ['imageType' => 'gallery', 'uploaded_to' => $page->id])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,20 @@
 | 
				
			||||||
 | 
					<div class="floating-toolbox">
 | 
				
			||||||
 | 
					    <div class="tabs primary-background-light">
 | 
				
			||||||
 | 
					        <span tab-button class="active"><i class="zmdi zmdi-tag"></i></span>
 | 
				
			||||||
 | 
					        <span tab-button><i class="zmdi zmdi-wrench"></i></span>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					    <div tab-content ng-controller="PageTagController" page-id="{{ $page->id or 0 }}">
 | 
				
			||||||
 | 
					        <form ng-submit="saveTags()" >
 | 
				
			||||||
 | 
					            <h4>Page Tags</h4>
 | 
				
			||||||
 | 
					            <div class="padded tags">
 | 
				
			||||||
 | 
					                <table class="no-style" style="width: 100%;">
 | 
				
			||||||
 | 
					                    <tr ng-repeat="tag in tags">
 | 
				
			||||||
 | 
					                        <td><input class="outline" type="text" ng-model="tag.name" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag"></td>
 | 
				
			||||||
 | 
					                        <td><input class="outline" type="text" ng-model="tag.value" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag Value (Optional)"></td>
 | 
				
			||||||
 | 
					                    </tr>
 | 
				
			||||||
 | 
					                </table>
 | 
				
			||||||
 | 
					            </div>
 | 
				
			||||||
 | 
					            <button class="button pos" type="submit">Save Tags</button>
 | 
				
			||||||
 | 
					        </form>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
| 
						 | 
					@ -1,9 +1,9 @@
 | 
				
			||||||
@if(Setting::get('app-color'))
 | 
					@if(Setting::get('app-color'))
 | 
				
			||||||
    <style>
 | 
					    <style>
 | 
				
			||||||
        header, #back-to-top {
 | 
					        header, #back-to-top, .primary-background {
 | 
				
			||||||
            background-color: {{ Setting::get('app-color') }};
 | 
					            background-color: {{ Setting::get('app-color') }};
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        .faded-small {
 | 
					        .faded-small, .primary-background-light {
 | 
				
			||||||
            background-color: {{ Setting::get('app-color-light') }};
 | 
					            background-color: {{ Setting::get('app-color-light') }};
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        .button-base, .button, input[type="button"], input[type="submit"] {
 | 
					        .button-base, .button, input[type="button"], input[type="submit"] {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,128 +0,0 @@
 | 
				
			||||||
<?php namespace Entity;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use BookStack\Attribute;
 | 
					 | 
				
			||||||
use BookStack\Page;
 | 
					 | 
				
			||||||
use BookStack\Services\PermissionService;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class AttributeTests extends \TestCase
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    protected $defaultAttrCount = 20;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Get an instance of a page that has many attributes.
 | 
					 | 
				
			||||||
     * @param Attribute[]|bool $attributes
 | 
					 | 
				
			||||||
     * @return mixed
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    protected function getPageWithAttributes($attributes = false)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $page = Page::first();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!$attributes) {
 | 
					 | 
				
			||||||
            $attributes = factory(Attribute::class, $this->defaultAttrCount)->make();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $page->attributes()->saveMany($attributes);
 | 
					 | 
				
			||||||
        return $page;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function test_get_page_attributes()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $page = $this->getPageWithAttributes();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Add some other attributes to check they don't interfere
 | 
					 | 
				
			||||||
        factory(Attribute::class, $this->defaultAttrCount)->create();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id)
 | 
					 | 
				
			||||||
            ->shouldReturnJson();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $json = json_decode($this->response->getContent());
 | 
					 | 
				
			||||||
        $this->assertTrue(count($json) === $this->defaultAttrCount, "Returned JSON item count is not as expected");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function test_attribute_name_suggestions()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // Create some attributes with similar names to test with
 | 
					 | 
				
			||||||
        $attrs = collect();
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'city']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'county']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'planet']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'plans']));
 | 
					 | 
				
			||||||
        $page = $this->getPageWithAttributes($attrs);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->asAdmin()->get('/ajax/attributes/suggest?search=dog')->seeJsonEquals([]);
 | 
					 | 
				
			||||||
        $this->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
 | 
					 | 
				
			||||||
        $this->get('/ajax/attributes/suggest?search=cou')->seeJsonEquals(['country', 'county']);
 | 
					 | 
				
			||||||
        $this->get('/ajax/attributes/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function test_entity_permissions_effect_attribute_suggestions()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $permissionService = $this->app->make(PermissionService::class);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Create some attributes with similar names to test with and save to a page
 | 
					 | 
				
			||||||
        $attrs = collect();
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
 | 
					 | 
				
			||||||
        $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
 | 
					 | 
				
			||||||
        $page = $this->getPageWithAttributes($attrs);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
					 | 
				
			||||||
        $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Set restricted permission the page
 | 
					 | 
				
			||||||
        $page->restricted = true;
 | 
					 | 
				
			||||||
        $page->save();
 | 
					 | 
				
			||||||
        $permissionService->buildJointPermissionsForEntity($page);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
					 | 
				
			||||||
        $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals([]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function test_entity_attribute_updating()
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $page = $this->getPageWithAttributes();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $testJsonData = [
 | 
					 | 
				
			||||||
            ['name' => 'color', 'value' => 'red'],
 | 
					 | 
				
			||||||
            ['name' => 'color', 'value' => ' blue '],
 | 
					 | 
				
			||||||
            ['name' => 'city', 'value' => 'London '],
 | 
					 | 
				
			||||||
            ['name' => 'country', 'value' => ' England'],
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
        $testResponseJsonData = [
 | 
					 | 
				
			||||||
            ['name' => 'color', 'value' => 'red'],
 | 
					 | 
				
			||||||
            ['name' => 'color', 'value' => 'blue'],
 | 
					 | 
				
			||||||
            ['name' => 'city', 'value' => 'London'],
 | 
					 | 
				
			||||||
            ['name' => 'country', 'value' => 'England'],
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Do update request
 | 
					 | 
				
			||||||
        $this->asAdmin()->json("POST", "/ajax/attributes/update/page/" . $page->id, ['attributes' => $testJsonData]);
 | 
					 | 
				
			||||||
        $updateData = json_decode($this->response->getContent());
 | 
					 | 
				
			||||||
        // Check data is correct
 | 
					 | 
				
			||||||
        $testDataCorrect = true;
 | 
					 | 
				
			||||||
        foreach ($updateData->attributes as $data) {
 | 
					 | 
				
			||||||
            $testItem = ['name' => $data->name, 'value' => $data->value];
 | 
					 | 
				
			||||||
            if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
 | 
					 | 
				
			||||||
        $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
 | 
					 | 
				
			||||||
        $this->assertTrue(isset($updateData->message), "No message returned in attribute update response");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Do get request
 | 
					 | 
				
			||||||
        $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id);
 | 
					 | 
				
			||||||
        $getResponseData = json_decode($this->response->getContent());
 | 
					 | 
				
			||||||
        // Check counts
 | 
					 | 
				
			||||||
        $this->assertTrue(count($getResponseData) === count($testJsonData), "The received attribute count is incorrect");
 | 
					 | 
				
			||||||
        // Check data is correct
 | 
					 | 
				
			||||||
        $testDataCorrect = true;
 | 
					 | 
				
			||||||
        foreach ($getResponseData as $data) {
 | 
					 | 
				
			||||||
            $testItem = ['name' => $data->name, 'value' => $data->value];
 | 
					 | 
				
			||||||
            if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
 | 
					 | 
				
			||||||
        $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,128 @@
 | 
				
			||||||
 | 
					<?php namespace Entity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use BookStack\Tag;
 | 
				
			||||||
 | 
					use BookStack\Page;
 | 
				
			||||||
 | 
					use BookStack\Services\PermissionService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TagTests extends \TestCase
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected $defaultTagCount = 20;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Get an instance of a page that has many tags.
 | 
				
			||||||
 | 
					     * @param Tag[]|bool $tags
 | 
				
			||||||
 | 
					     * @return mixed
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected function getPageWithTags($tags = false)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $page = Page::first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$tags) {
 | 
				
			||||||
 | 
					            $tags = factory(Tag::class, $this->defaultTagCount)->make();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $page->tags()->saveMany($tags);
 | 
				
			||||||
 | 
					        return $page;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_get_page_tags()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $page = $this->getPageWithTags();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Add some other tags to check they don't interfere
 | 
				
			||||||
 | 
					        factory(Tag::class, $this->defaultTagCount)->create();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
 | 
				
			||||||
 | 
					            ->shouldReturnJson();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $json = json_decode($this->response->getContent());
 | 
				
			||||||
 | 
					        $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_tag_name_suggestions()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Create some tags with similar names to test with
 | 
				
			||||||
 | 
					        $attrs = collect();
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
 | 
				
			||||||
 | 
					        $page = $this->getPageWithTags($attrs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->asAdmin()->get('/ajax/tags/suggest?search=dog')->seeJsonEquals([]);
 | 
				
			||||||
 | 
					        $this->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
 | 
				
			||||||
 | 
					        $this->get('/ajax/tags/suggest?search=cou')->seeJsonEquals(['country', 'county']);
 | 
				
			||||||
 | 
					        $this->get('/ajax/tags/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_entity_permissions_effect_tag_suggestions()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $permissionService = $this->app->make(PermissionService::class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Create some tags with similar names to test with and save to a page
 | 
				
			||||||
 | 
					        $attrs = collect();
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
 | 
				
			||||||
 | 
					        $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
 | 
				
			||||||
 | 
					        $page = $this->getPageWithTags($attrs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->asAdmin()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
				
			||||||
 | 
					        $this->asEditor()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Set restricted permission the page
 | 
				
			||||||
 | 
					        $page->restricted = true;
 | 
				
			||||||
 | 
					        $page->save();
 | 
				
			||||||
 | 
					        $permissionService->buildJointPermissionsForEntity($page);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->asAdmin()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
 | 
				
			||||||
 | 
					        $this->asEditor()->get('/ajax/tags/suggest?search=co')->seeJsonEquals([]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function test_entity_tag_updating()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $page = $this->getPageWithTags();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $testJsonData = [
 | 
				
			||||||
 | 
					            ['name' => 'color', 'value' => 'red'],
 | 
				
			||||||
 | 
					            ['name' => 'color', 'value' => ' blue '],
 | 
				
			||||||
 | 
					            ['name' => 'city', 'value' => 'London '],
 | 
				
			||||||
 | 
					            ['name' => 'country', 'value' => ' England'],
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					        $testResponseJsonData = [
 | 
				
			||||||
 | 
					            ['name' => 'color', 'value' => 'red'],
 | 
				
			||||||
 | 
					            ['name' => 'color', 'value' => 'blue'],
 | 
				
			||||||
 | 
					            ['name' => 'city', 'value' => 'London'],
 | 
				
			||||||
 | 
					            ['name' => 'country', 'value' => 'England'],
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Do update request
 | 
				
			||||||
 | 
					        $this->asAdmin()->json("POST", "/ajax/tags/update/page/" . $page->id, ['tags' => $testJsonData]);
 | 
				
			||||||
 | 
					        $updateData = json_decode($this->response->getContent());
 | 
				
			||||||
 | 
					        // Check data is correct
 | 
				
			||||||
 | 
					        $testDataCorrect = true;
 | 
				
			||||||
 | 
					        foreach ($updateData->tags as $data) {
 | 
				
			||||||
 | 
					            $testItem = ['name' => $data->name, 'value' => $data->value];
 | 
				
			||||||
 | 
					            if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
 | 
				
			||||||
 | 
					        $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
 | 
				
			||||||
 | 
					        $this->assertTrue(isset($updateData->message), "No message returned in tag update response");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Do get request
 | 
				
			||||||
 | 
					        $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id);
 | 
				
			||||||
 | 
					        $getResponseData = json_decode($this->response->getContent());
 | 
				
			||||||
 | 
					        // Check counts
 | 
				
			||||||
 | 
					        $this->assertTrue(count($getResponseData) === count($testJsonData), "The received tag count is incorrect");
 | 
				
			||||||
 | 
					        // Check data is correct
 | 
				
			||||||
 | 
					        $testDataCorrect = true;
 | 
				
			||||||
 | 
					        foreach ($getResponseData as $data) {
 | 
				
			||||||
 | 
					            $testItem = ['name' => $data->name, 'value' => $data->value];
 | 
				
			||||||
 | 
					            if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
 | 
				
			||||||
 | 
					        $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue