'Text (190 Zeichen)', 'full_text' => 'Full Text (50K Zeichen)', 'file' => 'Datei (PDF/JPG/PNG)', 'integer' => 'Zahl (10 Stellen)', 'decimal' => 'Kommazahl (10,2 Stellen)', ]; public static $icon_ext = [ 'default' => 'fa fa-file', 'pdf'=> 'fa fa-file-pdf', 'jpg'=> 'fa fa-file-image', 'png'=> 'fa fa-file-image', ]; protected $connection = 'mysql_stern'; protected $table = 'c_m_s_contents'; protected $fillable = [ 'name', 'slug', 'identifier', 'field', 'text', 'full_text', 'object', 'integer', 'decimal', 'pos', ]; protected $casts = [ 'object' => 'array', 'pos' => 'int' ]; public function sluggable() { return [ 'slug' => [ 'source' => 'name' ] ]; } public static function getFieldsOptions($setKey = false){ $options = self::$fields; $ret = ""; foreach ($options as $key => $option){ $attr = ($key == $setKey) ? 'selected="selected"' : ''; $ret .= '\n'; } return $ret; } public function getFieldName(){ return isset(self::$fields[$this->field]) ? self::$fields[$this->field] : ''; } public function getPreviewContent(){ if($this->isFile()){ $content = ' Vorschau | '.$this->formatBytes().''; }else{ $content = $this->{$this->field}; if(strlen($content) > 40){ return substr($content, 0, 40)." ..."; } } return $content; } public function getContent(){ switch ($this->field){ case 'text': return $this->text; break; case 'full_text': return $this->full_text; break; case 'file': // return \GuzzleHttp\json_decode($this->full_text); return $this->object; break; case 'array': return $this->object; break; case 'integer': return $this->integer; break; case 'decimal': return $this->decimal; break; } } //FILE ------------------------ public function isFile(){ return $this->id > 0 && $this->field === 'file' ? true : false; } /*'identifier', 'filename', 'dir', 'original_name', 'ext', 'mine', 'size'*/ public function getFileContent($key= false){ if($key && $this->isFile()){ // $file = \GuzzleHttp\json_decode($this->full_text); return isset($this->object[$key]) ? $this->object[$key] : false; } return false; } public function getArrayContent($key= false){ if($key && $this->field === 'array'){ // $file = \GuzzleHttp\json_decode($this->full_text); return isset($this->object[$key]) ? $this->object[$key] : false; } return false; } public function getIconExt(){ $ext = $this->getFileContent('ext'); return isset(self::$icon_ext[$ext]) ? self::$icon_ext[$ext] : self::$icon_ext['default']; } public function getURL($do = false){ return route('storage_file', [$this->id, 'cms_file', $do]); } public function getPath(){ $dir = $this->getFileContent('dir'); $filename = $this->getFileContent('filename'); return \Storage::disk('public')->path($dir.$filename); } public function formatBytes($precision = 2) { $size = $this->getFileContent('size'); if ($size > 0) { $size = (int) $size; $base = log($size) / log(1024); $suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB'); return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; } return $size; } public function deleteFile(){ $dir = $this->getFileContent('dir'); $filename = $this->getFileContent('filename'); return \Storage::disk('public')->delete($dir.$filename); } //end FILE ------------------------ 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'] = floatval(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'] = intval($value); } public function getObjectBy($key){ return isset($this->object[$key]) ? $this->object[$key] : false; } public function setObjectBy($key, $value){ $obj = $this->object; $obj[$key] = $value; $this->object = $obj; } public static function getContentBySlug($slug){ $CMSContent = CMSContent::whereSlug(trim($slug))->first(); if($CMSContent){ switch ($CMSContent->field){ case 'text': return $CMSContent->text; break; case 'full_text': return $CMSContent->full_text; break; case 'file': return $CMSContent->object; //\GuzzleHttp\json_decode($CMSContent->full_text); break; case 'array': return $CMSContent->object; //\GuzzleHttp\json_decode($CMSContent->full_text); break; case 'integer': return $CMSContent->integer; break; case 'decimal': return $CMSContent->decimal; break; } } return false; } public static function getModelBySlug($slug){ return CMSContent::whereSlug(trim($slug))->first(); } }