Address book templates
[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-2006 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 $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('identity', $identity).
57 addHidden('mailbox', $mailbox).
58 addHidden('from_htmladdr_search', 'true');
59 }
60
61
62 /**
63 * List search results
64 * @param array $res Array containing results of search
65 * @param bool $includesource If true, adds backend column to address listing
66 */
67 function addr_display_result($res, $includesource = true) {
68 global $color, $javascript_on, $PHP_SELF, $squirrelmail_language;
69
70 global $oTemplate, $oErrorHandler;
71
72 if (sizeof($res) <= 0) return;
73
74 echo addForm($PHP_SELF, 'post', 'addressbook').
75 addHidden('html_addr_search_done', 'true');
76 addr_insert_hidden();
77
78 $oTemplate->assign('use_js', false);
79 $oTemplate->assign('include_abook_name', $includesource);
80 $oTemplate->assign('addresses', formatAddressList($res));
81
82 $oTemplate->display('addrbook_search_list.tpl');
83
84 echo '</form>';
85 }
86
87 /* --- End functions --- */
88
89 if ($compose_new_win == '1') {
90 compose_Header($color, $mailbox);
91 }
92 else {
93 displayPageHeader($color, $mailbox);
94 }
95
96 /** set correct value of $default_charset */
97 global $default_charset;
98 set_my_charset();
99
100 /* Initialize addressbook */
101 $abook = addressbook_init();
102
103
104 /* Search form */
105 echo addForm($PHP_SELF.'?html_addr_search=true', 'post', 'f');
106 addr_insert_hidden();
107 if (isset($session)) {
108 echo addHidden('session', $session);
109 }
110
111 $oTemplate->assign('use_js', false);
112 $oTemplate->assign('backends', getBackends());
113
114 $oTemplate->display('addressbook_search_form.tpl');
115
116 echo "</form>\n";
117 do_hook('addrbook_html_search_below');
118 /* End search form */
119
120 /* List addresses. Show personal addressbook */
121 if ($addrquery == '' || ! empty($listall)) {
122 // TODO: recheck all conditions and simplity if statements
123 if (! isset($backend) || $backend != -1 || $addrquery == '') {
124 if ($addrquery == '' && empty($listall)) {
125 $backend = $abook->localbackend;
126 }
127
128 /* echo '<h3 align="center">' . $abook->backends[$backend]->sname) . "</h3>\n"; */
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 ?>