Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Comptoir
Comptoir-srv
Commits
1a08e356
Commit
1a08e356
authored
Jun 09, 2020
by
Fabrice Gangler
🎨
Committed by
Matthieu FAURE
Jun 10, 2020
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
Changes
5
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
src/Cache/AppCacheTrait.php
View file @
1a08e356
...
...
@@ -21,8 +21,8 @@ trait AppCacheTrait
/////////// Taxonomy //////////////////////////////////////////////////////////////////////////////////////////
/**
* Return mapping first levels
* @return array
* Return
s
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]
...
...
src/Controller/Api/V1/SoftwaresController.php
View file @
1a08e356
...
...
@@ -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
*
...
...
src/Controller/Api/V1/TaxonomysSoftwaresController.php
View file @
1a08e356
...
...
@@ -97,7 +97,7 @@ class TaxonomysSoftwaresController extends CommonTaxonomySoftwareController
}
// Get already existing associations between the user, the software and the taxonomies
$existingEntries
=
$this
->
TaxonomysSoftwares
->
get
Objs
ListByUserIdBySofwareId
(
$userId
,
$softwareId
);
$existingEntries
=
$this
->
TaxonomysSoftwares
->
getListByUserIdBySofwareId
(
$userId
,
$softwareId
);
// example: Array( <taxonId> => taxonomySoftware, ...)
// Form processing
...
...
src/Controller/Api/V1/UsersController.php
View file @
1a08e356
...
...
@@ -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
*
...
...
src/Model/Table/TaxonomysSoftwaresTable.php
View file @
1a08e356
...
...
@@ -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
get
Objs
ListByUserIdBySofwareId
(
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
$
existingEntries
ByTaxonId
;
return
$
recordsGroup
ByTaxonId
;
}
/**
* 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
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment