1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 2012-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * Compose view utilities for AJAX data.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2012-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Ajax_Application_Compose
24{
25    /**
26     * Forward mapping of id -> compose object constants.
27     *
28     * @var array
29     */
30    public $forward_map = array(
31        'editasnew' => IMP_Compose::EDITASNEW,
32        'forward_attach' => IMP_Compose::FORWARD_ATTACH,
33        'forward_auto' => IMP_Compose::FORWARD_AUTO,
34        'forward_body' => IMP_Compose::FORWARD_BODY,
35        'forward_both' => IMP_Compose::FORWARD_BOTH
36    );
37
38    /**
39     * Reply mapping of id -> compose object constant.
40     *
41     * @var array
42     */
43    public $reply_map = array(
44        'reply' => IMP_Compose::REPLY_SENDER,
45        'reply_all' => IMP_Compose::REPLY_ALL,
46        'reply_auto' => IMP_Compose::REPLY_AUTO,
47        'reply_list' => IMP_Compose::REPLY_LIST
48    );
49
50    /**
51     * Compose object.
52     *
53     * @var IMP_Compose
54     */
55    protected $_compose;
56
57    /**
58     * Compose type.
59     *
60     * @var string
61     */
62    protected $_type;
63
64    /**
65     * Constuctor.
66     *
67     * @param IMP_Compose $ob  Compose object.
68     * @param string $type     Compose type.
69     */
70    public function __construct(IMP_Compose $ob, $type = null)
71    {
72        $this->_composeOb = $ob;
73        $this->_type = $type;
74    }
75
76    /**
77     */
78    public function getResponse($result)
79    {
80        $ob = $this->getBaseResponse($result);
81
82        $ob->body = $result['body'];
83        $ob->format = $result['format'];
84        $ob->identity = $result['identity'];
85
86        if (!empty($result['attach'])) {
87            $ob->opts->attach = 1;
88        }
89
90        if ($search = array_search($result['type'], $this->reply_map)) {
91            if ($this->_type == 'reply_auto') {
92                $ob->opts->auto = $search;
93
94                if (isset($result['reply_list_id'])) {
95                    $ob->opts->reply_list_id = $result['reply_list_id'];
96                }
97                if (isset($result['reply_recip'])) {
98                    $ob->opts->reply_recip = $result['reply_recip'];
99                }
100            }
101
102            if (!empty($result['lang'])) {
103                $ob->opts->reply_lang = array_values($result['lang']);
104            }
105
106            $ob->opts->focus = 'composeMessage';
107        } elseif ($search = array_search($result['type'], $this->forward_map)) {
108            if ($this->_type == 'forward_auto') {
109                $ob->opts->auto = $search;
110            }
111        } else {
112            $ob->opts->priority = $result['priority'];
113            $ob->opts->readreceipt = $result['readreceipt'];
114        }
115
116        return $ob;
117    }
118
119    /**
120     */
121    public function getBaseResponse($result = array())
122    {
123        $ob = new stdClass;
124        $ob->body = '';
125        $ob->opts = new stdClass;
126        $ob->subject = isset($result['subject'])
127            ? $result['subject']
128            : '';
129        $ob->type = $this->_type;
130
131        if (isset($result['addr'])) {
132            $ob->addr = array(
133                'to' => array_map('strval', $result['addr']['to']->base_addresses),
134                'cc' => array_map('strval', $result['addr']['cc']->base_addresses),
135                'bcc' => array_map('strval', $result['addr']['bcc']->base_addresses),
136            );
137        }
138
139        return $ob;
140    }
141
142}
143