187 lines
6.2 KiB
PHP
187 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Cviebrock\EloquentSluggable\Sluggable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* App\Models\CMSInfo
|
|
*
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string $slug
|
|
* @property string $field
|
|
* @property string|null $text
|
|
* @property string|null $full_text
|
|
* @property int|null $integer
|
|
* @property float|null $decimal
|
|
* @property int $active
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo findSimilarSlugs($attribute, $config, $slug)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereDecimal($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereField($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereFullText($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereInteger($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereSlug($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereText($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereUpdatedAt($value)
|
|
* @property string $type
|
|
* @property int $bool
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereBool($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSInfo whereType($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|CMSInfo withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class CMSInfo extends Model
|
|
{
|
|
use Sluggable;
|
|
|
|
|
|
protected static $types = [
|
|
'text' => 'Text (190 Zeichen)',
|
|
'full_text' => 'Full Text (50K Zeichen)',
|
|
'integer' => 'Zahl (10 Stellen)',
|
|
'decimal' => 'Kommazahl (10,2 Stellen)',
|
|
'bool' => 'an (1) / aus (0)',
|
|
];
|
|
|
|
protected $connection = 'mysql_stern';
|
|
protected $table = 'c_m_s_infos';
|
|
|
|
protected $fillable = [
|
|
'name', 'slug', 'type', 'text', 'full_text', 'integer', 'decimal', 'bool'
|
|
];
|
|
|
|
|
|
public function sluggable(): array
|
|
{
|
|
return [
|
|
'slug' => [
|
|
'source' => 'name'
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function getTypeOptions($setKey = false){
|
|
$options = self::$types;
|
|
$ret = "";
|
|
foreach ($options as $key => $option){
|
|
$attr = ($key == $setKey) ? 'selected="selected"' : '';
|
|
$ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public function getTypeName(){
|
|
return isset(self::$types[$this->type]) ? self::$types[$this->type] : '';
|
|
}
|
|
|
|
public function getPreviewContent(){
|
|
|
|
$content = $this->{$this->type};
|
|
if(strlen($content) > 40){
|
|
return substr($content, 0, 40)." ...";
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
|
|
public function _format_number($value){
|
|
return preg_replace("/[^0-9,]/", "", $value);
|
|
}
|
|
|
|
public function setDecimalAttribute($value)
|
|
{
|
|
$value = $this->_format_number($value);
|
|
$value = substr($value, -13);
|
|
$this->attributes['decimal'] = (float) str_replace(',', '.', $value);
|
|
}
|
|
|
|
public function getDecimalAttribute()
|
|
{
|
|
if(isset($this->attributes['decimal'])){
|
|
// 2 = decimal places | '.' = decimal seperator | ',' = thousand seperator
|
|
return number_format(($this->attributes['decimal']), 2, ',', '.');
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public function setIntegerAttribute($value)
|
|
{
|
|
$value = $this->_format_number($value);
|
|
$value = substr($value, -10);
|
|
$this->attributes['integer'] = (int) $value;
|
|
}
|
|
|
|
public function setBoolAttribute($value)
|
|
{
|
|
|
|
$this->attributes['bool'] = (bool) $value;
|
|
}
|
|
|
|
public static function getContentBySlug($slug){
|
|
$content = CMSInfo::whereSlug(trim($slug))->first();
|
|
if($content){
|
|
switch ($content->type){
|
|
case 'text':
|
|
return $content->text;
|
|
break;
|
|
case 'full_text':
|
|
return $content->full_text;
|
|
break;
|
|
case 'integer':
|
|
return $content->integer;
|
|
break;
|
|
case 'decimal':
|
|
return $content->decimal;
|
|
break;
|
|
case 'bool':
|
|
return $content->bool;
|
|
break;
|
|
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function setContentBySlug($slug, $value, $type = "text"){
|
|
|
|
$content = CMSInfo::whereSlug(trim($slug))->first();
|
|
if(!$content) {
|
|
$content = CMSInfo::create([
|
|
'name' => $slug,
|
|
'type' => $type,
|
|
]);
|
|
}
|
|
$content->type = $type;
|
|
switch ($content->type){
|
|
case 'text':
|
|
$content->text = $value;
|
|
break;
|
|
case 'full_text':
|
|
$content->full_text = $value;
|
|
break;
|
|
case 'integer':
|
|
$content->integer = $value;
|
|
break;
|
|
case 'decimal':
|
|
$content->decimal = $value;
|
|
break;
|
|
case 'bool':
|
|
$content->bool = $value;
|
|
break;
|
|
}
|
|
|
|
$content->save();
|
|
return $content;
|
|
}
|
|
}
|