Address book templates
[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 *
47ccfad4 11 * @copyright &copy; 1999-2006 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 */
66global $default_charset;
67set_my_charset();
68
b0c39665 69/* Empty search */
6027422c 70if (empty($query) && empty($show) && !isset($listall)) {
3f92c0c7 71 $oTemplate->assign('note', htmlspecialchars(_("No persons matching your search were found")));
72 $oTemplate->display('note.tpl');
73# exit;
b0c39665 74}
9487c2ff 75
6dda6453 76/* Initialize addressbook, show init errors only in bottom frame */
77$showerr=($show=='form' ? false : true);
78$abook = addressbook_init($showerr);
9487c2ff 79
6027422c 80/* Create search form (top frame) */
81if ($show == 'form' && ! isset($listall)) {
3f92c0c7 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";
b0c39665 89} else {
6027422c 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)) {
9487c2ff 97
98 if($backend != -1 || $show == 'blank') {
b0c39665 99 if ($show == 'blank') {
9487c2ff 100 $backend = $abook->localbackend;
b0c39665 101 }
9487c2ff 102 $res = $abook->list_addr($backend);
103
104 if(is_array($res)) {
a10110a5 105 usort($res,'alistcmp');
9487c2ff 106 display_result($res, false);
107 } else {
3f92c0c7 108 plain_error_message(sprintf(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname));
9487c2ff 109 }
9487c2ff 110 } else {
b0c39665 111 $res = $abook->list_addr();
a10110a5 112 usort($res,'alistcmp');
b0c39665 113 display_result($res, true);
9487c2ff 114 }
115
6027422c 116 } elseif (!empty($query)) {
117 /* Do the search (listall is not set. query is set.)*/
91e0dccc 118
6027422c 119 if($backend == -1) {
120 $res = $abook->s_search($query);
121 } else {
122 $res = $abook->s_search($query, $backend);
123 }
91e0dccc 124
6027422c 125 if (!is_array($res)) {
3f92c0c7 126 plain_error_message( _("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)) );
766ad164 127 } elseif (sizeof($res) == 0) {
3f92c0c7 128 $oTemplate->assign('note', _("No persons matching your search were found"));
129 $oTemplate->display('note.tpl');
766ad164 130 } else {
131 display_result($res);
b0c39665 132 }
6027422c 133 } else {
134 /**
135 * listall is not set, query is not set or empty.
136 * User hit search button without entering search expression.
137 */
3f92c0c7 138 plain_error_message(_("Nothing to search"));
6027422c 139 }
b0c39665 140}
3f92c0c7 141
5c4ff7bf 142$oTemplate->display('footer.tpl');
3f92c0c7 143?>