Commit 1a08e356 authored by Fabrice Gangler's avatar Fabrice Gangler 🎨 Committed by Matthieu FAURE
Browse files

FEAT(mapping): prepare diplays for software and users pages

Refs: #918 #917
parent 994099be
Pipeline #9974 passed with stage
in 3 minutes and 43 seconds
......@@ -21,8 +21,8 @@ trait AppCacheTrait
/////////// Taxonomy //////////////////////////////////////////////////////////////////////////////////////////
/**
* Return mapping first levels
* @return array
* Returns mapping first levels
* @return array example: [<taxonId> => <taxonName>, ...]
*/
final public function getMappingFirstLevels(string $lang = 'en')
{
......@@ -34,7 +34,17 @@ trait AppCacheTrait
}
/**
* Return mapping data
* Returns mapping data
* example:
* [ <taxonId_1> => [ 'slug' => <taxonSlug>,
* 'title' => <taxonTitle>,
* 'desc => <description>,
* 'children' => [<taxonId> => <taxonName>, ...], ]
* <taxonId_4> => [ 'slug' => <taxonSlug>,
* 'title' => <taxonTitle>,
* 'desc => <description>,
* 'id_parent' => <taxonParenId> ]
*
* @return array
*/
final public function getMappingTaxons(string $lang = 'en')
......@@ -45,6 +55,56 @@ trait AppCacheTrait
return $this->appCache['mappingTaxons'][$lang];
}
/**
* Returns mapping data, filtered from provided taxon IDs.
* Parent taxons are included.
*
* example:
* [ <taxonId_1> => [ 'slug' => <taxonSlug>,
* 'title' => <taxonTitle>,
* 'desc => <description>,
* 'children' => [<taxonId> => <taxonName>, ...], ]
* <taxonId_4> => [ 'slug' => <taxonSlug>,
* 'title' => <taxonTitle>,
* 'desc => <description>,
* 'id_parent' => <taxonParenId> ]
*
* @param array $taxonIds example: [<taxonId_4>, <taxonId_5>, ...]
* @param string $lang (optional) language code, by default it's 'en'
* @return array
*/
final public function getMappingTaxonsWithTaxonIdsFilter(array $taxonIds, string $lang = 'en')
{
$taxonIds = array_flip($taxonIds);
$data = $this->getMappingTaxons($lang);
$filteredData = [];
$parentList = [];
// Filtring except first level taxons
foreach ($data as $taxonId => $taxonData) {
if (!isset($taxonData['id_parent'])) { // First level
$parentList[$taxonId] = $taxonData;
} elseif (isset($taxonIds[$taxonId])) {
$filteredData[$taxonId] = $taxonData;
}
}
// Filtring for first level taxons
foreach ($parentList as $parentId => $parentData) {
foreach ($parentData['children'] as $childId => $child) {
if (!isset($taxonIds[$childId])) {
unset($parentList[$parentId]['children'][$childId]);
}
}
if (count($parentList[$parentId]['children']) === 0) {
unset($parentList[$parentId]);
}
}
$filteredData = $parentList + $filteredData;
return $filteredData;
}
/**
* Populate:
* - $this->appCache['mappingTaxons'][$lang]
......
......@@ -319,6 +319,9 @@ class SoftwaresController extends AppController
]
);
// Load mapping data if available for current software and populate view data
$this->commonMappingForCurrentSoftware($software->id);
$this->set(compact(['software']));
$this->set('_serialize', ['software']);
......@@ -359,6 +362,32 @@ class SoftwaresController extends AppController
}
}
/**
* Load mapping data if available for given software ID and populate view data
* @param int $softwareId
*/
protected function commonMappingForCurrentSoftware(int $softwareId)
{
// Get taxonomy records grouped by taxon ID for this software
$this->loadModel("TaxonomysSoftwares");
$taxonomiesSoftware = $this->TaxonomysSoftwares->getListBySofwareId($softwareId);
// Load mapping data if necessary
$mappingFirstLevels = [];
$mappingTaxons = [];
if (count($taxonomiesSoftware) > 0) {
$taxonIds = array_keys($taxonomiesSoftware);
$mappingFirstLevels = $this->getMappingFirstLevels($this->selectedLanguage);
$mappingTaxons = $this->getMappingTaxonsWithTaxonIdsFilter($taxonIds, $this->selectedLanguage);
}
// Populate view data
$this->set(compact(['mappingFirstLevels']));
$this->set(compact(['mappingTaxons']));
$this->set(compact(['taxonomiesSoftware']));
$this->set('_serialize', ['taxonomiesSoftware','mappingTaxons','mappingFirstLevels']);
}
/**
* Add method
*
......
......@@ -97,7 +97,7 @@ class TaxonomysSoftwaresController extends CommonTaxonomySoftwareController
}
// Get already existing associations between the user, the software and the taxonomies
$existingEntries = $this->TaxonomysSoftwares->getObjsListByUserIdBySofwareId($userId, $softwareId);
$existingEntries = $this->TaxonomysSoftwares->getListByUserIdBySofwareId($userId, $softwareId);
// example: Array( <taxonId> => taxonomySoftware, ...)
// Form processing
......
......@@ -368,6 +368,9 @@ class UsersController extends AppController
return $this->redirect("$allowedUrl", 301);
}
// Load mapping data if available for current user and populate view data
$this->commonMappingForCurrentUser($user->id);
//For Social MEDIAS => OPENGRAPH
$openGraph = [
"title" => $user->username,
......@@ -398,6 +401,33 @@ class UsersController extends AppController
$this->setBreadcrumbsUser($links, $user);
}
/**
* Load mapping data if available for given user ID and populate view data
* @param int $userId
*/
protected function commonMappingForCurrentUser(int $userId)
{
// Get taxonomy records grouped by taxon ID for this user
$this->loadModel("TaxonomysSoftwares");
$taxonomiesSoftware = $this->TaxonomysSoftwares->getListByUserId($userId);
// Load mapping data if necessary
$mappingFirstLevels = [];
$mappingTaxons = [];
if (count($taxonomiesSoftware) > 0) {
$taxonIds = array_keys($taxonomiesSoftware);
$mappingFirstLevels = $this->getMappingFirstLevels($this->selectedLanguage);
$mappingTaxons = $this->getMappingTaxonsWithTaxonIdsFilter($taxonIds, $this->selectedLanguage);
}
// Populate view data
$this->set(compact(['mappingFirstLevels']));
$this->set(compact(['mappingTaxons']));
$this->set(compact(['taxonomiesSoftware']));
$this->set('_serialize', ['taxonomiesSoftware','mappingTaxons','mappingFirstLevels']);
}
/**
* Add method
*
......
......@@ -281,34 +281,77 @@ class TaxonomysSoftwaresTable extends Table
/**
* Return a records list
* Returns a records list (with user ID not NULL)
* grouped by Taxon for given software ID
*
* @param int $softwareId
* @return array example: [ <taxonId> => [taxonomySoftware, ...], ]
*/
public function getListBySofwareId(int $softwareId)
{
$existingEntries = $this->find()->where(['software_id' => $softwareId, 'user_id IS NOT NULL'])->toArray();
return $this->getRecordsGroupByTaxonId($existingEntries);
}
/**
* Returns a records list grouped by Taxon
* for given user ID
*
* @param int $userId
* @return array example: [ <taxonId> => [taxonomySoftware, ...], ]
*/
public function getListByUserId(int $userId)
{
$existingEntries = $this->find()->where(['user_id' => $userId,])->toArray();
return $this->getRecordsGroupByTaxonId($existingEntries);
}
/**
* Returns a records list grouped by Taxon
* for given user ID and given software ID
*
* @param int $userId
* @param int $softwareId
* @return array example: [ <taxonId> => taxonomySoftware, ...]
* @return array example: [ <taxonId> => [taxonomySoftware, ...], ]
*/
public function getObjsListByUserIdBySofwareId(int $userId, int $softwareId)
public function getListByUserIdBySofwareId(int $userId, int $softwareId)
{
$existingEntries = $this->find()->where(['software_id' => $softwareId, 'user_id' => $userId])->toList();
$existingEntriesByTaxonId = [];
foreach ($existingEntries as $existingEntry) {
$existingEntriesByTaxonId[$existingEntry->taxonomy_id] = $existingEntry;
return $this->getRecordsGroupByTaxonId($existingEntries);
}
/**
* Returns a records list grouped by Taxon
* from records list
*
* @param $records example: [ taxonomySoftware, ...]
* @return array example: [ <taxonId> => [taxonomySoftware, ...], ]
*/
final private function getRecordsGroupByTaxonId($records)
{
$recordsGroupByTaxonId = [];
if (is_array($records) && count($records) > 0) {
foreach ($records as $record) {
$recordsGroupByTaxonId[$record->taxonomy_id][] = $record;
}
}
return $existingEntriesByTaxonId;
return $recordsGroupByTaxonId;
}
/**
* Delete a records list
*
* @param array $recordsList example: [ taxonomySoftware, ...]
* @return bool
* @param array $recordsList example: [ <taxonId> => [taxonomySoftware, ...], ]
* @return bool returns false if one of records could not be deleted.
*/
public function deleteRecords(array $recordsList)
{
$processing = true;
foreach ($recordsList as $obj) {
if (false === $this->delete($obj)) {
$processing = false;
foreach ($recordsList as $recordsListForOneTaxon) {
foreach ($recordsListForOneTaxon as $obj) {
if (false === $this->delete($obj)) {
$processing = false;
}
}
}
return $processing;
......
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