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