Commit 8e9f5370 authored by Fabrice Gangler's avatar Fabrice Gangler 🎨
Browse files

REFACTOR: use AppCacheTrait for mapping data

parent 5bc0723f
Pipeline #9962 passed with stage
in 3 minutes and 40 seconds
......@@ -5,6 +5,7 @@ namespace App\Cache;
use App\Network\Exception\RelationshipNotFoundException;
use App\Network\Exception\UserTypeNotFoundException;
use Cake\ORM\TableRegistry;
use Cake\Utility\Text;
/**
* A trait that provides methods for some database cache
......@@ -16,6 +17,86 @@ trait AppCacheTrait
*/
private $appCache = [];
/////////// Taxonomy //////////////////////////////////////////////////////////////////////////////////////////
/**
* Return mapping first levels
* @return array
*/
final public function getMappingFirstLevels(string $lang = 'en')
{
if (!isset($this->appCache['mappingFirstLevels'][$lang])
|| !array($this->appCache['mappingFirstLevels'][$lang])) {
$this->getMappingFromCache($lang);
}
return $this->appCache['mappingFirstLevels'][$lang];
}
/**
* Return mapping data
* @return array
*/
final public function getMappingTaxons(string $lang = 'en')
{
if (!isset($this->appCache['mappingTaxons'][$lang]) || !array($this->appCache['mappingTaxons'][$lang])) {
$this->getMappingFromCache($lang);
}
return $this->appCache['mappingTaxons'][$lang];
}
/**
* Populate:
* - $this->appCache['mappingTaxons'][$lang]
* - $this->appCache['mappingFirstLevels'][$lang]
*
* @param string $lang
* @return void
*/
private function getMappingFromCache(string $lang = 'en')
{
$this->initAppCache();
if (isset($this->appCache['mappingTaxons'][$lang]) && array($this->appCache['mappingTaxons'][$lang])) {
return;
}
$titlePropertie = "title_i18n_$lang";
$descPropertie = "description_i18n_$lang";
$mapping = [];
$mappingFirstLevels = [];
$registry = TableRegistry::get("Taxonomys");
$taxonomys = $registry->find('all');
foreach ($taxonomys as $taxonomy) {
$id = $taxonomy->id;
$title = $taxonomy->$titlePropertie; // property "title_i18n_fr" or "title_i18n_en"
$desc = $taxonomy->$descPropertie; // property "description_i18n_fr" or "description_i18n_en"
$mapping[$id]['slug'] = strtolower(Text::slug($title));
$mapping[$id]['title'] = $title ;
$mapping[$id]['desc'] = $desc ;
if (is_null($taxonomy->parent_id)) {
$mappingFirstLevels[$id] = $title;
} else {
$idParent = $taxonomy->parent_id;
$mapping[$id]['id_parent'] = $idParent ;
$mapping[$idParent]['children'][$id] = $title;
}
}
ksort($mappingFirstLevels);
foreach ($mapping as $id => $dataTaxon) {
if (isset($mapping[$id]['children'])) {
asort($mapping[$id]['children']);
}
}
$this->appCache['mappingFirstLevels'][$lang] = $mappingFirstLevels;
$this->appCache['mappingTaxons'][$lang] = $mapping;
}
/////////// Relationships //////////////////////////////////////////////////////////////////////////////////////////
/**
* Get relationships list
*
......@@ -23,9 +104,7 @@ trait AppCacheTrait
*/
final protected function getRelationshipsListFromCache()
{
if (!isset($this->appCache)) {
$this->appCache = [];
}
$this->initAppCache();
if (!isset($this->appCache['relationships']) || !array($this->appCache['relationships'])) {
$registry = TableRegistry::get('Relationships');
$result = $registry->find("all")->toList();
......@@ -54,6 +133,9 @@ trait AppCacheTrait
}
}
/////////// User type //////////////////////////////////////////////////////////////////////////////////////////////
/**
* Get user types list
*
......@@ -61,9 +143,7 @@ trait AppCacheTrait
*/
final protected function getUserTypesListFromCache()
{
if (!isset($this->appCache)) {
$this->appCache = [];
}
$this->initAppCache();
if (!isset($this->appCache['userTypes']) || !array($this->appCache['userTypes'])) {
$registry = TableRegistry::get('userTypes');
$result = $registry->find("all")->toList();
......@@ -91,4 +171,17 @@ trait AppCacheTrait
throw new UserTypeNotFoundException("The relationship with the cd (nameSlug) [ $name ] does not exist");
}
}
//////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
/**
* Initializes property $this->appCache, if it does not exist
*/
final protected function initAppCache()
{
if (!isset($this->appCache)) {
$this->appCache = [];
}
}
}
......@@ -218,70 +218,4 @@ class MetaTaxonomyController extends AppController
}
return $url ;
}
/**
* Return mapping first levels
* @return array
*/
final protected function getmappingFirstLevels()
{
if (!is_array($this->mappingTaxons) || count($this->mappingTaxons) === 0) {
$this->loadMappingData();
}
return $this->mappingFirstLevels;
}
/**
* Return mapping data
* @return array
*/
final protected function getMappingTaxons()
{
if (!is_array($this->mappingTaxons) || count($this->mappingTaxons) === 0) {
$this->loadMappingData();
}
return $this->mappingTaxons;
}
/**
* Populate $this->mappingHead and $this->mappingTaxons
*
* @return void
*/
private function loadMappingData()
{
$lang = $this->selectedLanguage;
$titlePropertie = "title_i18n_$lang";
$descPropertie = "description_i18n_$lang";
$mapping = [];
$mappingFirstLevels = [];
$registry = TableRegistry::get("Taxonomys");
$taxonomys = $registry->find('all');
foreach ($taxonomys as $taxonomy) {
$id = $taxonomy->id;
$title = $taxonomy->$titlePropertie; // property "title_i18n_fr" or "title_i18n_en"
$desc = $taxonomy->$descPropertie; // property "description_i18n_fr" or "description_i18n_en"
$mapping[$id]['slug'] = strtolower(Text::slug($title));
$mapping[$id]['title'] = $title ;
$mapping[$id]['desc'] = $desc ;
if (is_null($taxonomy->parent_id)) {
$mappingFirstLevels[$id] = $title;
} else {
$idParent = $taxonomy->parent_id;
$mapping[$id]['id_parent'] = $idParent ;
$mapping[$idParent]['children'][$id] = $title;
}
}
ksort($mappingFirstLevels);
foreach ($mapping as $id => $dataTaxon) {
if (isset($mapping[$id]['children'])) {
asort($mapping[$id]['children']);
}
}
$this->mappingFirstLevels = $mappingFirstLevels;
$this->mappingTaxons = $mapping;
}
}
......@@ -78,8 +78,8 @@ class TaxonomysController extends CommonTaxonomyController
}
// Get data
$mappingTaxons = $this->getMappingTaxons();
$mappingHead = $this->getmappingFirstLevels();
$mappingTaxons = $this->getMappingTaxons($this->selectedLanguage);
$mappingHead = $this->getMappingFirstLevels($this->selectedLanguage);
// Parameters for the view
$this->set(compact('mappingTaxons'));
......@@ -112,8 +112,8 @@ class TaxonomysController extends CommonTaxonomyController
public function mappingPrimaryLevel($primaryId = null)
{
// Get data
$mappingTaxons = $this->getMappingTaxons();
$mappingHead = $this->getmappingFirstLevels();
$mappingTaxons = $this->getMappingTaxons($this->selectedLanguage);
$mappingHead = $this->getMappingFirstLevels($this->selectedLanguage);
// Get the ID from the URL slug
$primaryId = null;
......@@ -184,8 +184,8 @@ class TaxonomysController extends CommonTaxonomyController
public function mappingTaxon($taxonId = null)
{
// Get data
$mappingTaxons = $this->getMappingTaxons();
$mappingHead = $this->getmappingFirstLevels();
$mappingTaxons = $this->getMappingTaxons($this->selectedLanguage);
$mappingHead = $this->getMappingFirstLevels($this->selectedLanguage);
// Get the ID from the URL
$taxonId = null;
......@@ -266,7 +266,7 @@ class TaxonomysController extends CommonTaxonomyController
public function mappingTaxonUsersOf()
{
// Get data
$mappingTaxons = $this->getMappingTaxons();
$mappingTaxons = $this->getMappingTaxons($this->selectedLanguage);
$mappingHead = $this->getMappingFirstLevels();
// Get the IDs from the URL
......
......@@ -64,8 +64,8 @@ class TaxonomysSoftwaresController extends CommonTaxonomySoftwareController
public function mappingForm($softwareId = null)
{
// Get taxonomy data
$mappingTaxons = $this->getMappingTaxons();
$mappingHead = $this->getmappingFirstLevels();
$mappingTaxons = $this->getMappingTaxons($this->selectedLanguage);
$mappingHead = $this->getmappingFirstLevels($this->selectedLanguage);
// Get user
$user = TableRegistry::get("Users")->get($this->Auth->user('id'));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment