From 4948b443b6ce3088fbdf99dbdc1d7621d1eb73c4 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 24 Jun 2018 13:38:19 +0100 Subject: [PATCH] Started work on bookshelves --- app/Book.php | 8 -- app/BookShelf.php | 74 +++++++++++++++++++ ..._06_24_115700_create_bookshelves_table.php | 70 ++++++++++++++++++ resources/views/books/show.blade.php | 2 +- 4 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 app/BookShelf.php create mode 100644 database/migrations/2018_06_24_115700_create_bookshelves_table.php 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 @@ +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 @@ +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 @@ @icon('more'){{ trans('common.more') }}