File: D:/HostingSpaces/centrum8a/centrum8a.com/app/KommaApp/Kms/Core/Entities/KmsTranslatableEntity.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core\Entities;
abstract class KmsTranslatableEntity extends KmsEntity
{
public $translations = [];
public function addTranslationEntity(KmsTranslationEntity $entity)
{
$this->translations[$entity->getLanguageId()] = $entity;
}
public function getValue($key)
{
// If the key is found in $this object, return it
if( ! $value = parent::getValue($key))
{
// Extract the language id from the key name ($key: 'key_languageId' eg. 'name_104')
$key = explode('_', $key);
$languageId = null;
if( is_numeric( $key[count($key)-1] ) )
$languageId = array_pop($key);
$key = implode('_', $key);
// Find the key in the translations and return the value if found
foreach($this->translations as $translation)
{
if($languageId == $translation->getLanguageId())
{
return $translation->getValue($key);
}
}
}
return $value;
}
public function setValue($key, $value)
{
if( ! $savedValue = parent::setValue($key, $value))
{
// Extract the language id from the key name ($key: 'key_languageId' eg. 'name_104')
$key = explode('_', $key);
$languageId = null;
if( is_numeric( $key[count($key)-1] ) )
$languageId = array_pop($key);
$key = implode('_', $key);
// Find the key in the translations and return the value if found
foreach($this->translations as $translation)
{
if($languageId == $translation->getLanguageId())
{
return $translation->setValue($key, $value);
}
}
return null;
}
return $savedValue;
}
public function getTranslations()
{
return $this->translations;
}
public function hasTranslationWithId($id)
{
return isset($this->translations[$id]);
}
public function getName($languageId = null)
{
if(count($this->translations) > 0) {
if (!$languageId) {
reset($this->translations);
return $this->translations[key($this->translations)]->getName();
}
return $this->translations[$languageId]->getName();
}
return 'None';
}
public function getThumbnail($languageId = null)
{
if(isset($this->thumbnail))
{
return '<img src="'.$this->thumbnail.'"/>';
}
return substr($this->getName($languageId), 0, 1);
}
public function getKeyInOtherLanguages($key, $excludedLanguageId = null, $siteId = null)
{
$excludedLanguageId = $excludedLanguageId ? [$excludedLanguageId] : [];
$sitePart = $siteId ? '_'.$siteId : '';
return $this->stringWithIdsSeparatedByCommas($key.'_[[id]]'.$sitePart, $excludedLanguageId);
}
public function stringWithIdsSeparatedByCommas($template = '[[id]]', array $excludeIds = [])
{
$output = [];
$ids = $this->getIdsOfTranslations($excludeIds);
foreach($ids as $id)
{
$output[] = str_replace('[[id]]', $id, $template);
}
return implode(',', $output);
}
protected function getIdsOfTranslations(array $excludeIds = [])
{
$translationIds = [];
foreach($this->translations as $translationId => $translation)
{
if(!in_array($translationId, $excludeIds))
$translationIds[] = $translationId;
}
return $translationIds;
}
}