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