~ubuntu-branches/debian/sid/php-horde-turba/sid

« back to all changes in this revision

Viewing changes to turba-4.1.1/lib/Form/AddContact.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 13:16:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130811131625-z91stjvq51jr9onv
Tags: 4.1.1-1
New upstream version 4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        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']);
 
67
            } else {
 
68
                $this->_contact->setValue($info_key, $info_val);
 
69
            }
 
70
        }
 
71
        $contact = $this->_contact->attributes;
 
72
        unset($contact['__owner']);
 
73
 
 
74
        /* Create Contact. */
 
75
        try {
 
76
            $key = $driver->add($contact);
 
77
        } catch (Turba_Exception $e) {
 
78
            Horde::logMessage($e, 'ERR');
 
79
            $key = null;
 
80
        }
 
81
 
 
82
        if ($key) {
 
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) {
 
86
                try {
 
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']);
 
92
                    $url->redirect();
 
93
                } catch (Horde_Exception_NotFound $e) {
 
94
                }
 
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
}