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

120 lines
3.5 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 $random, $group, $css_class;
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'
],
'random' => [
'title' => 'Random',
'description' => 'Random order',
'type' => 'checkbox',
'default' => 0,
],
'width' =>[
'title' => 'Item width',
'type' => 'string',
],
'height' => [
'title' => 'Item height',
'type' => 'string',
]
];
}
public function getCodeOptions()
{
return Group::all()->pluck('name','code')->toArray();
}
public function getTypeOptions()
{
return [
'single'=>'Single',
'slider'=>'Slider',
'carousel'=>'Carousel',
];
}
public function onDisplay(){
$random = input('random');
$this->page['group'] = $group = Group::where('code',$this->property('code'))
->with(['adds' => function ($query) use ($random){
if($random)
$query->inRandomOrder();
else
$query->orderBy('order');
}])
->first();
$this->page['css_class'] = input('css_class','blat');
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'),
];
}
}
}