mivita/app/Models/UserShop.php
2019-03-04 17:05:44 +01:00

128 lines
No EOL
3.4 KiB
PHP

<?php
namespace App\Models;
use App\Http\Controllers\Api\KasController;
use Carbon\Carbon;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UserShop extends Model
{
protected $table = 'user_shops';
protected $is_online = NULL;
protected $casts = [
'featured' => 'array',
];
protected $fillable = [
'user_id', 'name', 'active', 'active_date',
];
use SoftDeletes;
protected $dates = ['deleted_at'];
use Sluggable;
public function sluggable()
{
return [
'slug' => [
'source' => 'name'
]
];
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function on_sites(){
return $this->hasMany('App\Models\UserShopOnSite', 'user_shop_id', 'id');
}
public function getActiveDateFormat(){
if(!$this->attributes['active_date']){ return ""; }
return Carbon::parse($this->attributes['active_date'])->format(\Util::formatDateTimeDB());
}
public function getActiveDateFormatSmall(){
if(!$this->attributes['active_date']){ return ""; }
return Carbon::parse($this->attributes['active_date'])->format("d.m.Y");
}
public function getSubdomain()
{
return config('app.protocol').$this->attributes['slug'].".".config('app.domain');
}
public function getSubdomainStatus()
{
if($this->is_online !== NULL){
return $this->is_online;
}
$kas = new KasController();
$domain = 'mivita.care';
$pra = array(
'subdomain_name' => $this->attributes['slug'].".".$domain,
'domain_name' => $this->attributes['slug'].".".$domain,
);
//check if exisist
$subdomain = $kas->action('get_subdomains', $pra);
if(!empty($subdomain[0]['is_active']) && $subdomain[0]['is_active'] == 'Y'){
$this->is_online = true;
return true;
}
$this->is_online = false;
return false;
}
public function getSubdomainAvailable ()
{
$rCurlHandle = curl_init ( $this->attributes['slug'].".mivita.care" );
curl_setopt ( $rCurlHandle, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt ( $rCurlHandle, CURLOPT_HEADER, TRUE );
curl_setopt ( $rCurlHandle, CURLOPT_NOBODY, TRUE );
curl_setopt ( $rCurlHandle, CURLOPT_RETURNTRANSFER, TRUE );
$strResponse = curl_exec ( $rCurlHandle );
curl_close ( $rCurlHandle );
if ( !$strResponse )
{
return FALSE;
}
$url = "http://".$this->attributes['slug'].".mivita.care/domain/check";
if(@file_get_contents($url) != 'checked'){
return FALSE;
}
return TRUE;
}
public function isImage(){
if(empty($this->attributes['filename']) || @$this->attributes['filename'] == null || @$this->attributes['filename'] == ""){
return false;
}
if(!\Storage::disk('public')->has('images/shop/'.$this->filename)){
return false;
}
return true;
}
public function getImage(){
if($this->isImage()){
$link = 'images/shop/'.$this->filename;
return '/storage/'.$link.'?=lm='.\Storage::disk('public')->lastModified($link);
}
return false;
}
}