1: <?php
2: 3: 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:
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:
22: if (count($addSources) > 1) {
23:
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: 38:
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:
58:
59: global $driver, $notification;
60:
61:
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:
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:
85:
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: