Adapted the form code to the new capabilities within the Kolab_Server
authorGunnar Wrobel <p@rdus.de>
Sun, 5 Apr 2009 20:54:10 +0000 (22:54 +0200)
committerGunnar Wrobel <p@rdus.de>
Sun, 5 Apr 2009 20:54:24 +0000 (22:54 +0200)
library. The application itself is still in a rather rough state.

koward/app/controllers/ObjectController.php
koward/app/views/Object/listall.html.php
koward/config/labels.php [new file with mode: 0644]
koward/config/objects.php
koward/lib/Form/Object.php
koward/lib/Koward.php

index f76f32d..911b75d 100644 (file)
@@ -12,6 +12,13 @@ require_once dirname(__FILE__) . '/ApplicationController.php';
 class ObjectController extends Koward_ApplicationController
 {
 
+    var $object_type;
+    var $objectlist;
+    var $attributes;
+    var $tabs;
+    var $object;
+    var $post;
+
     public function listall()
     {
         require_once 'Horde/UI/Tabs.php';
@@ -20,11 +27,22 @@ class ObjectController extends Koward_ApplicationController
 
         $this->object_type = $this->params->get('id', $this->types[0]);
 
-        if (isset($this->koward->objects[$this->object_type])) {
-            $this->attributes = $this->koward->objects[$this->object_type]['attributes'];
+        if (isset($this->koward->objects[$this->object_type]['list_attributes'])) {
+            $this->attributes = $this->koward->objects[$this->object_type]['list_attributes'];
+        } else if (isset($this->koward->objects[$this->object_type]['attributes']['fields'])) {
+            $this->attributes = $this->koward->objects[$this->object_type]['attributes']['fields'];
+        } else {
+            $this->koward->notification->push(sprintf('No attributes have been defined for the list view of objects with type %s.',
+                                                      $this->object_type),
+                                              'horde.error');
+        }
+
+        if (isset($this->attributes)
+            && isset($this->koward->objects[$this->object_type])) {
             $params = array('attributes' => array_keys($this->attributes));
-            $this->objectlist = $this->koward->server->listHash($this->object_type,
-                                                               $params);
+            $class = $this->koward->objects[$this->object_type]['class'];
+            $this->objectlist = $this->koward->server->listHash($class,
+                                                                $params);
             foreach ($this->objectlist as $uid => $info) {
                 $this->objectlist[$uid]['edit_url'] = Horde::link(
                     $this->urlFor(array('controller' => 'object', 
@@ -45,9 +63,8 @@ class ObjectController extends Koward_ApplicationController
                                         'action' => 'view',
                                         'id' => $uid)), _("View"));
             }
-        } else {
-            $this->objectlist = 'Unkown object type.';
         }
+
         $this->tabs = new Horde_UI_Tabs(null, Variables::getDefaultVariables());
         foreach ($this->koward->objects as $key => $configuration) {
             $this->tabs->addTab($configuration['list_label'],
@@ -126,10 +143,12 @@ class ObjectController extends Koward_ApplicationController
             if ($this->form->validate()) {
                 $object = $this->form->execute();
 
-                header('Location: ' . $this->urlFor(array('controller' => 'object', 
-                                                          'action' => 'view',
-                                                          'id' => $object->get(Horde_Koward_Server_Object::ATTRIBUTE_UID))));
-                exit;
+                if (!empty($object)) {
+                    header('Location: ' . $this->urlFor(array('controller' => 'object', 
+                                                              'action' => 'view',
+                                                              'id' => $object->get(Horde_Kolab_Server_Object::ATTRIBUTE_UID))));
+                    exit;
+                }
             }
         } catch (Exception $e) {
             $this->koward->notification->push($e->getMessage(), 'horde.error');
index d53aa87..a790967 100644 (file)
@@ -3,6 +3,8 @@
 
 <?= $this->tabs->render($this->object_type); ?>
 
+<?php if (isset($this->objectlist)): ?>
+
 <table cellspacing="0" width="100%" class="linedRow">
  <thead>
   <tr>
@@ -35,3 +37,4 @@
   <?php endforeach; ?>
  </tbody>
 </table>
+<?php endif; ?>
diff --git a/koward/config/labels.php b/koward/config/labels.php
new file mode 100644 (file)
index 0000000..76ad670
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+
+$labels['givenName'] = _("First name");
+$labels['middleNames'] = _("Additional names");
+$labels['sn'] = _("Last name");
+$labels['kolabSalutation'] = _("Salutation");
index f34ddaf..b4fb39d 100644 (file)
@@ -1,21 +1,63 @@
 <?php
 
-$objects['Horde_Kolab_Server_Object'] = array(
+$objects['object'] = array(
+    'class'       => 'Horde_Kolab_Server_Object',
     'label'       => _("Object"),
     'list_label'  => _("Objects"),
     'attributes'  => array(
-        'id' => array(
-            'title' => _("Object id"),
-            'width' => 80,
-            'link_view'=> true,
+        'override' => true,
+        'fields' => array(
+            'id' => array(
+                'title' => _("Object id"),
+                'width' => 80,
+                'link_view'=> true,
+            ),
         ),
     ),
 );
 
-$objects['Horde_Kolab_Server_Object_Kolab_User'] = array(
+$objects['user'] = array(
+    'class'       => 'Horde_Kolab_Server_Object_Kolab_User',
     'label'       => _("User"),
     'list_label'  => _("Users"),
     'attributes'  => array(
+        'override' => true,
+        'fields' => array(
+            'sn' => array(
+                'title' => _("Last name"),
+                'width' => 20,
+            ),
+            'givenName' => array(
+                'title' => _("First name"),
+                'width' => 20,
+            ),
+            'mail' => array(
+                'title' => _("E-mail"),
+                'width' => 20,
+                'link_view'=> true,
+            ),
+            'uid' => array(
+                'title' => _("User ID"),
+                'width' => 20,
+            ),
+        ),
+    ),
+);
+
+$objects['admin'] = array(
+    'class'       => 'Horde_Kolab_Server_Object_Kolab_Administrator',
+    'label'       => _("Administrator"),
+    'list_label'  => _("Administrators"),
+    'attributes'  => array(
+    ),
+);
+
+$objects['kolabuser'] = array(
+    'class'       => 'Horde_Kolab_Server_Object_Kolab_User',
+    'preferred'   => true,
+    'label'       => _("Kolab user"),
+    'list_label'  => _("Kolab users"),
+    'list_attributes'  => array(
         'sn' => array(
             'title' => _("Last name"),
             'width' => 20,
@@ -34,11 +76,40 @@ $objects['Horde_Kolab_Server_Object_Kolab_User'] = array(
             'width' => 20,
         ),
     ),
-);
-
-$objects['Horde_Kolab_Server_Object_Kolab_Administrator'] = array(
-    'label'       => _("Administrator"),
-    'list_label'  => _("Administrators"),
     'attributes'  => array(
+        'hide' => array(
+            'objectClass',
+            'userPassword',
+            'seeAlso',
+            'x121Address',
+            'registeredAddress',
+            'destinationIndicator',
+            'preferredDeliveryMethod',
+            'telexNumber',
+            'teletexTerminalIdentifier',
+            'internationaliSDNNumber',
+            'kolabEncryptedPassword',
+            'kolabHomeMTA',
+            'kolabDelegate',
+        ),
+        'order' => array(
+            'mail' => 1,
+            'kolabSalutation' => 2,
+            'givenName' => 3,
+            'middleNames' => 4,
+            'sn' => 5,
+        ),
+        'labels' => array(
+            'mail' => _("Account ID"),
+        ),
+        'fields' => array(
+            'kolabSalutation' => array(
+                'label' => _("Salutation"),
+                'type' => 'enum',
+                'params' => array('values' => array(_("Mr.") => _("Mr."),
+                                                    _("Mrs.") => _("Mrs.")),
+                                  'prompt' => true),
+            ),
+        ),
     ),
 );
index 5a4a352..e4ba07a 100644 (file)
@@ -23,6 +23,8 @@ class Koward_Form_Object extends Horde_Form {
 
         parent::Horde_Form($vars);
 
+        $type = false;
+
         if (empty($this->object)) {
             $title = _("Add Object");
             $this->setButtons(_("Add"));
@@ -39,14 +41,27 @@ class Koward_Form_Object extends Horde_Form {
                 $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName())) {
                 $this->koward->notification->push(sprintf(_("Selected object type \"%s\"."), $object_conf[$vars->get('type')]['label']), 'horde.message');
             }
+
+            $type = $vars->get('type');
         } else {
             $title = _("Edit Object");
-            $type = get_class($this->object);
+            $class_name = get_class($this->object);
+            foreach ($this->koward->objects as $name => $config) {
+                if ($config['class'] == $class_name) {
+                    $type = $name;
+                    if (!empty($config['preferred'])) {
+                        break;
+                    }
+                }
+            }
+            if (empty($type)) {
+                throw new Koward_Exception('Undefined object class!');
+            }
             if (!$this->isSubmitted()) {
                 $vars->set('type', $type);
-                $keys = array_keys($this->koward->objects[$type]['attributes']);
+                $keys = array_keys($this->_getFields($this->koward->objects[$type]));
                 $vars->set('object', $this->object->toHash($keys));
-                $this->setButtons(_("Edit"));
+                $this->setButtons(true);
             }
         }
 
@@ -56,19 +71,88 @@ class Koward_Form_Object extends Horde_Form {
 
         $this->setTitle($title);
 
-        $type = $vars->get('type');
-        if (isset($type)) {
+        if (!empty($type)) {
             $this->_addFields($this->koward->objects[$type]);
         }
     }
 
     /**
+     * Get the fields for an object type
+     */
+    function _getFields($config)
+    {
+        if (isset($config['attributes']['fields']) && !empty($config['attribute']['override'])) {
+            return $config['attributes']['fields'];
+        } else {
+            list($attributes, $attribute_map) = $this->koward->server->getAttributes($config['class']);
+
+            if (isset($config['attributes']['show'])) {
+                $akeys = $config['attributes']['show'];
+            } else {
+                $akeys = array_keys($attributes);
+                if (isset($config['attributes']['hide'])) {
+                    $akeys = array_diff($akeys, $config['attributes']['hide']);
+                }
+            }
+
+            foreach ($akeys as $key) {
+                if (isset($config['attributes']['hide'])
+                    && in_array($key, $config['attributes']['hide'])) {
+                    continue;
+                }
+                if (isset($config['attributes']['fields'][$key])) {
+                    $form_attributes[$key] = $config['attributes']['fields'][$key];
+                } else {
+                    $form_attributes[$key] = array(
+                        'type' => 'text',
+                        'required' => in_array($key, $attribute_map['required']),
+                        'readonly' => in_array($key, $attribute_map['locked']),
+                        'params' => array('regex' => '', 'size' => 40, 'maxlength' => 255)
+                    );
+                }
+                if (isset($config['attributes']['order'][$key])) {
+                    $form_attributes[$key]['order'] = $config['attributes']['order'][$key];
+                } else {
+                    $form_attributes[$key]['order'] = -1;
+                }
+                if (isset($config['attributes']['labels'][$key])) {
+                    $form_attributes[$key]['label'] = $config['attributes']['labels'][$key];
+                } else if (isset($this->koward->labels[$key])) {
+                    $form_attributes[$key]['label'] = $this->koward->labels[$key];
+                } else {
+                    $form_attributes[$key]['label'] = $key;
+                }
+            }
+            uasort($form_attributes, array($this, '_sortFields'));
+            return $form_attributes;
+        }
+        return array();
+    }
+
+    /**
+     * Sort fields for an object type
+     */
+    function _sortFields($a, $b)
+    {
+        if ($a['order'] == -1) {
+            return 1;
+        }
+        if ($b['order'] == -1) {
+            return -1;
+        }
+        if ($a['order'] == $b['order']) {
+            return 0;
+        }
+        return ($a['order'] < $b['order']) ? -1 : 1;
+    }
+
+    /**
      * Set up the Horde_Form fields for the attributes of this object type.
      */
     function _addFields($config)
     {
         // Now run through and add the form variables.
-        $fields = isset($config['attributes']) ? $config['attributes'] : array();
+        $fields = $this->_getFields($config);
         $tabs   = isset($config['tabs']) ? $config['tabs'] : array('' => $fields);
 
         foreach ($tabs as $tab => $tab_fields) {
@@ -76,12 +160,13 @@ class Koward_Form_Object extends Horde_Form {
                 $this->setSection($tab, $tab);
             }
             foreach ($tab_fields as $key => $field) {
-                if (!in_array($key, array_keys($fields)) ||
-                    !isset($this->koward->attributes[$key])) {
+                if (!in_array($key, array_keys($fields))
+                    //                    || !isset($this->koward->attributes[$key])
+                ) {
                     continue;
                 }
-
-                $attribute = $this->koward->attributes[$key];
+                $attribute = $field;
+                //                $attribute = $this->koward->attributes[$key];
                 $params = isset($attribute['params']) ? $attribute['params'] : array();
                 $desc = isset($attribute['desc']) ? $attribute['desc'] : null;
 
@@ -101,7 +186,13 @@ class Koward_Form_Object extends Horde_Form {
         if (isset($info['object'])) {
             if (empty($this->object)) {
                 if (isset($info['type'])) {
-                    $object = $this->koward->server->add(array_merge(array('type' => $info['type']),
+                    if (isset($this->koward->objects[$info['type']]['class'])) {
+                        $class = $this->koward->objects[$info['type']]['class'];
+                    } else {
+                        throw new Koward_Exception(sprintf('Invalid type \"%s\" specified!',
+                                                           $info['type']));
+                    }
+                    $object = $this->koward->server->add(array_merge(array('type' => $class),
                                                                  $info['object']));
                     $this->koward->notification->push(_("Successfully added the object."),
                                                      'horde.message');
index 744eea6..d0af465 100644 (file)
@@ -35,7 +35,9 @@ class Koward_Koward {
         $this->conf       = Horde::loadConfiguration('conf.php', 'conf');
         $this->objects    = Horde::loadConfiguration('objects.php', 'objects');
         $this->attributes = Horde::loadConfiguration('attributes.php', 'attributes');
-        $this->server     = Horde_Kolab_Server::singleton();
+        $this->labels     = Horde::loadConfiguration('labels.php', 'labels');
+        $this->server     = Horde_Kolab_Server::singleton(array('user' => Auth::getAuth(),
+                                                                'pass' => Auth::getCredential('password')));
     }
 
     /**