From 2bb7416f9fa0ba83dd402aa19bd3286e0fc96c26 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 2 Jul 2010 17:42:39 +0200 Subject: [PATCH] Reorganize forms. --- turba/add.php | 4 +- turba/addressbooks/create.php | 4 +- turba/addressbooks/delete.php | 4 +- turba/addressbooks/edit.php | 4 +- turba/edit.php | 6 +- turba/lib/{Forms => Form}/AddContact.php | 13 +-- turba/lib/{Forms => Form}/Contact.php | 15 +-- turba/lib/{Forms => Form}/CreateAddressBook.php | 13 +-- turba/lib/{Forms => Form}/DeleteAddressBook.php | 13 +-- turba/lib/{Forms => Form}/EditAddressBook.php | 13 +-- turba/lib/Form/EditContact.php | 89 ++++++++++++++++++ .../EditContact.php => Form/EditContactGroup.php} | 104 +-------------------- turba/lib/View/Contact.php | 5 +- turba/lib/View/EditContact.php | 5 +- 14 files changed, 123 insertions(+), 169 deletions(-) rename turba/lib/{Forms => Form}/AddContact.php (94%) rename turba/lib/{Forms => Form}/Contact.php (94%) rename turba/lib/{Forms => Form}/CreateAddressBook.php (79%) rename turba/lib/{Forms => Form}/DeleteAddressBook.php (87%) rename turba/lib/{Forms => Form}/EditAddressBook.php (77%) create mode 100644 turba/lib/Form/EditContact.php rename turba/lib/{Forms/EditContact.php => Form/EditContactGroup.php} (53%) 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/Forms/AddContact.php b/turba/lib/Form/AddContact.php similarity index 94% rename from turba/lib/Forms/AddContact.php rename to turba/lib/Form/AddContact.php index bf381f129..5c3b0f892 100644 --- a/turba/lib/Forms/AddContact.php +++ b/turba/lib/Form/AddContact.php @@ -2,18 +2,11 @@ /** * @package Turba */ - -/** Turba_ContactForm */ -require_once dirname(__FILE__) . '/Contact.php'; - -/** - * @package Turba - */ -class Turba_AddContactForm extends Turba_ContactForm { - +class Turba_Form_AddContact extends Turba_Form_Contact +{ var $_contact = null; - function Turba_AddContactForm(&$vars, &$contact) + public function __construct(&$vars, &$contact) { global $addSources, $notification; diff --git a/turba/lib/Forms/Contact.php b/turba/lib/Form/Contact.php similarity index 94% rename from turba/lib/Forms/Contact.php rename to turba/lib/Form/Contact.php index eb0da664d..a66c1a9c0 100644 --- a/turba/lib/Forms/Contact.php +++ b/turba/lib/Form/Contact.php @@ -2,20 +2,13 @@ /** * @package Turba */ - -/** Horde_Form_Action */ -require_once 'Horde/Form/Action.php'; - -/** - * @package Turba - */ -class Turba_ContactForm extends Horde_Form { - - function Turba_ContactForm(&$vars, &$contact) +class Turba_Form_Contact extends Horde_Form +{ + public function __construct(&$vars, &$contact) { global $conf, $notification; - parent::Horde_Form($vars, '', 'Turba_View_Contact'); + parent::__construct($vars, '', 'Turba_View_Contact'); /* Get the values through the Turba_Object class. */ $object = array(); diff --git a/turba/lib/Forms/CreateAddressBook.php b/turba/lib/Form/CreateAddressBook.php similarity index 79% rename from turba/lib/Forms/CreateAddressBook.php rename to turba/lib/Form/CreateAddressBook.php index 3bc8e7961..fe682b255 100644 --- a/turba/lib/Forms/CreateAddressBook.php +++ b/turba/lib/Form/CreateAddressBook.php @@ -8,21 +8,18 @@ * @package Turba */ -/** Horde_Form_Renderer */ -require_once 'Horde/Form/Renderer.php'; - /** - * The Turba_CreateAddressBookForm class provides the form for + * The Turba_Form_CreateAddressBook class provides the form for * creating an address book. * * @author Chuck Hagenbuch * @package Turba */ -class Turba_CreateAddressBookForm extends Horde_Form { - - function Turba_CreateAddressBookForm(&$vars) +class Turba_Form_CreateAddressBook extends Horde_Form +{ + public function __construct(&$vars) { - parent::Horde_Form($vars, _("Create Address Book")); + parent::__construct($vars, _("Create Address Book")); $this->addVariable(_("Name"), 'name', 'text', true); $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60)); diff --git a/turba/lib/Forms/DeleteAddressBook.php b/turba/lib/Form/DeleteAddressBook.php similarity index 87% rename from turba/lib/Forms/DeleteAddressBook.php rename to turba/lib/Form/DeleteAddressBook.php index 94075fc04..e01c36e9d 100644 --- a/turba/lib/Forms/DeleteAddressBook.php +++ b/turba/lib/Form/DeleteAddressBook.php @@ -8,27 +8,24 @@ * @package Turba */ -/** Horde_Form_Renderer */ -require_once 'Horde/Form/Renderer.php'; - /** - * The Turba_DeleteAddressbookForm class provides the form for + * The Turba_Form_DeleteAddressbook class provides the form for * deleting an address book. * * @author Chuck Hagenbuch * @package Turba */ -class Turba_DeleteAddressBookForm extends Horde_Form { - +class Turba_Form_DeleteAddressBook extends Horde_Form +{ /** * Address book being deleted */ var $_addressbook; - function Turba_DeleteAddressBookForm(&$vars, &$addressbook) + public function __construct(&$vars, &$addressbook) { $this->_addressbook = &$addressbook; - parent::Horde_Form($vars, sprintf(_("Delete %s"), $addressbook->get('name'))); + 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); diff --git a/turba/lib/Forms/EditAddressBook.php b/turba/lib/Form/EditAddressBook.php similarity index 77% rename from turba/lib/Forms/EditAddressBook.php rename to turba/lib/Form/EditAddressBook.php index 79ac2abce..c0abb14f5 100644 --- a/turba/lib/Forms/EditAddressBook.php +++ b/turba/lib/Form/EditAddressBook.php @@ -8,27 +8,24 @@ * @package Turba */ -/** Horde_Form_Renderer */ -require_once 'Horde/Form/Renderer.php'; - /** - * The Turba_EditAddressBookForm class provides the form for + * The Turba_Form_EditAddressBook class provides the form for * editing an address book. * * @author Chuck Hagenbuch * @package Turba */ -class Turba_EditAddressBookForm extends Horde_Form { - +class Turba_Form_EditAddressBook extends Horde_Form +{ /** * Address book being edited */ var $_addressbook; - function Turba_EditAddressBookForm(&$vars, &$addressbook) + public function __construct(&$vars, &$addressbook) { $this->_addressbook = &$addressbook; - parent::Horde_Form($vars, sprintf(_("Edit %s"), $addressbook->get('name'))); + parent::__construct($vars, sprintf(_("Edit %s"), $addressbook->get('name'))); $this->addHidden('', 'a', 'text', true); $this->addVariable(_("Name"), 'name', 'text', 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/Forms/EditContact.php b/turba/lib/Form/EditContactGroup.php similarity index 53% rename from turba/lib/Forms/EditContact.php rename to turba/lib/Form/EditContactGroup.php index 65bf06314..77f417275 100644 --- a/turba/lib/Forms/EditContact.php +++ b/turba/lib/Form/EditContactGroup.php @@ -2,111 +2,15 @@ /** * @package Turba */ - -/** Turba_ContactForm */ -require_once dirname(__FILE__) . '/Contact.php'; - -/** - * @package Turba - */ -class Turba_EditContactForm extends Turba_ContactForm { - - var $_source; - var $_contact; - - function Turba_EditContactForm(&$vars, &$contact) - { - global $conf; - - parent::Horde_Form($vars, '', 'Turba_View_EditContact'); - $this->_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) +class Turba_Form_EditContactGroup extends Turba_Form_EditContact +{ + public function __construct(&$vars, &$contact) { $this->addHidden('', 'objectkeys', 'text', false); $this->addHidden('', 'original_source', 'text', false); $this->addHidden('', 'actionID', 'text', false); - parent::Turba_EditContactForm($vars, $contact); + parent::Turba_Form_EditContact($vars, $contact); $vars->set('actionID', 'groupedit'); $objectkeys = $vars->get('objectkeys'); 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 ''; -- 2.11.0