Overview

Packages

  • Horde
    • Data
  • None
  • Turba

Classes

  • Turba
  • Turba_Api
  • Turba_Driver
  • Turba_Driver_Facebook
  • Turba_Driver_Favourites
  • Turba_Driver_Group
  • Turba_Driver_Imsp
  • Turba_Driver_Kolab
  • Turba_Driver_Ldap
  • Turba_Driver_Prefs
  • Turba_Driver_Share
  • Turba_Driver_Sql
  • Turba_Driver_Vbook
  • Turba_Exception
  • Turba_Factory_Driver
  • Turba_Form_AddContact
  • Turba_Form_Contact
  • Turba_Form_ContactBase
  • Turba_Form_CreateAddressBook
  • Turba_Form_DeleteAddressBook
  • Turba_Form_EditAddressBook
  • Turba_Form_EditContact
  • Turba_Form_EditContactGroup
  • Turba_List
  • Turba_LoginTasks_SystemTask_Upgrade
  • Turba_Object
  • Turba_Object_Group
  • Turba_Test
  • Turba_View_Browse
  • Turba_View_Contact
  • Turba_View_DeleteContact
  • Turba_View_Duplicates
  • Turba_View_EditContact
  • Turba_View_List
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @package Turba
  4:  */
  5: class Turba_Form_AddContact extends Turba_Form_ContactBase
  6: {
  7:     protected $_contact = null;
  8: 
  9:     public function __construct($vars, Turba_Object $contact)
 10:     {
 11:         // @TODO: $addSources should be injected
 12:         global $addSources, $notification;
 13: 
 14:         parent::__construct($vars, '', 'turba_form_addcontact');
 15:         $this->_contact = $contact;
 16: 
 17:         $this->setButtons(_("Add"));
 18:         $this->addHidden('', 'url', 'text', false);
 19:         $this->addHidden('', 'key', 'text', false);
 20: 
 21:         /* Check if a source selection box is required. */
 22:         if (count($addSources) > 1) {
 23:             /* Multiple sources, show a selection box. */
 24:             $options = array();
 25:             foreach ($addSources as $key => $config) {
 26:                 $options[$key] = $config['title'];
 27:             }
 28:             $v = $this->addVariable(_("Choose an address book"), 'source', 'enum', true, false, null, array($options, true));
 29:             $action = Horde_Form_Action::factory('submit');
 30:             $v->setAction($action);
 31:             $v->setOption('trackchange', true);
 32:             if (is_null($vars->get('formname')) &&
 33:                 $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName())) {
 34:                 $notification->push(sprintf(_("Selected address book \"%s\"."), $addSources[$vars->get('source')]['title']), 'horde.message');
 35:             }
 36:         } else {
 37:             /* One source, no selection box but store the value in a
 38:              * hidden field. */
 39:             $this->addHidden('', 'source', 'text', true);
 40:         }
 41: 
 42:         if ($this->_contact) {
 43:             parent::_addFields($this->_contact);
 44:         }
 45:     }
 46: 
 47:     public function validate()
 48:     {
 49:         if (!$this->_vars->get('source')) {
 50:             return false;
 51:         }
 52:         return parent::validate($this->_vars);
 53:     }
 54: 
 55:     public function execute()
 56:     {
 57:         // @TODO $driver should be injected, or at the very least, obtained
 58:         //       via the injector
 59:         global $driver, $notification;
 60: 
 61:         /* Form valid, save data. */
 62:         $this->getInfo($this->_vars, $info);
 63:         $source = $info['source'];
 64:         foreach ($info['object'] as $info_key => $info_val) {
 65:             if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) {
 66:                 $this->_contact->setValue($info_key, file_get_contents($info_val['file']));
 67:                 $this->_contact->setValue($info_key . 'type', $info_val['type']);
 68:             } else {
 69:                 $this->_contact->setValue($info_key, $info_val);
 70:             }
 71:         }
 72:         $contact = $this->_contact->attributes;
 73:         unset($contact['__owner']);
 74: 
 75:         /* Create Contact. */
 76:         try {
 77:             $key = $driver->add($contact);
 78:         } catch (Turba_Exception $e) {
 79:             Horde::logMessage($e, 'ERR');
 80:             $key = null;
 81:         }
 82: 
 83:         if ($key) {
 84:             // Try 3 times to get the new entry. We retry to allow setups like
 85:             // LDAP replication to work.
 86:             for ($i = 0; $i < 3; ++$i) {
 87:                 try {
 88:                     $ob = $driver->getObject($key);
 89:                     $notification->push(sprintf(_("%s added."), $ob->getValue('name')), 'horde.success');
 90:                     $url = empty($info['url'])
 91:                         ? $ob->url('Contact', true)
 92:                         : new Horde_Url($info['url']);
 93:                     $url->redirect();
 94:                 } catch (Turba_Exception $e) {}
 95:                 sleep(1);
 96:             }
 97:         }
 98: 
 99:         $notification->push(_("There was an error adding the new contact. Contact your system administrator for further help."), 'horde.error');
100:     }
101: 
102: }
103: 
API documentation generated by ApiGen