167 lines
5.3 KiB
PHP
167 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Helper\HTMLHelper;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* App\Models\CMSInfoAvailable
|
|
*
|
|
* @property int $id
|
|
* @property string $type
|
|
* @property bool $active
|
|
* @property string|null $from
|
|
* @property string|null $to
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereFrom($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereTo($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereType($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereUpdatedAt($value)
|
|
* @mixin \Eloquent
|
|
* @property int $wday
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereWday($value)
|
|
* @property int|null $special
|
|
* @property string|null $date
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfoAvailable whereSpecial($value)
|
|
*/
|
|
class CMSInfoAvailable extends Model
|
|
{
|
|
protected $connection = 'mysql_stern';
|
|
protected $table = 'c_m_s_info_availables';
|
|
|
|
protected $fillable = [
|
|
'type', 'wday', 'active', 'from', 'to', 'special', 'date'
|
|
];
|
|
|
|
protected $casts = [
|
|
'active' => 'bool',
|
|
'special' => 'bool',
|
|
];
|
|
|
|
protected static $days;
|
|
|
|
public function getDateAttribute()
|
|
{
|
|
return isset($this->attributes['date']) ? Carbon::parse($this->attributes['date'])->format('d.m.Y') : '';
|
|
}
|
|
|
|
public function setDateAttribute($value)
|
|
{
|
|
if (!$value) {
|
|
$this->attributes['date'] = null;
|
|
} else {
|
|
$this->attributes['date'] = Carbon::parse($value)->format('Y-m-d');
|
|
}
|
|
}
|
|
|
|
public static function getWeekWithDate($wday = false)
|
|
{
|
|
if(!self::$days){
|
|
self::createWeekWithDate();
|
|
}
|
|
if($wday){
|
|
return self::$days[$wday];
|
|
}
|
|
return self::$days;
|
|
}
|
|
public static function createWeekWithDate(){
|
|
|
|
$days = HTMLHelper::getDeDays();
|
|
$now = \Carbon::now();
|
|
$day_of = $now->dayOfWeekIso;
|
|
|
|
//days after now
|
|
for ($i = $day_of; $i<=7; $i++){
|
|
$days[$i] = [
|
|
'name'=>$days[$i],
|
|
'date'=>$now->format("d.m.Y"),
|
|
];
|
|
$now->modify('+1 day');
|
|
}
|
|
|
|
//days before now
|
|
for ($i = 1; $i<$day_of; $i++) {
|
|
$days[$i] = [
|
|
'name'=>$days[$i],
|
|
'date'=>$now->format("d.m.Y"),
|
|
];
|
|
$now->modify('+1 day');
|
|
}
|
|
|
|
//add
|
|
self::$days = $days;
|
|
}
|
|
public static function getContentBySlug($type, $wday, $special = false){
|
|
$model = CMSInfoAvailable::whereType($type)->whereWday($wday)->whereSpecial($special)->first();
|
|
if($model){
|
|
return $model;
|
|
}
|
|
return new CMSInfoAvailable();
|
|
}
|
|
|
|
public static function setContentBySlug($type, $wday, $values, $special = false){
|
|
|
|
$content = CMSInfoAvailable::whereType($type)->whereWday($wday)->whereSpecial($special)->first();
|
|
if(!$content) {
|
|
$content = CMSInfoAvailable::create([
|
|
'type' => $type,
|
|
'wday' => $wday,
|
|
]);
|
|
}
|
|
$content->fill($values);
|
|
$content->save();
|
|
return $content;
|
|
}
|
|
|
|
public static function getSpecialsBy($type){
|
|
return CMSInfoAvailable::whereType($type)->whereSpecial(true)->get();
|
|
|
|
}
|
|
|
|
public static function getNextSpecialDateBy($type, $wday){
|
|
$content = CMSInfoAvailable::whereType($type)->whereWday($wday)->whereSpecial(true)->orderBy('date', 'DESC')->first();
|
|
$now = \Carbon::now();
|
|
$week = 7;
|
|
if($content){
|
|
$now = \Carbon::parse($content->date);
|
|
}
|
|
$day_of = $now->dayOfWeekIso;
|
|
$week += ($wday - $day_of);
|
|
$now->modify('+'.$week.' days');
|
|
return $now;
|
|
|
|
|
|
}
|
|
|
|
public function fromMinutes (){
|
|
|
|
if(strpos($this->from, ":")){
|
|
$from = explode(":", $this->from);
|
|
$hours = isset($from[0]) ? (int) $from[0] : 0;
|
|
$minute = isset($from[1]) ? (int) $from[1] : 0;
|
|
return $hours * 60 + $minute;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function toMinutes (){
|
|
|
|
if(strpos($this->to, ":")){
|
|
$to = explode(":", $this->to);
|
|
$hours = isset($to[0]) ? (int) $to[0] : 0;
|
|
$minute = isset($to[1]) ? (int) $to[1] : 0;
|
|
return $hours * 60 + $minute;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|