From c9451ffb3323dc2c46e691c30c77a68d83c2e7fc Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Sun, 5 Apr 2009 22:54:10 +0200 Subject: [PATCH] Adapted the form code to the new capabilities within the Kolab_Server library. The application itself is still in a rather rough state. --- koward/app/controllers/ObjectController.php | 39 +++++++--- koward/app/views/Object/listall.html.php | 3 + koward/config/labels.php | 6 ++ koward/config/objects.php | 93 ++++++++++++++++++++--- koward/lib/Form/Object.php | 113 +++++++++++++++++++++++++--- koward/lib/Koward.php | 4 +- 6 files changed, 225 insertions(+), 33 deletions(-) create mode 100644 koward/config/labels.php diff --git a/koward/app/controllers/ObjectController.php b/koward/app/controllers/ObjectController.php index f76f32dac..911b75db0 100644 --- a/koward/app/controllers/ObjectController.php +++ b/koward/app/controllers/ObjectController.php @@ -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'); diff --git a/koward/app/views/Object/listall.html.php b/koward/app/views/Object/listall.html.php index d53aa87c2..a790967fd 100644 --- a/koward/app/views/Object/listall.html.php +++ b/koward/app/views/Object/listall.html.php @@ -3,6 +3,8 @@ tabs->render($this->object_type); ?> +objectlist)): ?> + @@ -35,3 +37,4 @@
+ diff --git a/koward/config/labels.php b/koward/config/labels.php new file mode 100644 index 000000000..76ad6704a --- /dev/null +++ b/koward/config/labels.php @@ -0,0 +1,6 @@ + '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), + ), + ), ), ); diff --git a/koward/lib/Form/Object.php b/koward/lib/Form/Object.php index 5a4a35208..e4ba07ac7 100644 --- a/koward/lib/Form/Object.php +++ b/koward/lib/Form/Object.php @@ -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'); diff --git a/koward/lib/Koward.php b/koward/lib/Koward.php index 744eea6fa..d0af465d8 100644 --- a/koward/lib/Koward.php +++ b/koward/lib/Koward.php @@ -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'))); } /** -- 2.11.0