File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Kms/Core/Entities/KmsSiteableEntity.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core\Entities;
abstract class KmsSiteableEntity extends KmsEntity
{
public $sites = [];
public function addSiteEntity(KmsSiteEntity $entity)
{
$this->sites[$entity->getSiteId()] = $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);
$siteId = null;
if( is_numeric( $key[count($key)-1] ) )
$siteId = array_pop($key);
$key = implode('_', $key);
// Find the key in the translations and return the value if found
foreach($this->sites as $site)
{
if($siteId == $site->getSiteId())
{
return $site->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_siteId' eg. 'name_104')
$key = explode('_', $key);
$siteId = null;
if( is_numeric( $key[count($key)-1] ) )
$siteId = array_pop($key);
$key = implode('_', $key);
// Find the key in the translations and return the value if found
foreach($this->sites as $site)
{
if($siteId == $site->getSiteId())
{
return $site->setValue($key, $value);
}
}
return null;
}
return $savedValue;
}
public function getSites()
{
return $this->sites;
}
public function hasSiteWithId($id)
{
return isset($this->sites[$id]);
}
public function getName($siteId = null, $languageId = null)
{
if(!$siteId){
reset($this->sites);
return $this->sites[key($this->sites)]->getName($languageId);
}
return $this->sites[$siteId]->getName($languageId);
}
public function getThumbnail($siteId = null, $languageId = null)
{
if(isset($this->thumbnail))
{
return '<img src="'.$this->thumbnail.'"/>';
}
return substr($this->getName($siteId, $languageId), 0, 1);
}
public function getKeyInOtherLanguages($key, $excludedLanguageId = null, $siteId = null)
{
return $this->sites[$siteId]->getKeyInOtherLanguages($key, $excludedLanguageId, $siteId);
}
}