File: ObjectView.php

package info (click to toggle)
turba2 2.0.2-1sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,996 kB
  • ctags: 1,071
  • sloc: php: 4,725; xml: 617; sql: 136; makefile: 62; sh: 46; perl: 17
file content (167 lines) | stat: -rw-r--r-- 5,760 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
 * The Turba_ObjectView:: class provides an interface for visualizing
 * a Turba_AbstractObject.
 *
 * $Horde: turba/lib/ObjectView.php,v 1.27 2004/12/05 02:00:05 chuck Exp $
 *
 * @author   Chuck Hagenbuch <chuck@horde.org>
 * @version  $Revision: 1.27 $
 * @since    Turba 0.0.1
 * @package  Turba
 */
class Turba_ObjectView {

    /**
     * A reference to the Turba_AbstractObject that this
     * Turba_ObjectView displays.
     * @private
     * @var	object Turba_AbstractObject $_object
     */
    var $_object;

    /**
     * The template used to display the object.
     * @private
     * @var string $_template
     */
    var $_template;

    /**
     * Do we have the creation timestamp of the object?
     * @var boolean $_created
     */
    var $_created = false;

    /**
     * Do we have the modified timestamp of the object?
     * @var boolean $_modified
     */
    var $_modified = false;

    /**
     * Constructs a new Turba_ObjectView object.
     *
     * @param object Turba_AbstractObject $object    The object to display.
     * @param string                      $template  Which template file to display this object with.
     */
    function Turba_ObjectView(&$object, $template = null)
    {
        $this->_object = &$object;
        $this->_template = $template;
    }

    /**
     * Set up the Horde_Form for the current object's attributes.
     *
     * @param object Horde_Form &$form  The form to set variables on.
     */
    function setupForm(&$form)
    {
        global $attributes;

        // Run through once to see what form actions, if any, we need
        // to set up.
        $actions = array();
        $map = $this->_object->driver->map;
        $fields = array_keys($this->_object->driver->getCriteria());
        foreach ($fields as $field) {
            if (is_array($map[$field])) {
                foreach ($map[$field]['fields'] as $action_field) {
                    if (!isset($actions[$action_field])) {
                        $actions[$action_field] = array();
                    }
                    $actions[$action_field]['fields'] = $map[$field]['fields'];
                    $actions[$action_field]['format'] = $map[$field]['format'];
                    $actions[$action_field]['target'] = $field;
                }
            }
        }

        // Now run through and add the form variables.
        $tabs = $this->_object->driver->tabs;
        if (!count($tabs)) {
            $tabs = array('' => $fields);
        }
        foreach ($tabs as $tab => $tab_fields) {
            if (!empty($tab)) {
                $form->setSection($tab, $tab);
            }
            foreach ($tab_fields as $field) {
                if (!in_array($field, $fields) ||
                    !isset($attributes[$field])) {
                    continue;
                }

                $attribute = $attributes[$field];
                $params = isset($attribute['params']) ? $attribute['params'] : array();
                $desc = isset($attribute['desc']) ? $attribute['desc'] : null;

                if (is_array($map[$field])) {
                    $v = &$form->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], false, false, $desc, $params);
                    $v->disable();
                } else {
                    $readonly = isset($attribute['readonly']) ? $attribute['readonly'] : null;

                    /* We link emails, so we need to switch to the
                     * 'html' renderer since the 'email' renderer will
                     * nuke our formatting.  However, only do this on
                     * the display.php page. */
                    if (($attribute['type'] == 'email') &&
                        (basename($_SERVER['PHP_SELF']) == 'display.php')) {
                        $params = array(false, false, true, $form->_vars->get('name'));
                    }

                    $v = &$form->addVariable($attribute['label'], 'object[' . $field . ']', $attribute['type'], $attribute['required'], $readonly, $desc, $params);

                    if (!empty($actions[$field])) {
                        require_once 'Horde/Form/Action.php';
                        $actionfields = array();
                        foreach ($actions[$field]['fields'] as $f) {
                            $actionfields[] = 'object[' . $f . ']';
                        }
                        $a = &Horde_Form_Action::factory('updatefield',
                                                         array('format' => $actions[$field]['format'],
                                                               'target' => 'object[' . $actions[$field]['target'] . ']',
                                                               'fields' => $actionfields));
                        $v->setAction($a);
                    }
                }
            }
        }

        if ($this->_created) {
            $v = &$form->addVariable(_("Created"), 'object[__created]', 'text', false, false);
            $v->disable();
        }

        if ($this->_modified) {
            $v = &$form->addVariable(_("Last Modified"), 'object[__modified]', 'text', false, false);
            $v->disable();
        }
    }

    /**
     * Renders the object into an HTML view.
     */
    function display()
    {
        global $attributes;

        $fields = $this->_object->driver->getCriteria();
        require $this->_template;
    }

    /**
     * Set $attribute to $value.
     *
     * @param string $attribute  The attribute to set.
     * @param mixed  $value      The value for $attribute.
     */
    function set($attribute, $value)
    {
        $attribute = '_' . $attribute;
        $this->$attribute = $value;
    }

}