ORIENT/plugins/tps/reklama/components/Advertisement.php

129 lines
3.7 KiB
PHP
Raw Normal View History

2021-04-01 06:04:39 +00:00
<?php namespace Tps\Reklama\Components;
2021-04-08 08:08:59 +00:00
use Carbon\Carbon;
2021-04-01 06:04:39 +00:00
use Cms\Classes\ComponentBase;
2021-04-08 08:08:59 +00:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Pheanstalk\Exception;
2021-04-01 06:04:39 +00:00
use Tps\Reklama\Models\Group;
2021-04-08 08:08:59 +00:00
use Tps\Reklama\Models\Statistika;
2021-04-01 06:04:39 +00:00
class Advertisement extends ComponentBase
{
2021-05-10 06:43:50 +00:00
public $random, $group, $css_class;
2021-05-10 12:01:19 +00:00
2021-04-01 06:04:39 +00:00
public function componentDetails()
{
return [
'name' => 'Advertisements',
'description' => 'Reklamajyklar'
];
}
public function defineProperties(){
return [
'code' => [
'title' => 'Group',
'description' => 'Choose advertisement group',
'type' => 'dropdown',
'required' => 'required',
'validationMessage' => 'Please select group'
],
'type' => [
'title' => 'Display type',
'description' => 'Choose display type group',
'type' => 'dropdown',
'default' => 'single',
'required' => 'required',
'validationMessage' => 'Please select type'
],
2021-04-13 09:07:38 +00:00
'random' => [
'title' => 'Random',
'description' => 'Random order',
'type' => 'checkbox',
'default' => 0,
],
2021-04-01 06:04:39 +00:00
'width' =>[
'title' => 'Item width',
2021-04-13 09:07:38 +00:00
'type' => 'string',
2021-04-01 06:04:39 +00:00
],
'height' => [
'title' => 'Item height',
2021-04-13 09:07:38 +00:00
'type' => 'string',
2021-04-01 06:04:39 +00:00
]
];
}
public function getCodeOptions()
{
return Group::all()->pluck('name','code')->toArray();
}
public function getTypeOptions()
{
return [
2021-04-13 09:07:38 +00:00
'single'=>'Single',
'slider'=>'Slider',
'carousel'=>'Carousel',
2021-04-01 06:04:39 +00:00
];
}
2021-05-10 12:01:19 +00:00
public function onRun()
{
$this->tayyarla($this->property('random'));
}
private function tayyarla($random){
$this->group = Group::where('code',$this->property('code'))
2021-04-13 09:07:38 +00:00
->with(['adds' => function ($query) use ($random){
if($random)
$query->inRandomOrder();
2021-04-08 08:08:59 +00:00
else
$query->orderBy('order');
}])
2021-04-01 06:04:39 +00:00
->first();
2021-05-10 12:01:19 +00:00
// dd($group->adds);
2021-05-01 14:01:36 +00:00
$this->page['css_class'] = input('css_class','blat');
2021-05-10 12:01:19 +00:00
if ($this->group && $this->group->adds){
2021-04-08 08:08:59 +00:00
try {
DB::beginTransaction();
2021-05-10 12:01:19 +00:00
foreach ($this->group->adds as $adv){
2021-04-08 08:08:59 +00:00
if($adv->enable_stats){
$statistika = Statistika::firstOrCreate(['item_id' => $adv->id,'date' => Carbon::today()]);
$statistika->increment('view');
}
}
DB::commit();
}catch (Exception $ex){
DB::rollback();
}
}
}
2021-05-10 12:01:19 +00:00
public function onShow(){
$random = input('random');
$this->tayyarla($random);
return [
'#'.$this->alias => $this->renderPartial('@'.$this->property('type')),
];
}
2021-04-08 08:08:59 +00:00
public function onRedirect(){
$data = post();
if($data && $data['id']){
$statistika = Statistika::firstOrCreate(['item_id' => $data['id'],'date' => Carbon::today()]);
$statistika->increment('click');
}
if($data['url']){
$this->page['url'] = $data['url'];
return [
'@#'.$this->alias => $this->renderPartial('@redirect'),
];
}
2021-04-01 06:04:39 +00:00
}
}