mein-sterntours/app/Models/CMSContent.php
2025-04-01 10:40:14 +02:00

282 lines
9.3 KiB
PHP

<?php
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\CMSContent
*
* @property int $id
* @property string $name
* @property string $slug
* @property string|null $identifier
* @property string $field
* @property string|null $text
* @property string|null $full_text
* @property array|null $object
* @property int|null $integer
* @property string|null $decimal
* @property int|null $pos
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent findSimilarSlugs(string $attribute, array $config, string $slug)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent query()
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereDecimal($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereField($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereFullText($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereIdentifier($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereInteger($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereObject($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereSlug($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereText($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|CMSContent withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug)
* @mixin \Eloquent
*/
class CMSContent extends Model
{
use Sluggable;
protected static $fields = [
'text' => '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(): array
{
return [
'slug' => [
'source' => 'name'
]
];
}
public function setNameAttribute($value){
$this->attributes['name'] = $value == '' ? '#empty#' : $value;
}
public function getNameWithEmpty(){
if(!$this->attributes['name']){ return ""; }
return $this->attributes['name'] === '#empty#' ? '' : $this->attributes['name'];
}
public static function getFieldsOptions($setKey = false){
$options = self::$fields;
$ret = "";
foreach ($options as $key => $option){
$attr = ($key == $setKey) ? 'selected="selected"' : '';
$ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
}
return $ret;
}
public function getFieldName(){
return isset(self::$fields[$this->field]) ? self::$fields[$this->field] : '';
}
public function getPreviewContent(){
if($this->isFile()){
$content = '<i class="'.$this->getIconExt().'"></i> <a href="'.$this->getURL().'" target="_blank">Vorschau | '.$this->formatBytes().'</a>';
}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 findObjectsBy($key, $value){
$find = '"'.$key.'":"'.$value.'"';
return CMSContent::where('object', 'LIKE', '%'.$find.'%')->get();
}
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();
}
}