]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/func.inc
Imported Upstream version 0.2~alpha
[roundcube.git] / program / steps / addressbook / func.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/func.inc                                    |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Provide addressbook functionality and GUI objects                   |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: func.inc 1412 2008-05-21 07:45:54Z alec $
19
20 */
21
22 // instantiate a contacts object according to the given source
23 if (($source = get_input_value('_source', RCUBE_INPUT_GPC)) && isset($CONFIG['ldap_public'][$source]))
24   $CONTACTS = new rcube_ldap($CONFIG['ldap_public'][$source]);
25 else if (strtolower($CONFIG['address_book_type']) == 'ldap') {
26     // Get the first LDAP address book.
27     $source = key((array)$CONFIG['ldap_public']);
28     $prop = current((array)$CONFIG['ldap_public']);
29     $CONTACTS = new rcube_ldap($prop);
30 } else {
31     $CONTACTS = new rcube_contacts($DB, $_SESSION['user_id']);
32 } // end else
33
34 $CONTACTS->set_pagesize($CONFIG['pagesize']);
35
36 // set list properties and session vars
37 if (!empty($_GET['_page']))
38   $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
39 else
40   $CONTACTS->set_page(isset($_SESSION['page']) ?$_SESSION['page'] : 1);
41
42 // set message set for search result
43 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
44   $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
45
46 // set data source env
47 $OUTPUT->set_env('source', $source ? $source : '0');
48 $OUTPUT->set_env('readonly', $CONTACTS->readonly, false);
49
50 // add list of address sources to client env
51 $js_list = array();
52 if (strtolower($CONFIG['address_book_type']) != 'ldap') {
53   // We are using the DB address book, add it.
54   $js_list = array("0" => array('id' => 0, 'readonly' => false));
55 } // end if
56 foreach ((array)$CONFIG['ldap_public'] as $id => $prop)
57   $js_list[$id] = array('id' => $id, 'readonly' => !$prop['writable']);
58 $OUTPUT->set_env('address_sources', $js_list);
59
60
61 function rcmail_directory_list($attrib)
62 {
63   global $CONFIG, $OUTPUT;
64   
65   if (!$attrib['id'])
66     $attrib['id'] = 'rcmdirectorylist';
67
68   $local_id = '0';
69   $current = get_input_value('_source', RCUBE_INPUT_GPC);
70   $line_templ = '<li id="%s" class="%s"><a href="%s"' .
71     ' onclick="return %s.command(\'list\',\'%s\',this)"' .
72     ' onmouseover="return %s.focus_folder(\'%s\')"' .
73     ' onmouseout="return %s.unfocus_folder(\'%s\')"' .
74     ' onmouseup="return %s.folder_mouse_up(\'%s\')">%s'.
75     "</a></li>\n";
76     
77   // allow the following attributes to be added to the <ul> tag
78   $out = '<ul' . create_attrib_string($attrib, array('style', 'class', 'id')) . ">\n";
79   if (strtolower($CONFIG['address_book_type']) != 'ldap') {
80     $out .= sprintf($line_templ,
81       'rcmli'.$local_id,
82       !$current ? 'selected' : '',
83       Q(rcmail_url('list', array('_source' => 0))),
84       JS_OBJECT_NAME,
85       $local_id,
86       JS_OBJECT_NAME,
87       $local_id,
88       JS_OBJECT_NAME,
89       $local_id,
90       JS_OBJECT_NAME,
91       $local_id,
92       rcube_label('personaladrbook'));
93   } // end if
94   else {
95     // DB address book not used, see if a source is set, if not use the
96     // first LDAP directory.
97     if (!$current) {
98       $current = key((array)$CONFIG['ldap_public']);
99     } // end if
100   } // end else
101   
102   foreach ((array)$CONFIG['ldap_public'] as $id => $prop)
103   {
104     $js_id = JQ($id);
105     $dom_id = preg_replace('/[^a-z0-9\-_]/i', '', $id);
106     $out .= sprintf($line_templ,
107       'rcmli'.$dom_id,
108       $current == $id ? 'selected' : '',
109       Q(rcmail_url('list', array('_source' => $id))),
110       JS_OBJECT_NAME,
111       $js_id,
112       JS_OBJECT_NAME,
113       $js_id,
114       JS_OBJECT_NAME,
115       $js_id,
116       JS_OBJECT_NAME,
117       $js_id,
118       !empty($prop['name']) ? Q($prop['name']) : Q($id));
119   }
120   
121   $out .= '</ul>';
122
123   $OUTPUT->add_gui_object('folderlist', $attrib['id']);
124   
125   return $out;
126 }
127
128
129 // return the message list as HTML table
130 function rcmail_contacts_list($attrib)
131   {
132   global $CONTACTS, $OUTPUT;
133   
134   // count contacts for this user
135   $result = $CONTACTS->list_records();
136   
137   // add id to message list table if not specified
138   if (!strlen($attrib['id']))
139     $attrib['id'] = 'rcmAddressList';
140   
141   // define list of cols to be displayed
142   $a_show_cols = array('name');
143
144   // create XHTML table
145   $out = rcube_table_output($attrib, $result->records, $a_show_cols, $CONTACTS->primary_key);
146   
147   // set client env
148   $OUTPUT->add_gui_object('contactslist', $attrib['id']);
149   $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
150   $OUTPUT->set_env('pagecount', ceil($result->count/$CONTACTS->page_size));
151   $OUTPUT->include_script('list.js');
152   
153   // add some labels to client
154   rcube_add_label('deletecontactconfirm');
155   
156   return $out;
157   }
158
159
160 function rcmail_js_contacts_list($result, $prefix='')
161   {
162   global $OUTPUT;
163
164   if (empty($result) || $result->count == 0)
165     return;
166
167   // define list of cols to be displayed
168   $a_show_cols = array('name');
169   
170   while ($row = $result->next())
171     {
172     $a_row_cols = array();
173     
174     // format each col
175     foreach ($a_show_cols as $col)
176       $a_row_cols[$col] = Q($row[$col]);
177     
178     $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
179     }
180   }
181
182
183 // similar function as /steps/settings/identities.inc::rcmail_identity_frame()
184 function rcmail_contact_frame($attrib)
185   {
186   global $OUTPUT;
187
188   if (!$attrib['id'])
189     $attrib['id'] = 'rcmcontactframe';
190     
191   $attrib['name'] = $attrib['id'];
192   $attrib_str = create_attrib_string($attrib, array('name', 'id', 'class', 'style', 'src', 'width', 'height', 'frameborder'));
193
194   $OUTPUT->set_env('contentframe', $attrib['name']);
195   $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
196   return '<iframe'. $attrib_str . '></iframe>';
197   }
198
199
200 function rcmail_rowcount_display($attrib)
201   {
202   global $OUTPUT;
203   
204   if (!$attrib['id'])
205     $attrib['id'] = 'rcmcountdisplay';
206
207   $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
208
209   // allow the following attributes to be added to the <span> tag
210   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
211
212   $out = '<span' . $attrib_str . '>';
213   $out .= rcmail_get_rowcount_text();
214   $out .= '</span>';
215   return $out;
216   }
217
218
219
220 function rcmail_get_rowcount_text()
221   {
222   global $CONTACTS;
223   
224   // read nr of contacts
225   $result = $CONTACTS->get_result();
226   if (!$result)
227     $result = $CONTACTS->count();
228   
229   if ($result->count == 0)
230     $out = rcube_label('nocontactsfound');
231   else
232     $out = rcube_label(array(
233       'name' => 'contactsfromto',
234       'vars' => array(
235         'from'  => $result->first + 1,
236         'to'    => min($result->count, $result->first + $CONTACTS->page_size),
237         'count' => $result->count)
238       ));
239
240   return $out;
241   }
242
243 $OUTPUT->set_pagetitle(rcube_label('addressbook'));
244   
245 // register UI objects
246 $OUTPUT->add_handlers(array(
247   'directorylist' => 'rcmail_directory_list',
248   'addresslist' => 'rcmail_contacts_list',
249   'addressframe' => 'rcmail_contact_frame',
250   'recordscountdisplay' => 'rcmail_rowcount_display',
251   'searchform' => array($OUTPUT, 'search_form')
252 ));
253
254 ?>