diff --git a/app/Book.php b/app/Book.php index 51ea226b4..effd6ca45 100644 --- a/app/Book.php +++ b/app/Book.php @@ -48,14 +48,6 @@ class Book extends Entity { return $this->belongsTo(Image::class, 'image_id'); } - /* - * Get the edit url for this book. - * @return string - */ - public function getEditUrl() - { - return $this->getUrl() . '/edit'; - } /** * Get all pages within this book. diff --git a/app/BookShelf.php b/app/BookShelf.php new file mode 100644 index 000000000..47f873bcd --- /dev/null +++ b/app/BookShelf.php @@ -0,0 +1,74 @@ +<?php namespace BookStack; + + +class BookShelf extends Entity +{ + protected $table = 'bookshelves'; + + public $searchFactor = 3; + + protected $fillable = ['name', 'description', 'image_id']; + + /** + * Get the url for this bookshelf. + * @param string|bool $path + * @return string + */ + public function getUrl($path = false) + { + if ($path !== false) { + return baseUrl('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/')); + } + return baseUrl('/shelves/' . urlencode($this->slug)); + } + + /** + * Returns BookShelf cover image, if cover does not exists return default cover image. + * @param int $width - Width of the image + * @param int $height - Height of the image + * @return string + */ + public function getBookCover($width = 440, $height = 250) + { + $default = baseUrl('/book_default_cover.png'); + if (!$this->image_id) { + return $default; + } + + try { + $cover = $this->cover ? baseUrl($this->cover->getThumb($width, $height, false)) : $default; + } catch (\Exception $err) { + $cover = $default; + } + return $cover; + } + + /** + * Get the cover image of the book + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function cover() + { + return $this->belongsTo(Image::class, 'image_id'); + } + + /** + * Get an excerpt of this book's description to the specified length or less. + * @param int $length + * @return string + */ + public function getExcerpt($length = 100) + { + $description = $this->description; + return strlen($description) > $length ? substr($description, 0, $length-3) . '...' : $description; + } + + /** + * Return a generalised, common raw query that can be 'unioned' across entities. + * @return string + */ + public function entityRawQuery() + { + return "'BookStack\\\\BookShelf' as entity_type, id, id as entity_id, slug, name, {$this->textField} as text,'' as html, '0' as book_id, '0' as priority, '0' as chapter_id, '0' as draft, created_by, updated_by, updated_at, created_at"; + } +} diff --git a/database/migrations/2018_06_24_115700_create_bookshelves_table.php b/database/migrations/2018_06_24_115700_create_bookshelves_table.php new file mode 100644 index 000000000..173e9214b --- /dev/null +++ b/database/migrations/2018_06_24_115700_create_bookshelves_table.php @@ -0,0 +1,70 @@ +<?php + +use Illuminate\Support\Facades\Schema; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class CreateBookshelvesTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('bookshelves', function (Blueprint $table) { + $table->increments('id'); + $table->string('name', 200); + $table->string('slug', 200); + $table->text('description'); + $table->integer('created_by')->nullable()->default(null); + $table->integer('updated_by')->nullable()->default(null); + $table->boolean('restricted')->default(false); + $table->integer('image_id')->nullable()->default(null); + $table->timestamps(); + + $table->index('slug'); + $table->index('created_by'); + $table->index('updated_by'); + $table->index('restricted'); + }); + + // Get roles with permissions we need to change + $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id; + $editorRole = DB::table('roles')->where('name', '=', 'editor')->first(); + + // TODO - Copy existing role permissions from Books + $entity = 'BookShelf'; + $ops = ['View All', 'View Own', 'Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own']; + foreach ($ops as $op) { + $permId = DB::table('permissions')->insertGetId([ + 'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)), + 'display_name' => $op . ' ' . 'BookShelves', + 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), + 'updated_at' => \Carbon\Carbon::now()->toDateTimeString() + ]); + // Assign view permission to all current roles + DB::table('permission_role')->insert([ + 'role_id' => $adminRoleId, + 'permission_id' => $permId + ]); + if ($editorRole !== null) { + DB::table('permission_role')->insert([ + 'role_id' => $editorRole->id, + 'permission_id' => $permId + ]); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('bookshelves'); + } +} diff --git a/resources/views/books/show.blade.php b/resources/views/books/show.blade.php index d0a2eb2f7..e5845b495 100644 --- a/resources/views/books/show.blade.php +++ b/resources/views/books/show.blade.php @@ -25,7 +25,7 @@ <a dropdown-toggle class="text-primary text-button">@icon('more'){{ trans('common.more') }}</a> <ul> @if(userCan('book-update', $book)) - <li><a href="{{$book->getEditUrl()}}" class="text-primary">@icon('edit'){{ trans('common.edit') }}</a></li> + <li><a href="{{ $book->getUrl('/edit') }}" class="text-primary">@icon('edit'){{ trans('common.edit') }}</a></li> <li><a href="{{ $book->getUrl('/sort') }}" class="text-primary">@icon('sort'){{ trans('common.sort') }}</a></li> @endif @if(userCan('restrictions-manage', $book))