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