'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() { 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 .= '\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, ]); } 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; } }