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