Commit 128df17d authored by Antoine Le Gonidec's avatar Antoine Le Gonidec

Add an alternative flat output for archives shown from scripts

Related to #15
parent 3dc602c0
......@@ -99,6 +99,24 @@ class Archive extends Model {
return $children;
}
/**
* Get a flat list of children, with no hierarchy
*
* @return array
*/
public function getFlatChildrenList() {
$children = $this->children;
$list = [];
foreach ( $children as $child ) {
$child->makeHidden([ 'children' ]);
$list[] = $child;
if ( ! empty($child->children) ) {
$list = array_merge($list, $child->getFlatChildrenList());
}
}
return $list;
}
/**
* Get list of dependencies from dedicated database table
*
......
......@@ -43,6 +43,23 @@ class ScriptsController extends Controller {
$error = 'No script found for id '.$id;
return response()->json([ 'error' => $error ], 404);
}
return response()->json($script->toArray());
if ( $request->has('archives_view') ) {
switch ( $request->get('archives_view') ) {
case 'tree':
$answer = $script->toArray();
break;
case 'flat':
$archives = $script->getArchivesFlatList();
$answer = $script->toArray();
$answer['archives'] = $archives;
break;
default:
return [ 'error' => 'Invalid value for \'archives_view\' parameter' ];
break;
}
} else {
$answer = $script->toArray();
}
return response()->json($answer);
}
}
......@@ -43,6 +43,35 @@ class Script extends Model {
return $archives;
}
/**
* Show archives as alternative groups, with no hierarchy
*
* @return array
*/
public function getArchivesFlatList() {
$archives_tree = $this->archives;
$archives_flat = [];
foreach ( $archives_tree as $group_id => $group ) {
foreach ( $group as $archive ) {
$archive->makeHidden([ 'children' ]);
$archives_group = [];
$archives_group[] = $archive;
if ( ! empty($archive->children) ) {
$children = $archive->getFlatChildrenList();
$archives_group = array_merge($archives_group, $children);
}
if ( $group_id === 0 ) {
$archives_flat[] = $archives_group;
} else {
foreach ( array_keys($archives_flat) as $key ) {
$archives_flat[$key] = array_merge($archives_flat[$key], $archives_group);
}
}
}
}
return $archives_flat;
}
/**
* Get the games supported by current script
*
......
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