b2e337743df8d1e35f3abfeb2bdb9cd41be42c5a
[squirrelmail.git] / src / addrbook_search.php
1 <?php
2 /**
3 * addrbook_search.php
4 *
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 *
11 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package squirrelmail
15 * @subpackage addressbook
16 */
17
18 /**
19 * Include the SquirrelMail initialization file.
20 */
21 require('../include/init.php');
22
23 include_once(SM_PATH . 'functions/forms.php');
24 include_once(SM_PATH . 'functions/addressbook.php');
25 include_once(SM_PATH . 'templates/util_addressbook.php');
26
27 /**
28 * List search results
29 * @param array $res Array of search results
30 * @param bool $includesource [Default=true]
31 * @return void
32 */
33 function display_result($res, $includesource = true) {
34 global $oTemplate, $oErrorHandler;
35
36 if(sizeof($res) <= 0) return;
37
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');
43 }
44
45 /* ================= End of functions ================= */
46
47 /** lets get the global vars we may need */
48
49 if (! sqgetGlobalVar('show' , $show)) {
50 $show = '';
51 }
52 if (! sqgetGlobalVar('query', $query, SQ_POST)) {
53 $query = '';
54 }
55 if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
56 unset($listall);
57 }
58 if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
59 $backend = '';
60 }
61
62 displayHtmlHeader();
63 echo "<body>\n";
64
65 /** set correct value of $default_charset */
66 set_my_charset();
67
68 /* Empty search */
69 if (empty($query) && empty($show) && !isset($listall)) {
70 $oTemplate->assign('note', htmlspecialchars(_("No persons matching your search were found")));
71 $oTemplate->display('note.tpl');
72 # exit;
73 }
74
75 /* Initialize addressbook, show init errors only in bottom frame */
76 $showerr=($show=='form' ? false : true);
77 $abook = addressbook_init($showerr);
78
79 /* Create search form (top frame) */
80 if ($show == 'form' && ! isset($listall)) {
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";
88 } else {
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)) {
96
97 if($backend != -1 || $show == 'blank') {
98 if ($show == 'blank') {
99 $backend = $abook->localbackend;
100 }
101 $res = $abook->list_addr($backend);
102
103 if(is_array($res)) {
104 usort($res,'alistcmp');
105 display_result($res, false);
106 } else {
107 plain_error_message(sprintf(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname));
108 }
109 } else {
110 $res = $abook->list_addr();
111 usort($res,'alistcmp');
112 display_result($res, true);
113 }
114
115 } elseif (!empty($query)) {
116 /* Do the search (listall is not set. query is set.)*/
117
118 if($backend == -1) {
119 $res = $abook->s_search($query);
120 } else {
121 $res = $abook->s_search($query, $backend);
122 }
123
124 if (!is_array($res)) {
125 plain_error_message( _("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)) );
126 } elseif (sizeof($res) == 0) {
127 $oTemplate->assign('note', _("No persons matching your search were found"));
128 $oTemplate->display('note.tpl');
129 } else {
130 display_result($res);
131 }
132 } else {
133 /**
134 * listall is not set, query is not set or empty.
135 * User hit search button without entering search expression.
136 */
137 plain_error_message(_("Nothing to search"));
138 }
139 }
140
141 $oTemplate->display('footer.tpl');