r2l by Yoav
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2
3 /**
4 * addrbook_search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 require_once('../src/validate.php');
19 require_once('../functions/strings.php');
20 require_once('../functions/html.php');
21
22 /* Function to include JavaScript code */
23 function insert_javascript() {
24 ?>
25 <SCRIPT LANGUAGE="Javascript"><!--
26
27 function to_and_close($addr) {
28 to_address($addr);
29 parent.close();
30 }
31
32 function to_address($addr) {
33 var prefix = "";
34 var pwintype = typeof parent.opener.document.compose;
35
36 $addr = $addr.replace(/ {1,35}$/, "");
37
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 }
47 }
48
49 function cc_address($addr) {
50 var prefix = "";
51 var pwintype = typeof parent.opener.document.compose;
52
53 $addr = $addr.replace(/ {1,35}$/, "");
54
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 }
64 }
65
66 function bcc_address($addr) {
67 var prefix = "";
68 var pwintype = typeof parent.opener.document.compose;
69
70 $addr = $addr.replace(/ {1,35}$/, "");
71
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 }
81 }
82
83 // --></SCRIPT>
84 <?php
85 } /* End of included JavaScript */
86
87
88 /* List search results */
89 function display_result($res, $includesource = true) {
90 global $color;
91
92 if(sizeof($res) <= 0) return;
93
94 insert_javascript();
95
96 $line = 0;
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' );
103
104 if ($includesource) {
105 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', 'width="10%"' );
106 }
107 echo "</tr>\n";
108
109 while (list($undef, $row) = each($res)) {
110 $tr_bgcolor = '';
111 if ($line % 2) { $tr_bgcolor = $color[0]; }
112 echo html_tag( 'tr', '', '', $tr_bgcolor, 'nowrap' ) .
113 html_tag( 'td',
114 '<small><a href="javascript:to_address(' .
115 "'" . $row['email'] . "');\">To</A> | " .
116 '<a href="javascript:cc_address(' .
117 "'" . $row['email'] . "');\">Cc</A> | " .
118 '<a href="javascript:bcc_address(' .
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;' .
123 '<a href="javascript:to_and_close(' .
124 "'" . $row['email'] . "');\">" . $row['email'] . '</A>'
125 , 'left', '', 'valign="top"' ) .
126 html_tag( 'td', $row['label'], 'left', '', 'valign="top" nowrap' );
127 if ($includesource) {
128 echo html_tag( 'td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap' );
129 }
130
131 echo "</tr>\n";
132 $line++;
133 }
134 echo '</table>';
135 }
136
137 /* ================= End of functions ================= */
138
139 require_once('../functions/array.php');
140 require_once('../functions/strings.php');
141 require_once('../functions/addressbook.php');
142
143 displayHtmlHeader();
144
145 /* Initialize vars */
146 if (!isset($query)) { $query = ''; }
147 if (!isset($show)) { $show = ''; }
148 if (!isset($backend)) { $backend = ''; }
149
150 /* Choose correct colors for top and bottom frame */
151 if ($show == 'form' && !isset($listall)) {
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 }
161
162 /* Empty search */
163 if (empty($query) && empty($show) && empty($listall)) {
164 echo html_tag( 'p', '<br>' .
165 _("No persons matching your search was found"),
166 'center' ) .
167 "\n</BODY></HTML>\n",
168 exit;
169 }
170
171 /* Initialize addressbook */
172 $abook = addressbook_init();
173
174 /* Create search form */
175 if ($show == 'form' && empty($listall)) {
176 echo '<FORM NAME=sform TARGET=abookres ACTION="addrbook_search.php'.
177 '" METHOD="POST">' . "\n" .
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";
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";
192 }
193 echo "</SELECT>\n";
194 } else {
195 echo '<INPUT TYPE=hidden NAME=backend VALUE=-1>' . "\n";
196 }
197
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";
210 } else {
211
212 /* Show personal addressbook */
213 if ($show == 'blank' && empty($listall)) {
214
215 if($backend != -1 || $show == 'blank') {
216 if ($show == 'blank') {
217 $backend = $abook->localbackend;
218 }
219 $res = $abook->list_addr($backend);
220
221 if(is_array($res)) {
222 usort($res,'alistcmp');
223 display_result($res, false);
224 } else {
225 echo html_tag( 'p', '<strong>' .
226 sprintf(_("Unable to list addresses from %s"),
227 $abook->backends[$backend]->sname) . '</strong>' ,
228 'center' ) . "\n";
229 }
230 } else {
231 $res = $abook->list_addr();
232 usort($res,'alistcmp');
233 display_result($res, true);
234 }
235
236 } else {
237 if( !empty( $listall ) ){
238 $query = '*';
239 }
240
241 /* Do the search */
242 if (!empty($query)) {
243
244 if($backend == -1) {
245 $res = $abook->s_search($query);
246 } else {
247 $res = $abook->s_search($query, $backend);
248 }
249
250 if (!is_array($res)) {
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";
256 exit;
257 }
258
259 if (sizeof($res) == 0) {
260 echo html_tag( 'p', '<br><b>' .
261 _("No persons matching your search was found") . "</b>\n" ,
262 'center' ) .
263 "\n</BODY></HTML>\n";
264 exit;
265 }
266
267 display_result($res);
268 }
269 }
270
271 }
272
273 echo "</BODY></HTML>\n";
274
275 ?>