From: Jan Schneider Date: Fri, 2 Jul 2010 15:42:39 +0000 (+0200) Subject: Reorganize forms. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=2bb7416f9fa0ba83dd402aa19bd3286e0fc96c26;p=horde.git Reorganize forms. --- diff --git a/turba/add.php b/turba/add.php index 82562fd37..3e1196355 100644 --- a/turba/add.php +++ b/turba/add.php @@ -13,8 +13,6 @@ require_once dirname(__FILE__) . '/lib/Application.php'; Horde_Registry::appInit('turba'); -require_once TURBA_BASE . '/lib/Forms/AddContact.php'; - /* Setup some variables. */ $contact = null; $vars = Horde_Variables::getDefaultVariables(); @@ -62,7 +60,7 @@ if ($source) { } /* Set up the form. */ -$form = new Turba_AddContactForm($vars, $contact); +$form = new Turba_Form_AddContact($vars, $contact); /* Validate the form. */ if ($form->validate()) { diff --git a/turba/addressbooks/create.php b/turba/addressbooks/create.php index eee5d36a3..a5695ca03 100644 --- a/turba/addressbooks/create.php +++ b/turba/addressbooks/create.php @@ -11,8 +11,6 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('turba'); -require_once TURBA_BASE . '/lib/Forms/CreateAddressBook.php'; - // Exit if this isn't an authenticated user, or if there's no source // configured for shares. if (!$GLOBALS['registry']->getAuth() || empty($_SESSION['turba']['has_share'])) { @@ -22,7 +20,7 @@ if (!$GLOBALS['registry']->getAuth() || empty($_SESSION['turba']['has_share'])) } $vars = Horde_Variables::getDefaultVariables(); -$form = new Turba_CreateAddressBookForm($vars); +$form = new Turba_Form_CreateAddressBook($vars); // Execute if the form is valid. if ($form->validate($vars)) { diff --git a/turba/addressbooks/delete.php b/turba/addressbooks/delete.php index 16b644d3f..340bb792f 100644 --- a/turba/addressbooks/delete.php +++ b/turba/addressbooks/delete.php @@ -11,8 +11,6 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('turba'); -require_once TURBA_BASE . '/lib/Forms/DeleteAddressBook.php'; - // Exit if this isn't an authenticated user, or if there's no source // configured for shares. if (!$GLOBALS['registry']->getAuth() || empty($_SESSION['turba']['has_share'])) { @@ -44,7 +42,7 @@ if (!$GLOBALS['registry']->getAuth() || exit; } -$form = new Turba_DeleteAddressBookForm($vars, $addressbook); +$form = new Turba_Form_DeleteAddressBook($vars, $addressbook); // Execute if the form is valid (must pass with POST variables only). if ($form->validate(new Horde_Variables($_POST))) { diff --git a/turba/addressbooks/edit.php b/turba/addressbooks/edit.php index 7e86608a2..502a81a9a 100644 --- a/turba/addressbooks/edit.php +++ b/turba/addressbooks/edit.php @@ -11,8 +11,6 @@ require_once dirname(__FILE__) . '/../lib/Application.php'; Horde_Registry::appInit('turba'); -require_once TURBA_BASE . '/lib/Forms/EditAddressBook.php'; - // Exit if this isn't an authenticated user, or if there's no source // configured for shares. if (!$GLOBALS['registry']->getAuth() || empty($_SESSION['turba']['has_share'])) { @@ -36,7 +34,7 @@ if (!$GLOBALS['registry']->getAuth() || header('Location: ' . Horde::applicationUrl('addressbooks/', true)); exit; } -$form = new Turba_EditAddressBookForm($vars, $addressbook); +$form = new Turba_Form_EditAddressBook($vars, $addressbook); // Execute if the form is valid. if ($form->validate($vars)) { diff --git a/turba/edit.php b/turba/edit.php index 67538da85..e9e2019c8 100644 --- a/turba/edit.php +++ b/turba/edit.php @@ -13,8 +13,6 @@ require_once dirname(__FILE__) . '/lib/Application.php'; Horde_Registry::appInit('turba'); -require_once TURBA_BASE . '/lib/Forms/EditContact.php'; - $listView = null; $vars = Horde_Variables::getDefaultVariables(); $source = $vars->get('source'); @@ -74,9 +72,9 @@ if (!$contact->hasPermission(Horde_Perms::EDIT)) { /* Create the edit form. */ if ($groupedit) { - $form = new Turba_EditContactGroupForm($vars, $contact); + $form = new Turba_Form_EditContactGroup($vars, $contact); } else { - $form = new Turba_EditContactForm($vars, $contact); + $form = new Turba_Form_EditContact($vars, $contact); } /* Execute() checks validation first. */ diff --git a/turba/lib/Form/AddContact.php b/turba/lib/Form/AddContact.php new file mode 100644 index 000000000..5c3b0f892 --- /dev/null +++ b/turba/lib/Form/AddContact.php @@ -0,0 +1,94 @@ +_contact = &$contact; + + $this->setButtons(_("Add")); + $this->addHidden('', 'url', 'text', false); + $this->addHidden('', 'key', 'text', false); + + /* Check if a source selection box is required. */ + if (count($addSources) > 1) { + /* Multiple sources, show a selection box. */ + $options = array(); + foreach ($addSources as $key => $config) { + $options[$key] = $config['title']; + } + $v = &$this->addVariable(_("Choose an address book"), 'source', 'enum', true, false, null, array($options, true)); + $action = Horde_Form_Action::factory('submit'); + $v->setAction($action); + $v->setOption('trackchange', true); + if (is_null($vars->get('formname')) && + $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName())) { + $notification->push(sprintf(_("Selected address book \"%s\"."), $addSources[$vars->get('source')]['title']), 'horde.message'); + } + } else { + /* One source, no selection box but store the value in a + * hidden field. */ + $this->addHidden('', 'source', 'text', true); + } + + if ($this->_contact) { + parent::_addFields($this->_contact); + } + } + + function validate() + { + if (!$this->_vars->get('source')) { + return false; + } + return parent::validate($this->_vars); + } + + function execute() + { + global $driver, $notification; + + /* Form valid, save data. */ + $this->getInfo($this->_vars, $info); + $source = $info['source']; + foreach ($info['object'] as $info_key => $info_val) { + if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) { + $this->_contact->setValue($info_key, file_get_contents($info_val['file'])); + $this->_contact->setValue($info_key . 'type', $info_val['type']); + } else { + $this->_contact->setValue($info_key, $info_val); + } + } + $contact = $this->_contact->attributes; + unset($contact['__owner']); + + /* Create Contact. */ + $key = $driver->add($contact); + if (is_a($key, 'PEAR_Error')) { + Horde::logMessage($key, 'ERR'); + } else { + // Try 3 times to get the new entry. We retry to allow setups like + // LDAP replication to work. + for ($i = 0; $i < 3; $i++) { + $ob = $driver->getObject($key); + if (!is_a($ob, 'PEAR_Error')) { + $notification->push(sprintf(_("%s added."), $ob->getValue('name')), 'horde.success'); + header('Location: ' . (!empty($info['url']) ? $info['url'] : $ob->url('Contact', true))); + exit; + } + sleep(1); + } + Horde::logMessage($ob, 'ERR'); + } + + $notification->push(_("There was an error adding the new contact. Contact your system administrator for further help."), 'horde.error'); + } + +} diff --git a/turba/lib/Form/Contact.php b/turba/lib/Form/Contact.php new file mode 100644 index 000000000..a66c1a9c0 --- /dev/null +++ b/turba/lib/Form/Contact.php @@ -0,0 +1,106 @@ +driver->getCriteria() as $info_key => $info_val) { + $object[$info_key] = $contact->getValue($info_key); + } + $vars->set('object', $object); + + $this->_addFields($contact); + + /* List files. */ + $v_params = $GLOBALS['injector']->getInstance('Horde_Vfs')->getConfig('documents'); + if ($v_params['type'] != 'none') { + $files = $contact->listFiles(); + if (is_a($files, 'PEAR_Error')) { + $notification->push($files, 'horde.error'); + } else { + $this->addVariable(_("Files"), '__vfs', 'html', false); + $vars->set('__vfs', implode('
', array_map(array($contact, 'vfsEditUrl'), $files))); + } + } + } + + /** + * Set up the Horde_Form fields for $contact's attributes. + */ + function _addFields($contact) + { + global $attributes; + + // Run through once to see what form actions, if any, we need + // to set up. + $actions = array(); + $map = $contact->driver->map; + $fields = array_keys($contact->driver->getCriteria()); + foreach ($fields as $field) { + if (is_array($map[$field])) { + foreach ($map[$field]['fields'] as $action_field) { + if (!isset($actions[$action_field])) { + $actions[$action_field] = array(); + } + $actions[$action_field]['fields'] = $map[$field]['fields']; + $actions[$action_field]['format'] = $map[$field]['format']; + $actions[$action_field]['target'] = $field; + } + } + } + + // Now run through and add the form variables. + $tabs = $contact->driver->tabs; + if (!count($tabs)) { + $tabs = array('' => $fields); + } + foreach ($tabs as $tab => $tab_fields) { + if (!empty($tab)) { + $this->setSection($tab, $tab); + } + foreach ($tab_fields as $field) { + if (!in_array($field, $fields) || + !isset($attributes[$field])) { + continue; + } + + $attribute = $attributes[$field]; + $params = isset($attribute['params']) ? $attribute['params'] : array(); + $desc = isset($attribute['desc']) ? $attribute['desc'] : null; + + if (is_array($map[$field])) { + $v = &$this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], false, false, $desc, $params); + $v->disable(); + } else { + $readonly = isset($attribute['readonly']) ? $attribute['readonly'] : null; + $v = &$this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], $attribute['required'], $readonly, $desc, $params); + + if (!empty($actions[$field])) { + $actionfields = array(); + foreach ($actions[$field]['fields'] as $f) { + $actionfields[] = 'object[' . $f . ']'; + } + $a = Horde_Form_Action::factory('updatefield', + array('format' => $actions[$field]['format'], + 'target' => 'object[' . $actions[$field]['target'] . ']', + 'fields' => $actionfields)); + $v->setAction($a); + } + } + + if (isset($attribute['default'])) { + $v->setDefault($attribute['default']); + } + } + } + } + +} diff --git a/turba/lib/Form/CreateAddressBook.php b/turba/lib/Form/CreateAddressBook.php new file mode 100644 index 000000000..fe682b255 --- /dev/null +++ b/turba/lib/Form/CreateAddressBook.php @@ -0,0 +1,48 @@ + + * @package Turba + */ +class Turba_Form_CreateAddressBook extends Horde_Form +{ + public function __construct(&$vars) + { + parent::__construct($vars, _("Create Address Book")); + + $this->addVariable(_("Name"), 'name', 'text', true); + $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60)); + + $this->setButtons(array(_("Create"))); + } + + function execute() + { + // Need a clean cfgSources array + include TURBA_BASE . '/config/sources.php'; + + $driver = Turba_Driver::singleton($cfgSources[$GLOBALS['conf']['shares']['source']]); + if ($driver instanceof PEAR_Error) { + return $driver; + } + + $params = array( + 'params' => array('source' => $GLOBALS['conf']['shares']['source']), + 'name' => $this->_vars->get('name'), + 'desc' => $this->_vars->get('description'), + ); + return $driver->createShare(md5(mt_rand()), $params); + } + +} diff --git a/turba/lib/Form/DeleteAddressBook.php b/turba/lib/Form/DeleteAddressBook.php new file mode 100644 index 000000000..e01c36e9d --- /dev/null +++ b/turba/lib/Form/DeleteAddressBook.php @@ -0,0 +1,84 @@ + + * @package Turba + */ +class Turba_Form_DeleteAddressBook extends Horde_Form +{ + /** + * Address book being deleted + */ + var $_addressbook; + + public function __construct(&$vars, &$addressbook) + { + $this->_addressbook = &$addressbook; + parent::__construct($vars, sprintf(_("Delete %s"), $addressbook->get('name'))); + + $this->addHidden('', 'a', 'text', true); + $this->addVariable(sprintf(_("Really delete the address book \"%s\"? This cannot be undone and all contacts in this address book will be permanently removed."), $this->_addressbook->get('name')), 'desc', 'description', false); + + $this->setButtons(array(_("Delete"), _("Cancel"))); + } + + /** + * @TODO Remove share from 'addressbooks' pref + */ + function execute() + { + // If cancel was clicked, return false. + if ($this->_vars->get('submitbutton') == _("Cancel")) { + return false; + } + + if (!$GLOBALS['registry']->getAuth() || + $this->_addressbook->get('owner') != $GLOBALS['registry']->getAuth()) { + return PEAR::raiseError(_("You do not have permissions to delete this address book.")); + } + + $driver = &Turba_Driver::singleton($this->_addressbook->getName()); + if (is_a($driver, 'PEAR_Error')) { + return $driver; + } + + // We have a Turba_Driver, try to delete the address book. + $result = $driver->deleteAll(); + if (is_a($result, 'PEAR_Error')) { + return $result; + } + + // Address book successfully deleted from backend, remove the + // share. + try { + $GLOBALS['turba_shares']->removeShare($this->_addressbook); + } catch (Horde_Share_Exception $e) { + Horde::logMessage($e->getMessage(), 'ERR'); + throw new Turba_Exception($e); + } + + if (isset($_SESSION['turba']['source']) && $_SESSION['turba']['source'] == Horde_Util::getFormData('deleteshare')) { + unset($_SESSION['turba']['source']); + } + + $abooks = json_decode($GLOBALS['prefs']->getValue('addressbooks')); + if (($pos = array_search($this->_addressbook->getName(), $abooks)) !== false) { + unset($abooks[$pos]); + $GLOBALS['prefs']->setValue('addressbooks', json_encode($abooks)); + } + + return true; + } + +} diff --git a/turba/lib/Form/EditAddressBook.php b/turba/lib/Form/EditAddressBook.php new file mode 100644 index 000000000..c0abb14f5 --- /dev/null +++ b/turba/lib/Form/EditAddressBook.php @@ -0,0 +1,48 @@ + + * @package Turba + */ +class Turba_Form_EditAddressBook extends Horde_Form +{ + /** + * Address book being edited + */ + var $_addressbook; + + public function __construct(&$vars, &$addressbook) + { + $this->_addressbook = &$addressbook; + parent::__construct($vars, sprintf(_("Edit %s"), $addressbook->get('name'))); + + $this->addHidden('', 'a', 'text', true); + $this->addVariable(_("Name"), 'name', 'text', true); + $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60)); + + $this->setButtons(array(_("Save"))); + } + + function execute() + { + $this->_addressbook->set('name', $this->_vars->get('name')); + $this->_addressbook->set('desc', $this->_vars->get('description')); + $result = $this->_addressbook->save(); + if (is_a($result, 'PEAR_Error')) { + return PEAR::raiseError(sprintf(_("Unable to save address book \"%s\": %s"), $id, $result->getMessage())); + } + return true; + } + +} diff --git a/turba/lib/Form/EditContact.php b/turba/lib/Form/EditContact.php new file mode 100644 index 000000000..baf4ea202 --- /dev/null +++ b/turba/lib/Form/EditContact.php @@ -0,0 +1,89 @@ +_contact = &$contact; + + $this->setButtons(_("Save")); + $this->addHidden('', 'url', 'text', false); + $this->addHidden('', 'source', 'text', true); + $this->addHidden('', 'key', 'text', false); + + parent::_addFields($this->_contact); + + if ($conf['documents']['type'] != 'none') { + $this->addVariable(_("Add file"), 'vfs', 'file', false); + } + + $object_values = $vars->get('object'); + $object_keys = array_keys($contact->attributes); + foreach ($object_keys as $info_key) { + if (!isset($object_values[$info_key])) { + $object_values[$info_key] = $contact->getValue($info_key); + } + } + $vars->set('object', $object_values); + $vars->set('source', $contact->getSource()); + } + + function getSource() + { + return $this->_source; + } + + function execute() + { + global $conf, $notification; + + if (!$this->validate($this->_vars)) { + return PEAR::raiseError('Invalid'); + } + + /* Form valid, save data. */ + $this->getInfo($this->_vars, $info); + + /* Update the contact. */ + foreach ($info['object'] as $info_key => $info_val) { + if ($info_key != '__key') { + if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) { + $this->_contact->setValue($info_key, file_get_contents($info_val['file'])); + if (isset($info_val['type'])) { + $this->_contact->setValue($info_key . 'type', $info_val['type']); + } + } else { + $this->_contact->setValue($info_key, $info_val); + } + } + } + + $result = $this->_contact->store(); + if (!is_a($result, 'PEAR_Error')) { + if ($conf['documents']['type'] != 'none' && isset($info['vfs'])) { + $result = $this->_contact->addFile($info['vfs']); + if (is_a($result, 'PEAR_Error')) { + $notification->push(sprintf(_("\"%s\" updated, but saving the uploaded file failed: %s"), $this->_contact->getValue('name'), $result->getMessage()), 'horde.warning'); + } else { + $notification->push(sprintf(_("\"%s\" updated."), $this->_contact->getValue('name')), 'horde.success'); + } + } else { + $notification->push(sprintf(_("\"%s\" updated."), $this->_contact->getValue('name')), 'horde.success'); + } + return true; + } else { + Horde::logMessage($result, 'ERR'); + $notification->push(_("There was an error saving the contact. Contact your system administrator for further help."), 'horde.error'); + return $result; + } + } + +} diff --git a/turba/lib/Form/EditContactGroup.php b/turba/lib/Form/EditContactGroup.php new file mode 100644 index 000000000..77f417275 --- /dev/null +++ b/turba/lib/Form/EditContactGroup.php @@ -0,0 +1,95 @@ +addHidden('', 'objectkeys', 'text', false); + $this->addHidden('', 'original_source', 'text', false); + $this->addHidden('', 'actionID', 'text', false); + + parent::Turba_Form_EditContact($vars, $contact); + $vars->set('actionID', 'groupedit'); + + $objectkeys = $vars->get('objectkeys'); + $source = $vars->get('source'); + $key = $vars->get('key'); + if ($source . ':' . $key == $objectkeys[0]) { + /* First contact */ + $this->setButtons(_("Next")); + } elseif ($source . ':' . $key == $objectkeys[count($objectkeys) - 1]) { + /* Last contact */ + $this->setButtons(_("Previous")); + } else { + /* In between */ + $this->setButtons(_("Previous")); + $this->appendButtons(_("Next")); + } + $this->appendButtons(_("Finish")); + } + + function renderActive($renderer, &$vars, $action, $method) + { + parent::renderActive($renderer, $vars, $action, $method); + + /* Read the columns to display from the preferences. */ + $source = $vars->get('source'); + $sources = Turba::getColumns(); + $columns = isset($sources[$source]) ? $sources[$source] : array(); + + $results = new Turba_List($vars->get('objectkeys')); + $listView = new Turba_View_List($results, array('Group' => true), $columns); + echo '
' . $listView->getPage($numDisplayed); + } + + function execute() + { + $result = parent::execute(); + if (is_a($result, 'PEAR_Error')) { + return $result; + } + + $this->getInfo($this->_vars, $info); + + $next_page = Horde::applicationUrl('edit.php', true); + $next_page = Horde_Util::addParameter($next_page, + array('source' => $info['source'], + 'original_source' => $info['original_source'], + 'objectkeys' => $info['objectkeys'], + 'url' => $info['url'], + 'actionID' => 'groupedit'), + null, false); + $objectkey = array_search($info['source'] . ':' . $info['key'], $info['objectkeys']); + + $submitbutton = $this->_vars->get('submitbutton'); + if ($submitbutton == _("Finish")) { + $next_page = Horde::url('browse.php', true); + if ($info['original_source'] == '**search') { + $next_page = Horde_Util::addParameter($next_page, 'key', $info['original_source'], false); + } else { + $next_page = Horde_Util::addParameter($next_page, 'source', $info['original_source'], false); + } + } elseif ($submitbutton == _("Previous") && $info['source'] . ':' . $info['key'] != $info['objectkeys'][0]) { + /* Previous contact */ + list(, $previous_key) = explode(':', $info['objectkeys'][$objectkey - 1]); + $next_page = Horde_Util::addParameter($next_page, 'key', $previous_key, false); + if ($this->getOpenSection()) { + $next_page = Horde_Util::addParameter($next_page, '__formOpenSection', $this->getOpenSection(), false); + } + } elseif ($submitbutton == _("Next") && + $info['source'] . ':' . $info['key'] != $info['objectkeys'][count($info['objectkeys']) - 1]) { + /* Next contact */ + list(, $next_key) = explode(':', $info['objectkeys'][$objectkey + 1]); + $next_page = Horde_Util::addParameter($next_page, 'key', $next_key, false); + if ($this->getOpenSection()) { + $next_page = Horde_Util::addParameter($next_page, '__formOpenSection', $this->getOpenSection(), false); + } + } + + header('Location: ' . $next_page); + exit; + } + +} diff --git a/turba/lib/Forms/AddContact.php b/turba/lib/Forms/AddContact.php deleted file mode 100644 index bf381f129..000000000 --- a/turba/lib/Forms/AddContact.php +++ /dev/null @@ -1,101 +0,0 @@ -_contact = &$contact; - - $this->setButtons(_("Add")); - $this->addHidden('', 'url', 'text', false); - $this->addHidden('', 'key', 'text', false); - - /* Check if a source selection box is required. */ - if (count($addSources) > 1) { - /* Multiple sources, show a selection box. */ - $options = array(); - foreach ($addSources as $key => $config) { - $options[$key] = $config['title']; - } - $v = &$this->addVariable(_("Choose an address book"), 'source', 'enum', true, false, null, array($options, true)); - $action = Horde_Form_Action::factory('submit'); - $v->setAction($action); - $v->setOption('trackchange', true); - if (is_null($vars->get('formname')) && - $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName())) { - $notification->push(sprintf(_("Selected address book \"%s\"."), $addSources[$vars->get('source')]['title']), 'horde.message'); - } - } else { - /* One source, no selection box but store the value in a - * hidden field. */ - $this->addHidden('', 'source', 'text', true); - } - - if ($this->_contact) { - parent::_addFields($this->_contact); - } - } - - function validate() - { - if (!$this->_vars->get('source')) { - return false; - } - return parent::validate($this->_vars); - } - - function execute() - { - global $driver, $notification; - - /* Form valid, save data. */ - $this->getInfo($this->_vars, $info); - $source = $info['source']; - foreach ($info['object'] as $info_key => $info_val) { - if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) { - $this->_contact->setValue($info_key, file_get_contents($info_val['file'])); - $this->_contact->setValue($info_key . 'type', $info_val['type']); - } else { - $this->_contact->setValue($info_key, $info_val); - } - } - $contact = $this->_contact->attributes; - unset($contact['__owner']); - - /* Create Contact. */ - $key = $driver->add($contact); - if (is_a($key, 'PEAR_Error')) { - Horde::logMessage($key, 'ERR'); - } else { - // Try 3 times to get the new entry. We retry to allow setups like - // LDAP replication to work. - for ($i = 0; $i < 3; $i++) { - $ob = $driver->getObject($key); - if (!is_a($ob, 'PEAR_Error')) { - $notification->push(sprintf(_("%s added."), $ob->getValue('name')), 'horde.success'); - header('Location: ' . (!empty($info['url']) ? $info['url'] : $ob->url('Contact', true))); - exit; - } - sleep(1); - } - Horde::logMessage($ob, 'ERR'); - } - - $notification->push(_("There was an error adding the new contact. Contact your system administrator for further help."), 'horde.error'); - } - -} diff --git a/turba/lib/Forms/Contact.php b/turba/lib/Forms/Contact.php deleted file mode 100644 index eb0da664d..000000000 --- a/turba/lib/Forms/Contact.php +++ /dev/null @@ -1,113 +0,0 @@ -driver->getCriteria() as $info_key => $info_val) { - $object[$info_key] = $contact->getValue($info_key); - } - $vars->set('object', $object); - - $this->_addFields($contact); - - /* List files. */ - $v_params = $GLOBALS['injector']->getInstance('Horde_Vfs')->getConfig('documents'); - if ($v_params['type'] != 'none') { - $files = $contact->listFiles(); - if (is_a($files, 'PEAR_Error')) { - $notification->push($files, 'horde.error'); - } else { - $this->addVariable(_("Files"), '__vfs', 'html', false); - $vars->set('__vfs', implode('
', array_map(array($contact, 'vfsEditUrl'), $files))); - } - } - } - - /** - * Set up the Horde_Form fields for $contact's attributes. - */ - function _addFields($contact) - { - global $attributes; - - // Run through once to see what form actions, if any, we need - // to set up. - $actions = array(); - $map = $contact->driver->map; - $fields = array_keys($contact->driver->getCriteria()); - foreach ($fields as $field) { - if (is_array($map[$field])) { - foreach ($map[$field]['fields'] as $action_field) { - if (!isset($actions[$action_field])) { - $actions[$action_field] = array(); - } - $actions[$action_field]['fields'] = $map[$field]['fields']; - $actions[$action_field]['format'] = $map[$field]['format']; - $actions[$action_field]['target'] = $field; - } - } - } - - // Now run through and add the form variables. - $tabs = $contact->driver->tabs; - if (!count($tabs)) { - $tabs = array('' => $fields); - } - foreach ($tabs as $tab => $tab_fields) { - if (!empty($tab)) { - $this->setSection($tab, $tab); - } - foreach ($tab_fields as $field) { - if (!in_array($field, $fields) || - !isset($attributes[$field])) { - continue; - } - - $attribute = $attributes[$field]; - $params = isset($attribute['params']) ? $attribute['params'] : array(); - $desc = isset($attribute['desc']) ? $attribute['desc'] : null; - - if (is_array($map[$field])) { - $v = &$this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], false, false, $desc, $params); - $v->disable(); - } else { - $readonly = isset($attribute['readonly']) ? $attribute['readonly'] : null; - $v = &$this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], $attribute['required'], $readonly, $desc, $params); - - if (!empty($actions[$field])) { - $actionfields = array(); - foreach ($actions[$field]['fields'] as $f) { - $actionfields[] = 'object[' . $f . ']'; - } - $a = Horde_Form_Action::factory('updatefield', - array('format' => $actions[$field]['format'], - 'target' => 'object[' . $actions[$field]['target'] . ']', - 'fields' => $actionfields)); - $v->setAction($a); - } - } - - if (isset($attribute['default'])) { - $v->setDefault($attribute['default']); - } - } - } - } - -} diff --git a/turba/lib/Forms/CreateAddressBook.php b/turba/lib/Forms/CreateAddressBook.php deleted file mode 100644 index 3bc8e7961..000000000 --- a/turba/lib/Forms/CreateAddressBook.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @package Turba - */ -class Turba_CreateAddressBookForm extends Horde_Form { - - function Turba_CreateAddressBookForm(&$vars) - { - parent::Horde_Form($vars, _("Create Address Book")); - - $this->addVariable(_("Name"), 'name', 'text', true); - $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60)); - - $this->setButtons(array(_("Create"))); - } - - function execute() - { - // Need a clean cfgSources array - include TURBA_BASE . '/config/sources.php'; - - $driver = Turba_Driver::singleton($cfgSources[$GLOBALS['conf']['shares']['source']]); - if ($driver instanceof PEAR_Error) { - return $driver; - } - - $params = array( - 'params' => array('source' => $GLOBALS['conf']['shares']['source']), - 'name' => $this->_vars->get('name'), - 'desc' => $this->_vars->get('description'), - ); - return $driver->createShare(md5(mt_rand()), $params); - } - -} diff --git a/turba/lib/Forms/DeleteAddressBook.php b/turba/lib/Forms/DeleteAddressBook.php deleted file mode 100644 index 94075fc04..000000000 --- a/turba/lib/Forms/DeleteAddressBook.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @package Turba - */ -class Turba_DeleteAddressBookForm extends Horde_Form { - - /** - * Address book being deleted - */ - var $_addressbook; - - function Turba_DeleteAddressBookForm(&$vars, &$addressbook) - { - $this->_addressbook = &$addressbook; - parent::Horde_Form($vars, sprintf(_("Delete %s"), $addressbook->get('name'))); - - $this->addHidden('', 'a', 'text', true); - $this->addVariable(sprintf(_("Really delete the address book \"%s\"? This cannot be undone and all contacts in this address book will be permanently removed."), $this->_addressbook->get('name')), 'desc', 'description', false); - - $this->setButtons(array(_("Delete"), _("Cancel"))); - } - - /** - * @TODO Remove share from 'addressbooks' pref - */ - function execute() - { - // If cancel was clicked, return false. - if ($this->_vars->get('submitbutton') == _("Cancel")) { - return false; - } - - if (!$GLOBALS['registry']->getAuth() || - $this->_addressbook->get('owner') != $GLOBALS['registry']->getAuth()) { - return PEAR::raiseError(_("You do not have permissions to delete this address book.")); - } - - $driver = &Turba_Driver::singleton($this->_addressbook->getName()); - if (is_a($driver, 'PEAR_Error')) { - return $driver; - } - - // We have a Turba_Driver, try to delete the address book. - $result = $driver->deleteAll(); - if (is_a($result, 'PEAR_Error')) { - return $result; - } - - // Address book successfully deleted from backend, remove the - // share. - try { - $GLOBALS['turba_shares']->removeShare($this->_addressbook); - } catch (Horde_Share_Exception $e) { - Horde::logMessage($e->getMessage(), 'ERR'); - throw new Turba_Exception($e); - } - - if (isset($_SESSION['turba']['source']) && $_SESSION['turba']['source'] == Horde_Util::getFormData('deleteshare')) { - unset($_SESSION['turba']['source']); - } - - $abooks = json_decode($GLOBALS['prefs']->getValue('addressbooks')); - if (($pos = array_search($this->_addressbook->getName(), $abooks)) !== false) { - unset($abooks[$pos]); - $GLOBALS['prefs']->setValue('addressbooks', json_encode($abooks)); - } - - return true; - } - -} diff --git a/turba/lib/Forms/EditAddressBook.php b/turba/lib/Forms/EditAddressBook.php deleted file mode 100644 index 79ac2abce..000000000 --- a/turba/lib/Forms/EditAddressBook.php +++ /dev/null @@ -1,51 +0,0 @@ - - * @package Turba - */ -class Turba_EditAddressBookForm extends Horde_Form { - - /** - * Address book being edited - */ - var $_addressbook; - - function Turba_EditAddressBookForm(&$vars, &$addressbook) - { - $this->_addressbook = &$addressbook; - parent::Horde_Form($vars, sprintf(_("Edit %s"), $addressbook->get('name'))); - - $this->addHidden('', 'a', 'text', true); - $this->addVariable(_("Name"), 'name', 'text', true); - $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60)); - - $this->setButtons(array(_("Save"))); - } - - function execute() - { - $this->_addressbook->set('name', $this->_vars->get('name')); - $this->_addressbook->set('desc', $this->_vars->get('description')); - $result = $this->_addressbook->save(); - if (is_a($result, 'PEAR_Error')) { - return PEAR::raiseError(sprintf(_("Unable to save address book \"%s\": %s"), $id, $result->getMessage())); - } - return true; - } - -} diff --git a/turba/lib/Forms/EditContact.php b/turba/lib/Forms/EditContact.php deleted file mode 100644 index 65bf06314..000000000 --- a/turba/lib/Forms/EditContact.php +++ /dev/null @@ -1,191 +0,0 @@ -_contact = &$contact; - - $this->setButtons(_("Save")); - $this->addHidden('', 'url', 'text', false); - $this->addHidden('', 'source', 'text', true); - $this->addHidden('', 'key', 'text', false); - - parent::_addFields($this->_contact); - - if ($conf['documents']['type'] != 'none') { - $this->addVariable(_("Add file"), 'vfs', 'file', false); - } - - $object_values = $vars->get('object'); - $object_keys = array_keys($contact->attributes); - foreach ($object_keys as $info_key) { - if (!isset($object_values[$info_key])) { - $object_values[$info_key] = $contact->getValue($info_key); - } - } - $vars->set('object', $object_values); - $vars->set('source', $contact->getSource()); - } - - function getSource() - { - return $this->_source; - } - - function execute() - { - global $conf, $notification; - - if (!$this->validate($this->_vars)) { - return PEAR::raiseError('Invalid'); - } - - /* Form valid, save data. */ - $this->getInfo($this->_vars, $info); - - /* Update the contact. */ - foreach ($info['object'] as $info_key => $info_val) { - if ($info_key != '__key') { - if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) { - $this->_contact->setValue($info_key, file_get_contents($info_val['file'])); - if (isset($info_val['type'])) { - $this->_contact->setValue($info_key . 'type', $info_val['type']); - } - } else { - $this->_contact->setValue($info_key, $info_val); - } - } - } - - $result = $this->_contact->store(); - if (!is_a($result, 'PEAR_Error')) { - if ($conf['documents']['type'] != 'none' && isset($info['vfs'])) { - $result = $this->_contact->addFile($info['vfs']); - if (is_a($result, 'PEAR_Error')) { - $notification->push(sprintf(_("\"%s\" updated, but saving the uploaded file failed: %s"), $this->_contact->getValue('name'), $result->getMessage()), 'horde.warning'); - } else { - $notification->push(sprintf(_("\"%s\" updated."), $this->_contact->getValue('name')), 'horde.success'); - } - } else { - $notification->push(sprintf(_("\"%s\" updated."), $this->_contact->getValue('name')), 'horde.success'); - } - return true; - } else { - Horde::logMessage($result, 'ERR'); - $notification->push(_("There was an error saving the contact. Contact your system administrator for further help."), 'horde.error'); - return $result; - } - } - -} - -/** - * @package Turba - */ -class Turba_EditContactGroupForm extends Turba_EditContactForm { - - function Turba_EditContactGroupForm(&$vars, &$contact) - { - $this->addHidden('', 'objectkeys', 'text', false); - $this->addHidden('', 'original_source', 'text', false); - $this->addHidden('', 'actionID', 'text', false); - - parent::Turba_EditContactForm($vars, $contact); - $vars->set('actionID', 'groupedit'); - - $objectkeys = $vars->get('objectkeys'); - $source = $vars->get('source'); - $key = $vars->get('key'); - if ($source . ':' . $key == $objectkeys[0]) { - /* First contact */ - $this->setButtons(_("Next")); - } elseif ($source . ':' . $key == $objectkeys[count($objectkeys) - 1]) { - /* Last contact */ - $this->setButtons(_("Previous")); - } else { - /* In between */ - $this->setButtons(_("Previous")); - $this->appendButtons(_("Next")); - } - $this->appendButtons(_("Finish")); - } - - function renderActive($renderer, &$vars, $action, $method) - { - parent::renderActive($renderer, $vars, $action, $method); - - /* Read the columns to display from the preferences. */ - $source = $vars->get('source'); - $sources = Turba::getColumns(); - $columns = isset($sources[$source]) ? $sources[$source] : array(); - - $results = new Turba_List($vars->get('objectkeys')); - $listView = new Turba_View_List($results, array('Group' => true), $columns); - echo '
' . $listView->getPage($numDisplayed); - } - - function execute() - { - $result = parent::execute(); - if (is_a($result, 'PEAR_Error')) { - return $result; - } - - $this->getInfo($this->_vars, $info); - - $next_page = Horde::applicationUrl('edit.php', true); - $next_page = Horde_Util::addParameter($next_page, - array('source' => $info['source'], - 'original_source' => $info['original_source'], - 'objectkeys' => $info['objectkeys'], - 'url' => $info['url'], - 'actionID' => 'groupedit'), - null, false); - $objectkey = array_search($info['source'] . ':' . $info['key'], $info['objectkeys']); - - $submitbutton = $this->_vars->get('submitbutton'); - if ($submitbutton == _("Finish")) { - $next_page = Horde::url('browse.php', true); - if ($info['original_source'] == '**search') { - $next_page = Horde_Util::addParameter($next_page, 'key', $info['original_source'], false); - } else { - $next_page = Horde_Util::addParameter($next_page, 'source', $info['original_source'], false); - } - } elseif ($submitbutton == _("Previous") && $info['source'] . ':' . $info['key'] != $info['objectkeys'][0]) { - /* Previous contact */ - list(, $previous_key) = explode(':', $info['objectkeys'][$objectkey - 1]); - $next_page = Horde_Util::addParameter($next_page, 'key', $previous_key, false); - if ($this->getOpenSection()) { - $next_page = Horde_Util::addParameter($next_page, '__formOpenSection', $this->getOpenSection(), false); - } - } elseif ($submitbutton == _("Next") && - $info['source'] . ':' . $info['key'] != $info['objectkeys'][count($info['objectkeys']) - 1]) { - /* Next contact */ - list(, $next_key) = explode(':', $info['objectkeys'][$objectkey + 1]); - $next_page = Horde_Util::addParameter($next_page, 'key', $next_key, false); - if ($this->getOpenSection()) { - $next_page = Horde_Util::addParameter($next_page, '__formOpenSection', $this->getOpenSection(), false); - } - } - - header('Location: ' . $next_page); - exit; - } - -} diff --git a/turba/lib/View/Contact.php b/turba/lib/View/Contact.php index a44eb3aed..b3ec22251 100644 --- a/turba/lib/View/Contact.php +++ b/turba/lib/View/Contact.php @@ -1,7 +1,4 @@ contact); + $form = new Turba_Form_Contact($vars, $this->contact); $userId = $GLOBALS['registry']->getAuth(); /* Get the contact's history. */ diff --git a/turba/lib/View/EditContact.php b/turba/lib/View/EditContact.php index 1c7da086a..e04e1d4d2 100644 --- a/turba/lib/View/EditContact.php +++ b/turba/lib/View/EditContact.php @@ -1,7 +1,4 @@ '; - $form = &new Turba_EditContactForm($vars, $this->contact); + $form = &new Turba_Form_EditContact($vars, $this->contact); $form->renderActive(new Horde_Form_Renderer, $vars, 'edit.php', 'post'); echo '';