6 * Handle addressbook searching in the popup window.
8 * NOTE: A lot of this code is similar to the code in
9 * addrbook_search_html.html -- If you change one,
10 * change the other one too!
12 * @copyright © 1999-2005 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
15 * @package squirrelmail
16 * @subpackage addressbook
20 * Path for SquirrelMail required files.
23 define('SM_PATH','../');
25 /** SquirrelMail required files. */
26 require_once(SM_PATH
. 'include/validate.php');
27 include_once(SM_PATH
. 'functions/strings.php');
28 include_once(SM_PATH
. 'functions/global.php');
29 include_once(SM_PATH
. 'functions/html.php');
30 include_once(SM_PATH
. 'functions/forms.php');
31 include_once(SM_PATH
. 'functions/addressbook.php');
34 * Function to include JavaScript code
37 function insert_javascript() {
39 <script language
="Javascript"><!--
41 function to_and_close($addr) {
46 function to_address($addr) {
48 var pwintype
= typeof parent
.opener
.document
.compose
;
50 $addr = $addr.replace(/ {1,35}$
/, "");
52 if (pwintype
!= "undefined") {
53 if (parent
.opener
.document
.compose
.send_to
.value
) {
55 parent
.opener
.document
.compose
.send_to
.value
=
56 parent
.opener
.document
.compose
.send_to
.value +
", " +
$addr;
58 parent
.opener
.document
.compose
.send_to
.value
= $addr;
63 function cc_address($addr) {
65 var pwintype
= typeof parent
.opener
.document
.compose
;
67 $addr = $addr.replace(/ {1,35}$
/, "");
69 if (pwintype
!= "undefined") {
70 if (parent
.opener
.document
.compose
.send_to_cc
.value
) {
72 parent
.opener
.document
.compose
.send_to_cc
.value
=
73 parent
.opener
.document
.compose
.send_to_cc
.value +
", " +
$addr;
75 parent
.opener
.document
.compose
.send_to_cc
.value
= $addr;
80 function bcc_address($addr) {
82 var pwintype
= typeof parent
.opener
.document
.compose
;
84 $addr = $addr.replace(/ {1,35}$
/, "");
86 if (pwintype
!= "undefined") {
87 if (parent
.opener
.document
.compose
.send_to_bcc
.value
) {
89 parent
.opener
.document
.compose
.send_to_bcc
.value
=
90 parent
.opener
.document
.compose
.send_to_bcc
.value +
", " +
$addr;
92 parent
.opener
.document
.compose
.send_to_bcc
.value
= $addr;
99 } /* End of included JavaScript */
103 * List search results
104 * @param array $res Array of search results
105 * @param bool $includesource [Default=true]
108 function display_result($res, $includesource = true) {
111 if(sizeof($res) <= 0) return;
116 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
117 html_tag( 'tr', '', '', $color[9] ) .
118 html_tag( 'th', ' ', 'left' ) .
119 html_tag( 'th', ' ' . _("Name"), 'left' ) .
120 html_tag( 'th', ' ' . _("E-mail"), 'left' ) .
121 html_tag( 'th', ' ' . _("Info"), 'left' );
123 if ($includesource) {
124 echo html_tag( 'th', ' ' . _("Source"), 'left', '', 'width="10%"' );
128 while (list($undef, $row) = each($res)) {
129 $email = htmlspecialchars(addcslashes(AddressBook
::full_address($row), "'"), ENT_QUOTES
);
131 $tr_bgcolor = $color[12];
133 $tr_bgcolor = $color[4];
135 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
137 '<small><a href="javascript:to_address(' .
138 "'" . $email . "');\">"._("To")."</a> | " .
139 '<a href="javascript:cc_address(' .
140 "'" . $email . "');\">"._("Cc")."</a> | " .
141 '<a href="javascript:bcc_address(' .
142 "'" . $email . "');\">"._("Bcc")."</a></small>",
143 'center', '', 'valign="top" width="5%" style="white-space: nowrap;"' ) .
144 html_tag( 'td', ' ' . htmlspecialchars($row['name']), 'left', '', 'valign="top" style="white-space: nowrap;"' ) .
145 html_tag( 'td', ' ' .
146 '<a href="javascript:to_and_close(' .
147 "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>'
148 , 'left', '', 'valign="top"' ) .
149 html_tag( 'td', htmlspecialchars($row['label']), 'left', '', 'valign="top" style="white-space: nowrap;"' );
150 if ($includesource) {
151 echo html_tag( 'td', ' ' . $row['source'], 'left', '', 'valign="top" style="white-space: nowrap;"' );
160 /* ================= End of functions ================= */
162 /** lets get the global vars we may need */
163 sqgetGlobalVar('key', $key, SQ_COOKIE
);
164 sqgetGlobalVar('username', $username, SQ_SESSION
);
165 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
166 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
168 if (! sqgetGlobalVar('show' , $show)) {
171 if (! sqgetGlobalVar('query', $query, SQ_POST
)) {
174 if (! sqgetGlobalVar('listall', $listall, SQ_POST
)) {
177 if (! sqgetGlobalVar('backend', $backend, SQ_POST
)) {
183 /** set correct value of $default_charset */
184 global $default_charset;
187 /* Choose correct colors for top and bottom frame */
188 if ($show == 'form' && !isset($listall)) {
189 echo '<body text="' . $color[6] . '" bgcolor="' . $color[3] . '" ' .
190 'link="' . $color[6] . '" vlink="' . $color[6] . '" ' .
191 'alink="' . $color[6] . '" ' .
192 'OnLoad="document.sform.query.focus();">';
194 echo '<body text="' . $color[8] . '" bgcolor="' . $color[4] . '" ' .
195 'link="' . $color[7] . '" vlink="' . $color[7] . '" ' .
196 'alink="' . $color[7] . "\">\n";
200 if (empty($query) && empty($show) && !isset($listall)) {
201 echo html_tag( 'p', '<br />' .
202 _("No persons matching your search were found"),
204 "\n</body></html>\n";
208 /* Initialize addressbook, show init errors only in bottom frame */
209 $showerr=($show=='form' ?
false : true);
210 $abook = addressbook_init($showerr);
212 /* Create search form (top frame) */
213 if ($show == 'form' && ! isset($listall)) {
214 echo '<form name="sform" target="abookres" action="addrbook_search.php'.
215 '" method="post">' . "\n" .
216 html_tag( 'table', '', '', '', 'border="0" width="100%" height="100%"' ) .
218 html_tag( 'td', ' <strong>' . _("Search for") . "</strong>\n", 'left', '', 'style="white-space: nowrap;" valign="middle" width="10%"' ) .
219 html_tag( 'td', '', 'left', '', '' ) .
220 addInput('query', $query, 28);
222 /* List all backends to allow the user to choose where to search */
223 if ($abook->numbackends
> 1) {
224 echo '<strong>' . _("in") . '</strong> '."\n";
226 $selopts['-1'] = _("All address books");
228 $ret = $abook->get_backend_list();
229 while (list($undef,$v) = each($ret)) {
230 $selopts[$v->bnum
] = $v->sname
;
232 echo addSelect('backend', $selopts, '-1', TRUE);
234 echo addHidden('backend', '-1');
239 html_tag( 'td', '', 'left' ) .
241 '<input type="submit" value="' . _("Search") . '" name="show" />' .
242 ' | <input type="submit" value="' . _("List all") .
243 '" name="listall" />' . "\n" .
244 ' | <input type="button" value="' . _("Close") .
245 '" onclick="parent.close();" />' . "\n" ,
248 '</table></form>' . "\n";
251 * List addresses (bottom frame)
252 * If listall is set, list all entries in selected backend.
253 * If $show is 'blank' (initial call of address book popup) - list
254 * personal address book.
256 if ($show == 'blank' ||
isset($listall)) {
258 if($backend != -1 ||
$show == 'blank') {
259 if ($show == 'blank') {
260 $backend = $abook->localbackend
;
262 $res = $abook->list_addr($backend);
265 usort($res,'alistcmp');
266 display_result($res, false);
268 echo html_tag( 'p', '<strong>' .
269 sprintf(_("Unable to list addresses from %s"),
270 $abook->backends
[$backend]->sname
) . '</strong>' ,
274 $res = $abook->list_addr();
275 usort($res,'alistcmp');
276 display_result($res, true);
279 } elseif (!empty($query)) {
280 /* Do the search (listall is not set. query is set.)*/
283 $res = $abook->s_search($query);
285 $res = $abook->s_search($query, $backend);
288 if (!is_array($res)) {
289 echo html_tag( 'p', '<b><br />' .
290 _("Your search failed with the following error(s)") .
291 ':<br />' . $abook->error
. "</b>\n" ,
293 "\n</body></html>\n";
297 if (sizeof($res) == 0) {
298 echo html_tag( 'p', '<br /><b>' .
299 _("No persons matching your search were found") . "</b>\n" ,
301 "\n</body></html>\n";
305 display_result($res);
308 * listall is not set, query is not set or empty.
309 * User hit search button without entering search expression.
311 echo html_tag( 'p', '<br /><b>' . _("Nothing to search") . "</b>\n",'center' );