1<?php
2/**
3 * The Turba_View_Contact:: class provides an API for viewing events.
4 *
5 * @author  Chuck Hagenbuch <chuck@horde.org>
6 * @package Turba
7 */
8class Turba_View_Contact
9{
10    /**
11     * @var Turba_Object
12     */
13    public $contact;
14
15    /**
16     * @param Turba_Object &$contact
17     */
18    public function __construct(Turba_Object $contact)
19    {
20        $this->contact = $contact;
21    }
22
23    public function getTitle()
24    {
25        if (!$this->contact) {
26            return _("Not Found");
27        }
28        return $this->contact->getValue('name');
29    }
30
31    public function html($active = true)
32    {
33        global $browser, $conf, $registry;
34
35        if (!$this->contact ||
36            !$this->contact->hasPermission(Horde_Perms::READ)) {
37            echo '<h3>' . _("The requested contact was not found.") . '</h3>';
38            return;
39        }
40
41        $vars = new Horde_Variables();
42        $form = new Turba_Form_Contact($vars, $this->contact);
43        $form->setOpenSection(Horde_Util::getFormData('section', 0));
44
45        /* Get the contact's history. */
46        $history = $this->contact->getHistory();
47        foreach ($history as $what => $when) {
48            $v = $form->addVariable(
49                $what == 'created' ? _("Created") : _("Last Modified"),
50                'object[__' . $what . ']',
51                'text',
52                false,
53                false);
54            $v->disable();
55            $vars->set('object[__' . $what . ']', $when);
56        }
57
58        echo '<div id="Contact"' . ($active ? '' : ' style="display:none"') . '>';
59        $form->renderInactive($form->getRenderer(), $vars);
60
61        /* Comments. */
62        if (!empty($conf['comments']['allow']) && $registry->hasMethod('forums/doComments')) {
63            try {
64                $comments = $registry->call('forums/doComments', array('turba', $this->contact->driver->getName() . '.' . $this->contact->getValue('__key'), 'commentCallback'));
65            } catch (Horde_Exception $e) {
66                Horde::log($e, 'DEBUG');
67                $comments = array();
68            }
69        }
70        if (!empty($comments['threads'])) {
71            echo '<br />' . $comments['threads'];
72        }
73        if (!empty($comments['comments'])) {
74            echo '<br />' . $comments['comments'];
75        }
76
77        echo '</div>';
78
79        if ($active && $browser->hasFeature('dom')) {
80            if ($this->contact->hasPermission(Horde_Perms::EDIT)) {
81                $edit = new Turba_View_EditContact($this->contact);
82                $edit->html(false);
83            }
84            if ($this->contact->hasPermission(Horde_Perms::DELETE)) {
85                $delete = new Turba_View_DeleteContact($this->contact);
86                $delete->html(false);
87            }
88        }
89    }
90
91}
92