b877ca22c4dc2e8047ffff2272b277a215bd18a8
[squirrelmail.git] / src / addrbook_search_html.php
1 <?php
2 /**
3 * addrbook_search_html.php
4 *
5 * Handle addressbook searching with pure html.
6 *
7 * This file is included from compose.php
8 *
9 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage addressbook
14 */
15
16 /**
17 * Include the SquirrelMail initialization file.
18 * Because this file can also be included within compose we check for the $bInit
19 * var which is set inside ini.php. It's needed because compose already includes
20 * init.php.
21 */
22 if (!isset($bInit)) {
23 include('../include/init.php');
24 }
25
26 /** SquirrelMail required files. */
27 include_once(SM_PATH . 'functions/date.php');
28 include_once(SM_PATH . 'functions/addressbook.php');
29 include_once(SM_PATH . 'templates/util_addressbook.php');
30
31 sqgetGlobalVar('session', $session, SQ_POST);
32 sqgetGlobalVar('mailbox', $mailbox, SQ_POST);
33 if (! sqgetGlobalVar('query', $addrquery, SQ_POST))
34 $addrquery='';
35 sqgetGlobalVar('listall', $listall, SQ_POST);
36 sqgetGlobalVar('backend', $backend, SQ_POST);
37
38 /**
39 * Insert hidden data
40 */
41 function addr_insert_hidden() {
42 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
43 $request_mdn, $request_dr, $identity, $session;
44
45 if (substr($body, 0, 1) == "\r") {
46 echo addHidden('body', "\n".$body);
47 } else {
48 echo addHidden('body', $body);
49 }
50
51 echo addHidden('session', $session).
52 addHidden('subject', $subject).
53 addHidden('send_to', $send_to).
54 addHidden('send_to_bcc', $send_to_bcc).
55 addHidden('send_to_cc', $send_to_cc).
56 addHidden('mailprio', $mailprio).
57 addHidden('request_mdn', $request_mdn).
58 addHidden('request_dr', $request_dr).
59 addHidden('identity', $identity).
60 addHidden('mailbox', $mailbox).
61 addHidden('from_htmladdr_search', 'true');
62 }
63
64
65 /**
66 * List search results
67 * @param array $res Array containing results of search
68 * @param bool $includesource If true, adds backend column to address listing
69 */
70 function addr_display_result($res, $includesource = true) {
71 global $color, $PHP_SELF, $squirrelmail_language;
72
73 global $oTemplate, $oErrorHandler;
74
75
76 echo addForm($PHP_SELF, 'post', 'addressbook').
77 addHidden('html_addr_search_done', 'true');
78 addr_insert_hidden();
79
80 $oTemplate->assign('use_js', false);
81 $oTemplate->assign('include_abook_name', $includesource);
82 $oTemplate->assign('addresses', formatAddressList($res));
83
84 $oTemplate->display('addrbook_search_list.tpl');
85
86 echo '</form>';
87 }
88
89 /* --- End functions --- */
90
91 if ($compose_new_win == '1') {
92 compose_Header($color, $mailbox);
93 }
94 else {
95 displayPageHeader($color, $mailbox);
96 }
97
98 /** set correct value of $default_charset */
99 global $default_charset;
100 set_my_charset();
101
102 /* Initialize addressbook */
103 $abook = addressbook_init();
104
105
106 /* Search form */
107 echo addForm($PHP_SELF.'?html_addr_search=true', 'post', 'f');
108 addr_insert_hidden();
109 if (isset($session)) {
110 echo addHidden('session', $session);
111 }
112
113 $oTemplate->assign('use_js', false);
114 $oTemplate->assign('backends', getBackends());
115
116 $oTemplate->display('addressbook_search_form.tpl');
117
118 echo "</form>\n";
119 do_hook('addrbook_html_search_below', $null);
120 /* End search form */
121
122 /* List addresses. Show personal addressbook */
123 if ($addrquery == '' || ! empty($listall)) {
124 // TODO: recheck all conditions and simplity if statements
125 if (! isset($backend) || $backend != -1 || $addrquery == '') {
126 if ($addrquery == '' && empty($listall)) {
127 $backend = $abook->localbackend;
128 }
129
130 $res = $abook->list_addr($backend);
131
132 if (is_array($res)) {
133 usort($res,'alistcmp');
134 addr_display_result($res, false);
135 } else {
136 plain_error_message(_("Unable to list addresses from %s"), $abook->backends[$backend]->sname);
137 }
138
139 } else {
140 $res = $abook->list_addr();
141 usort($res,'alistcmp');
142 addr_display_result($res, true);
143 }
144 $oTemplate->display('footer.tpl');
145 exit;
146 } elseif (!empty($addrquery)) {
147 /* Do the search */
148 if ($backend == -1) {
149 $res = $abook->s_search($addrquery);
150 } else {
151 $res = $abook->s_search($addrquery, $backend);
152 }
153
154 if (!is_array($res)) {
155 plain_error_message(_("Your search failed with the following error(s)") .':<br />'. nl2br(htmlspecialchars($abook->error)));
156 } elseif (sizeof($res) == 0) {
157 $oTemplate->assign('note', _("No persons matching your search were found"));
158 $oTemplate->display('note.tpl');
159 } else {
160 addr_display_result($res);
161 }
162 } else {
163 // not first time display, not listall and search is empty
164 // TODO: I think, this part of control structure is never reached.
165 plain_error_message(_("Nothing to search"));
166 }
167
168 if ($addrquery == '' || sizeof($res) == 0) {
169 echo '<div style="text-align: center;">'.
170 addForm('compose.php','post','k');
171 addr_insert_hidden();
172 echo '<input type="submit" value="' . _("Return") . '" name="return" />' . "\n" .
173 '</form></div></nobr>';
174 }
175
176 echo '<hr />';
177
178 $oTemplate->display('footer.tpl');
179 ?>