]> _ Git - psq.git/commitdiff
labo article + adcampaigns subscriber only
authorLouis Jeckel <louis.jeckel@outlook.cm>
Mon, 14 Sep 2020 13:52:21 +0000 (15:52 +0200)
committerLouis Jeckel <louis.jeckel@outlook.cm>
Mon, 14 Sep 2020 13:52:21 +0000 (15:52 +0200)
16 files changed:
app/Models/AdCampaign.php
app/Models/HasSubscriberOnlyContent.php
app/Models/LaboArticle.php
app/Policies/AdCampaignPolicy.php [new file with mode: 0644]
app/Policies/LaboArticlePolicy.php [new file with mode: 0644]
app/Policies/SocialArticlePolicy.php [new file with mode: 0644]
app/Providers/AuthServiceProvider.php
app/View/Components/SubscriberOnlyContent.php [new file with mode: 0644]
database/migrations/2020_09_14_133836_add_subscriber_only_to_labo_articles.php [new file with mode: 0644]
database/migrations/2020_09_14_134440_add_subscriber_only_to_ad_campaigns.php [new file with mode: 0644]
resources/views/actu-labos/show.blade.php
resources/views/admin/adCampaigns/form.blade.php
resources/views/admin/laboArticles/form.blade.php
resources/views/com-campaigns/show.blade.php
resources/views/components/subscriber-only-content.blade.php [new file with mode: 0644]
resources/views/social-articles/show.blade.php

index eb8110c17210204e200c724add0b658f43c606e6..49054d12b4fe5ffd9202925657a9a6269073d119 100644 (file)
@@ -17,7 +17,7 @@ use Laravel\Scout\Searchable;
  */
 class AdCampaign extends Model implements Sortable, PillBoxContract
 {
-    use HasSlug, HasMedias, HasPosition;
+    use HasSlug, HasMedias, HasPosition, HasSubscriberOnlyContent;
     use Searchable;
 
     protected $fillable = [
@@ -32,6 +32,7 @@ class AdCampaign extends Model implements Sortable, PillBoxContract
         'box_title',
         'box_content',
         'box_link',
+        'subscriber_only'
     ];
 
     public $appends = [
index bb8e135209d51e87c717196dcc69ac18e2377032..4e7c9d0186b37d5242b5a7cf7b670ef025597eda 100644 (file)
@@ -15,26 +15,16 @@ use HtmlTruncator\Truncator;
 trait HasSubscriberOnlyContent
 {
 
+    protected int $maxContentWords = 100;
+
     /**
      * @return string
      * @throws InvalidHtmlException
      */
     public function getTruncatedContent(): string
     {
-        return Truncator::truncate($this->content, 100);
+        return Truncator::truncate($this->content, $this->maxContentWords);
     }
 
-    /**
-     * @param User|null $user
-     * @return bool
-     */
-    public function isVisibleByUser(User $user = null)
-    {
-        $user ??= \Auth::user();
-
-        return !$this->subscriber_only ||
-            ($user !== null && $user->hasValidSubscription());
-
-    }
 
 }
index 3b1c15c8311aabc8751134a4a334508d6abd7ab4..fcb674acdfa3f9f3bb973af4e0496527b11380c3 100644 (file)
@@ -18,7 +18,7 @@ use Laravel\Scout\Searchable;
  */
 class LaboArticle extends Model implements Sortable, PillBoxContract
 {
-    use HasSlug, HasMedias, HasPosition;
+    use HasSlug, HasMedias, HasPosition, HasSubscriberOnlyContent;
 
     protected $fillable = [
         'publish_start_date',
@@ -31,6 +31,7 @@ class LaboArticle extends Model implements Sortable, PillBoxContract
         'box_title',
         'box_content',
         'box_link',
+        'subscriber_only'
     ];
 
     public $slugAttributes = [
diff --git a/app/Policies/AdCampaignPolicy.php b/app/Policies/AdCampaignPolicy.php
new file mode 100644 (file)
index 0000000..75c5e52
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Policies;
+
+use App\Models\AdCampaign;
+use App\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class AdCampaignPolicy
+{
+    use HandlesAuthorization;
+
+    /**
+     * Determine whether the user can view any models.
+     *
+     * @param  \App\User  $user
+     * @return mixed
+     */
+    public function viewAny(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can view the model.
+     *
+     * @param  null|\App\User  $user
+     * @param  \App\Models\AdCampaign  $adCampaign
+     * @return mixed
+     */
+    public function view(?User $user, AdCampaign $adCampaign)
+    {
+        return !$adCampaign->subscriber_only ||
+            optional($user)->hasValidSubscription();
+    }
+
+    /**
+     * Determine whether the user can create models.
+     *
+     * @param  \App\User  $user
+     * @return mixed
+     */
+    public function create(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can update the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\AdCampaign  $adCampaign
+     * @return mixed
+     */
+    public function update(User $user, AdCampaign $adCampaign)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can delete the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\AdCampaign  $adCampaign
+     * @return mixed
+     */
+    public function delete(User $user, AdCampaign $adCampaign)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can restore the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\AdCampaign  $adCampaign
+     * @return mixed
+     */
+    public function restore(User $user, AdCampaign $adCampaign)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can permanently delete the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\AdCampaign  $adCampaign
+     * @return mixed
+     */
+    public function forceDelete(User $user, AdCampaign $adCampaign)
+    {
+        //
+    }
+}
diff --git a/app/Policies/LaboArticlePolicy.php b/app/Policies/LaboArticlePolicy.php
new file mode 100644 (file)
index 0000000..06d488d
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Policies;
+
+use App\Models\LaboArticle;
+use App\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class LaboArticlePolicy
+{
+    use HandlesAuthorization;
+
+    /**
+     * Determine whether the user can view any models.
+     *
+     * @param  \App\User  $user
+     * @return mixed
+     */
+    public function viewAny(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can view the model.
+     *
+     * @param  null|\App\User  $user
+     * @param  \App\Models\LaboArticle  $laboArticle
+     * @return mixed
+     */
+    public function view(?User $user, LaboArticle $laboArticle)
+    {
+        return !$laboArticle->subscriber_only ||
+            optional($user)->hasValidSubscription();
+    }
+
+    /**
+     * Determine whether the user can create models.
+     *
+     * @param  \App\User  $user
+     * @return mixed
+     */
+    public function create(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can update the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\LaboArticle  $laboArticle
+     * @return mixed
+     */
+    public function update(User $user, LaboArticle $laboArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can delete the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\LaboArticle  $laboArticle
+     * @return mixed
+     */
+    public function delete(User $user, LaboArticle $laboArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can restore the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\LaboArticle  $laboArticle
+     * @return mixed
+     */
+    public function restore(User $user, LaboArticle $laboArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can permanently delete the model.
+     *
+     * @param  \App\User  $user
+     * @param  \App\Models\LaboArticle  $laboArticle
+     * @return mixed
+     */
+    public function forceDelete(User $user, LaboArticle $laboArticle)
+    {
+        //
+    }
+}
diff --git a/app/Policies/SocialArticlePolicy.php b/app/Policies/SocialArticlePolicy.php
new file mode 100644 (file)
index 0000000..d787ba4
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Policies;
+
+use App\Models\SocialArticle;
+use App\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class SocialArticlePolicy
+{
+    use HandlesAuthorization;
+
+    /**
+     * Determine whether the user can view any models.
+     *
+     * @param User $user
+     * @return mixed
+     */
+    public function viewAny(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can view the model.
+     *
+     * @param User|null $user
+     * @param \App\Models\SocialArticle $socialArticle
+     * @return mixed
+     */
+    public function view(?User $user, SocialArticle $socialArticle)
+    {
+        return !$socialArticle->subscriber_only ||
+            optional($user)->hasValidSubscription();
+    }
+
+    /**
+     * Determine whether the user can create models.
+     *
+     * @param User $user
+     * @return mixed
+     */
+    public function create(User $user)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can update the model.
+     *
+     * @param User $user
+     * @param  \App\Models\SocialArticle  $socialArticle
+     * @return mixed
+     */
+    public function update(User $user, SocialArticle $socialArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can delete the model.
+     *
+     * @param User $user
+     * @param  \App\Models\SocialArticle  $socialArticle
+     * @return mixed
+     */
+    public function delete(User $user, SocialArticle $socialArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can restore the model.
+     *
+     * @param User $user
+     * @param  \App\Models\SocialArticle  $socialArticle
+     * @return mixed
+     */
+    public function restore(User $user, SocialArticle $socialArticle)
+    {
+        //
+    }
+
+    /**
+     * Determine whether the user can permanently delete the model.
+     *
+     * @param User $user
+     * @param  \App\Models\SocialArticle  $socialArticle
+     * @return mixed
+     */
+    public function forceDelete(User $user, SocialArticle $socialArticle)
+    {
+        //
+    }
+}
index 30490683b9eb0b302ec899af306ff9b29102fd17..43a00b85f29e49e9f7a6f27d9575a458d66d5c6f 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App\Providers;
 
+
 use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
 use Illuminate\Support\Facades\Gate;
 
@@ -13,7 +14,9 @@ class AuthServiceProvider extends ServiceProvider
      * @var array
      */
     protected $policies = [
-        // 'App\Model' => 'App\Policies\ModelPolicy',
+         \App\Models\SocialArticle::class => \App\Policies\SocialArticlePolicy::class,
+         \App\Models\LaboArticle::class => \App\Policies\LaboArticlePolicy::class,
+         \App\Models\AdCampaign::class => \App\Policies\AdCampaignPolicy::class,
     ];
 
     /**
diff --git a/app/View/Components/SubscriberOnlyContent.php b/app/View/Components/SubscriberOnlyContent.php
new file mode 100644 (file)
index 0000000..330a12c
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\View\Components;
+
+use App\Models\HasSubscriberOnlyContent;
+use Illuminate\View\Component;
+
+class SubscriberOnlyContent extends Component
+{
+
+
+    public $article;
+
+    /**
+     * Create a new component instance.
+     *
+     * @param $article
+     * @throws \RuntimeException
+     */
+    public function __construct($article)
+    {
+        throw_unless(
+            in_array(HasSubscriberOnlyContent::class, class_uses($article), true),
+            new \RuntimeException("The article must implement HasSubscriberOnly trait")
+        );
+
+
+        $this->article = $article;
+    }
+
+    /**
+     * Get the view / contents that represent the component.
+     *
+     * @return \Illuminate\View\View|string
+     */
+    public function render()
+    {
+        return view('components.subscriber-only-content');
+    }
+}
diff --git a/database/migrations/2020_09_14_133836_add_subscriber_only_to_labo_articles.php b/database/migrations/2020_09_14_133836_add_subscriber_only_to_labo_articles.php
new file mode 100644 (file)
index 0000000..059de2c
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddSubscriberOnlyToLaboArticles extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('labo_articles', function (Blueprint $table) {
+            $table->boolean('subscriber_only')->default(0);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('labo_articles', function (Blueprint $table) {
+            $table->dropColumn('subscriber_only');
+        });
+    }
+}
diff --git a/database/migrations/2020_09_14_134440_add_subscriber_only_to_ad_campaigns.php b/database/migrations/2020_09_14_134440_add_subscriber_only_to_ad_campaigns.php
new file mode 100644 (file)
index 0000000..2e5a1f6
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddSubscriberOnlyToAdCampaigns extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('ad_campaigns', function (Blueprint $table) {
+            $table->boolean('subscriber_only')->default(0);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('ad_campaigns', function (Blueprint $table) {
+            $table->dropColumn('subscriber_only');
+        });
+    }
+}
index 7acca187375528df821613d7df4ebf04e237356d..2c146e1faf8f405a1d22c608d47fd7a9d3a79942 100644 (file)
@@ -15,7 +15,7 @@
 {{--                <h2>{{$actu->title}}</h2>--}}
 {{--                <div class="chapo">{{$actu->chapo}}</div>--}}
                 <div class="content ck-content">
-                    {!! $actu->content !!}
+                    <x-subscriber-only-content :article="$actu"></x-subscriber-only-content>
                 </div>
 
 
index 6d8cfcee75826809e9877c6f80dd38db7ebc4316..3712a7d9086c7dcf7fe9c50b96ffb55a8d6e9f30 100644 (file)
         'label' => 'Image',
     ])
 
+    @formField('radios', [
+        'name' => 'subscriber_only',
+        'label' => 'Article réservé aux abonnés',
+        'default' => false,
+        'inline' => true,
+        'options' => [
+            [
+                'value' => false,
+                'label' => 'Non'
+            ],
+            [
+                'value' => true,
+                'label' => 'Oui'
+            ],
+        ]
+    ])
+
     @formField('ckeditor', [
     'name' => 'content',
     'label' => 'Article complet',
index 012c00b20eecf4cc2ffb4a26a462a5a0eca88665..7f17c73a2528ef1aacc0457179b7ad2fc3c625a6 100644 (file)
 
     ])
 
+    @formField('radios', [
+        'name' => 'subscriber_only',
+        'label' => 'Article réservé aux abonnés',
+        'default' => false,
+        'inline' => true,
+        'options' => [
+            [
+                'value' => false,
+                'label' => 'Non'
+            ],
+            [
+                'value' => true,
+                'label' => 'Oui'
+            ],
+        ]
+    ])
+
     @formField('ckeditor', [
         'label' => 'Article complet',
         'name' => 'content',
index 618b44d235a20628ee3ed195083b2c244805eaef..64162d0009a428cd3872cf8b7b948e3ba7d95dba 100644 (file)
@@ -12,7 +12,7 @@
             </div>
             <div class="col-sm-8">
                 <div class="content ck-content">
-                    {!! $campaign->content !!}
+                    <x-subscriber-only-content :article="$campaign"></x-subscriber-only-content>
                 </div>
             </div>
         </div>
diff --git a/resources/views/components/subscriber-only-content.blade.php b/resources/views/components/subscriber-only-content.blade.php
new file mode 100644 (file)
index 0000000..2b40656
--- /dev/null
@@ -0,0 +1,14 @@
+<div>
+    @can('view', $article)
+        {!! $article->content !!}
+    @else
+        <div class="truncated clearfix">
+            {!! $article->getTruncatedContent() !!}
+        </div>
+        <div class="alert alert-warning mt-4">
+            Cet article est réservé aux abonnés ! Pour y accéder merci de vous connecter, ou
+            <a href="{{route('not-registered')}}" >cliquez ici pour découvrir nos formules d'abonnement.</a>
+        </div>
+    @endif
+
+</div>
index 759214a356d7d675ee02c6ae5e883f1d605ece2f..db4a58e9345d972d0250e5db66d0617c90c2702d 100644 (file)
@@ -2,35 +2,25 @@
 
 @section('content')
 
-    <article class="container">
+    <div class="container">
         <div class="row">
 
             <div class="col-sm-4">
                 <x-pill-box slug="article-reseaux-sociaux" :object="$article" class="mb-4"></x-pill-box>
             </div>
-            <div class="col-sm-8">
+            <article class="col-sm-8">
                 <h2>{{$article->title}}</h2>
                 <div class="chapo">{!! $article->chapo !!}</div>
-                <div class="content ck-content mt-5">
 
-                    @if($article->isVisibleByUser())
-                        {!! $article->content !!}
-                    @else
-                        <div class="truncated">
-                            {!! $article->getTruncatedContent() !!}
-                        </div>
-                        <div class="alert alert-warning mt-4">
-                            Cet article est réservé aux abonnés ! Pour y accéder merci de vous connecter, ou
-                            <a href="{{route('not-registered')}}" >cliquez ici pour découvrir nos formules d'abonnement.</a>
-                        </div>
-                    @endif
+                <div class="content ck-content mt-5">
+                    <x-subscriber-only-content :article="$article"></x-subscriber-only-content>
 
                 </div>
 
-            </div>
+            </article>
 
         </div>
 
-    </article>
+    </div>
 
 @endsection