Address book templates
[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-2006 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 global $default_charset;
67 set_my_charset();
68
69 /* Empty search */
70 if (empty($query) && empty($show) && !isset($listall)) {
71 $oTemplate->assign('note', htmlspecialchars(_("No persons matching your search were found")));
72 $oTemplate->display('note.tpl');
73 # exit;
74 }
75
76 /* Initialize addressbook, show init errors only in bottom frame */
77 $showerr=($show=='form' ? false : true);
78 $abook = addressbook_init($showerr);
79
80 /* Create search form (top frame) */
81 if ($show == 'form' && ! isset($listall)) {
82 echo "<form name=\"sform\" target=\"abookres\" action=\"addrbook_search.php\" method=\"POST\">\n";
83
84 $oTemplate->assign('use_js', true);
85 $oTemplate->assign('backends', getBackends());
86 $oTemplate->display('addressbook_search_form.tpl');
87
88 echo "</form>\n";
89 } else {
90 /**
91 * List addresses (bottom frame)
92 * If listall is set, list all entries in selected backend.
93 * If $show is 'blank' (initial call of address book popup) - list
94 * personal address book.
95 */
96 if ($show == 'blank' || isset($listall)) {
97
98 if($backend != -1 || $show == 'blank') {
99 if ($show == 'blank') {
100 $backend = $abook->localbackend;
101 }
102 $res = $abook->list_addr($backend);
103
104 if(is_array($res)) {
105 usort($res,'alistcmp');
106 display_result($res, false);
107 } else {
108 plain_error_message(sprintf(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname));
109 }
110 } else {
111 $res = $abook->list_addr();
112 usort($res,'alistcmp');
113 display_result($res, true);
114 }
115
116 } elseif (!empty($query)) {
117 /* Do the search (listall is not set. query is set.)*/
118
119 if($backend == -1) {
120 $res = $abook->s_search($query);
121 } else {
122 $res = $abook->s_search($query, $backend);
123 }
124
125 if (!is_array($res)) {
126 plain_error_message( _("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)) );
127 } elseif (sizeof($res) == 0) {
128 $oTemplate->assign('note', _("No persons matching your search were found"));
129 $oTemplate->display('note.tpl');
130 } else {
131 display_result($res);
132 }
133 } else {
134 /**
135 * listall is not set, query is not set or empty.
136 * User hit search button without entering search expression.
137 */
138 plain_error_message(_("Nothing to search"));
139 }
140 }
141
142 $oTemplate->display('footer.tpl');
143 ?>