'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', 'field', 'text', 'full_text', 'integer', 'decimal',
];
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);
break;
case 'integer':
return $this->integer;
break;
case 'decimal':
return $this->decimal;
break;
}
}
//FILE ------------------------
public function isFile(){
return $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($file->{$key}) ? $file->{$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 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 \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();
}
}