113 lines
3.3 KiB
PHP
113 lines
3.3 KiB
PHP
<?php namespace Tps\Reklama\Components;
|
|
|
|
|
|
use Carbon\Carbon;
|
|
use Cms\Classes\ComponentBase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Pheanstalk\Exception;
|
|
use Tps\Reklama\Models\Group;
|
|
use Tps\Reklama\Models\Statistika;
|
|
|
|
class Advertisement extends ComponentBase
|
|
{
|
|
public $group;
|
|
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'
|
|
],
|
|
'width' =>[
|
|
'title' => 'Item width',
|
|
'type' => 'number',
|
|
],
|
|
'height' => [
|
|
'title' => 'Item height',
|
|
'type' => 'number',
|
|
]
|
|
];
|
|
}
|
|
public function getCodeOptions()
|
|
{
|
|
return Group::all()->pluck('name','code')->toArray();
|
|
}
|
|
public function getTypeOptions()
|
|
{
|
|
return [
|
|
'single'=>'single',
|
|
'single_slide'=>'single_slide',
|
|
'single_random'=>'single_random',
|
|
'slider'=>'slider',
|
|
];
|
|
}
|
|
|
|
public function onDisplay(){
|
|
$type = $this->property('type');
|
|
$this->page['group'] = $group = Group::where('code',$this->property('code'))
|
|
->with(['adds' => function ($query) use ($type){
|
|
if($type === 'single_random')
|
|
$query->inRandomOrder()->limit(1);
|
|
else
|
|
$query->orderBy('order');
|
|
}])
|
|
->first();
|
|
|
|
if ($group && $group->adds){
|
|
try {
|
|
DB::beginTransaction();
|
|
foreach ($group->adds as $adv){
|
|
if($adv->enable_stats){
|
|
$statistika = Statistika::firstOrCreate(['item_id' => $adv->id,'date' => Carbon::today()]);
|
|
$statistika->increment('view');
|
|
}
|
|
}
|
|
DB::commit();
|
|
}catch (Exception $ex){
|
|
DB::rollback();
|
|
}
|
|
return [
|
|
'#'.$this->alias => $this->renderPartial('@'.$this->property('type')),
|
|
];
|
|
}
|
|
}
|
|
|
|
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'),
|
|
];
|
|
}
|
|
|
|
}
|
|
}
|