1<?php
2/**
3 * Base class for forms dealing with a contact.
4 *
5 * @package Turba
6 */
7abstract class Turba_Form_ContactBase extends Horde_Form
8{
9    /**
10     * Set up the Horde_Form fields for $contact's attributes.
11     *
12     * @param Turba_Object $contact  The contact
13     */
14    protected function _addFields(Turba_Object $contact, $useTabs = true)
15    {
16        // @TODO: inject this
17        global $attributes, $injector;
18
19        // Run through once to see what form actions, if any, we need
20        // to set up.
21        $actions = array();
22        $map = $contact->driver->map;
23        $fields = array_keys($contact->driver->getCriteria());
24        foreach ($fields as $field) {
25            if (is_array($map[$field])) {
26                foreach ($map[$field]['fields'] as $action_field) {
27                    if (!isset($actions[$action_field])) {
28                        $actions[$action_field] = array();
29                    }
30                    $actions[$action_field]['fields'] = $map[$field]['fields'];
31                    $actions[$action_field]['format'] = $map[$field]['format'];
32                    $actions[$action_field]['target'] = $field;
33                }
34            }
35        }
36
37        // Now run through and add the form variables.
38        $tabs = $contact->driver->tabs;
39        if (!count($tabs)) {
40            $tabs = array('' => $fields);
41        }
42        $i = 0;
43        foreach ($tabs as $tab => $tab_fields) {
44            if (!empty($tab)) {
45                if ($useTabs) {
46                    $this->setSection($i++, $tab);
47                } else {
48                    $this->addVariable($tab, '', 'header', false);
49                }
50            }
51            foreach ($tab_fields as $field) {
52                if (!in_array($field, $fields) ||
53                    !isset($attributes[$field])) {
54                    continue;
55                }
56
57                $attribute = $attributes[$field];
58                $params = isset($attribute['params']) ? $attribute['params'] : array();
59                $desc = isset($attribute['desc']) ? $attribute['desc'] : null;
60
61                if (is_array($map[$field])) {
62                    $v = $this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], false, false, $desc, $params);
63                    $v->disable();
64                } else {
65                    $readonly = isset($attribute['readonly']) ? $attribute['readonly'] : null;
66                    $v = $this->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], $attribute['required'], $readonly, $desc, $params);
67
68                    if (!empty($actions[$field])) {
69                        $actionfields = array();
70                        foreach ($actions[$field]['fields'] as $f) {
71                            $actionfields[] = $this->_getId('object[' . $f . ']');
72                        }
73                        $a = Horde_Form_Action::factory('updatefield',
74                                                        array('format' => $actions[$field]['format'],
75                                                              'target' => $this->_getId('object[' . $actions[$field]['target'] . ']'),
76                                                              'fields' => $actionfields));
77                        $v->setAction($a);
78                    }
79                }
80
81                if (isset($attribute['default'])) {
82                    $v->setDefault($attribute['default']);
83                }
84            }
85        }
86
87        /* Add tags. */
88        if (isset($map['__uid']) &&
89            ($tagger = $injector->getInstance('Turba_Tagger')) &&
90            !($tagger instanceof Horde_Core_Tagger_Null)) {
91            $this->addVariable(
92                _("Tags"),
93                'object[__tags]',
94                'Turba:TurbaTags',
95                false
96            );
97        }
98    }
99
100    /**
101     * Converts a field name into an element ID as used in Horde_Form.
102     *
103     * @param string $id  A form field name.
104     *
105     * @return string  The ID for the form field.
106     */
107    protected function _getId($id)
108    {
109        return preg_replace('/[^A-Za-z0-9-_:.]+/', '_', $id);
110    }
111
112    /**
113     * Returns a custom renderer.
114     *
115     * @param array $params  A hash of renderer-specific parameters.
116     *
117     * @return object Horde_Form_Renderer  The form renderer.
118     */
119    public function getRenderer($params = array())
120    {
121        $params['varrenderer_driver'] = array('turba', 'turba');
122        return new Horde_Form_Renderer($params);
123    }
124}
125