public dash and view counting

This commit is contained in:
2018-07-29 11:52:29 +02:00
parent f77b1789fa
commit a79b5ae7d8
37 changed files with 415 additions and 663 deletions
@@ -27,7 +27,7 @@ class LoginController extends Controller
*
* @var string
*/
protected $redirectTo = '/';
protected $redirectTo = '/home';
/**
* Create a new controller instance.
@@ -29,7 +29,7 @@ class RegisterController extends Controller
*
* @var string
*/
protected $redirectTo = '/';
protected $redirectTo = '/home';
/**
* Create a new controller instance.
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers;
use App\Models\Table;
use App\Models\User;
class DashController extends Controller
{
public function view()
{
$users = User::orderBy('updated_at', 'desc')
->paginate(15, ['id', 'title', 'name'], 'pageu');
// TODO visit counting
$tables = Table::forList()
->orderBy('visits', 'desc')
->paginate(10, ['*'], 'paget');
return view('welcome', compact('users', 'tables'));
}
}
+1 -4
View File
@@ -18,10 +18,7 @@ class ProfileController extends Controller
*/
public function view(User $user)
{
$tables = $user->tables()
->with('revision:id,row_count')
->orderByDesc('updated_at')
->paginate(10);
$tables = $user->tables()->forList()->orderByDesc('updated_at')->paginate(10);
return view('profile.view')->with(compact('tables', 'user'));
}
+12 -1
View File
@@ -20,7 +20,10 @@ class TableController extends Controller
]);
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
$tableModel = $user->tables()->withCount(['favourites', 'forks', 'revisions', 'comments'])
->where('name', $table)->first();
if ($tableModel === null) abort(404, "No such table.");
// make it possible to show other revisions
if ($input->has('rev')) {
@@ -31,6 +34,14 @@ class TableController extends Controller
$revision = $tableModel->revision;
}
$cookieName = "view!$user->name!$table";
if (!$request->cookie($cookieName, false)) {
$tableModel->countVisit();
\Cookie::queue($cookieName, true, 86400);
}
return view('table.view', [
'table' => $tableModel,
'revision' => $revision,
+2
View File
@@ -25,6 +25,8 @@ class Proposal extends Model
use Reportable;
protected $guarded = [];
protected $touches = ['author', 'table'];
/** Authoring user */
public function author()
{
+30 -2
View File
@@ -2,11 +2,10 @@
namespace App\Models;
use App\Models\Concerns\ManyManyThrough;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
/**
* A data table object (referencing revisions)
@@ -22,6 +21,7 @@ use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
* @property string $description
* @property string $license
* @property string $origin
* @property int $visits
* @property-read string $viewPage
* @property-read User $owner
* @property-read Table $parentTable
@@ -38,6 +38,21 @@ class Table extends Model
use Reportable;
protected $guarded = [];
protected $touches = ['owner'];
public function countVisit()
{
// Temporarily disable timestamps to avoid touching updated_at
$oldt = $this->touches;
$this->touches = [];
$this->timestamps = false;
$this->increment('visits');
$this->timestamps = true;
$this->touches = $oldt;
}
/** Get owner from the instance cache (use when building table lists) */
public function cachedOwner()
{
@@ -92,6 +107,14 @@ class Table extends Model
return $this->belongsToMany(User::class, 'table_favourites');
}
/**
* Relation to the pivot table, for favourite counting
*/
public function favourites()
{
return $this->hasMany(TableFavouritePivot::class);
}
/** Users to notify about comments */
public function discussionFollowers()
{
@@ -106,4 +129,9 @@ class Table extends Model
return parent::__get($name);
}
public function scopeForList(Builder $query)
{
return $query->with('revision:id,row_count')->withCount(['favourites', 'forks', 'revisions']);
}
}
+2
View File
@@ -26,6 +26,8 @@ class TableComment extends Model
use Reportable;
protected $guarded = [];
protected $touches = ['table'];
protected static function boot()
{
parent::boot();
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Model representing the pivot table, used for more efficient favourite counting
*/
class TableFavouritePivot extends Model
{
protected $table = 'table_favourites';
public $timestamps = [];
}
+5
View File
@@ -14,6 +14,11 @@ class WidgetFactory
$this->fieldCols = $fieldCols;
}
public function help($text)
{
return view('form._help-inner', ['help' => $text]);
}
public function header($hx, $text)
{
return "<div class=\"row\">".