File: AddContact.php

package info (click to toggle)
turba2 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,332 kB
  • ctags: 2,927
  • sloc: php: 11,046; xml: 1,690; sql: 507; makefile: 62; perl: 17; sh: 1
file content (93 lines) | stat: -rw-r--r-- 3,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
 * @package Turba
 *
 * $Horde: turba/lib/Forms/AddContact.php,v 1.9.2.1 2007/12/20 14:34:30 jan Exp $
 */

/** Turba_ContactForm */
require_once dirname(__FILE__) . '/Contact.php';

/**
 * @package Turba
 */
class Turba_AddContactForm extends Turba_ContactForm {

    var $_contact = null;

    function Turba_AddContactForm(&$vars, &$contact)
    {
        global $addSources, $notification;

        parent::Horde_Form($vars, _("Add Contact"));
        $this->_contact = &$contact;

        $this->setButtons(_("Add"));
        $this->addHidden('', 'url', 'text', false);
        $this->addHidden('', 'key', 'text', false);

        /* Check if a source selection box is required. */
        if (count($addSources) > 1) {
            /* Multiple sources, show a selection box. */
            $options = array();
            foreach ($addSources as $key => $config) {
                $options[$key] = $config['title'];
            }
            $v = &$this->addVariable(_("Choose an address book"), 'source', 'enum', true, false, null, array($options, true));
            $action = Horde_Form_Action::factory('submit');
            $v->setAction($action);
            $v->setOption('trackchange', true);
            if (is_null($vars->get('formname')) &&
                $vars->get($v->getVarName()) != $vars->get('__old_' . $v->getVarName())) {
                $notification->push(sprintf(_("Selected address book \"%s\"."), $addSources[$vars->get('source')]['title']), 'horde.message');
            }
        } else {
            /* One source, no selection box but store the value in a
             * hidden field. */
            $this->addHidden('', 'source', 'text', true);
        }

        if ($this->_contact) {
            parent::_addFields($this->_contact);
        }
    }

    function validate()
    {
        if (!$this->_vars->get('source')) {
            return false;
        }
        return parent::validate($this->_vars);
    }

    function execute()
    {
        global $driver, $notification;

        /* Form valid, save data. */
        $this->getInfo($this->_vars, $info);
        $source = $info['source'];
        foreach ($info['object'] as $info_key => $info_val) {
            $this->_contact->setValue($info_key, $info_val);
        }
        $contact = $this->_contact->attributes;
        unset($contact['__owner']);

        /* Create Contact. */
        $key = $driver->add($contact);
        if (is_a($key, 'PEAR_Error')) {
            Horde::logMessage($key, __FILE__, __LINE__, PEAR_LOG_ERR);
        } else {
            $ob = $driver->getObject($key);
            if (!is_a($ob, 'PEAR_Error')) {
                $notification->push(sprintf(_("%s added."), $ob->getValue('name')), 'horde.success');
                header('Location: ' . (!empty($info['url']) ? $info['url'] : $ob->url('Contact', true)));
                exit;
            }
            Horde::logMessage($ob, __FILE__, __LINE__, PEAR_LOG_ERR);
        }

        $notification->push(_("There was an error adding the new contact. Contact your system administrator for further help."), 'horde.error');
    }

}