summaryrefslogtreecommitdiffstats
path: root/lib/client/kolab_client_task_ou.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/client/kolab_client_task_ou.php')
-rw-r--r--lib/client/kolab_client_task_ou.php159
1 files changed, 159 insertions, 0 deletions
diff --git a/lib/client/kolab_client_task_ou.php b/lib/client/kolab_client_task_ou.php
index 67c1df3..e1d80a8 100644
--- a/lib/client/kolab_client_task_ou.php
+++ b/lib/client/kolab_client_task_ou.php
@@ -78,6 +78,55 @@ class kolab_client_task_ou extends kolab_client_task
$this->output->set_object('taskcontent', $output);
}
+ /**
+ * OU list result handler, tree list builder
+ */
+ protected function list_result_handler($result, $head, $foot, $table_class)
+ {
+ $result = $this->ou_list_sort($result);
+ $result = $this->ou_list_build_tree($result, $max_tree_level);
+
+ foreach ($result as $idx => $item) {
+ $class = array('selectable');
+ $tree = '';
+ $style = '';
+
+ if ($item['level']) {
+ $tree .= '<span class="level" style="width:' . ($item['level'] * 16) . 'px"></span>';
+ $style = 'display:none';
+ }
+
+ if ($item['has_children']) {
+ $tree .= '<span class="expando"></span>';
+ }
+ else if ($max_tree_level) {
+ $tree .= '<span class="spacer"></span>';
+ }
+
+ $i++;
+ $cells = array();
+ $cells[] = array(
+ 'class' => 'name',
+ 'body' => $tree . kolab_html::escape($item['name']),
+ 'onclick' => "kadm.command('ou.info', '$idx')",
+ );
+
+ $rows[] = array(
+ 'id' => $i,
+ 'class' => implode(' ', $class),
+ 'cells' => $cells,
+ 'style' => $style,
+ 'data-level' => !empty($item['level']) ? $item['level'] : '',
+ );
+ }
+
+ if ($max_tree_level) {
+ $this->output->command('tree_list_init');
+ }
+
+ return array($rows, $head, '', $table_class . ' tree');
+ }
+
private function ou_form($attribs, $data = array())
{
if (empty($attribs['id'])) {
@@ -95,6 +144,7 @@ class kolab_client_task_ou extends kolab_client_task
'type_id' => 'system',
'type_id_name' => 'system',
'ou' => 'system',
+ 'base_dn' => 'system',
'description' => 'system',
);
@@ -122,6 +172,17 @@ class kolab_client_task_ou extends kolab_client_task
$fields['type_id']['type'] = kolab_form::INPUT_HIDDEN;
}
+ $ou_roots = array_merge(array(''), $this->ou_list());
+
+ // Add OU root selector
+ $fields['base_dn'] = array(
+ 'section' => 'system',
+ 'type' => kolab_form::INPUT_SELECT,
+ 'options' => $ou_roots,
+ 'escaped' => true,
+ 'default' => $data['base_dn'],
+ );
+
// Create mode
if ($add_mode) {
// Page title
@@ -146,4 +207,102 @@ class kolab_client_task_ou extends kolab_client_task
return $form->output();
}
+
+ private function ou_list()
+ {
+ $result = $this->api_get('ous.list', null, array('page_size' => 999));
+ $list = (array) $result->get('list');
+
+ $sorted = $this->ou_list_sort($list);
+
+ return $this->ou_list_build($sorted);
+ }
+
+ private function ou_list_sort($list)
+ {
+ // build the list for sorting
+ foreach (array_keys($list) as $dn) {
+ $name = kolab_utils::dn2ufn($dn);
+ $name = explode(',', $name);
+
+ $sort_name = array_pop($name) . ',' . implode(',', array_reverse($name));
+
+ $list[$dn] = mb_strtolower($sort_name);
+ }
+
+ // sort
+ asort($list, SORT_LOCALE_STRING);
+
+ return $list;
+ }
+
+ private function ou_list_build($list)
+ {
+ // @TODO: this code assumes parent always exist
+ foreach (array_keys($list) as $dn) {
+ $item = $this->parse_dn($dn);
+ $list[$dn] = str_repeat('&nbsp;&nbsp;&nbsp;', $item['level']) . kolab_html::escape($item['name']);
+ }
+
+ return $list;
+ }
+
+ /**
+ * Builds a tree from OUs list
+ */
+ private function ou_list_build_tree($list, &$max_level)
+ {
+ $last = '';
+ $result = array();
+
+ foreach (array_keys($list) as $dn) {
+ $item = $this->parse_dn($dn);
+
+ if ($item['level']) {
+ // create a parent unit entry if it does not exist (search result)
+ $parent = $this->domain_dn($item['domain']);
+
+ foreach ($item['path'] as $sub) {
+ $parent = "ou=$sub,$parent";
+ if (!isset($result[$parent])) {
+ $result[$parent] = $this->parse_dn($parent);
+ $last = $parent;
+ }
+
+ $result[$parent]['has_children'] = true;
+ }
+ }
+
+ $result[$dn] = $item;
+ $last = $dn;
+ $max_level = max($max_level, $item['level']);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Parse OU DN string into an array
+ */
+ private function parse_dn($dn)
+ {
+ $path = kolab_utils::dn2ufn($dn);
+ $path = explode(', ', $path);
+ $domain = array_pop($path); // remove domain
+
+ return array(
+ 'name' => array_shift($path),
+ 'path' => array_reverse($path),
+ 'level' => count($path),
+ 'domain' => $domain,
+ );
+ }
+
+ /**
+ * Converts domain name into DN string
+ */
+ private function domain_dn($domain)
+ {
+ return "dc=" . implode(',dc=', explode('.', $domain));
+ }
}