Attendize/app/Models/Category.php

140 lines
4.4 KiB
PHP
Raw Normal View History

2018-12-10 07:27:23 +00:00
<?php
/**
* Created by PhpStorm.
* User: merdan
* Date: 12/9/2018
* Time: 9:52 PM
*/
namespace App\Models;
2019-09-28 07:05:26 +00:00
use Illuminate\Support\Carbon;
2019-09-14 12:27:41 +00:00
use Illuminate\Support\Facades\Config;
2019-09-28 07:05:26 +00:00
use Illuminate\Support\Facades\DB;
2019-09-14 12:27:41 +00:00
use Illuminate\Support\Str;
2019-08-24 06:55:14 +00:00
class Category extends \Illuminate\Database\Eloquent\Model{
use \Backpack\CRUD\CrudTrait;
2018-12-10 07:27:23 +00:00
/**
* Indicates whether the model should be timestamped.
*
* @var bool $timestamps
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string $table
*/
protected $table = 'categories';
/**
* Indicates whether the model should use soft deletes.
*
* @var bool $softDelete
*/
protected $softDelete = false;
protected $fillable = ['title_tk','title_ru','view_type','lft','rgt','parent_id','depth','events_limit'];
2019-09-14 12:27:41 +00:00
/**
* Get the url of the event.
*
* @return string
*/
public function getUrlAttribute()
{
2019-10-02 14:08:07 +00:00
// switch ($this->view_type){
// case 'concert' : $rout_name = "showSubCategoryEventsPage";
// break;
// default : $rout_name = "showCategoryEventsPage";
// break;
// }
2019-10-05 11:05:56 +00:00
if($this->parent_id > 0)
$rout_name = 'showSubCategoryEventsPage';
else
$rout_name = "showCategoryEventsPage";
2019-10-02 14:08:07 +00:00
return route($rout_name, ["cat_id"=>$this->id, "cat_slug"=>Str::slug($this->title)]);
2019-09-14 12:27:41 +00:00
//return URL::to('/') . '/e/' . $this->id . '/' . Str::slug($this->title);
}
2019-10-05 11:05:56 +00:00
public function customUrl($qs = array()){
$url = $this->url;
foreach($qs as $key => $value){
$qs[$key] = sprintf('%s=%s',$key, urlencode($value));
}
return sprintf('%s?%s', $url, implode('&', $qs));
}
2019-09-14 12:27:41 +00:00
public function getTitleAttribute(){
return $this->{'title_'.Config::get('app.locale')};
}
2018-12-10 07:27:23 +00:00
/**
2019-08-24 06:55:14 +00:00
* The events associated with the category.
2018-12-10 07:27:23 +00:00
*
2019-08-24 06:55:14 +00:00
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2018-12-10 07:27:23 +00:00
*/
2019-08-24 06:55:14 +00:00
public function events(){
return $this->hasMany(\App\Models\Event::class);
}
2019-09-28 07:05:26 +00:00
public function cat_events(){
return $this->hasMany(\App\Models\Event::class,'sub_category_id')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));
}]);
}
2020-03-28 09:00:22 +00:00
public function scopeCategoryLiveEvents($query,$limit,$view){
2019-09-28 07:05:26 +00:00
// dd($this->view_type);
2020-03-28 09:00:22 +00:00
return $query->select('id','title_tk','title_ru','view_type')
->where('view_type',$view)
2019-09-28 07:05:26 +00:00
->orderBy('lft')
->with(['events' => function($q) use($limit){
2020-02-10 12:15:58 +00:00
$q->select('id','title_'.Config::get('app.locale'),'description_'.Config::get('app.locale'),'category_id','sub_category_id','start_date')
2019-09-28 07:05:26 +00:00
->limit($limit)
->with('starting_ticket')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));
}])
->onLive();
}]);
}
public function parent(){
return $this->belongsTo(Category::class,'parent_id');
}
public function children(){
return $this->hasMany(Category::class,'parent_id')
2020-02-10 12:15:58 +00:00
->select('id','title_'.Config::get('app.locale'),'parent_id','lft')
2019-09-28 07:05:26 +00:00
->orderBy('lft');
}
2019-08-24 06:55:14 +00:00
public function scopeMain($query){
2019-08-28 06:34:37 +00:00
return $query->where('depth',1)->orderBy('lft','asc');
2019-08-24 06:55:14 +00:00
}
2019-09-28 07:05:26 +00:00
2019-08-24 06:55:14 +00:00
public function scopeSub($query){
2019-09-10 13:33:55 +00:00
return $query->where('depth',2)->orderBy('lft','asc');
2019-08-24 06:55:14 +00:00
}
public function scopeChildren($query,$parent_id){
return $query->where('parent_id',$parent_id)->orderBy('lft','asc');
2018-12-10 07:27:23 +00:00
}
2019-09-16 13:30:51 +00:00
2019-10-03 09:18:21 +00:00
public function scopeWithLiveEvents($query, $orderBy, $start_date = null,$end_date = null, $limit = 8 ){
return $query->with(['cat_events' => function($query) use ($start_date, $end_date, $limit, $orderBy) {
2020-02-10 12:15:58 +00:00
$query->select('id','title_'.Config::get('app.locale'),'description_'.Config::get('app.locale'),'category_id','sub_category_id','start_date')
2020-02-25 12:59:55 +00:00
->take($limit)
2020-03-28 10:17:13 +00:00
->with('starting_ticket')
->withCount(['stats as views' => function($q){
$q->select(DB::raw("SUM(views) as v"));}])
2020-02-25 13:07:07 +00:00
->onLive($start_date, $end_date)//event scope onLive get only live events
2019-10-03 09:18:21 +00:00
->orderBy($orderBy['field'],$orderBy['order']);
2019-09-28 07:05:26 +00:00
}]);
2019-09-16 13:30:51 +00:00
}
2019-09-28 07:05:26 +00:00
2020-02-10 12:15:58 +00:00
}