Skip to content

Commit

Permalink
feat!(export): export categorized keywords for apps (#84)
Browse files Browse the repository at this point in the history
* feat!(export): export categorized keywords for apps

BREAKING CHANGE: export for apps returns JSON containing categorized keywords instead of fixed set of categories

* fix(export): duplicate field for Discord
  • Loading branch information
Fenrikur authored Jul 16, 2024
1 parent 90642e7 commit 6b9b724
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/Applications/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ public function exportAppData()
}

foreach ($applications as $row) {
if (!is_array($row) && !empty($row->Keywords)) {
$categorizedKeywords = array_reduce(explode('$$', $row->Keywords), function ($categorizedKeywords, $categoryKeyword) {
list($category, $keyword) = explode('::', $categoryKeyword, 2);
if (array_key_exists($category, $categorizedKeywords)) {
array_push($categorizedKeywords[$category], $keyword);
} else {
$categorizedKeywords[$category] = [$keyword];
}
return $categorizedKeywords;
}, array());
$row->Keywords = json_encode($categorizedKeywords);
}
fputcsv($csvFile, (array)$row, $separator);
}
fflush($csvFile);
Expand Down
21 changes: 13 additions & 8 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public static function getAllApplicationsForExport(): \Illuminate\Support\Collec
$keywords = Profile::query()->toBase()
->leftJoin('keyword_profile', 'keyword_profile.profile_id', '=', 'profiles.id')
->leftJoin('keywords', 'keywords.id', '=', 'keyword_profile.keyword_id')
->leftJoin('categories', 'keywords.id', '=', 'categories.id')
->leftJoin('categories', 'keywords.category_id', '=', 'categories.id')
->groupBy('profiles.id')
->select(DB::raw('GROUP_CONCAT(`keywords`.`name` SEPARATOR \',\') AS `keywords`, GROUP_CONCAT(`categories`.`name` SEPARATOR \',\') AS `categories`, `profiles`.`id` AS `profile_id`'));

Expand Down Expand Up @@ -395,9 +395,19 @@ public static function getAllApplicationsForExport(): \Illuminate\Support\Collec

public static function getAllApplicationsForAppExport(): \Illuminate\Support\Collection
{
$keywords = Profile::query()->toBase()
->leftJoin('keyword_profile', 'keyword_profile.profile_id', '=', 'profiles.id')
->leftJoin('keywords', 'keywords.id', '=', 'keyword_profile.keyword_id')
->leftJoin('categories', 'keywords.category_id', '=', 'categories.id')
->groupBy('profiles.id')
->select(DB::raw('GROUP_CONCAT(CONCAT_WS(\'::\', `categories`.`name`, `keywords`.`name`) SEPARATOR \'$$\') AS `categorized_keywords`, `profiles`.`id` AS `profile_id`'));

$applications = self::query()->toBase()
->leftJoin('profiles', 'applications.id', '=', 'profiles.application_id')
->leftJoin('users', 'user_id', '=', 'users.id')
->leftJoinSub($keywords, 'profile_keywords', function (JoinClause $join) {
$join->on('profiles.id', '=', 'profile_keywords.profile_id');
})
->select(
'applications.id AS Reg No.',
'users.name AS Nick',
Expand All @@ -414,20 +424,15 @@ public static function getAllApplicationsForAppExport(): \Illuminate\Support\Col
'art_desc AS About the Art',
'profiles.website as Website',
'twitter as Twitter',
'discord as Discord',
'mastodon as Mastodon',
'bluesky as Bluesky',
'telegram as Telegram',
'art_preview_caption as Art Preview Caption',
DB::raw("CASE WHEN image_thumbnail IS NOT NULL THEN 'X' ELSE '' END AS 'ThumbailImg'"),
DB::raw("CASE WHEN image_artist IS NOT NULL THEN 'X' ELSE '' END AS 'ArtistImg'"),
DB::raw("CASE WHEN image_art IS NOT NULL THEN 'X' ELSE '' END AS 'ArtImg'"),
// TODO: Export keywords and categories once supported in app backend
DB::raw("'X' AS 'Cat. Prints'"),
DB::raw("'X' AS 'Cat. Artwork'"),
DB::raw("'X' AS 'Cat. Fursuit'"),
DB::raw("'X' AS 'Cat. Commissions'"),
DB::raw("'X' AS 'Cat. Misc'"),
'discord as Discord',
'profile_keywords.categorized_keywords as Keywords',
'tweet as Tweet',
'table_number as Table Number',
'type as Type',
Expand Down

0 comments on commit 6b9b724

Please sign in to comment.