]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/func.inc
Imported Upstream version 0.5
[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 4151 2010-10-28 07:10:11Z alec $
19
20 */
21
22 // add list of address sources to client env
23 $js_list = $RCMAIL->get_address_sources();
24
25 // select source
26 $source = get_input_value('_source', RCUBE_INPUT_GPC);
27
28 // if source is not set use first directory
29 if (empty($source))
30     $source = $js_list[key($js_list)]['id'];
31
32 // instantiate a contacts object according to the given source
33 $CONTACTS = $RCMAIL->get_address_book($source);
34
35 $CONTACTS->set_pagesize($CONFIG['pagesize']);
36
37 // set list properties and session vars
38 if (!empty($_GET['_page']))
39     $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
40 else
41     $CONTACTS->set_page(isset($_SESSION['page']) ?$_SESSION['page'] : 1);
42   
43 if (!empty($_REQUEST['_gid']))
44     $CONTACTS->set_group(get_input_value('_gid', RCUBE_INPUT_GPC));
45
46 // set message set for search result
47 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
48     $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
49
50 // set data source env
51 $OUTPUT->set_env('source', $source ? $source : '0');
52 $OUTPUT->set_env('readonly', $CONTACTS->readonly, false);
53 if (!$OUTPUT->ajax_call) {
54     $OUTPUT->set_env('address_sources', $js_list);
55     $OUTPUT->set_pagetitle(rcube_label('addressbook'));
56 }
57
58
59 function rcmail_directory_list($attrib)
60 {
61     global $RCMAIL, $OUTPUT;
62
63     if (!$attrib['id'])
64         $attrib['id'] = 'rcmdirectorylist';
65
66     $out = '';
67     $local_id = '0';
68     $jsdata = array();
69     $current = get_input_value('_source', RCUBE_INPUT_GPC);
70     $line_templ = html::tag('li', array(
71         'id' => 'rcmli%s', 'class' => 'addressbook %s'),
72         html::a(array('href' => '%s',
73             'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
74
75     if (!$current && strtolower($RCMAIL->config->get('address_book_type', 'sql')) != 'ldap') {
76         $current = '0';
77     }
78     else if (!$current) {
79         // DB address book not used, see if a source is set, if not use the
80         // first LDAP directory.
81         $current = key((array)$RCMAIL->config->get('ldap_public', array()));
82     }
83
84     foreach ((array)$OUTPUT->env['address_sources'] as $j => $source) {
85         $id = $source['id'] ? $source['id'] : $j;
86         $js_id = JQ($id);
87         $dom_id = preg_replace('/[^a-z0-9\-_]/i', '', $id);
88         $out .= sprintf($line_templ, $dom_id, ($current == $id ? 'selected' : ''),
89             Q(rcmail_url(null, array('_source' => $id))),
90             $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
91         $groupdata = rcmail_contact_groups(array('out' => $out, 'jsdata' => $jsdata, 'source' => $id));
92         $jsdata = $groupdata['jsdata'];
93         $out = $groupdata['out'];
94     }
95
96     $OUTPUT->set_env('contactgroups', $jsdata); 
97     $OUTPUT->add_gui_object('folderlist', $attrib['id']);
98
99     return html::tag('ul', $attrib, $out, html::$common_attrib);
100 }
101
102
103 function rcmail_contact_groups($args)
104 {
105     global $RCMAIL;
106
107     $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
108
109     if (!empty($groups)) {
110         $line_templ = html::tag('li', array(
111             'id' => 'rcmliG%s%s', 'class' => 'contactgroup'),
112             html::a(array('href' => '#',
113                 'onclick' => "return ".JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
114
115         $jsdata = array();
116         foreach ($groups as $group) {
117             $args['out'] .= sprintf($line_templ, $args['source'], $group['ID'], $args['source'], $group['ID'], Q($group['name']));
118             $args['jsdata']['G'.$args['source'].$group['ID']] = array(
119                 'source' => $args['source'], 'id' => $group['ID'],
120                 'name' => $group['name'], 'type' => 'group');
121         }
122     }
123
124     return $args;
125 }
126
127
128 // return the message list as HTML table
129 function rcmail_contacts_list($attrib)
130 {
131     global $CONTACTS, $OUTPUT;
132
133     // count contacts for this user
134     $result = $CONTACTS->list_records();
135
136     // add id to message list table if not specified
137     if (!strlen($attrib['id']))
138         $attrib['id'] = 'rcmAddressList';
139
140     // define list of cols to be displayed
141     $a_show_cols = array('name');
142
143     // create XHTML table
144     $out = rcube_table_output($attrib, $result->records, $a_show_cols, $CONTACTS->primary_key);
145
146     // set client env
147     $OUTPUT->add_gui_object('contactslist', $attrib['id']);
148     $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
149     $OUTPUT->set_env('pagecount', ceil($result->count/$CONTACTS->page_size));
150     $OUTPUT->include_script('list.js');
151
152     // add some labels to client
153     $OUTPUT->add_label('deletecontactconfirm');
154
155     return $out;
156 }
157
158
159 function rcmail_js_contacts_list($result, $prefix='')
160 {
161     global $OUTPUT;
162
163     if (empty($result) || $result->count == 0)
164         return;
165
166     // define list of cols to be displayed
167     $a_show_cols = array('name');
168   
169     while ($row = $result->next()) {
170         $a_row_cols = array();
171     
172         // format each col
173         foreach ($a_show_cols as $col)
174             $a_row_cols[$col] = Q($row[$col]);
175
176         $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
177     }
178 }
179
180
181 // similar function as /steps/settings/identities.inc::rcmail_identity_frame()
182 function rcmail_contact_frame($attrib)
183 {
184     global $OUTPUT;
185
186     if (!$attrib['id'])
187         $attrib['id'] = 'rcmcontactframe';
188     
189     $attrib['name'] = $attrib['id'];
190
191     $OUTPUT->set_env('contentframe', $attrib['name']);
192     $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
193
194     return html::iframe($attrib);
195 }
196
197
198 function rcmail_rowcount_display($attrib)
199 {
200     global $OUTPUT;
201
202     if (!$attrib['id'])
203         $attrib['id'] = 'rcmcountdisplay';
204
205     $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
206
207     return html::span($attrib, rcmail_get_rowcount_text());
208 }
209
210
211 function rcmail_get_rowcount_text()
212 {
213     global $CONTACTS;
214   
215     // read nr of contacts
216     $result = $CONTACTS->get_result();
217     if (!$result) {
218         $result = $CONTACTS->count();
219     }
220
221     if ($result->count == 0)
222         $out = rcube_label('nocontactsfound');
223     else
224         $out = rcube_label(array(
225             'name'  => 'contactsfromto',
226             'vars'  => array(
227             'from'  => $result->first + 1,
228             'to'    => min($result->count, $result->first + $CONTACTS->page_size),
229             'count' => $result->count)
230         ));
231
232     return $out;
233 }
234
235
236 function rcmail_contact_form($form, $record)
237 {
238     global $RCMAIL;
239
240     // Allow plugins to modify contact form content
241     $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
242         'form' => $form, 'record' => $record));
243
244     $form = $plugin['form'];
245     $record = $plugin['record'];
246     $out = '';
247
248     foreach ($form as $fieldset) {
249         if (empty($fieldset['content']))
250             continue;
251
252         $content = '';
253         if (is_array($fieldset['content'])) {
254             $table = new html_table(array('cols' => 2));
255
256             foreach ($fieldset['content'] as $col => $colprop) {
257                 $colprop['id'] = 'rcmfd_'.$col;
258
259                 $label = !empty($colprop['label']) ? $colprop['label'] : rcube_label($col);
260
261                 if (!empty($colprop['value'])) {
262                     $value = $colprop['value'];
263                 }
264                 else if ($RCMAIL->action == 'show') {
265                     $value = $record[$col];
266                 }
267                 else {
268                     $value = rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']);
269                 }
270
271                 $table->add('title', sprintf('<label for="%s">%s</label>', $colprop['id'], Q($label)));
272                 $table->add(null, $value);
273             }
274             $content = $table->show();
275         }
276         else {
277             $content = $fieldset['content'];
278         }
279
280         $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n";
281     }
282   
283     return $out;
284 }
285
286
287 // register UI objects
288 $OUTPUT->add_handlers(array(
289     'directorylist' => 'rcmail_directory_list',
290 //  'groupslist' => 'rcmail_contact_groups',
291     'addresslist' => 'rcmail_contacts_list',
292     'addressframe' => 'rcmail_contact_frame',
293     'recordscountdisplay' => 'rcmail_rowcount_display',
294     'searchform' => array($OUTPUT, 'search_form')
295 ));