2 commits - lib/client lib/kolab_client_task.php
Aleksander Machniak
machniak at kolabsys.com
Thu Mar 15 15:44:05 CET 2012
lib/client/kolab_client_task_user.php | 200 ++---------------------------
lib/kolab_client_task.php | 227 +++++++++++++++++++++++++++++++++-
2 files changed, 240 insertions(+), 187 deletions(-)
New commits:
commit d09b405ca61e5cb73253300884669156d0e7685b
Author: Aleksander Machniak <alec at alec.pl>
Date: Thu Mar 15 15:43:08 2012 +0100
Move generic form generation code to kolab_client_task class (would be shared by 'group' and other modules)
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 6ce5dd8..df9d9df 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -192,16 +192,6 @@ class kolab_client_task_user extends kolab_client_task
$attribs['id'] = 'user-form';
}
- $form = new kolab_form($attribs);
- $utypes = (array) $this->user_types();
- $form_id = $attribs['id'];
- $add_mode = empty($data['user']);
- $accttypes = array();
-
- foreach ($utypes as $idx => $elem) {
- $accttypes[$idx] = array('value' => $idx, 'content' => $elem['name']);
- }
-
// Form sections
$sections = array(
'personal' => 'user.personal',
@@ -211,7 +201,7 @@ class kolab_client_task_user extends kolab_client_task
);
// field-to-section map and fields order
- $fields = array(
+ $fields_map = array(
'user_type_id' => 'personal',
'user_type_id_name' => 'personal',
'givenname' => 'personal',
@@ -254,85 +244,18 @@ class kolab_client_task_user extends kolab_client_task
'shell' => 'config',
);
- $event_fields = array();
- $auto_fields = array();
- $form_fields = array();
- $_fields = array();
- $auto_attribs = array();
-
- // Selected account type
- if (!empty($data['user_type_id'])) {
- $utype = $data['user_type_id'];
- }
- else {
- $utype = key($accttypes);
- $data['user_type_id'] = $utype;
- }
-
- if ($utype) {
- $auto_fields = (array) $utypes[$utype]['attributes']['auto_form_fields'];
- $form_fields = (array) $utypes[$utype]['attributes']['form_fields'];
- }
-
- // Mark automatically generated fields as read-only, etc.
- foreach ($auto_fields as $idx => $field) {
- if (!is_array($field)) {
- continue;
- }
- // merge with field definition from
- if (isset($form_fields[$idx])) {
- $field = array_merge($field, $form_fields[$idx]);
- }
- // remove auto-generated value on user type change, it will be re-generated
- else if ($add_mode) {
- unset($data[$idx]);
- }
+ // Prepare fields
+ list($fields, $types, $type) = $this->form_prepare('user', $data);
- $field['name'] = $idx;
- $_fields[$idx] = $this->form_element_type($field);
- $_fields[$idx]['section'] = isset($fields[$idx]) ? $fields[$idx] : 'other';
- $_fields[$idx]['readonly'] = true;
- $_fields[$idx]['disabled'] = true;
-
- // build auto_attribs and event_fields lists
- $is_data = 0;
- if (!empty($field['data'])) {
- foreach ($field['data'] as $fd) {
- $event_fields[$fd][] = $idx;
- if (isset($data[$fd])) {
- $is_data++;
- }
- }
- if (count($field['data']) == $is_data) {
- $auto_attribs[] = $idx;
- }
- }
- else {
- $auto_attribs[] = $idx;
- }
- }
+ $add_mode = empty($data['user']);
+ $accttypes = array();
- // Other fields
- foreach ($form_fields as $idx => $field) {
- if (!isset($_fields[$idx])) {
- $field['name'] = $idx;
- $_fields[$idx] = $this->form_element_type($field);
- $_fields[$idx]['section'] = isset($fields[$idx]) ? $fields[$idx] : 'other';
- }
-// $_fields[$idx]['required'] = true;
- $_fields[$idx]['readonly'] = false;
- $_fields[$idx]['disabled'] = false;
-
- // Attach on-change events to some fields, to update
- // auto-generated field values
- if (!empty($event_fields[$idx])) {
- $event = json_encode(array_unique($event_fields[$idx]));
- $_fields[$idx]['onchange'] = "kadm.form_value_change($event)";
- }
+ foreach ($types as $idx => $elem) {
+ $accttypes[$idx] = array('value' => $idx, 'content' => $elem['name']);
}
// Add user type id selector
- $_fields['user_type_id'] = array(
+ $fields['user_type_id'] = array(
'section' => 'personal',
'type' => kolab_form::INPUT_SELECT,
'options' => $accttypes,
@@ -340,24 +263,17 @@ class kolab_client_task_user extends kolab_client_task
);
// Add password confirmation
- if (isset($_fields['userpassword'])) {
- $_fields['userpassword2'] = $_fields['userpassword'];
+ if (isset($fields['userpassword'])) {
+ $fields['userpassword2'] = $fields['userpassword'];
}
// Hide account type selector if there's only one type
if (count($accttypes) < 2 || !$add_mode) {
- $_fields['user_type_id']['type'] = kolab_form::INPUT_HIDDEN;
+ $fields['user_type_id']['type'] = kolab_form::INPUT_HIDDEN;
}
// Create mode
if ($add_mode) {
- // (Re-|Pre-)populate auto_form_fields
- if (!empty($auto_attribs)) {
- $data = array_merge((array)$data, array('attributes' => $auto_attribs));
- $resp = $this->api->post('form_value.generate', null, $data);
- $data = array_merge((array)$data, (array)$resp->get());
- }
-
// copy password to password confirm field
$data['userpassword2'] = $data['userpassword'];
@@ -372,106 +288,18 @@ class kolab_client_task_user extends kolab_client_task
$data['userpassword'] = '';
// Add user type name
- $_fields['user_type_id_name'] = array(
+ $fields['user_type_id_name'] = array(
'label' => 'user.user_type_id',
'section' => 'personal',
- 'value' => $accttypes[$utype]['content'],
+ 'value' => $accttypes[$type]['content'],
);
}
- // Sort
- foreach ($fields as $idx => $val) {
- if (array_key_exists($idx, $_fields)) {
- $fields[$idx] = $_fields[$idx];
- unset($_fields[$idx]);
- }
- else {
- unset($fields[$idx]);
- }
- }
- if (!empty($_fields)) {
- $fields = array_merge($fields, $_fields);
- }
-
- // Parse elements and add them to the form object
- foreach ($sections as $section_idx => $section) {
- $form->add_section($section_idx, kolab_html::escape($this->translate($section)));
-
- foreach ($fields as $idx => $field) {
- if ($field['section'] != $section_idx) {
- continue;
- }
-
- if (empty($field['label'])) {
- $field['label'] = "user.$idx";
- }
-
- $field['label'] = kolab_html::escape($this->translate($field['label']));
- $field['description'] = "user.$idx.desc";
- $field['section'] = $section_idx;
-
- if (!empty($data[$idx])) {
- if (is_array($data[$idx])) {
- $field['value'] = array_map(array('kolab_html', 'escape'), $data[$idx]);
- $field['value'] = implode("\n", $field['value']);
- }
- else {
- $field['value'] = kolab_html::escape($data[$idx]);
- }
- }
-/*
- if (!empty($field['suffix'])) {
- $field['suffix'] = kolab_html::escape($this->translate($field['suffix']));
- }
-*/
- if (!empty($field['options'])) {
- foreach ($field['options'] as $opt_idx => $option) {
- if (is_array($option)) {
- $field['options'][$opt_idx]['content'] = kolab_html::escape($this->translate($option['content']));
- }
- else {
- $field['options'][$opt_idx] = kolab_html::escape($this->translate($option));
- }
- }
- }
-
- if (!empty($field['description'])) {
- $description = $this->translate($field['description']);
- if ($description != $field['description']) {
- $field['title'] = $description;
- }
- unset($field['description']);
- }
-
- if (empty($field['name'])) {
- $field['name'] = $idx;
- }
-
- $form->add_element($field);
- }
- }
+ // Create form object and populate with fields
+ $form = $this->form_create('user', $attribs, $sections, $fields, $fields_map, $data);
$form->set_title(kolab_html::escape($title));
- $form->add_button(array(
- 'value' => kolab_html::escape($this->translate('submit.button')),
- 'onclick' => "kadm.user_save()",
- ));
-
- if (!$add_mode) {
- $user = $data['user'];
- $form->add_button(array(
- 'value' => kolab_html::escape($this->translate('delete.button')),
- 'onclick' => "kadm.user_delete('$user')",
- ));
- }
-
- if (!empty($data['section'])) {
- $form->activate_section($data['section']);
- }
-
- $this->output->set_env('auto_fields', $auto_fields);
- $this->output->set_env('form_id', $form_id);
$this->output->add_translation('user.password.mismatch',
'user.add.success', 'user.delete.success');
diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index af7b76f..cc9dcea 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -595,14 +595,13 @@ class kolab_client_task
$data['attributes'] = array($field['name']);
$resp = $this->api->post('form_value.select_options', null, $data);
$field['values'] = $resp->get($field['name']);
-console($resp);
}
if (!empty($field['values']['default'])) {
$result['value'] = $field['values']['default'];
unset($field['values']['default']);
}
-console($field['values']);
+
$result['type'] = kolab_form::INPUT_SELECT;
if (!empty($field['values'])) {
$result['options'] = array_combine($field['values'], $field['values']);
@@ -626,4 +625,224 @@ console($field['values']);
return $result;
}
+
+ /**
+ * HTML Form elements preparation.
+ *
+ * @param string $name Object name (user, group, etc.)
+ * @param array $data Object data
+ *
+ * @return array Fields list, Object types list, Current type ID
+ */
+ protected function form_prepare($name, &$data)
+ {
+ $types = (array) $this->{$name . '_types'}();
+ $form_id = $attribs['id'];
+ $add_mode = empty($data[$name]);
+
+ $event_fields = array();
+ $auto_fields = array();
+ $form_fields = array();
+ $fields = array();
+ $auto_attribs = array();
+
+ // Selected account type
+ if (!empty($data[$name . '_type_id'])) {
+ $type = $data[$name . '_type_id'];
+ }
+ else {
+ $data[$name . '_type_id'] = $type = key($types);
+ }
+
+ if ($type) {
+ $auto_fields = (array) $types[$type]['attributes']['auto_form_fields'];
+ $form_fields = (array) $types[$type]['attributes']['form_fields'];
+ }
+
+ // Mark automatically generated fields as read-only, etc.
+ foreach ($auto_fields as $idx => $field) {
+ if (!is_array($field)) {
+ continue;
+ }
+ // merge with field definition from
+ if (isset($form_fields[$idx])) {
+ $field = array_merge($field, $form_fields[$idx]);
+ }
+ // remove auto-generated value on type change, it will be re-generated
+ else if ($add_mode) {
+ unset($data[$idx]);
+ }
+
+ $field['name'] = $idx;
+ $fields[$idx] = $this->form_element_type($field);
+ $fields[$idx]['readonly'] = true;
+ $fields[$idx]['disabled'] = true;
+
+ // build auto_attribs and event_fields lists
+ $is_data = 0;
+ if (!empty($field['data'])) {
+ foreach ($field['data'] as $fd) {
+ $event_fields[$fd][] = $idx;
+ if (isset($data[$fd])) {
+ $is_data++;
+ }
+ }
+ if (count($field['data']) == $is_data) {
+ $auto_attribs[] = $idx;
+ }
+ }
+ else {
+ $auto_attribs[] = $idx;
+ }
+ }
+
+ // Other fields
+ foreach ($form_fields as $idx => $field) {
+ if (!isset($fields[$idx])) {
+ $field['name'] = $idx;
+ $fields[$idx] = $this->form_element_type($field);
+ }
+// $fields[$idx]['required'] = true;
+ $fields[$idx]['readonly'] = false;
+ $fields[$idx]['disabled'] = false;
+
+ // Attach on-change events to some fields, to update
+ // auto-generated field values
+ if (!empty($event_fields[$idx])) {
+ $event = json_encode(array_unique($event_fields[$idx]));
+ $fields[$idx]['onchange'] = "kadm.form_value_change($event)";
+ }
+ }
+
+ $this->output->set_env('auto_fields', $auto_fields);
+
+ // (Re-|Pre-)populate auto_form_fields
+ if ($add_mode) {
+ if (!empty($auto_attribs)) {
+ $data = array_merge((array)$data, array('attributes' => $auto_attribs));
+ $resp = $this->api->post('form_value.generate', null, $data);
+ $data = array_merge((array)$data, (array)$resp->get());
+ }
+ }
+
+ return array($fields, $types, $type);
+ }
+
+ /**
+ * HTML Form creation.
+ *
+ * @param string $name Object name (user, group, etc.)
+ * @param array $attribs HTML attributes of the form
+ * @param array $sections List of form sections
+ * @param array $fields Fields list (from self::form_prepare())
+ * @param array $fields_map Fields map (used for sorting and sections assignment)
+ * @param array $data Object data
+ *
+ * @return kolab_form HTML Form object
+ */
+ protected function form_create($name, $attribs, $sections, $fields, $fields_map, $data)
+ {
+ // Assign sections to fields
+ foreach ($fields as $idx => $field) {
+ if (!$field['section']) {
+ $fields[$idx]['section'] = isset($fields_map[$idx]) ? $fields_map[$idx] : 'other';
+ }
+ }
+
+ // Sort
+ foreach ($fields_map as $idx => $val) {
+ if (array_key_exists($idx, $fields)) {
+ $fields_map[$idx] = $fields[$idx];
+ unset($fields[$idx]);
+ }
+ else {
+ unset($fields_map[$idx]);
+ }
+ }
+ if (!empty($fields)) {
+ $fields_map = array_merge($fields_map, $fields);
+ }
+
+ $form = new kolab_form($attribs);
+
+ // Parse elements and add them to the form object
+ foreach ($sections as $section_idx => $section) {
+ $form->add_section($section_idx, kolab_html::escape($this->translate($section)));
+
+ foreach ($fields_map as $idx => $field) {
+ if ($field['section'] != $section_idx) {
+ continue;
+ }
+
+ if (empty($field['label'])) {
+ $field['label'] = "user.$idx";
+ }
+
+ $field['label'] = kolab_html::escape($this->translate($field['label']));
+ $field['description'] = "user.$idx.desc";
+ $field['section'] = $section_idx;
+
+ if (!empty($data[$idx])) {
+ if (is_array($data[$idx])) {
+ $field['value'] = array_map(array('kolab_html', 'escape'), $data[$idx]);
+ $field['value'] = implode("\n", $field['value']);
+ }
+ else {
+ $field['value'] = kolab_html::escape($data[$idx]);
+ }
+ }
+/*
+ if (!empty($field['suffix'])) {
+ $field['suffix'] = kolab_html::escape($this->translate($field['suffix']));
+ }
+*/
+ if (!empty($field['options'])) {
+ foreach ($field['options'] as $opt_idx => $option) {
+ if (is_array($option)) {
+ $field['options'][$opt_idx]['content'] = kolab_html::escape($this->translate($option['content']));
+ }
+ else {
+ $field['options'][$opt_idx] = kolab_html::escape($this->translate($option));
+ }
+ }
+ }
+
+ if (!empty($field['description'])) {
+ $description = $this->translate($field['description']);
+ if ($description != $field['description']) {
+ $field['title'] = $description;
+ }
+ unset($field['description']);
+ }
+
+ if (empty($field['name'])) {
+ $field['name'] = $idx;
+ }
+
+ $form->add_element($field);
+ }
+ }
+
+ if (!empty($data['section'])) {
+ $form->activate_section($data['section']);
+ }
+
+ $form->add_button(array(
+ 'value' => kolab_html::escape($this->translate('submit.button')),
+ 'onclick' => "kadm.{$name}_save()",
+ ));
+
+ if ($data[$name]) {
+ $id = $data[$name];
+ $form->add_button(array(
+ 'value' => kolab_html::escape($this->translate('delete.button')),
+ 'onclick' => "kadm.{$name}_delete('{$id}')",
+ ));
+ }
+
+ $this->output->set_env('form_id', $attribs['id']);
+
+ return $form;
+ }
+
}
commit 9817003573c4ada433403cdafdf26a6a4f9b751c
Author: Aleksander Machniak <alec at alec.pl>
Date: Thu Mar 15 14:45:05 2012 +0100
Use form_value.select_options
diff --git a/lib/client/kolab_client_task_user.php b/lib/client/kolab_client_task_user.php
index 503c1bf..6ce5dd8 100644
--- a/lib/client/kolab_client_task_user.php
+++ b/lib/client/kolab_client_task_user.php
@@ -288,6 +288,7 @@ class kolab_client_task_user extends kolab_client_task
unset($data[$idx]);
}
+ $field['name'] = $idx;
$_fields[$idx] = $this->form_element_type($field);
$_fields[$idx]['section'] = isset($fields[$idx]) ? $fields[$idx] : 'other';
$_fields[$idx]['readonly'] = true;
@@ -314,6 +315,7 @@ class kolab_client_task_user extends kolab_client_task
// Other fields
foreach ($form_fields as $idx => $field) {
if (!isset($_fields[$idx])) {
+ $field['name'] = $idx;
$_fields[$idx] = $this->form_element_type($field);
$_fields[$idx]['section'] = isset($fields[$idx]) ? $fields[$idx] : 'other';
}
diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php
index b6ca8e3..af7b76f 100644
--- a/lib/kolab_client_task.php
+++ b/lib/kolab_client_task.php
@@ -581,24 +581,28 @@ class kolab_client_task
* Returns form element definition based on field attributes
*
* @param array $field Field attributes
+ * @param array $data Attribute values
*
* @return array Field definition
*/
- protected function form_element_type($field)
+ protected function form_element_type($field, $data = array())
{
$result = array();
switch ($field['type']) {
case 'select':
if (!isset($field['values'])) {
- // @TODO: call form_value.list_options
+ $data['attributes'] = array($field['name']);
+ $resp = $this->api->post('form_value.select_options', null, $data);
+ $field['values'] = $resp->get($field['name']);
+console($resp);
}
if (!empty($field['values']['default'])) {
$result['value'] = $field['values']['default'];
unset($field['values']['default']);
}
-
+console($field['values']);
$result['type'] = kolab_form::INPUT_SELECT;
if (!empty($field['values'])) {
$result['options'] = array_combine($field['values'], $field['values']);
More information about the commits
mailing list