5
class Turba_Form_AddContact extends Turba_Form_ContactBase
7
protected $_contact = null;
9
public function __construct($vars, Turba_Object $contact)
11
// @TODO: $addSources should be injected
12
global $addSources, $notification;
14
parent::__construct($vars, '', 'turba_form_addcontact');
15
$this->_contact = $contact;
17
$this->setButtons(_("Add"));
18
$this->addHidden('', 'url', 'text', false);
19
$this->addHidden('', 'key', 'text', false);
21
/* Check if a source selection box is required. */
22
if (count($addSources) > 1) {
23
/* Multiple sources, show a selection box. */
25
foreach ($addSources as $key => $config) {
26
$options[$key] = $config['title'];
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');
37
/* One source, no selection box but store the value in a
39
$this->addHidden('', 'source', 'text', true);
42
if ($this->_contact) {
43
parent::_addFields($this->_contact);
47
public function validate()
49
if (!$this->_vars->get('source')) {
52
return parent::validate($this->_vars);
55
public function execute()
57
// @TODO $driver should be injected, or at the very least, obtained
59
global $driver, $notification;
61
/* Form valid, save data. */
62
$this->getInfo($this->_vars, $info);
63
foreach ($info['object'] as $info_key => $info_val) {
64
if ($GLOBALS['attributes'][$info_key]['type'] == 'image' && !empty($info_val['file'])) {
65
$this->_contact->setValue($info_key, file_get_contents($info_val['file']));
66
$this->_contact->setValue($info_key . 'type', $info_val['type']);
68
$this->_contact->setValue($info_key, $info_val);
71
$contact = $this->_contact->attributes;
72
unset($contact['__owner']);
76
$key = $driver->add($contact);
77
} catch (Turba_Exception $e) {
78
Horde::logMessage($e, 'ERR');
83
// Try 3 times to get the new entry. We retry to allow setups like
84
// LDAP replication to work.
85
for ($i = 0; $i < 3; ++$i) {
87
$ob = $driver->getObject($key);
88
$notification->push(sprintf(_("%s added."), $ob->getValue('name')), 'horde.success');
89
$url = empty($info['url'])
90
? $ob->url('Contact', true)
91
: new Horde_Url($info['url']);
93
} catch (Horde_Exception_NotFound $e) {
99
$notification->push(_("There was an error adding the new contact. Contact your system administrator for further help."), 'horde.error');