diff --git a/app/Http/Controllers/Gallery/AlbumController.php b/app/Http/Controllers/Gallery/AlbumController.php index fef5ec2..cbfdc9d 100644 --- a/app/Http/Controllers/Gallery/AlbumController.php +++ b/app/Http/Controllers/Gallery/AlbumController.php @@ -57,6 +57,11 @@ class AlbumController extends Controller ->get(); } + DB::transaction(function () use ($album) { + $album->hits++; + $album->save(); + }); + return Theme::render(sprintf('gallery.album_%s', $requestedView), [ 'album' => $album, 'allowed_views' => $validViews, diff --git a/app/Http/Controllers/Gallery/PhotoController.php b/app/Http/Controllers/Gallery/PhotoController.php index 858fd97..7f7586f 100644 --- a/app/Http/Controllers/Gallery/PhotoController.php +++ b/app/Http/Controllers/Gallery/PhotoController.php @@ -13,6 +13,7 @@ use App\Http\Middleware\VerifyCsrfToken; use App\Photo; use Guzzle\Http\Mimetypes; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Gate; use Symfony\Component\HttpFoundation\Request; @@ -60,6 +61,11 @@ class PhotoController extends Controller $photoStream = $album->getAlbumSource()->fetchPhotoContent($photo, $thumbnail); $mimeType = Mimetypes::getInstance()->fromFilename($photo->storage_file_name); + DB::transaction(function () use ($photo) { + $photo->hits_download++; + $photo->save(); + }); + return response()->stream( function() use ($photoStream) { @@ -95,6 +101,11 @@ class PhotoController extends Controller $returnAlbumUrl = $referer; } + DB::transaction(function () use ($photo) { + $photo->hits++; + $photo->save(); + }); + return Theme::render('gallery.photo', [ 'album' => $album, 'is_original_allowed' => $isOriginalAllowed, diff --git a/app/VisitorHit.php b/app/VisitorHit.php new file mode 100644 index 0000000..5347fce --- /dev/null +++ b/app/VisitorHit.php @@ -0,0 +1,10 @@ +bigIncrements('id'); + $table->unsignedInteger('album_id')->nullable(); + $table->unsignedBigInteger('photo_id')->nullable(); + $table->unsignedInteger('user_id')->nullable(); + $table->string('album_view')->nullable(); + $table->timestamp('hit_at'); + $table->string('ip_address'); + $table->string('user_agent'); + + $table->foreign('album_id') + ->references('id')->on('albums') + ->onDelete('no action'); + $table->foreign('photo_id') + ->references('id')->on('photos') + ->onDelete('no action'); + $table->foreign('user_id') + ->references('id')->on('users') + ->onDelete('no action'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('visitor_hits'); + } +} diff --git a/database/migrations/2017_04_16_095657_attach_hit_columns.php b/database/migrations/2017_04_16_095657_attach_hit_columns.php new file mode 100644 index 0000000..3a1ca59 --- /dev/null +++ b/database/migrations/2017_04_16_095657_attach_hit_columns.php @@ -0,0 +1,42 @@ +bigInteger('hits'); + }); + + Schema::table('photos', function (Blueprint $table) { + $table->bigInteger('hits'); + $table->bigInteger('hits_download'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('photos', function (Blueprint $table) { + $table->bigInteger('hits_download'); + $table->dropColumn('hits'); + }); + + Schema::table('albums', function (Blueprint $table) { + $table->dropColumn('hits'); + }); + } +}