Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2015-11-21 18:05:03 +0000
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
61ae96c5f84acfad45aea8077783de6a88c08bc5
61ae96c5
1 parent
ea55b7f1
Added better entity deletion and commented up repos
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
150 additions
and
29 deletions
app/Http/Controllers/BookController.php
app/Http/Controllers/ChapterController.php
app/Http/Controllers/HomeController.php
app/Http/Controllers/PageController.php
app/Repos/BookRepo.php
app/Repos/ChapterRepo.php
app/Repos/PageRepo.php
resources/views/books/index.blade.php
resources/views/home.blade.php
resources/views/partials/entity-list.blade.php
app/Http/Controllers/BookController.php
View file @
61ae96c
...
...
@@ -42,7 +42,7 @@ class BookController extends Controller
public
function
index
()
{
$books
=
$this
->
bookRepo
->
getAllPaginated
(
10
);
$recents
=
$this
->
bookRepo
->
getRecentlyViewed
(
10
,
0
)
;
$recents
=
$this
->
signedIn
?
$this
->
bookRepo
->
getRecentlyViewed
(
10
,
0
)
:
false
;
return
view
(
'books/index'
,
[
'books'
=>
$books
,
'recents'
=>
$recents
]);
}
...
...
app/Http/Controllers/ChapterController.php
View file @
61ae96c
...
...
@@ -146,15 +146,8 @@ class ChapterController extends Controller
$this
->
checkPermission
(
'chapter-delete'
);
$book
=
$this
->
bookRepo
->
getBySlug
(
$bookSlug
);
$chapter
=
$this
->
chapterRepo
->
getBySlug
(
$chapterSlug
,
$book
->
id
);
if
(
count
(
$chapter
->
pages
)
>
0
)
{
foreach
(
$chapter
->
pages
as
$page
)
{
$page
->
chapter_id
=
0
;
$page
->
save
();
}
}
Activity
::
removeEntity
(
$chapter
);
Activity
::
addMessage
(
'chapter_delete'
,
$book
->
id
,
$chapter
->
name
);
$
chapter
->
delete
(
);
$
this
->
chapterRepo
->
destroy
(
$chapter
);
return
redirect
(
$book
->
getUrl
());
}
}
...
...
app/Http/Controllers/HomeController.php
View file @
61ae96c
...
...
@@ -34,8 +34,8 @@ class HomeController extends Controller
public
function
index
()
{
$activity
=
Activity
::
latest
();
$recent
lyViewed
=
Views
::
getUserRecentlyViewed
(
10
,
0
);
return
view
(
'home'
,
[
'activity'
=>
$activity
,
'recents'
=>
$recent
lyViewed
]);
$recent
s
=
$this
->
signedIn
?
Views
::
getUserRecentlyViewed
(
10
,
0
)
:
$this
->
bookRepo
->
getLatest
(
1
0
);
return
view
(
'home'
,
[
'activity'
=>
$activity
,
'recents'
=>
$recent
s
]);
}
}
...
...
app/Http/Controllers/PageController.php
View file @
61ae96c
...
...
@@ -164,8 +164,7 @@ class PageController extends Controller
$book
=
$this
->
bookRepo
->
getBySlug
(
$bookSlug
);
$page
=
$this
->
pageRepo
->
getBySlug
(
$pageSlug
,
$book
->
id
);
Activity
::
addMessage
(
'page_delete'
,
$book
->
id
,
$page
->
name
);
Activity
::
removeEntity
(
$page
);
$page
->
delete
();
$this
->
pageRepo
->
destroy
(
$page
);
return
redirect
(
$book
->
getUrl
());
}
...
...
app/Repos/BookRepo.php
View file @
61ae96c
<?php
namespace
BookStack\Repos
;
use
BookStack\
Activity
;
use
Activity
;
use
Illuminate\Support\Str
;
use
BookStack\Book
;
use
Views
;
...
...
@@ -10,16 +10,19 @@ class BookRepo
protected
$book
;
protected
$pageRepo
;
protected
$chapterRepo
;
/**
* BookRepo constructor.
* @param Book $book
* @param PageRepo $pageRepo
* @param ChapterRepo $chapterRepo
*/
public
function
__construct
(
Book
$book
,
PageRepo
$pageRepo
)
public
function
__construct
(
Book
$book
,
PageRepo
$pageRepo
,
ChapterRepo
$chapterRepo
)
{
$this
->
book
=
$book
;
$this
->
pageRepo
=
$pageRepo
;
$this
->
chapterRepo
=
$chapterRepo
;
}
/**
...
...
@@ -52,6 +55,23 @@ class BookRepo
return
$this
->
book
->
orderBy
(
'name'
,
'asc'
)
->
paginate
(
$count
);
}
/**
* Get the latest books.
* @param int $count
* @return mixed
*/
public
function
getLatest
(
$count
=
10
)
{
return
$this
->
book
->
orderBy
(
'created_at'
,
'desc'
)
->
take
(
$count
)
->
get
();
}
/**
* Gets the most recently viewed for a user.
* @param int $count
* @param int $page
* @return mixed
*/
public
function
getRecentlyViewed
(
$count
=
10
,
$page
=
0
)
{
return
Views
::
getUserRecentlyViewed
(
$count
,
$page
,
$this
->
book
);
...
...
@@ -105,13 +125,12 @@ class BookRepo
{
$book
=
$this
->
getBySlug
(
$bookSlug
);
foreach
(
$book
->
pages
as
$page
)
{
\Activity
::
removeEntity
(
$page
);
$page
->
delete
();
$this
->
pageRepo
->
destroy
(
$page
);
}
foreach
(
$book
->
chapters
as
$chapter
)
{
\Activity
::
removeEntity
(
$chapter
);
$chapter
->
delete
();
$this
->
chapterRepo
->
destroy
(
$chapter
);
}
$book
->
views
()
->
delete
();
$book
->
delete
();
}
...
...
app/Repos/ChapterRepo.php
View file @
61ae96c
<?php
namespace
BookStack\Repos
;
use
Activity
;
use
Illuminate\Support\Str
;
use
BookStack\Chapter
;
...
...
@@ -18,37 +19,80 @@ class ChapterRepo
$this
->
chapter
=
$chapter
;
}
/**
* Check if an id exists.
* @param $id
* @return bool
*/
public
function
idExists
(
$id
)
{
return
$this
->
chapter
->
where
(
'id'
,
'='
,
$id
)
->
count
()
>
0
;
}
/**
* Get a chapter by a specific id.
* @param $id
* @return mixed
*/
public
function
getById
(
$id
)
{
return
$this
->
chapter
->
findOrFail
(
$id
);
}
/**
* Get all chapters.
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public
function
getAll
()
{
return
$this
->
chapter
->
all
();
}
/**
* Get a chapter that has the given slug within the given book.
* @param $slug
* @param $bookId
* @return mixed
*/
public
function
getBySlug
(
$slug
,
$bookId
)
{
return
$this
->
chapter
->
where
(
'slug'
,
'='
,
$slug
)
->
where
(
'book_id'
,
'='
,
$bookId
)
->
first
();
}
/**
* Create a new chapter from request input.
* @param $input
* @return $this
*/
public
function
newFromInput
(
$input
)
{
return
$this
->
chapter
->
fill
(
$input
);
}
public
function
destroyById
(
$id
)
/**
* Destroy a chapter and its relations by providing its slug.
* @param Chapter $chapter
*/
public
function
destroy
(
Chapter
$chapter
)
{
$page
=
$this
->
getById
(
$id
);
$page
->
delete
();
if
(
count
(
$chapter
->
pages
)
>
0
)
{
foreach
(
$chapter
->
pages
as
$page
)
{
$page
->
chapter_id
=
0
;
$page
->
save
();
}
}
Activity
::
removeEntity
(
$chapter
);
$chapter
->
views
()
->
delete
();
$chapter
->
delete
();
}
/**
* Check if a chapter's slug exists.
* @param $slug
* @param $bookId
* @param bool|false $currentId
* @return bool
*/
public
function
doesSlugExist
(
$slug
,
$bookId
,
$currentId
=
false
)
{
$query
=
$this
->
chapter
->
where
(
'slug'
,
'='
,
$slug
)
->
where
(
'book_id'
,
'='
,
$bookId
);
...
...
@@ -58,6 +102,14 @@ class ChapterRepo
return
$query
->
count
()
>
0
;
}
/**
* Finds a suitable slug for the provided name.
* Checks database to prevent duplicate slugs.
* @param $name
* @param $bookId
* @param bool|false $currentId
* @return string
*/
public
function
findSuitableSlug
(
$name
,
$bookId
,
$currentId
=
false
)
{
$slug
=
Str
::
slug
(
$name
);
...
...
@@ -67,6 +119,12 @@ class ChapterRepo
return
$slug
;
}
/**
* Get chapters by the given search term.
* @param $term
* @param array $whereTerms
* @return mixed
*/
public
function
getBySearch
(
$term
,
$whereTerms
=
[])
{
$terms
=
explode
(
' '
,
preg_quote
(
trim
(
$term
)));
...
...
@@ -80,6 +138,12 @@ class ChapterRepo
return
$chapters
;
}
/**
* Sets a chapters book id.
* @param $bookId
* @param Chapter $chapter
* @return Chapter
*/
public
function
setBookId
(
$bookId
,
Chapter
$chapter
)
{
$chapter
->
book_id
=
$bookId
;
...
...
app/Repos/PageRepo.php
View file @
61ae96c
<?php
namespace
BookStack\Repos
;
use
Activity
;
use
BookStack\Book
;
use
BookStack\Chapter
;
use
Illuminate\Http\Request
;
...
...
@@ -26,21 +27,41 @@ class PageRepo
$this
->
pageRevision
=
$pageRevision
;
}
/**
* Check if a page id exists.
* @param $id
* @return bool
*/
public
function
idExists
(
$id
)
{
return
$this
->
page
->
where
(
'page_id'
,
'='
,
$id
)
->
count
()
>
0
;
}
/**
* Get a page via a specific ID.
* @param $id
* @return mixed
*/
public
function
getById
(
$id
)
{
return
$this
->
page
->
findOrFail
(
$id
);
}
/**
* Get all pages.
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public
function
getAll
()
{
return
$this
->
page
->
all
();
}
/**
* Get a page identified by the given slug.
* @param $slug
* @param $bookId
* @return mixed
*/
public
function
getBySlug
(
$slug
,
$bookId
)
{
return
$this
->
page
->
where
(
'slug'
,
'='
,
$slug
)
->
where
(
'book_id'
,
'='
,
$bookId
)
->
first
();
...
...
@@ -56,6 +77,12 @@ class PageRepo
return
$page
;
}
/**
* Count the pages with a particular slug within a book.
* @param $slug
* @param $bookId
* @return mixed
*/
public
function
countBySlug
(
$slug
,
$bookId
)
{
return
$this
->
page
->
where
(
'slug'
,
'='
,
$slug
)
->
where
(
'book_id'
,
'='
,
$bookId
)
->
count
();
...
...
@@ -137,12 +164,14 @@ class PageRepo
return
$html
;
}
public
function
destroyById
(
$id
)
{
$page
=
$this
->
getById
(
$id
);
$page
->
delete
();
}
/**
* Gets pages by a search term.
* Highlights page content for showing in results.
* @param string $term
* @param array $whereTerms
* @return mixed
*/
public
function
getBySearch
(
$term
,
$whereTerms
=
[])
{
$terms
=
explode
(
' '
,
preg_quote
(
trim
(
$term
)));
...
...
@@ -299,7 +328,6 @@ class PageRepo
/**
* Gets a suitable slug for the resource
*
* @param $name
* @param $bookId
* @param bool|false $currentId
...
...
@@ -314,5 +342,17 @@ class PageRepo
return
$slug
;
}
/**
* Destroy a given page along with its dependencies.
* @param $page
*/
public
function
destroy
(
$page
)
{
Activity
::
removeEntity
(
$page
);
$page
->
views
()
->
delete
();
$page
->
revisions
()
->
delete
();
$page
->
delete
();
}
}
\ No newline at end of file
...
...
resources/views/books/index.blade.php
View file @
61ae96c
...
...
@@ -35,8 +35,10 @@
</div>
<div
class=
"col-sm-4 col-sm-offset-1"
>
<div
class=
"margin-top large"
>
</div>
@if($recents)
<h3>
Recently Viewed
</h3>
@include('partials/entity-list', ['entities' => $recents])
@endif
</div>
</div>
</div>
...
...
resources/views/home.blade.php
View file @
61ae96c
...
...
@@ -6,7 +6,11 @@
<div
class=
"row"
>
<div
class=
"col-md-7"
>
@if($signedIn)
<h2>
My Recently Viewed
</h2>
@else
<h2>
Recent Books
</h2>
@endif
@include('partials/entity-list', ['entities' => $recents])
</div>
...
...
resources/views/partials/entity-list.blade.php
View file @
61ae96c
...
...
@@ -12,6 +12,6 @@
@endforeach
@else
<p
class=
"text-muted"
>
No items available
:(
No items available
</p>
@endif
\ No newline at end of file
...
...
Please
register
or
sign in
to post a comment