Skip to content

Commit

Permalink
Started work on user profile pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Feb 16, 2016
1 parent 293be70 commit 4442a2e
Show file tree
Hide file tree
Showing 30 changed files with 290 additions and 155 deletions.
21 changes: 17 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BookStack\Http\Controllers;

use BookStack\Activity;
use Illuminate\Http\Request;

use Illuminate\Http\Response;
Expand Down Expand Up @@ -92,10 +93,9 @@ public function store(Request $request)
$user->save();
}

return redirect('/users');
return redirect('/settings/users');
}


/**
* Show the form for editing the specified user.
* @param int $id
Expand Down Expand Up @@ -159,7 +159,7 @@ public function update(Request $request, $id)
}

$user->save();
return redirect('/users');
return redirect('/settings/users');
}

/**
Expand Down Expand Up @@ -197,6 +197,19 @@ public function destroy($id)
}
$this->userRepo->destroy($user);

return redirect('/users');
return redirect('/settings/users');
}

/**
* Show the user profile page
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showProfilePage($id)
{
$user = $this->userRepo->getById($id);
$userActivity = $this->userRepo->getActivity($user);
$recentPages = $this->userRepo->getCreatedPages($user, 5, 0);
return view('users/profile', ['user' => $user, 'activity' => $userActivity, 'recentPages' => $recentPages]);
}
}
24 changes: 14 additions & 10 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@

});

// Users
Route::get('/users', 'UserController@index');
Route::get('/users/create', 'UserController@create');
Route::get('/users/{id}/delete', 'UserController@delete');
Route::post('/users/create', 'UserController@store');
Route::get('/users/{id}', 'UserController@edit');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@destroy');
// User Profile routes
Route::get('/user/{userId}', 'UserController@showProfilePage');

// Image routes
Route::group(['prefix' => 'images'], function() {
Expand Down Expand Up @@ -82,8 +76,18 @@
Route::get('/home', 'HomeController@index');

// Settings
Route::get('/settings', 'SettingController@index');
Route::post('/settings', 'SettingController@update');
Route::group(['prefix' => 'settings'], function() {
Route::get('/', 'SettingController@index');
Route::post('/', 'SettingController@update');
// Users
Route::get('/users', 'UserController@index');
Route::get('/users/create', 'UserController@create');
Route::get('/users/{id}/delete', 'UserController@delete');
Route::post('/users/create', 'UserController@store');
Route::get('/users/{id}', 'UserController@edit');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@destroy');
});

});

Expand Down
48 changes: 46 additions & 2 deletions app/Repos/UserRepo.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace BookStack\Repos;


use BookStack\Page;
use BookStack\Role;
use BookStack\Services\EntityService;
use BookStack\User;
use Setting;

Expand All @@ -10,15 +12,19 @@ class UserRepo

protected $user;
protected $role;
protected $entityService;

/**
* UserRepo constructor.
* @param $user
* @param User $user
* @param Role $role
* @param EntityService $entityService
*/
public function __construct(User $user, Role $role)
public function __construct(User $user, Role $role, EntityService $entityService)
{
$this->user = $user;
$this->role = $role;
$this->entityService = $entityService;
}

/**
Expand Down Expand Up @@ -112,4 +118,42 @@ public function destroy(User $user)
$user->socialAccounts()->delete();
$user->delete();
}

/**
* Get the latest activity for a user.
* @param User $user
* @param int $count
* @param int $page
* @return array
*/
public function getActivity(User $user, $count = 20, $page = 0)
{
return \Activity::userActivity($user, $count, $page);
}

/**
* Get the pages the the given user has created.
* @param User $user
* @param int $count
* @param int $page
* @return mixed
*/
public function getCreatedPages(User $user, $count = 20, $page = 0)
{
return $this->entityService->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
->skip($page * $count)->take($count)->get();
}

/**
* Get asset created counts for the give user.
* @return array
*/
public function getAssetCounts(User $user)
{
return [
'pages' => $this->entityService->page->where('created_by', '=', $user->id)->count(),
'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->count(),
'books' => $this->entityService->book->where('created_by', '=', $user->id)->count(),
];
}
}
25 changes: 20 additions & 5 deletions app/Services/ActivityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,46 @@ public function latest($count = 20, $page = 0)
}

/**
* Gets the latest activity for an entitiy, Filtering out similar
* Gets the latest activity for an entity, Filtering out similar
* items to prevent a message activity list.
* @param Entity $entity
* @param int $count
* @param int $page
* @return array
*/
function entityActivity($entity, $count = 20, $page = 0)
public function entityActivity($entity, $count = 20, $page = 0)
{
$activity = $entity->hasMany('BookStack\Activity')->orderBy('created_at', 'desc')
->skip($count * $page)->take($count)->get();

return $this->filterSimilar($activity);
}

/**
* Get latest activity for a user, Filtering out similar
* items.
* @param $user
* @param int $count
* @param int $page
* @return array
*/
public function userActivity($user, $count = 20, $page = 0)
{
$activity = $this->activity->where('user_id', '=', $user->id)
->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get();
return $this->filterSimilar($activity);
}

/**
* Filters out similar activity.
* @param Activity[] $activity
* @param Activity[] $activities
* @return array
*/
protected function filterSimilar($activity)
protected function filterSimilar($activities)
{
$newActivity = [];
$previousItem = false;
foreach ($activity as $activityItem) {
foreach ($activities as $activityItem) {
if ($previousItem === false) {
$previousItem = $activityItem;
$newActivity[] = $activityItem;
Expand Down
29 changes: 29 additions & 0 deletions app/Services/EntityService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace BookStack\Services;


use BookStack\Book;
use BookStack\Chapter;
use BookStack\Page;

class EntityService
{

public $book;
public $chapter;
public $page;

/**
* EntityService constructor.
* @param $book
* @param $chapter
* @param $page
*/
public function __construct(Book $book, Chapter $chapter, Page $page)
{
$this->book = $book;
$this->chapter = $chapter;
$this->page = $page;
}


}
2 changes: 1 addition & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ public function avatar()
*/
public function getEditUrl()
{
return '/users/' . $this->id;
return '/settings/users/' . $this->id;
}
}
Loading

0 comments on commit 4442a2e

Please sign in to comment.