Skip to content

Commit

Permalink
dashboard and image ad upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tushargugnani committed Jan 27, 2023
1 parent c36635a commit 555016a
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 92 deletions.
1 change: 1 addition & 0 deletions database/factories/SmartAdFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function definition()
return [
'name' => $adname,
'body' => fake()->randomHtml(),
'adType' => 'HTML',
'slug' => implode('-', explode(' ', $adname))
];
}
Expand Down
6 changes: 5 additions & 1 deletion database/migrations/create_smart_ads_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class CreateSmartAdsTable extends Migration
$table->id();
$table->string('name')->unique();
$table->string('slug');
$table->text('body');
$table->text('body')->nullable();
$table->string('adType');
$table->string('image')->nullable();
$table->string('imageUrl')->nullable();
$table->string('imageAlt')->nullable();
$table->integer('views')->default(0);
$table->integer('clicks')->default(0);
$table->boolean('enabled')->nullable();
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/css/app.css

Large diffs are not rendered by default.

91 changes: 77 additions & 14 deletions resources/views/smart-ad-manager/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Create New Ad
</h2>

<form action="/smart-ad-manager/ads/store" method="POST">
<form action="/smart-ad-manager/ads/store" method="POST" enctype="multipart/form-data">
@csrf
<div
class="px-4 py-3 mb-8 bg-white rounded-lg shadow-md dark:bg-gray-800"
Expand All @@ -23,19 +23,81 @@ class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:bor
@enderror
</label>

<label class="block mt-4 text-sm">
<span class="text-gray-700 dark:text-gray-400">Ad Body</span>
<textarea
class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
rows="7"
placeholder="Enter html body of the form." name="body"
>{{old('body')}}</textarea>
@error('body')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
<div x-data="{
adType: '{{!empty(old('adType')) ? old('adType') : 'HTML'}}'
}">
<div class="w-max flex mt-4">
<label class="text-gray-700 my-1 flex items-center mr-4">
<input x-model="adType" type="radio" name="adType" value="HTML" class="mr-2 w-4 h-4" checked="">
<span>HTML Ad</span>
</label>
<label class="text-gray-700 my-1 flex items-center mr-4">
<input x-model="adType" type="radio" name="adType" value="IMAGE" class="mr-2 w-4 h-4">
<span>Image Ad</span>
</label>
</div>

<div x-show="adType == 'HTML'">
<label class="block mt-4 text-sm">
<span class="text-gray-700 dark:text-gray-400">Ad Body</span>
<textarea
class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
rows="7"
placeholder="Enter html body of the form." name="body"
>{{old('body')}}</textarea>
@error('body')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
</div>

<div x-show="adType == 'IMAGE'">
<div>
<label class="w-96 mt-4 block text-sm">
<span class="text-gray-700 dark:text-gray-400">Upload Image File</span>
<input type="file" name="image" class="form-control block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none" id="formFile">
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="file_input_help">SVG, PNG, JPG or GIF (MAX. 800x400px).</p>
@error('image')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>

<label class="block text-sm mt-4">
<span class="text-gray-700 dark:text-gray-400">Image URL</span>
<input
class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
placeholder="Image URL" name="imageUrl" value="{{old('imageURL')}}"
/>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="image_url_help">url address where advert should redirect on click</p>
@error('imageUrl')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>

<label class="block text-sm mt-4">
<span class="text-gray-700 dark:text-gray-400">Image Alt</span>
<input
class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
placeholder="Image Alt Text" name="imageAlt" value="{{old('imageAlt')}}"
/>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="image_url_help">Image alt parameter tag</p>
@error('imageAlt')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
</div>


</div>
</div>

<div x-data="{
selected: null,
Expand Down Expand Up @@ -116,6 +178,7 @@ class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:bor
</div>

</div>
<input type="hidden" name="form_type" value="create" />
</form>


Expand Down
90 changes: 77 additions & 13 deletions resources/views/smart-ad-manager/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,82 @@ class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:bor
@enderror
</label>

<label class="block mt-4 text-sm">
<span class="text-gray-700 dark:text-gray-400">Ad Body</span>
<textarea
class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
rows="7"
placeholder="Enter html body of the form." name="body"
>{{$smartAd->body}}</textarea>
@error('body')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
<div x-data="{
adType: '{{$smartAd->adType}}'
}">
<div class="w-max flex mt-4">
<label class="text-gray-700 my-1 flex items-center mr-4">
<input x-model="adType" type="radio" name="adType" value="HTML" class="mr-2 w-4 h-4" checked="">
<span>HTML Ad</span>
</label>
<label class="text-gray-700 my-1 flex items-center mr-4">
<input x-model="adType" type="radio" name="adType" value="IMAGE" class="mr-2 w-4 h-4">
<span>Image Ad</span>
</label>
</div>

<div x-show="adType == 'HTML'">
<label class="block mt-4 text-sm">
<span class="text-gray-700 dark:text-gray-400">Ad Body</span>
<textarea
class="block w-full mt-1 text-sm dark:text-gray-300 dark:border-gray-600 dark:bg-gray-700 form-textarea focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:focus:shadow-outline-gray"
rows="7"
placeholder="Enter html body of the form." name="body"
>{{$smartAd->body}}</textarea>
@error('body')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
</div>

<div x-show="adType == 'IMAGE'">
<div>
<img src="{{asset($smartAd->image)}}" alt="{{$smartAd->imageAlt}}" />
<label class="w-96 mt-4 block text-sm">
<span class="text-gray-700 dark:text-gray-400">Change Image File</span>
<input type="file" name="image" class="form-control block w-full px-3 py-1.5 text-base font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none" id="formFile">
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="file_input_help">SVG, PNG, JPG or GIF (MAX. 800x400px).</p>
@error('image')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>

<label class="block text-sm mt-4">
<span class="text-gray-700 dark:text-gray-400">Image URL</span>
<input
class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
placeholder="Image URL" name="imageUrl" value="{{$smartAd->imageUrl}}"
/>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="image_url_help">url address where advert should redirect on click</p>
@error('imageUrl')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>

<label class="block text-sm mt-4">
<span class="text-gray-700 dark:text-gray-400">Image Alt</span>
<input
class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:border-purple-400 focus:outline-none focus:shadow-outline-purple dark:text-gray-300 dark:focus:shadow-outline-gray form-input"
placeholder="Image Alt Text" name="imageAlt" value="{{$smartAd->imageAlt}}"
/>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-300" id="image_url_help">Image alt parameter tag</p>
@error('imageAlt')
<span class="text-xs text-red-600 dark:text-red-400">
{{$message}}
</span>
@enderror
</label>
</div>


</div>
</div>

<div x-data="{
selected: null,
Expand Down Expand Up @@ -122,6 +185,7 @@ class="block w-full mt-1 text-sm dark:border-gray-600 dark:bg-gray-700 focus:bor
</div>

</div>
<input type="hidden" name="form_type" value="edit" />
</form>


Expand Down
59 changes: 39 additions & 20 deletions resources/views/smart-ad-manager/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@
<h4 class="mb-4 font-semibold text-gray-600 dark:text-gray-300">
{{$smartAd->slug}}
</h4>
<div class="my-1 font-semibold text-gray-800">Ad Body</div>


@if($smartAd->adType == "HTML")
<div class="my-1 font-semibold text-gray-800">Ad HTML Body</div>
<p class="text-gray-600 dark:text-gray-400">
<pre><code class="lang-html">
{{ $smartAd->body }}
</code></pre>
</p>
@elseif($smartAd->adType == "IMAGE")
<div class="my-1 font-semibold text-gray-800">Ad Image</div>
<p class="text-gray-600 dark:text-gray-400">
<img src="{{asset($smartAd->image)}}" alt="{{$smartAd->imageAlt}}" />
</p>

<div class="my-1 font-semibold text-gray-800">Image URL</div>
<p class="text-gray-500 dark:text-gray-400">
<p>{{$smartAd->imageUrl}}</p>
</p>
@endif

<div class="text-gray-600 mt-5">
<div class="my-1 font-semibold text-gray-800">Usage (Manual Placement)</div>
Expand All @@ -32,28 +45,34 @@
</div>
</div>

@isset($smartAd->placements)
<div class="mt-5">
<div class="my-1 font-semibold text-gray-800">Auto Placement</div>
@foreach(json_decode($smartAd->placements) as $placement)
@switch($placement->position)
@case('beforebegin')
Before HTML Element
@break
@case('afterend')
Before HTML Element
@break
@case('afterbegin')
Inside HTML Selector (At Beginning)
@break
@case('beforeend')
Inside HTML Selector (At End)
@break
@endswitch
-> {{$placement->selector}} <br/>
@endforeach
@isset($smartAd->placements)
@foreach(json_decode($smartAd->placements) as $placement)
@if(!empty($placement->selector))
@switch($placement->position)
@case('beforebegin')
Before HTML Element
@break
@case('afterend')
Before HTML Element
@break
@case('afterbegin')
Inside HTML Selector (At Beginning)
@break
@case('beforeend')
Inside HTML Selector (At End)
@break
@endswitch
-> {{$placement->selector}} <br/>
@endif
@endforeach
@else
<p> No Auto placements set for this Ad</p>
@endisset


</div>
@endisset

</div>

Expand Down
11 changes: 10 additions & 1 deletion src/Http/Controllers/SmartAdManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ public function create(){
}

public function store(StoreSmartAdRequest $request){

if(isset($request->image)){
$imagePath = $request->file('image')->store('image', 'public');
}

$smartAd = SmartAd::create([
'name' => $request->name,
'slug' => $this->slug($request->name),
'body' => $request->body,
'adType' => $request->adType,
'body' => isset($request->body) ? $request->body : null,
'image' => isset($imagePath) ? $imagePath : null,
'imageUrl' => $request->imageUrl,
'imageAlt' => $request->imageAlt,
'placements' => $request->placements,
]);
return redirect("/smart-ad-manager/ads/{$smartAd->id}")->with(['message' => 'Ad Created', 'color' => 'green']);
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Livewire/AdReportComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function calculateClicksReport(){
array_push($this->clicksPerAd, ['name' => $ad->name, 'clicks' => $adClicksCollection->sum($ad->slug)]);
}

$this->clicksPerAd = collect($this->clicksPerAd)->sortByDesc('clicks')->values()->all();;
$this->clicksPerAd = collect($this->clicksPerAd)->sortByDesc('clicks')->values()->all();

$this->emit('renderChart');

Expand Down
9 changes: 8 additions & 1 deletion src/Http/Requests/StoreSmartAdRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ public function authorize()
*/
public function rules()
{
if($this->form_type == 'edit'){
$image_rule = "nullable|image|mimes:jpg,png,jpeg,gif,svg|max:2048";
}else{
$image_rule = "required_if:adType,IMAGE|image|mimes:jpg,png,jpeg,gif,svg|max:2048";
}
return [
'name' => [
'required',
Rule::unique('smart_ads', 'name')->ignore($this->smartAd)
],
"body" => "required"
"body" => "required_if:adType,HTML",
"image" => $image_rule,
"imageUrl" => "nullable|url"
];
}
}
Loading

0 comments on commit 555016a

Please sign in to comment.