style police take 2
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
895905c0 2
35586184 3/**
4 * addrbook_search.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handle addressbook searching in the popup window.
10 *
11 * NOTE: A lot of this code is similar to the code in
12 * addrbook_search_html.html -- If you change one,
13 * change the other one too!
14 *
15 * $Id$
16 */
17
35586184 18require_once('../src/validate.php');
f740c049 19
b0c39665 20/* Function to include JavaScript code */
21function insert_javascript() {
22 ?>
23 <SCRIPT LANGUAGE="Javascript"><!--
87332e20 24
b0c39665 25 function to_and_close($addr) {
26 to_address($addr);
27 parent.close();
28 }
5100704d 29
b0c39665 30 function to_address($addr) {
31 var prefix = "";
32 var pwintype = typeof parent.opener.document.compose;
dfadb553 33
b0c39665 34 $addr = $addr.replace(/ {1,35}$/, "");
9487c2ff 35
b0c39665 36 if (pwintype != "undefined") {
37 if (parent.opener.document.compose.send_to.value) {
38 prefix = ", ";
39 parent.opener.document.compose.send_to.value =
40 parent.opener.document.compose.send_to.value + ", " + $addr;
41 } else {
42 parent.opener.document.compose.send_to.value = $addr;
43 }
44 }
5100704d 45 }
5100704d 46
b0c39665 47 function cc_address($addr) {
48 var prefix = "";
49 var pwintype = typeof parent.opener.document.compose;
5100704d 50
b0c39665 51 $addr = $addr.replace(/ {1,35}$/, "");
dfadb553 52
b0c39665 53 if (pwintype != "undefined") {
54 if (parent.opener.document.compose.send_to_cc.value) {
55 prefix = ", ";
56 parent.opener.document.compose.send_to_cc.value =
57 parent.opener.document.compose.send_to_cc.value + ", " + $addr;
58 } else {
59 parent.opener.document.compose.send_to_cc.value = $addr;
60 }
61 }
5100704d 62 }
5100704d 63
b0c39665 64 function bcc_address($addr) {
65 var prefix = "";
66 var pwintype = typeof parent.opener.document.compose;
9487c2ff 67
b0c39665 68 $addr = $addr.replace(/ {1,35}$/, "");
5100704d 69
b0c39665 70 if (pwintype != "undefined") {
71 if (parent.opener.document.compose.send_to_bcc.value) {
72 prefix = ", ";
73 parent.opener.document.compose.send_to_bcc.value =
74 parent.opener.document.compose.send_to_bcc.value + ", " + $addr;
75 } else {
76 parent.opener.document.compose.send_to_bcc.value = $addr;
77 }
78 }
5100704d 79 }
5100704d 80
81// --></SCRIPT>
9487c2ff 82<?php
b0c39665 83} /* End of included JavaScript */
2f73dc15 84
85
b0c39665 86/* List search results */
87function display_result($res, $includesource = true) {
88 global $color;
9487c2ff 89
b0c39665 90 if(sizeof($res) <= 0) return;
9487c2ff 91
b0c39665 92 insert_javascript();
9487c2ff 93
b0c39665 94 $line = 0;
95 echo '<TABLE BORDER="0" WIDTH="98%" ALIGN=center>' .
96 '<TR BGCOLOR="' . $color[9] . '"><TH ALIGN=left>&nbsp;' .
97 '<TH ALIGN=left>&nbsp;' . _("Name") .
98 '<TH ALIGN=left>&nbsp;' . _("E-mail") .
99 '<TH ALIGN=left>&nbsp;' . _("Info");
100
101 if ($includesource) {
102 echo '<TH ALIGN=left WIDTH="10%">&nbsp;' . _("Source");
103 }
104 echo "</TR>\n";
9487c2ff 105
b0c39665 106 while (list($undef, $row) = each($res)) {
ef97102b 107 echo '<tr';
108 if ($line % 2) { echo ' bgcolor="' . $color[0] . '"' }
109 echo ' nowrap><td valign=top nowrap align=center width="5%">' .
b0c39665 110 '<small><a href="javascript:to_address(' .
111 "'" . $row['email'] . "');\">To</A> | " .
112 '<a href="javascript:cc_address(' .
ef97102b 113 "'" . $row['email'] . "');\">Cc</A> | " .
b0c39665 114 '<a href="javascript:bcc_address(' .
ef97102b 115 "'" . $row['email'] . "');\">Bcc</A></small>" .
b0c39665 116 '<td nowrap valign=top>&nbsp;' .
117 $row['name'] . '&nbsp;<td nowrap valign=top>' .
118 '&nbsp;<a href="javascript:to_and_close(' .
ef97102b 119 "'" . $row['email'] . "');\">" . $row['email'] . '</A>&nbsp;' .
b0c39665 120 '<td valign=top>&nbsp;' . $row['label'] . '&nbsp;';
121 if ($includesource) {
122 echo '<td nowrap valign=top>&nbsp;' . $row['source'];
9487c2ff 123 }
b0c39665 124
125 echo "</TR>\n";
126 $line++;
9487c2ff 127 }
b0c39665 128 echo '</TABLE>';
129}
9487c2ff 130
b0c39665 131/* ================= End of functions ================= */
9487c2ff 132
b0c39665 133require_once('../functions/array.php');
134require_once('../functions/strings.php');
135require_once('../functions/addressbook.php');
9487c2ff 136
b0c39665 137displayHtmlHeader();
9487c2ff 138
b0c39665 139/* Initialize vars */
140if (!isset($query)) { $query = ''; }
141if (!isset($show)) { $show = ''; }
9487c2ff 142
b0c39665 143/* Choose correct colors for top and bottom frame */
144if ($show == 'form') {
145 echo '<BODY TEXT="' . $color[6] . '" BGCOLOR="' . $color[3] . '" ' .
146 'LINK="' . $color[6] . '" VLINK="' . $color[6] . '" ' .
147 'ALINK="' . $color[6] . '" ' .
148 'OnLoad="document.sform.query.focus();">';
149} else {
150 echo '<BODY TEXT="' . $color[8] . '" BGCOLOR="' . $color[4] . '" ' .
151 'LINK="' . $color[7] . '" VLINK="' . $color[7] . '" ' .
152 'ALINK="' . $color[7] . "\">\n";
153}
9487c2ff 154
b0c39665 155/* Empty search */
156if (empty($query) && empty($show) && empty($listall)) {
157 echo '<P ALIGN=center><BR>' .
158 _("No persons matching your search was found") .
159 "</P>\n</BODY></HTML>\n",
160 exit;
161}
9487c2ff 162
b0c39665 163/* Initialize addressbook */
164$abook = addressbook_init();
9487c2ff 165
b0c39665 166/* Create search form */
167if ($show == 'form') {
168 echo '<FORM NAME=sform TARGET=abookres ACTION="' . $PHP_SELF .
169 '" METHOD="POST">' . "\n" .
170 '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%">' .
171 '<TR><TD NOWRAP VALIGN=middle>' . "\n" .
172 ' <STRONG>' . _("Search for") . "</STRONG>\n" .
173 ' <INPUT TYPE=text NAME=query VALUE="' . htmlspecialchars($query) .
ef97102b 174 "\" SIZE=26>\n";
b0c39665 175
176 /* List all backends to allow the user to choose where to search */
177 if ($abook->numbackends > 1) {
178 echo '<STRONG>' . _("in") . '</STRONG>&nbsp;<SELECT NAME=backend>'."\n".
179 '<OPTION VALUE=-1 SELECTED>' . _("All address books") . "\n";
180 $ret = $abook->get_backend_list();
181 while (list($undef,$v) = each($ret)) {
182 echo '<OPTION VALUE=' . $v->bnum . '>' . $v->sname . "\n";
9487c2ff 183 }
b0c39665 184 echo "</SELECT>\n";
185 } else {
186 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
187 }
9487c2ff 188
b0c39665 189 echo '<INPUT TYPE=submit VALUE="' . _("Search") . '">' .
190 '&nbsp;|&nbsp;<INPUT TYPE=submit VALUE="' . _("List all") .
191 '" NAME=listall>' . "\n" .
192 '</TD><TD ALIGN=right>' . "\n" .
ef97102b 193 '<INPUT TYPE=button VALUE="' . _("Close window") .
b0c39665 194 '" onclick="parent.close();">' . "\n" .
195 '</TD></TR></TABLE></FORM>' . "\n";
196} else {
9487c2ff 197
b0c39665 198 /* Show personal addressbook */
199 if ($show == 'blank' || !empty($listall)) {
9487c2ff 200
201 if($backend != -1 || $show == 'blank') {
b0c39665 202 if ($show == 'blank') {
9487c2ff 203 $backend = $abook->localbackend;
b0c39665 204 }
9487c2ff 205 $res = $abook->list_addr($backend);
206
207 if(is_array($res)) {
208 display_result($res, false);
209 } else {
b0c39665 210 echo '<P ALIGN=center><STRONG>' .
211 sprintf(_("Unable to list addresses from %s"),
212 $abook->backends[$backend]->sname) .
213 '</STRONG></P>' . "\n";
9487c2ff 214 }
9487c2ff 215 } else {
b0c39665 216 $res = $abook->list_addr();
217 display_result($res, true);
9487c2ff 218 }
219
b0c39665 220 } else {
9487c2ff 221
b0c39665 222 /* Do the search */
223 if (!empty($query) && empty($listall)) {
9487c2ff 224
b0c39665 225 if($backend == -1) {
226 $res = $abook->s_search($query);
227 } else {
228 $res = $abook->s_search($query, $backend);
229 }
9487c2ff 230
b0c39665 231 if (!is_array($res)) {
232 echo '<P ALIGN=center><B><BR>' .
233 _("Your search failed with the following error(s)") .
234 ':<br>' . $abook->error . "</B></P>\n</BODY></HTML>\n";
235 exit;
236 }
9487c2ff 237
b0c39665 238 if (sizeof($res) == 0) {
239 echo '<P ALIGN=center><BR><B>' .
240 _("No persons matching your search was found") .
241 ".</B></P>\n</BODY></HTML>\n";
242 exit;
243 }
9487c2ff 244
b0c39665 245 display_result($res);
246 }
9487c2ff 247 }
248
b0c39665 249}
250
251echo "</BODY></HTML>\n";
9487c2ff 252
35586184 253?>