Skip to content

Commit

Permalink
Merge pull request concretecms#5177 from mlocati/simplify-groups-import
Browse files Browse the repository at this point in the history
Fix import of groups without path
  • Loading branch information
aembler committed Mar 7, 2017
2 parents 3c14ba6 + 938970e commit 5b8665b
Showing 1 changed file with 25 additions and 26 deletions.
@@ -1,7 +1,8 @@
<?php
namespace Concrete\Core\Backup\ContentImporter\Importer\Routine;

use Concrete\Core\Permission\Category;
use Concrete\Core\User\Group\Group;
use SimpleXMLElement;

class ImportGroupsRoutine extends AbstractRoutine
{
Expand All @@ -10,41 +11,39 @@ public function getHandle()
return 'groups';
}

public function import(\SimpleXMLElement $sx)
public function import(SimpleXMLElement $sx)
{
if (isset($sx->groups)) {

$groups = array();
$groups = [];
foreach ($sx->groups->group as $g) {
$groups[] = $g;
$name = (string) $g['name'];
$path = trim((string) $g['path'], '/');
$groups[] = [
'name' => $name,
'path' => ($path === '') ? "/$name" : "/$path",
'description' => (string) $g['description'],
'package' => (string) $g['package'],
];
}

usort($groups, function ($a, $b) {
$pathA = (string) $a['path'];
$pathB = (string) $b['path'];
$numA = count(explode('/', $pathA));
$numB = count(explode('/', $pathB));
if ($numA == $numB) {
return 0;
} else {
return ($numA < $numB) ? -1 : 1;
}
return count(explode('/', $a['path'])) - count(explode('/', $b['path']));
});

foreach($groups as $group) {
$existingGroup = \Concrete\Core\User\Group\Group::getByPath((string) $group['path']);
if (!is_object($existingGroup)) {
$parent = null;
if ((string) $group['path'] != '') {
$lastSlash = strrpos((string) $group['path'], '/');
$parentPath = substr((string) $group['path'], 0, $lastSlash);
if ($parentPath) {
$parent = \Concrete\Core\User\Group\Group::getByPath($parentPath);
}
foreach ($groups as $group) {
$existingGroup = Group::getByPath($group['path']);
if ($existingGroup === null) {
$pathChunks = explode('/', $group['path']);
if (count($pathChunks) === 2) {
$parentGroup = null;
} else {
array_pop($pathChunks);
$parentPath = implode('/', $pathChunks);
$parentGroup = Group::getByPath($parentPath);
}

$pkg = static::getPackageObject($g['package']);
\Concrete\Core\User\Group\Group::add((string) $group['name'], (string) $group['description'], $parent, $pkg);
$pkg = static::getPackageObject($group['package']);
Group::add($group['name'], $group['description'], $parentGroup, $pkg);
}
}
}
Expand Down

0 comments on commit 5b8665b

Please sign in to comment.