File: D:/HostingSpaces/Neopoints/momsecurity.be/vendor/komma/kms/src/Core/Entities/DisplayNameTrait.php
<?php
namespace Komma\KMS\Core\Entities;
use Illuminate\Support\Facades\Lang;
use Komma\KMS\Core\AbstractTranslatableModel;
use Illuminate\Support\Str;
use Komma\KMS\Helpers\KommaHelpers;
trait DisplayNameTrait
{
/**
* Get the name of this model by model or translation model
*
* @return null|string
*/
public function getDisplayName():?string
{
// If it is a new model the section name will filled by model.section.new
if(!$this->exists) return null;
// First try to get the name of the model
// if(isset($this->name) && $this->name != '') { //When you do an isset like this, you cannot use a magic method like in orders to set and return the name of a model
if(isset($this->name) && $this->name != '') {
return $this->name;
}
// Else detect if the model is a translatableModel
elseif(is_a($this, AbstractTranslatableModel::class)){
if(isset($this->translations) && $this->translations->count() != 0) $modelTranslation = $this->translations->first();
if(isset($modelTranslation) && isset($modelTranslation->name) && $modelTranslation->name != '')
return $modelTranslation->name;
}
return null;
}
/**
* @return string
*/
public function getSidebarName():string
{
// If there is no name defined generate a generic name
if(!$sidebarName = $this->getDisplayName()){
$translationKey = Str::plural(KommaHelpers::getShortNameFromClass($this, true)).'.entity';
if(Lang::has('shop/'.$translationKey)) $sidebarName = __('shop/'.$translationKey);
else $sidebarName = __('KMS::'.$translationKey);
$sidebarName .= ' '.$this->id;
}
else $sidebarName = KommaHelpers::str_limit_full_word($sidebarName, 75);
return $sidebarName;
}
}