summaryrefslogtreecommitdiffstats
path: root/lib/client/kolab_client_task_ou.php
blob: e1d80a84da1f7c9ca3efb9fc9c861c3d5b5f76ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2014, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

class kolab_client_task_ou extends kolab_client_task
{
    protected $ajax_only = true;

    protected $menu = array(
        'add'  => 'ou.add',
    );

    protected $list_attribs   = array('ou');
    protected $search_attribs = array('name'  => array('ou'));


    /**
     * Default action.
     */
    public function action_default()
    {
        $this->output->set_object('content', 'ou', true);
        $this->output->set_object('task_navigation', $this->menu());

        $this->action_list();

        // display form to add OU if logged-in user has right to do so
        $caps = $this->get_capability('actions');
        if (!empty($caps['ou.add'])) {
            $this->action_add();
        }
        else {
            $this->output->command('set_watermark', 'taskcontent');
        }
    }

    /**
     * Organizational unit adding (form) action.
     */
    public function action_add()
    {
        $data   = $this->get_input('data', 'POST');
        $output = $this->ou_form(null, $data, true);

        $this->output->set_object('taskcontent', $output);
    }

    /**
     * Organizational unit information (form) action.
     */
    public function action_info()
    {
        $id         = $this->get_input('id', 'POST');
        $result     = $this->api_get('ou.info', array('id' => $id));
        $resource   = $result->get();
        $output     = $this->ou_form(null, $resource);

        $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'])) {
            $attribs['id'] = 'ou-form';
        }

        // Form sections
        $sections = array(
            'system' => 'ou.system',
            'other'  => 'ou.other',
        );

        // field-to-section map and fields order
        $fields_map = array(
            'type_id'                   => 'system',
            'type_id_name'              => 'system',
            'ou'                        => 'system',
            'base_dn'                   => 'system',
            'description'               => 'system',
        );

        // Prepare fields
        list($fields, $types, $type) = $this->form_prepare('ou', $data);

        $add_mode  = empty($data['id']);
        $accttypes = array();

        foreach ($types as $idx => $elem) {
            $accttypes[$idx] = array('value' => $idx, 'content' => $elem['name']);
        }

        // Add OU type id selector
        $fields['type_id'] = array(
            'section'  => 'system',
            'type'     => kolab_form::INPUT_SELECT,
            'options'  => $accttypes,
            'onchange' => "kadm.ou_save(true, 'system')",
        );

        // Hide account type selector if there's only one type
        if (count($accttypes) < 2 || !$add_mode) {
            //console("setting type_id form type to hidden");
            $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
            $title = $this->translate('ou.add');
        }
        // Edit mode
        else {
            $title = $data['ou'];

            // Add OU type name
            $fields['type_id_name'] = array(
                'label'    => 'ou.type_id',
                'section'  => 'system',
                'value'    => $accttypes[$type]['content'],
            );
        }

        // Create form object and populate with fields
        $form = $this->form_create('ou', $attribs, $sections, $fields, $fields_map, $data, $add_mode);

        $form->set_title(kolab_html::escape($title));

        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));
    }
}