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