Happy New Year
[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 *
353d074a 11 * @copyright 1999-2018 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
ebd2391c 18/** This is the addrbook_search page */
19define('PAGE_NAME', 'addrbook_search');
20
8f6f9ba5 21/**
202bcbcc 22 * Include the SquirrelMail initialization file.
8f6f9ba5 23 */
202bcbcc 24require('../include/init.php');
86725763 25
6027422c 26include_once(SM_PATH . 'functions/forms.php');
27include_once(SM_PATH . 'functions/addressbook.php');
3f92c0c7 28include_once(SM_PATH . 'templates/util_addressbook.php');
2f73dc15 29
8f6f9ba5 30/**
31 * List search results
32 * @param array $res Array of search results
33 * @param bool $includesource [Default=true]
34 * @return void
35 */
b0c39665 36function display_result($res, $includesource = true) {
3f92c0c7 37 global $oTemplate, $oErrorHandler;
91e0dccc 38
b0c39665 39 if(sizeof($res) <= 0) return;
91e0dccc 40
0885e0d7 41 $oTemplate->assign('compose_addr_pop', true);
3f92c0c7 42 $oTemplate->assign('include_abook_name', $includesource);
43 $oTemplate->assign('addresses', formatAddressList($res));
44
45 $oTemplate->display('addrbook_search_list.tpl');
b0c39665 46}
9487c2ff 47
b0c39665 48/* ================= End of functions ================= */
91e0dccc 49
6027422c 50/** lets get the global vars we may need */
6027422c 51
52if (! sqgetGlobalVar('show' , $show)) {
53 $show = '';
54}
55if (! sqgetGlobalVar('query', $query, SQ_POST)) {
56 $query = '';
57}
58if (! sqgetGlobalVar('listall', $listall, SQ_POST)) {
59 unset($listall);
60}
61if (! sqgetGlobalVar('backend', $backend, SQ_POST)) {
62 $backend = '';
63}
91e0dccc 64
b0c39665 65displayHtmlHeader();
3f92c0c7 66echo "<body>\n";
91e0dccc 67
40262494 68/** set correct value of $default_charset */
40262494 69set_my_charset();
70
b0c39665 71/* Empty search */
6027422c 72if (empty($query) && empty($show) && !isset($listall)) {
3047e291 73 $oTemplate->assign('note', sm_encode_html_special_chars(_("No persons matching your search were found")));
3f92c0c7 74 $oTemplate->display('note.tpl');
75# exit;
b0c39665 76}
9487c2ff 77
6dda6453 78/* Initialize addressbook, show init errors only in bottom frame */
79$showerr=($show=='form' ? false : true);
80$abook = addressbook_init($showerr);
9487c2ff 81
6027422c 82/* Create search form (top frame) */
83if ($show == 'form' && ! isset($listall)) {
12b5083f 84 echo "<form name=\"sform\" target=\"abookres\" action=\"addrbook_search.php\" method=\"post\">\n";
3f92c0c7 85
0885e0d7 86 $oTemplate->assign('compose_addr_pop', true);
3f92c0c7 87 $oTemplate->assign('backends', getBackends());
88 $oTemplate->display('addressbook_search_form.tpl');
89
90 echo "</form>\n";
b0c39665 91} else {
6027422c 92 /**
93 * List addresses (bottom frame)
94 * If listall is set, list all entries in selected backend.
95 * If $show is 'blank' (initial call of address book popup) - list
96 * personal address book.
97 */
98 if ($show == 'blank' || isset($listall)) {
9487c2ff 99
100 if($backend != -1 || $show == 'blank') {
b0c39665 101 if ($show == 'blank') {
9487c2ff 102 $backend = $abook->localbackend;
b0c39665 103 }
9487c2ff 104 $res = $abook->list_addr($backend);
105
106 if(is_array($res)) {
a10110a5 107 usort($res,'alistcmp');
9487c2ff 108 display_result($res, false);
109 } else {
3f92c0c7 110 plain_error_message(sprintf(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname));
9487c2ff 111 }
9487c2ff 112 } else {
b0c39665 113 $res = $abook->list_addr();
a10110a5 114 usort($res,'alistcmp');
b0c39665 115 display_result($res, true);
9487c2ff 116 }
117
6027422c 118 } elseif (!empty($query)) {
119 /* Do the search (listall is not set. query is set.)*/
91e0dccc 120
6027422c 121 if($backend == -1) {
122 $res = $abook->s_search($query);
123 } else {
124 $res = $abook->s_search($query, $backend);
125 }
91e0dccc 126
6027422c 127 if (!is_array($res)) {
3047e291 128 plain_error_message( _("Your search failed with the following error(s)") .':<br />'. nl2br(sm_encode_html_special_chars($abook->error)) );
766ad164 129 } elseif (sizeof($res) == 0) {
3f92c0c7 130 $oTemplate->assign('note', _("No persons matching your search were found"));
131 $oTemplate->display('note.tpl');
766ad164 132 } else {
133 display_result($res);
b0c39665 134 }
6027422c 135 } else {
136 /**
137 * listall is not set, query is not set or empty.
138 * User hit search button without entering search expression.
139 */
3f92c0c7 140 plain_error_message(_("Nothing to search"));
6027422c 141 }
b0c39665 142}
3f92c0c7 143
5c4ff7bf 144$oTemplate->display('footer.tpl');