Add template for address book listings along with required files
[squirrelmail.git] / src / addressbook.php
1 <?php
2
3 /**
4 * addressbook.php
5 *
6 * Manage personal address book.
7 *
8 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage addressbook
13 */
14
15 /**
16 * Include the SquirrelMail initialization file.
17 */
18 include('../include/init.php');
19
20 /** SquirrelMail required files. */
21 /* address book functions */
22 require_once(SM_PATH . 'functions/addressbook.php');
23 /* form functions */
24 require_once(SM_PATH . 'functions/forms.php');
25
26 /** lets get the global vars we may need */
27
28 /* From the address form */
29 //sqgetGlobalVar('change_abook', $change_abook, SQ_POST);
30 sqgetGlobalVar('addaddr', $addaddr, SQ_POST);
31 sqgetGlobalVar('editaddr', $editaddr, SQ_POST);
32 sqgetGlobalVar('deladdr', $deladdr, SQ_POST);
33 sqgetGlobalVar('sel', $sel, SQ_POST);
34 sqgetGlobalVar('oldnick', $oldnick, SQ_POST);
35 sqgetGlobalVar('backend', $backend, SQ_POST);
36 sqgetGlobalVar('doedit', $doedit, SQ_POST);
37
38 /* Get sorting order */
39 $abook_sort_order = get_abook_sort();
40
41 /* Create page header before addressbook_init in order to display error messages correctly. */
42 displayPageHeader($color, 'None');
43
44 /* Open addressbook with error messages on.
45 remote backends (LDAP) are enabled because they can be used. (list_addr function)
46 */
47 $abook = addressbook_init(true, false);
48
49 // FIXME: do we have to stop use of address book, when localbackend is not present.
50 if($abook->localbackend == 0) {
51 plain_error_message(_("No personal address book is defined. Contact administrator."));
52 exit();
53 }
54
55 $current_backend = $abook->localbackend;
56 if (sqgetGlobalVar('new_bnum',$new_backend,SQ_POST) && array_key_exists($new_backend,$abook->backends)) {
57 $current_backend = (int) $new_backend;
58 }
59
60 $abook_selection = '&nbsp;';
61 $list_backends = array();
62 if (count($abook->backends) > 1) {
63 foreach($abook->get_backend_list() as $oBackend) {
64 if ($oBackend->listing) {
65 $list_backends[$oBackend->bnum]=$oBackend->sname;
66 }
67 }
68 if (count($list_backends)>1) {
69 $abook_selection = addSelect('new_bnum',$list_backends,$current_backend,true)
70 .addSubmit(_("Change"),'change_abook');
71 }
72 }
73
74 $defdata = array();
75 $formerror = '';
76 $abortform = false;
77 $showaddrlist = true;
78 $defselected = array();
79 $form_url = 'addressbook.php';
80
81 /* Handle user's actions */
82 //if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST' && !isset($change_abook)) {
83 if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'POST') {
84
85 /**************************************************
86 * Add new address *
87 **************************************************/
88 if (isset($addaddr)) {
89 if (isset($backend)) {
90 $r = $abook->add($addaddr, $backend);
91 } else {
92 $r = $abook->add($addaddr, $abook->localbackend);
93 }
94
95 /* Handle error messages */
96 if (!$r) {
97 /* Remove backend name from error string */
98 $errstr = $abook->error;
99 $errstr = ereg_replace('^\[.*\] *', '', $errstr);
100
101 $formerror = $errstr;
102 $showaddrlist = false;
103 $defdata = $addaddr;
104 }
105 } else {
106
107 /************************************************
108 * Delete address(es) *
109 ************************************************/
110 if ((!empty($deladdr)) && sizeof($sel) > 0) {
111 $orig_sel = $sel;
112 sort($sel);
113
114 /* The selected addresses are identidied by "backend:nickname". *
115 * Sort the list and process one backend at the time */
116 $prevback = -1;
117 $subsel = array();
118 $delfailed = false;
119
120 for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
121 list($sbackend, $snick) = explode(':', $sel[$i]);
122
123 /* When we get to a new backend, process addresses in *
124 * previous one. */
125 if ($prevback != $sbackend && $prevback != -1) {
126
127 $r = $abook->remove($subsel, $prevback);
128 if (!$r) {
129 $formerror = $abook->error;
130 $i = sizeof($sel);
131 $delfailed = true;
132 break;
133 }
134 $subsel = array();
135 }
136
137 /* Queue for processing */
138 array_push($subsel, $snick);
139 $prevback = $sbackend;
140 }
141
142 if (!$delfailed) {
143 $r = $abook->remove($subsel, $prevback);
144 if (!$r) { /* Handle errors */
145 $formerror = $abook->error;
146 $delfailed = true;
147 }
148 }
149
150 if ($delfailed) {
151 $showaddrlist = true;
152 $defselected = $orig_sel;
153 }
154
155 } else {
156
157 /***********************************************
158 * Update/modify address *
159 ***********************************************/
160 if (!empty($editaddr)) {
161 /* Stage one: Copy data into form */
162 if (isset($sel) && sizeof($sel) > 0) {
163 if(sizeof($sel) > 1) {
164 $formerror = _("You can only edit one address at the time");
165 $showaddrlist = true;
166 $defselected = $sel;
167 } else {
168 $abortform = true;
169 list($ebackend, $enick) = explode(':', current($sel));
170 $olddata = $abook->lookup($enick, $ebackend);
171 // Test if $olddata really contains anything and return an error message if it doesn't
172 if (!$olddata) {
173 error_box(nl2br(htmlspecialchars($abook->error)));
174 } else {
175 /* Display the "new address" form */
176 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata);
177 echo addHidden('oldnick', $olddata['nickname']).
178 addHidden('backend', $olddata['backend']).
179 addHidden('doedit', '1').
180 '</form>';
181 }
182 }
183 } elseif ($doedit == 1) {
184 /* Stage two: Write new data */
185 $newdata = $editaddr;
186 $r = $abook->modify($oldnick, $newdata, $backend);
187
188 /* Handle error messages */
189 if (!$r) {
190 /* Display error */
191 echo html_tag( 'table',
192 html_tag( 'tr',
193 html_tag( 'td',
194 "\n". '<strong><font color="' . $color[2] .
195 '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
196 'center' )
197 ),
198 'center', '', 'width="100%"' );
199
200 /* Display the "new address" form again */
201 abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata);
202 echo addHidden('oldnick', $oldnick).
203 addHidden('backend', $backend).
204 addHidden('doedit', '1').
205 "\n" . '</form>';
206 $abortform = true;
207 }
208 } else {
209 /**
210 * $editaddr is set, but $sel (address selection in address listing)
211 * and $doedit (address edit form) are not set.
212 * Assume that user clicked on "Edit address" without selecting any address.
213 */
214 $formerror = _("Please select address that you want to edit");
215 $showaddrlist = true;
216 } /* end of edit stage detection */
217 } /* !empty($editaddr) - Update/modify address */
218 } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
219 } /* !empty($addaddr['nickname']) - Add new address */
220
221 // Some times we end output before forms are printed
222 if($abortform) {
223 echo "</body></html>\n";
224 exit();
225 }
226 }
227
228
229 /* =================================================================== *
230 * The following is only executed on a GET request, or on a POST when *
231 * a user is added, or when "delete" or "modify" was successful. *
232 * =================================================================== */
233
234 /* Display error messages */
235 if (!empty($formerror)) {
236 plain_error_message(nl2br(htmlspecialchars($formerror)));
237 }
238
239
240 /* Display the address management part */
241 $addresses = array();
242 while (list($k, $backend) = each ($abook->backends)) {
243 $a = array();
244 $a['BackendID'] = $backend->bnum;
245 $a['BackendSource'] = $backend->sname;
246 $a['BackendWritable'] = $backend->writeable;
247 $a['Addresses'] = array();
248
249 $alist = $abook->list_addr($backend->bnum);
250 usort($alist,'alistcmp');
251 $start = 200;
252 $count = count($alist);
253 if ($start >= $count) $start = 0;
254 $alist = array_slice($alist,$start,15);
255
256 while(list($undef,$row) = each($alist)) {
257 $contact = array (
258 'FirstName' => htmlspecialchars($row['firstname']),
259 'LastName' => htmlspecialchars($row['lastname']),
260 'FullName' => htmlspecialchars($row['name']),
261 'NickName' => htmlspecialchars($row['nickname']),
262 'Email' => htmlspecialchars($row['email']),
263 'FullAddress' => htmlspecialchars($abook->full_address($row)),
264 'Info' => htmlspecialchars($row['label']),
265 'Extra' => (isset($row['extra']) ? $row['extra'] : NULL),
266 );
267 $a['Addresses'][] = $contact;
268 }
269
270 $addresses[$backend->bnum] = $a;
271 }
272
273
274 if ($showaddrlist) {
275 echo addForm($form_url, 'post');
276
277 $oTemplate->assign('addresses', $addresses);
278 $oTemplate->assign('current_backend', $current_backend);
279 $oTemplate->assign('backends', $list_backends);
280
281 $oTemplate->display('addressbook_list.tpl');
282
283 echo "</form>\n";
284 }
285
286 /* Display the "new address" form */
287 echo '<a name="AddAddress"></a>' . "\n";
288 abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata);
289 echo "</form>\n";
290
291 /* Hook for extra address book blocks */
292 echo "<!-- start of addressbook_bottom hook-->\n";
293 do_hook('addressbook_bottom');
294 echo "\n<!-- end of addressbook_bottom hook-->\n";
295
296 $oTemplate->display('footer.tpl');
297 ?>