Commit 258e4b19 authored by Fabrice Gangler's avatar Fabrice Gangler 🎨
Browse files

REFACTOR(mapping): improve user controller

parent aea40d24
Pipeline #27593 passed with stage
in 4 minutes and 28 seconds
......@@ -404,9 +404,73 @@ class UsersController extends AppController
/**
* Load mapping data if available for given user ID and populate view data
* @param int $userId
* @param User $user
*/
protected function commonMappingForUser(User $user)
{
$data = $this->getMappingForUser($user);
$taxonomiesSoftware = $data['taxonomiesSoftware'];
$usedSoftwareKeys = $data['usedSoftwareKeys'];
$mappingFirstLevels = $data['mappingFirstLevels'];
$mappingTaxons = $data['mappingTaxons'];
// Populate view data
$this->set(compact(['mappingFirstLevels']));
$this->set(compact(['mappingTaxons']));
$this->set(compact(['usedSoftwareKeys']));
$this->set(compact(['taxonomiesSoftware']));
$this->set('_serialize', [
'taxonomiesSoftware',
'usedSoftwareKeys',
'mappingTaxons',
'mappingFirstLevels'
]);
}
/**
* Load mapping data if available for given user ID
* and return a array of all software declared by user for one or multiple taxon
*
* @param User $user
* @return array example: [
* 'LibreOffice' => ['softwareName' => 'LibreOffice',
* 'softwareId' => 33,
* 'modified' => '2022-06-01T11:05:53+00:00',
* 'mappingTaxons' => [['slug' => 'bureautique', 'title' => 'Bureautique],]]
*/
protected function getSofwareMappingForUser(User $user)
{
$mappingData = $this->getMappingForUser($user);
$softwareDeclaredInTaxonomy = [];
if (count($mappingData['taxonomiesSoftware']) > 0) {
foreach ($mappingData['taxonomiesSoftware'] as $taxon) {
foreach ($taxon as $softwareName => $taxonData) {
$taxonomyId = $taxonData->taxonomy_id;
$mappingTaxons = $mappingData['mappingTaxons'][$taxonomyId];
$softwareDeclaredInTaxonomy[$softwareName]['softwareName'] = $softwareName;
$softwareDeclaredInTaxonomy[$softwareName]['softwareId'] = $taxonData->software_id;
$softwareDeclaredInTaxonomy[$softwareName]['mappingTaxons'][$taxonomyId] =$mappingTaxons;
$modified = $taxonData['modified']; // Cake\I18n\Time
if (!isset($softwareDeclaredInTaxonomy[$softwareName]['modified'])) {
$softwareDeclaredInTaxonomy[$softwareName]['modified'] = $modified; // Cake\I18n\Time
} elseif ($softwareDeclaredInTaxonomy[$softwareName]['modified'] < $modified) {
$softwareDeclaredInTaxonomy[$softwareName]['modified'] = $modified; // Cake\I18n\Time
}
}
}
}
return $softwareDeclaredInTaxonomy;
}
/**
* Load mapping data if available for given user ID
*
* @param User $user
* @return array
*/
protected function getMappingForUser(User $user)
{
$taxonomiesSoftware = [];
$usedSoftwareKeys = [];
......@@ -449,23 +513,16 @@ class UsersController extends AppController
}
}
}
// Populate view data
$this->set(compact(['mappingFirstLevels']));
$this->set(compact(['mappingTaxons']));
$this->set(compact(['usedSoftwareKeys']));
$this->set(compact(['taxonomiesSoftware']));
$this->set('_serialize', [
'taxonomiesSoftware',
'usedSoftwareKeys',
'mappingTaxons',
'mappingFirstLevels'
]);
return [
'taxonomiesSoftware' => $taxonomiesSoftware,
'usedSoftwareKeys' => $usedSoftwareKeys,
'mappingFirstLevels' => $mappingFirstLevels,
'mappingTaxons' => $mappingTaxons,
];
}
/**
* Tooling for "add" method: create anti-spam tokens
* - create anti-spam tokens (get, post, form)
......
Supports Markdown
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