]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/func.inc
Imported Upstream version 0.3
[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 2894 2009-08-29 20:56:00Z alec $
19
20 */
21
22 // add list of address sources to client env
23 $js_list = array();
24 if (strtolower($CONFIG['address_book_type']) != 'ldap') {
25   // We are using the DB address book, add it.
26   $js_list['0'] = array('id' => 0, 'name' => rcube_label('personaladrbook'), 'readonly' => false);
27 }
28 if (is_array($CONFIG['ldap_public'])) {
29   foreach ($CONFIG['ldap_public'] as $id => $prop)
30     $js_list[$id] = array('id' => $id, 'name' => $prop['name'], 'readonly' => !$prop['writable']);
31 }
32
33 $plugin = $RCMAIL->plugins->exec_hook('address_sources', array('sources' => $js_list));
34 $js_list = $plugin['sources'];
35
36 // select source
37 $source = get_input_value('_source', RCUBE_INPUT_GPC);
38
39 // if source is not set use first directory
40 if (empty($source))
41   $source = $js_list[key($js_list)]['id'];
42
43
44 // instantiate a contacts object according to the given source
45 $CONTACTS = $RCMAIL->get_address_book($source);
46
47 $CONTACTS->set_pagesize($CONFIG['pagesize']);
48
49 // set list properties and session vars
50 if (!empty($_GET['_page']))
51   $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
52 else
53   $CONTACTS->set_page(isset($_SESSION['page']) ?$_SESSION['page'] : 1);
54
55 // set message set for search result
56 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
57   $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
58
59 // set data source env
60 $OUTPUT->set_env('source', $source ? $source : '0');
61 $OUTPUT->set_env('readonly', $CONTACTS->readonly, false);
62 $OUTPUT->set_env('address_sources', $js_list);
63
64
65 function rcmail_directory_list($attrib)
66 {
67   global $RCMAIL, $OUTPUT;
68   
69   if (!$attrib['id'])
70     $attrib['id'] = 'rcmdirectorylist';
71
72   $out = '';
73   $local_id = '0';
74   $current = get_input_value('_source', RCUBE_INPUT_GPC);
75   $line_templ = html::tag('li', array('id' => 'rcmli%s', 'class' => '%s'),
76     html::a(array('href' => '%s', 'onclick' => "return ".JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
77
78   if (!$current && strtolower($RCMAIL->config->get('address_book_type', 'sql')) != 'ldap') {
79     $current = '0';
80   }
81   else if (!$current) {
82     // DB address book not used, see if a source is set, if not use the
83     // first LDAP directory.
84     $current = key((array)$RCMAIL->config->get('ldap_public', array()));
85   }
86
87   foreach ((array)$OUTPUT->env['address_sources'] as $j => $source) {
88     $id = $source['id'] ? $source['id'] : $j;
89     $js_id = JQ($id);
90     $dom_id = preg_replace('/[^a-z0-9\-_]/i', '', $id);
91     $out .= sprintf($line_templ, $dom_id, ($current == $id ? 'selected' : ''),
92       Q(rcmail_url(null, array('_source' => $id))), $js_id, (!empty($source['name']) ? Q($source['name']) : Q($id)));
93   }
94
95   $OUTPUT->add_gui_object('folderlist', $attrib['id']);
96   
97   return html::tag('ul', $attrib, $out, html::$common_attrib);
98 }
99
100
101 // return the message list as HTML table
102 function rcmail_contacts_list($attrib)
103   {
104   global $CONTACTS, $OUTPUT;
105   
106   // count contacts for this user
107   $result = $CONTACTS->list_records();
108   
109   // add id to message list table if not specified
110   if (!strlen($attrib['id']))
111     $attrib['id'] = 'rcmAddressList';
112   
113   // define list of cols to be displayed
114   $a_show_cols = array('name');
115
116   // create XHTML table
117   $out = rcube_table_output($attrib, $result->records, $a_show_cols, $CONTACTS->primary_key);
118   
119   // set client env
120   $OUTPUT->add_gui_object('contactslist', $attrib['id']);
121   $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
122   $OUTPUT->set_env('pagecount', ceil($result->count/$CONTACTS->page_size));
123   $OUTPUT->include_script('list.js');
124   
125   // add some labels to client
126   $OUTPUT->add_label('deletecontactconfirm');
127   
128   return $out;
129   }
130
131
132 function rcmail_js_contacts_list($result, $prefix='')
133   {
134   global $OUTPUT;
135
136   if (empty($result) || $result->count == 0)
137     return;
138
139   // define list of cols to be displayed
140   $a_show_cols = array('name');
141   
142   while ($row = $result->next())
143     {
144     $a_row_cols = array();
145     
146     // format each col
147     foreach ($a_show_cols as $col)
148       $a_row_cols[$col] = Q($row[$col]);
149     
150     $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
151     }
152   }
153
154
155 // similar function as /steps/settings/identities.inc::rcmail_identity_frame()
156 function rcmail_contact_frame($attrib)
157   {
158   global $OUTPUT;
159
160   if (!$attrib['id'])
161     $attrib['id'] = 'rcmcontactframe';
162     
163   $attrib['name'] = $attrib['id'];
164
165   $OUTPUT->set_env('contentframe', $attrib['name']);
166   $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
167
168   return html::iframe($attrib);
169   }
170
171
172 function rcmail_rowcount_display($attrib)
173   {
174   global $OUTPUT;
175   
176   if (!$attrib['id'])
177     $attrib['id'] = 'rcmcountdisplay';
178
179   $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
180
181   return html::span($attrib, rcmail_get_rowcount_text());
182   }
183
184
185
186 function rcmail_get_rowcount_text()
187   {
188   global $CONTACTS;
189   
190   // read nr of contacts
191   $result = $CONTACTS->get_result();
192   if (!$result)
193     $result = $CONTACTS->count();
194   
195   if ($result->count == 0)
196     $out = rcube_label('nocontactsfound');
197   else
198     $out = rcube_label(array(
199       'name' => 'contactsfromto',
200       'vars' => array(
201         'from'  => $result->first + 1,
202         'to'    => min($result->count, $result->first + $CONTACTS->page_size),
203         'count' => $result->count)
204       ));
205
206   return $out;
207   }
208
209
210 $OUTPUT->set_pagetitle(rcube_label('addressbook'));
211   
212 // register UI objects
213 $OUTPUT->add_handlers(array(
214   'directorylist' => 'rcmail_directory_list',
215   'addresslist' => 'rcmail_contacts_list',
216   'addressframe' => 'rcmail_contact_frame',
217   'recordscountdisplay' => 'rcmail_rowcount_display',
218   'searchform' => array($OUTPUT, 'search_form')
219 ));
220
221 ?>