removing code that could be used with plugin in SM 1.4.x. After init
[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
30 sqgetGlobalVar('session', $session, SQ_POST);
31 sqgetGlobalVar('mailbox', $mailbox, SQ_POST);
32 if (! sqgetGlobalVar('addrquery', $addrquery, SQ_POST))
33 $addrquery='';
34 sqgetGlobalVar('listall', $listall, SQ_POST);
35 sqgetGlobalVar('backend', $backend, SQ_POST);
36
37 /**
38 * Insert hidden data
39 */
40 function addr_insert_hidden() {
41 global $body, $subject, $send_to, $send_to_cc, $send_to_bcc, $mailbox,
42 $identity, $session;
43
44 if (substr($body, 0, 1) == "\r") {
45 echo addHidden('body', "\n".$body);
46 } else {
47 echo addHidden('body', $body);
48 }
49
50 echo addHidden('session', $session).
51 addHidden('subject', $subject).
52 addHidden('send_to', $send_to).
53 addHidden('send_to_bcc', $send_to_bcc).
54 addHidden('send_to_cc', $send_to_cc).
55 addHidden('identity', $identity).
56 addHidden('mailbox', $mailbox).
57 addHidden('from_htmladdr_search', 'true');
58 }
59
60
61 /**
62 * List search results
63 * @param array $res Array containing results of search
64 * @param bool $includesource If true, adds backend column to address listing
65 */
66 function addr_display_result($res, $includesource = true) {
67 global $color, $javascript_on, $PHP_SELF, $squirrelmail_language;
68
69 if (sizeof($res) <= 0) return;
70
71 echo addForm($PHP_SELF, 'post', 'addrbook').
72 addHidden('html_addr_search_done', 'true');
73 addr_insert_hidden();
74 $line = 0;
75
76 if ($javascript_on) {
77 print
78 '<script type="text/javascript">' .
79 "\n<!-- \n" .
80 "function CheckAll(ch) {\n" .
81 " for (var i = 0; i < document.addrbook.elements.length; i++) {\n" .
82 " if( document.addrbook.elements[i].type == 'checkbox' &&\n" .
83 " document.addrbook.elements[i].name.substr(0,16) == 'send_to_search['+ch ) {\n" .
84 " document.addrbook.elements[i].checked = !(document.addrbook.elements[i].checked);\n".
85 " }\n" .
86 " }\n" .
87 "}\n" .
88 "//-->\n" .
89 "</script>\n";
90 $chk_all = '<a href="#" onclick="CheckAll(\'T\');">'._("All").'</a>&nbsp;<font color="'.$color[9].'">'._("To").'</font>'.
91 '&nbsp;&nbsp;'.
92 '<a href="#" onclick="CheckAll(\'C\');">' . _("All") . '</a>&nbsp;<font color="'.$color[9].'">'._("Cc").'</font>'.
93 '&nbsp;&nbsp;'.
94 '<a href="#" onclick="CheckAll(\'B\');">' . _("All") . '</a>';
95 } else {
96 // check_all links are used only in JavaScript. disable links in js=off environment.
97 $chk_all = '';
98 }
99 echo html_tag( 'table', '', 'center', '', 'border="0" width="98%"' ) .
100 html_tag( 'tr', '', '', $color[9] ) .
101 html_tag( 'th', '&nbsp;' . $chk_all, 'left' ) .
102 html_tag( 'th', '&nbsp;' . _("Name"), 'left' ) .
103 html_tag( 'th', '&nbsp;' . _("E-mail"), 'left' ) .
104 html_tag( 'th', '&nbsp;' . _("Info"), 'left' );
105
106 if ($includesource) {
107 echo html_tag( 'th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"' );
108 }
109
110 echo "</tr>\n";
111
112 foreach ($res as $row) {
113 $email = AddressBook::full_address($row);
114 if ($line % 2) {
115 $tr_bgcolor = $color[12];
116 } else {
117 $tr_bgcolor = $color[4];
118 }
119 if ($squirrelmail_language == 'ja_JP')
120 {
121 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
122 html_tag( 'td',
123 '<input type="checkbox" name="send_to_search[T' . $line . ']" value = "' .
124 htmlspecialchars($email) . '" />&nbsp;' . _("To") . '&nbsp;' .
125 '<input type="checkbox" name="send_to_search[C' . $line . ']" value = "' .
126 htmlspecialchars($email) . '" />&nbsp;' . _("Cc") . '&nbsp;' .
127 '<input type="checkbox" name="send_to_search[B' . $line . ']" value = "' .
128 htmlspecialchars($email) . '" />&nbsp;' . _("Bcc") . '&nbsp;' ,
129 'center', '', 'width="5%" style="white-space: nowrap;"' ) .
130 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' ) .
131 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['email']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' ) .
132 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' );
133 } else {
134 echo html_tag( 'tr', '', '', $tr_bgcolor, 'style="white-space: nowrap;"' ) .
135 html_tag( 'td',
136 addCheckBox('send_to_search[T'.$line.']', FALSE, $email).
137 '&nbsp;' . _("To") . '&nbsp;' .
138 addCheckBox('send_to_search[C'.$line.']', FALSE, $email).
139 '&nbsp;' . _("Cc") . '&nbsp;' .
140 addCheckBox('send_to_search[B'.$line.']', FALSE, $email).
141 '&nbsp;' . _("Bcc") . '&nbsp;' ,
142 'center', '', 'width="5%" style="white-space: nowrap;"' ) .
143 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['name']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' ) .
144 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['email']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' ) .
145 html_tag( 'td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' );
146 }
147
148 if ($includesource) {
149 echo html_tag( 'td', '&nbsp;' . $row['source'] . '&nbsp;', 'left', '', 'style="white-space: nowrap;"' );
150 }
151 echo "</tr>\n";
152 $line ++;
153 }
154 if ($includesource) { $td_colspan = '5'; } else { $td_colspan = '4'; }
155 echo html_tag( 'tr',
156 html_tag( 'td',
157 '<input type="submit" name="addr_search_done" value="' .
158 _("Use Addresses") . '" /> ' .
159 '<input type="submit" name="addr_search_cancel" value="' .
160 _("Cancel") . '" />',
161 'center', '', 'colspan="'. $td_colspan .'"' )
162 ) .
163 '</table>' .
164 addHidden('html_addr_search_done', '1').
165 '</form>';
166 }
167
168 /* --- End functions --- */
169
170 if ($compose_new_win == '1') {
171 compose_Header($color, $mailbox);
172 }
173 else {
174 displayPageHeader($color, $mailbox);
175 }
176
177 /** set correct value of $default_charset */
178 global $default_charset;
179 set_my_charset();
180
181 /* Initialize addressbook */
182 $abook = addressbook_init();
183
184
185 echo '<br />' .
186 html_tag( 'table',
187 html_tag( 'tr',
188 html_tag( 'td', '<b>' . _("Address Book Search") . '</b>', 'center', $color[0] )
189 ) ,
190 'center', '', 'width="95%" cellpadding="2" cellspacing="2" border="0"' );
191
192
193 /* Search form */
194 echo '<div style="text-align: center;">' .
195 html_tag( 'table', '', 'center', '', 'border="0"' ) .
196 html_tag( 'tr' ) .
197 html_tag( 'td', '', 'left', '', 'style="white-space: nowrap;" valign="middle"' ) . "\n" .
198 addForm($PHP_SELF.'?html_addr_search=true', 'post', 'f').
199 "\n<div style=\"text-align: center;\">\n" .
200 ' <nobr><strong>' . _("Search for") . "</strong>\n";
201 addr_insert_hidden();
202 echo addInput('addrquery', $addrquery, 26);
203
204 /* List all backends to allow the user to choose where to search */
205 if (!isset($backend)) { $backend = ''; }
206 if ($abook->numbackends > 1) {
207 echo '<strong>' . _("in") . '</strong>&nbsp;';
208
209 $selopts['-1'] = _("All address books");
210 $ret = $abook->get_backend_list();
211
212 while (list($undef,$v) = each($ret)) {
213 $selopts[$v->bnum] = $v->sname;
214 }
215 echo addSelect('backend', $selopts, $backend, TRUE);
216 } else {
217 echo addHidden('backend', '-1');
218 }
219 if (isset($session)) {
220 echo addHidden('session', $session);
221 }
222
223 echo '<input type="submit" value="' . _("Search") . '" />' .
224 '&nbsp;|&nbsp;<input type="submit" value="' . _("List all") .
225 '" name="listall" />' . "\n" .
226 '</form></div></td></tr></table>' . "\n";
227 echo '</div>';
228 do_hook('addrbook_html_search_below');
229 /* End search form */
230
231 /* List addresses. Show personal addressbook */
232 if ($addrquery == '' || ! empty($listall)) {
233 // TODO: recheck all conditions and simplity if statements
234 if (! isset($backend) || $backend != -1 || $addrquery == '') {
235 if ($addrquery == '' && empty($listall)) {
236 $backend = $abook->localbackend;
237 }
238
239 /* echo '<h3 align="center">' . $abook->backends[$backend]->sname) . "</h3>\n"; */
240
241 $res = $abook->list_addr($backend);
242
243 if (is_array($res)) {
244 usort($res,'alistcmp');
245 addr_display_result($res, false);
246 } else {
247 echo html_tag( 'p', '<strong><br />' .
248 sprintf(_("Unable to list addresses from %s"),
249 $abook->backends[$backend]->sname) . "</strong>\n" ,
250 'center' );
251 }
252
253 } else {
254 $res = $abook->list_addr();
255 usort($res,'alistcmp');
256 addr_display_result($res, true);
257 }
258 $oTemplate->display('footer.tpl');
259 exit;
260 } elseif (!empty($addrquery)) {
261 /* Do the search */
262 if ($backend == -1) {
263 $res = $abook->s_search($addrquery);
264 } else {
265 $res = $abook->s_search($addrquery, $backend);
266 }
267
268 if (!is_array($res)) {
269 echo html_tag( 'p', '<b><br />' .
270 _("Your search failed with the following error(s)") .
271 ':<br />' . $abook->error . "</b>\n" ,
272 'center' ) . "\n";
273 $oTemplate->display('footer.tpl');
274 } else {
275 if (sizeof($res) == 0) {
276 echo html_tag( 'p', '<br /><b>' .
277 _("No persons matching your search were found") . "</b>\n" ,
278 'center' ) . "\n";
279 $oTemplate->display('footer.tpl');
280 } else {
281 addr_display_result($res);
282 }
283 }
284 } else {
285 // not first time display, not listall and search is empty
286 // TODO: I think, this part of control structure is never reached.
287 echo html_tag( 'p', '<br /><b>' .
288 _("Nothing to search") . "</b>\n" ,
289 'center' );
290 }
291
292 if ($addrquery == '' || sizeof($res) == 0) {
293 echo '<div style="text-align: center;">'.
294 addForm('compose.php','post','k');
295 addr_insert_hidden();
296 echo '<input type="submit" value="' . _("Return") . '" name="return" />' . "\n" .
297 '</form></div></nobr>';
298 }
299
300 $oTemplate->display('footer.tpl');
301 ?>