on second thought, revise r12527 to use one, generic constant
[squirrelmail.git] / src / addrbook_search.php
CommitLineData
5100704d 1<?php
35586184 2/**
3 * addrbook_search.php
4 *
35586184 5 * Handle addressbook searching in the popup window.
6 *
7 * NOTE: A lot of this code is similar to the code in
8 * addrbook_search_html.html -- If you change one,
9 * change the other one too!
10 *
4b5049de 11 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
91e0dccc 13 * @version $Id$
8f6f9ba5 14 * @package squirrelmail
de00443c 15 * @subpackage addressbook
35586184 16 */
17
8f6f9ba5 18/**
202bcbcc 19 * Include the SquirrelMail initialization file.
8f6f9ba5 20 */
202bcbcc 21require('../include/init.php');
86725763 22
6027422c 23include_once(SM_PATH . 'functions/forms.php');
24include_once(SM_PATH . 'functions/addressbook.php');
3f92c0c7 25include_once(SM_PATH . 'templates/util_addressbook.php');
2f73dc15 26
8f6f9ba5 27/**
28 * List search results
29 * @param array $res Array of search results
30 * @param bool $includesource [Default=true]
31 * @return void
32 */
b0c39665 33function display_result($res, $includesource = true) {
3f92c0c7 34 global $oTemplate, $oErrorHandler;
91e0dccc 35
b0c39665 36 if(sizeof($res) <= 0) return;
91e0dccc 37
3f92c0c7 38 $oTemplate->assign('use_js', true);
39 $oTemplate->assign('include_abook_name', $includesource);
40 $oTemplate->assign('addresses', formatAddressList($res));
41
42 $oTemplate->display('addrbook_search_list.tpl');
b0c39665 43}
9487c2ff 44
b0c39665 45/* ================= End of functions ================= */
91e0dccc 46
6027422c 47/** lets get the global vars we may need */
6027422c 48
49if (! sqgetGlobalVar('show' , $show)) {
50 $show = '';
51}
52if (! sqgetGlobalVar('query', $query, SQ_POST)) {
53 $query = '';
54}
55if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
56 unset($listall);
57}
58if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
59 $backend = '';
60}
91e0dccc 61
b0c39665 62displayHtmlHeader();
3f92c0c7 63echo "<body>\n";
91e0dccc 64
40262494 65/** set correct value of $default_charset */
40262494 66set_my_charset();
67
b0c39665 68/* Empty search */
6027422c 69if (empty($query) && empty($show) && !isset($listall)) {
3f92c0c7 70 $oTemplate->assign('note', htmlspecialchars(_("No persons matching your search were found")));
71 $oTemplate->display('note.tpl');
72# exit;
b0c39665 73}
9487c2ff 74
6dda6453 75/* Initialize addressbook, show init errors only in bottom frame */
76$showerr=($show=='form' ? false : true);
77$abook = addressbook_init($showerr);
9487c2ff 78
6027422c 79/* Create search form (top frame) */
80if ($show == 'form' && ! isset($listall)) {
3f92c0c7 81 echo "<form name=\"sform\" target=\"abookres\" action=\"addrbook_search.php\" method=\"POST\">\n";
82
83 $oTemplate->assign('use_js', true);
84 $oTemplate->assign('backends', getBackends());
85 $oTemplate->display('addressbook_search_form.tpl');
86
87 echo "</form>\n";
b0c39665 88} else {
6027422c 89 /**
90 * List addresses (bottom frame)
91 * If listall is set, list all entries in selected backend.
92 * If $show is 'blank' (initial call of address book popup) - list
93 * personal address book.
94 */
95 if ($show == 'blank' || isset($listall)) {
9487c2ff 96
97 if($backend != -1 || $show == 'blank') {
b0c39665 98 if ($show == 'blank') {
9487c2ff 99 $backend = $abook->localbackend;
b0c39665 100 }
9487c2ff 101 $res = $abook->list_addr($backend);
102
103 if(is_array($res)) {
a10110a5 104 usort($res,'alistcmp');
9487c2ff 105 display_result($res, false);
106 } else {
3f92c0c7 107 plain_error_message(sprintf(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname));
9487c2ff 108 }
9487c2ff 109 } else {
b0c39665 110 $res = $abook->list_addr();
a10110a5 111 usort($res,'alistcmp');
b0c39665 112 display_result($res, true);
9487c2ff 113 }
114
6027422c 115 } elseif (!empty($query)) {
116 /* Do the search (listall is not set. query is set.)*/
91e0dccc 117
6027422c 118 if($backend == -1) {
119 $res = $abook->s_search($query);
120 } else {
121 $res = $abook->s_search($query, $backend);
122 }
91e0dccc 123
6027422c 124 if (!is_array($res)) {
3f92c0c7 125 plain_error_message( _("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)) );
766ad164 126 } elseif (sizeof($res) == 0) {
3f92c0c7 127 $oTemplate->assign('note', _("No persons matching your search were found"));
128 $oTemplate->display('note.tpl');
766ad164 129 } else {
130 display_result($res);
b0c39665 131 }
6027422c 132 } else {
133 /**
134 * listall is not set, query is not set or empty.
135 * User hit search button without entering search expression.
136 */
3f92c0c7 137 plain_error_message(_("Nothing to search"));
6027422c 138 }
b0c39665 139}
3f92c0c7 140
5c4ff7bf 141$oTemplate->display('footer.tpl');