Preserve compose action during HTML address book activity
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2 /**
3 * addrbook_search_html.php
4 *
5 * Handle addressbook searching with pure html.
6 *
7 * This file is included from compose.php
8 *
9 * @copyright 1999-2013 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage addressbook
14 */
15
16 /**
17 * Include the SquirrelMail initialization file.
18 * Because this file can also be included within compose we check for the $bInit
19 * var which is set inside ini.php. It's needed because compose already includes
20 * init.php.
21 */
22 if (!isset($bInit)) {
23 /** This is the addrbook_search_html page */
24 define('PAGE_NAME', 'addrbook_search_html');
25
26 include('../include/init.php');
27 }
28
29 /** SquirrelMail required files. */
30 include_once(SM_PATH . 'functions/date.php');
31 include_once(SM_PATH . 'functions/addressbook.php');
32 include_once(SM_PATH . 'templates/util_addressbook.php');
33
34 sqgetGlobalVar('session', $session, SQ_POST);
35 sqgetGlobalVar('mailbox', $mailbox, SQ_POST);
36 if (! sqgetGlobalVar('query', $addrquery, SQ_POST))
37 $addrquery='';
38 sqgetGlobalVar('listall', $listall, SQ_POST);
39 sqgetGlobalVar('backend', $backend, SQ_POST);
40
41 /**
42 * Insert hidden data
43 */
44 function addr_insert_hidden() {
45 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox, $mailprio,
46 $request_mdn, $request_dr, $identity, $session, $composeMessage, $action;
47
48 //FIXME Do not echo HTML from the core. This file already uses templates mostly, so why are we echoing here at all?!?
49 if (substr($body, 0, 1) == "\r") {
50 echo addHidden('body', "\n".$body);
51 } else {
52 echo addHidden('body', $body);
53 }
54
55 if (is_object($composeMessage) && $composeMessage->entities)
56 echo addHidden('attachments', urlencode(serialize($composeMessage->entities)));
57
58 echo addHidden('session', $session).
59 addHidden('smaction', $action).
60 addHidden('subject', $subject).
61 addHidden('send_to', $send_to).
62 addHidden('send_to_bcc', $send_to_bcc).
63 addHidden('send_to_cc', $send_to_cc).
64 addHidden('mailprio', $mailprio).
65 addHidden('request_mdn', $request_mdn).
66 addHidden('request_dr', $request_dr).
67 addHidden('identity', $identity).
68 addHidden('mailbox', $mailbox).
69 addHidden('from_htmladdr_search', 'true');
70 }
71
72
73 /**
74 * List search results
75 * @param array $res Array containing results of search
76 * @param bool $includesource If true, adds backend column to address listing
77 */
78 function addr_display_result($res, $includesource = true) {
79 global $PHP_SELF, $oTemplate, $oErrorHandler;
80
81
82 //FIXME: no HTML output from core
83 echo addForm($PHP_SELF, 'post', 'addressbook', '', '', array(), TRUE).
84 addHidden('html_addr_search_done', 'true');
85 addr_insert_hidden();
86
87 $oTemplate->assign('compose_addr_pop', false);
88 $oTemplate->assign('include_abook_name', $includesource);
89 $oTemplate->assign('addresses', formatAddressList($res));
90
91 $oTemplate->display('addrbook_search_list.tpl');
92
93 echo '</form>';
94 }
95
96 /* --- End functions --- */
97
98 if ($compose_new_win == '1') {
99 compose_Header($color, $mailbox);
100 }
101 else {
102 displayPageHeader($color, $mailbox);
103 }
104
105 /** set correct value of $default_charset */
106 set_my_charset();
107
108 /* Initialize addressbook */
109 $abook = addressbook_init();
110
111
112 /* Search form */
113 echo addForm($PHP_SELF.'?html_addr_search=true', 'post', 'f');
114 addr_insert_hidden();
115 if (isset($session)) {
116 echo addHidden('session', $session);
117 }
118
119 $oTemplate->assign('compose_addr_pop', false);
120 $oTemplate->assign('backends', getBackends());
121
122 $oTemplate->display('addressbook_search_form.tpl');
123
124 echo "</form>\n";
125 do_hook('addrbook_html_search_below', $null);
126 /* End search form */
127
128 /* List addresses. Show personal addressbook */
129 if ($addrquery == '' || ! empty($listall)) {
130 // TODO: recheck all conditions and simplity if statements
131 if (! isset($backend) || $backend != -1 || $addrquery == '') {
132 if ($addrquery == '' && empty($listall)) {
133 $backend = $abook->localbackend;
134 }
135
136 $res = $abook->list_addr($backend);
137
138 if (is_array($res)) {
139 usort($res,'alistcmp');
140 addr_display_result($res, false);
141 } else {
142 plain_error_message(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname);
143 }
144
145 } else {
146 $res = $abook->list_addr();
147 usort($res,'alistcmp');
148 addr_display_result($res, true);
149 }
150 $oTemplate->display('footer.tpl');
151 exit;
152 } elseif (!empty($addrquery)) {
153 /* Do the search */
154 if ($backend == -1) {
155 $res = $abook->s_search($addrquery);
156 } else {
157 $res = $abook->s_search($addrquery, $backend);
158 }
159
160 if (!is_array($res)) {
161 plain_error_message(_("Your search failed with the following error(s)") .':<br />'. nl2br(sm_encode_html_special_chars($abook->error)));
162 } elseif (sizeof($res) == 0) {
163 $oTemplate->assign('note', _("No persons matching your search were found"));
164 $oTemplate->display('note.tpl');
165 } else {
166 addr_display_result($res);
167 }
168 } else {
169 // not first time display, not listall and search is empty
170 // TODO: I think, this part of control structure is never reached.
171 plain_error_message(_("Nothing to search"));
172 }
173
174 if ($addrquery == '' || sizeof($res) == 0) {
175 //FIXME don't echo HTML from core -- especially convoluted given that there is template code immediately above AND below this block
176 echo '<div style="text-align: center;">'.
177 addForm('compose.php','post','k', '', '', array(), TRUE);
178 addr_insert_hidden();
179 echo '<input type="submit" value="' . _("Return") . '" name="return" />' . "\n" .
180 '</form></div></nobr>';
181 }
182
183 echo '<hr />';
184
185 $oTemplate->display('footer.tpl');