Last active
June 8, 2019 02:55
-
-
Save yus-ham/411618819fdde2cc3bf5e206d9cc6ba5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class xxx { | |
// To convert path string into multi-dimensional array compatible for jstree jquery plugin | |
public function getAllPermissions() { | |
$perms = [ | |
'usupha/repos', | |
'usupha/repos/new', | |
'usupha/projects', | |
'usupha/stars', | |
]; | |
$this->allPerms = $this->buildPerms($perms); | |
// [ | |
// 'text' => 'usupha', | |
// 'children' => [ | |
// [ | |
// 'text' => 'repos', | |
// 'children' => [ | |
// [ | |
// 'text' => 'new' | |
// ], | |
// ], | |
// ], | |
// [ | |
// 'text' => 'projects', | |
// ], | |
// [ | |
// 'text' => 'repos', | |
// ] | |
// ] | |
// ] | |
return $this->allPerms; | |
} | |
protected function buildPerms($perms) { | |
$indexes = []; | |
$allPerms = []; | |
foreach ($perms as $perm) { | |
$pathParts = explode('/', $perm['name']); | |
$this->buildPermChildren($pathParts, $indexes, $allPerms, $perm); | |
} | |
return $allPerms; | |
} | |
// | |
protected function buildPermChildren($pathParts, &$indexes, &$allPerms, $model) { | |
$first = array_shift($pathParts); | |
if (empty($indexes[$first])) { | |
$indexes[$first]['children'] = []; | |
$indexes[$first]['idx'] = count($indexes)-1; | |
} | |
$idx = $indexes[$first]['idx']; | |
// jstree data | |
$allPerms[$idx]['text'] = $first; | |
$allPerms[$idx]['data']['path'] = $model['name']; | |
if (!empty($pathParts)) { | |
$perms = @$allPerms[$idx]['children'] ?: []; | |
$this->buildPermChildren($pathParts, $indexes[$first]['children'], $perms, $model); | |
$allPerms[$idx]['children'] = $perms; | |
} elseif ($model['is_assigned']) { | |
// jstree data | |
$allPerms[$idx]['state']['checked'] = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment