func.inc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/addressbook/func.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2005-2013, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Provide addressbook functionality and GUI objects |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. $SEARCH_MODS_DEFAULT = array('name'=>1, 'firstname'=>1, 'surname'=>1, 'email'=>1, '*'=>1);
  21. // general definition of contact coltypes
  22. $CONTACT_COLTYPES = array(
  23. 'name' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('name'), 'category' => 'main'),
  24. 'firstname' => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('firstname'), 'category' => 'main'),
  25. 'surname' => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('surname'), 'category' => 'main'),
  26. 'email' => array('type' => 'text', 'size' => 40, 'maxlength' => 254, 'label' => $RCMAIL->gettext('email'), 'subtypes' => array('home','work','other'), 'category' => 'main'),
  27. 'middlename' => array('type' => 'text', 'size' => 19, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('middlename'), 'category' => 'main'),
  28. 'prefix' => array('type' => 'text', 'size' => 8, 'maxlength' => 20, 'limit' => 1, 'label' => $RCMAIL->gettext('nameprefix'), 'category' => 'main'),
  29. 'suffix' => array('type' => 'text', 'size' => 8, 'maxlength' => 20, 'limit' => 1, 'label' => $RCMAIL->gettext('namesuffix'), 'category' => 'main'),
  30. 'nickname' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('nickname'), 'category' => 'main'),
  31. 'jobtitle' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('jobtitle'), 'category' => 'main'),
  32. 'organization' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('organization'), 'category' => 'main'),
  33. 'department' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('department'), 'category' => 'main'),
  34. 'gender' => array('type' => 'select', 'limit' => 1, 'label' => $RCMAIL->gettext('gender'), 'options' => array('male' => $RCMAIL->gettext('male'), 'female' => $RCMAIL->gettext('female')), 'category' => 'personal'),
  35. 'maidenname' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('maidenname'), 'category' => 'personal'),
  36. 'phone' => array('type' => 'text', 'size' => 40, 'maxlength' => 20, 'label' => $RCMAIL->gettext('phone'), 'subtypes' => array('home','home2','work','work2','mobile','main','homefax','workfax','car','pager','video','assistant','other'), 'category' => 'main'),
  37. 'address' => array('type' => 'composite', 'label' => $RCMAIL->gettext('address'), 'subtypes' => array('home','work','other'), 'childs' => array(
  38. 'street' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => $RCMAIL->gettext('street'), 'category' => 'main'),
  39. 'locality' => array('type' => 'text', 'size' => 28, 'maxlength' => 50, 'label' => $RCMAIL->gettext('locality'), 'category' => 'main'),
  40. 'zipcode' => array('type' => 'text', 'size' => 8, 'maxlength' => 15, 'label' => $RCMAIL->gettext('zipcode'), 'category' => 'main'),
  41. 'region' => array('type' => 'text', 'size' => 12, 'maxlength' => 50, 'label' => $RCMAIL->gettext('region'), 'category' => 'main'),
  42. 'country' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => $RCMAIL->gettext('country'), 'category' => 'main'),
  43. ), 'category' => 'main'),
  44. 'birthday' => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => $RCMAIL->gettext('birthday'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
  45. 'anniversary' => array('type' => 'date', 'size' => 12, 'maxlength' => 16, 'label' => $RCMAIL->gettext('anniversary'), 'limit' => 1, 'render_func' => 'rcmail_format_date_col', 'category' => 'personal'),
  46. 'website' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => $RCMAIL->gettext('website'), 'subtypes' => array('homepage','work','blog','profile','other'), 'category' => 'main'),
  47. 'im' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'label' => $RCMAIL->gettext('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','skype','other'), 'category' => 'main'),
  48. 'notes' => array('type' => 'textarea', 'size' => 40, 'rows' => 15, 'maxlength' => 500, 'label' => $RCMAIL->gettext('notes'), 'limit' => 1),
  49. 'photo' => array('type' => 'image', 'limit' => 1, 'category' => 'main'),
  50. 'assistant' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('assistant'), 'category' => 'personal'),
  51. 'manager' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('manager'), 'category' => 'personal'),
  52. 'spouse' => array('type' => 'text', 'size' => 40, 'maxlength' => 50, 'limit' => 1, 'label' => $RCMAIL->gettext('spouse'), 'category' => 'personal'),
  53. // TODO: define fields for vcards like GEO, KEY
  54. );
  55. $PAGE_SIZE = $RCMAIL->config->get('addressbook_pagesize', $RCMAIL->config->get('pagesize', 50));
  56. // Addressbook UI
  57. if (!$RCMAIL->action && !$OUTPUT->ajax_call) {
  58. // add list of address sources to client env
  59. $js_list = $RCMAIL->get_address_sources();
  60. // count all/writeable sources
  61. $writeable = 0;
  62. $count = 0;
  63. foreach ($js_list as $sid => $s) {
  64. $count++;
  65. if (!$s['readonly']) {
  66. $writeable++;
  67. }
  68. // unset hidden sources
  69. if ($s['hidden']) {
  70. unset($js_list[$sid]);
  71. }
  72. }
  73. $search_mods = $RCMAIL->config->get('addressbook_search_mods', $SEARCH_MODS_DEFAULT);
  74. $OUTPUT->set_env('search_mods', $search_mods);
  75. $OUTPUT->set_env('address_sources', $js_list);
  76. $OUTPUT->set_env('writable_source', $writeable);
  77. $OUTPUT->set_pagetitle($RCMAIL->gettext('contacts'));
  78. $_SESSION['addressbooks_count'] = $count;
  79. $_SESSION['addressbooks_count_writeable'] = $writeable;
  80. // select address book
  81. $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  82. // use first directory by default
  83. if (!strlen($source) || !isset($js_list[$source])) {
  84. $source = $RCMAIL->config->get('default_addressbook');
  85. if (!strlen($source) || !isset($js_list[$source])) {
  86. $source = strval(key($js_list));
  87. }
  88. }
  89. $CONTACTS = rcmail_contact_source($source, true);
  90. }
  91. // remove undo information...
  92. if ($undo = $_SESSION['contact_undo']) {
  93. // ...after timeout
  94. $undo_time = $RCMAIL->config->get('undo_timeout', 0);
  95. if ($undo['ts'] < time() - $undo_time)
  96. $RCMAIL->session->remove('contact_undo');
  97. }
  98. // register UI objects
  99. $OUTPUT->add_handlers(array(
  100. 'directorylist' => 'rcmail_directory_list',
  101. 'savedsearchlist' => 'rcmail_savedsearch_list',
  102. 'addresslist' => 'rcmail_contacts_list',
  103. 'addresslisttitle' => 'rcmail_contacts_list_title',
  104. 'addressframe' => 'rcmail_contact_frame',
  105. 'recordscountdisplay' => 'rcmail_rowcount_display',
  106. 'searchform' => array($OUTPUT, 'search_form')
  107. ));
  108. // register action aliases
  109. $RCMAIL->register_action_map(array(
  110. 'add' => 'edit.inc',
  111. 'group-create' => 'groups.inc',
  112. 'group-rename' => 'groups.inc',
  113. 'group-delete' => 'groups.inc',
  114. 'group-addmembers' => 'groups.inc',
  115. 'group-delmembers' => 'groups.inc',
  116. 'search-create' => 'search.inc',
  117. 'search-delete' => 'search.inc',
  118. ));
  119. // instantiate a contacts object according to the given source
  120. function rcmail_contact_source($source=null, $init_env=false, $writable=false)
  121. {
  122. global $RCMAIL, $OUTPUT, $CONTACT_COLTYPES, $PAGE_SIZE;
  123. if (!strlen($source)) {
  124. $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  125. }
  126. // Get object
  127. $CONTACTS = $RCMAIL->get_address_book($source, $writable);
  128. $CONTACTS->set_pagesize($PAGE_SIZE);
  129. // set list properties and session vars
  130. if (!empty($_GET['_page']))
  131. $CONTACTS->set_page(($_SESSION['page'] = intval($_GET['_page'])));
  132. else
  133. $CONTACTS->set_page(isset($_SESSION['page']) ? $_SESSION['page'] : 1);
  134. if (!empty($_REQUEST['_gid'])) {
  135. $group = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GPC);
  136. $CONTACTS->set_group($group);
  137. }
  138. if (!$init_env) {
  139. return $CONTACTS;
  140. }
  141. $OUTPUT->set_env('readonly', $CONTACTS->readonly);
  142. $OUTPUT->set_env('source', (string) $source);
  143. $OUTPUT->set_env('group', $group);
  144. // reduce/extend $CONTACT_COLTYPES with specification from the current $CONTACT object
  145. if (is_array($CONTACTS->coltypes)) {
  146. // remove cols not listed by the backend class
  147. $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
  148. $CONTACT_COLTYPES = array_intersect_key($CONTACT_COLTYPES, $contact_cols);
  149. // add associative coltypes definition
  150. if (!$CONTACTS->coltypes[0]) {
  151. foreach ($CONTACTS->coltypes as $col => $colprop) {
  152. if (is_array($colprop['childs'])) {
  153. foreach ($colprop['childs'] as $childcol => $childprop)
  154. $colprop['childs'][$childcol] = array_merge((array)$CONTACT_COLTYPES[$col]['childs'][$childcol], $childprop);
  155. }
  156. $CONTACT_COLTYPES[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], $colprop) : $colprop;
  157. }
  158. }
  159. }
  160. $OUTPUT->set_env('photocol', is_array($CONTACT_COLTYPES['photo']));
  161. return $CONTACTS;
  162. }
  163. function rcmail_set_sourcename($abook)
  164. {
  165. global $OUTPUT, $RCMAIL;
  166. // get address book name (for display)
  167. if ($abook && $_SESSION['addressbooks_count'] > 1) {
  168. $name = $abook->get_name();
  169. if (!$name) {
  170. $name = $RCMAIL->gettext('personaladrbook');
  171. }
  172. $OUTPUT->set_env('sourcename', html_entity_decode($name, ENT_COMPAT, 'UTF-8'));
  173. }
  174. }
  175. function rcmail_directory_list($attrib)
  176. {
  177. global $RCMAIL, $OUTPUT;
  178. if (!$attrib['id'])
  179. $attrib['id'] = 'rcmdirectorylist';
  180. $out = '';
  181. $jsdata = array();
  182. $line_templ = html::tag('li', array(
  183. 'id' => 'rcmli%s', 'class' => '%s', 'noclose' => true),
  184. html::a(array('href' => '%s',
  185. 'rel' => '%s',
  186. 'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('list','%s',this)"), '%s'));
  187. $sources = (array) $OUTPUT->get_env('address_sources');
  188. reset($sources);
  189. // currently selected source
  190. $current = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  191. foreach ($sources as $j => $source) {
  192. $id = strval(strlen($source['id']) ? $source['id'] : $j);
  193. $js_id = rcube::JQ($id);
  194. // set class name(s)
  195. $class_name = 'addressbook';
  196. if ($current === $id)
  197. $class_name .= ' selected';
  198. if ($source['readonly'])
  199. $class_name .= ' readonly';
  200. if ($source['class_name'])
  201. $class_name .= ' ' . $source['class_name'];
  202. $name = $source['name'] ?: $id;
  203. $out .= sprintf($line_templ,
  204. rcube_utils::html_identifier($id, true),
  205. $class_name,
  206. rcube::Q($RCMAIL->url(array('_source' => $id))),
  207. $source['id'],
  208. $js_id, $name);
  209. $groupdata = array('out' => $out, 'jsdata' => $jsdata, 'source' => $id);
  210. if ($source['groups'])
  211. $groupdata = rcmail_contact_groups($groupdata);
  212. $jsdata = $groupdata['jsdata'];
  213. $out = $groupdata['out'];
  214. $out .= '</li>';
  215. }
  216. $OUTPUT->set_env('contactgroups', $jsdata);
  217. $OUTPUT->set_env('collapsed_abooks', (string)$RCMAIL->config->get('collapsed_abooks',''));
  218. $OUTPUT->add_gui_object('folderlist', $attrib['id']);
  219. $OUTPUT->include_script('treelist.js');
  220. // add some labels to client
  221. $OUTPUT->add_label('deletegroupconfirm', 'groupdeleting', 'addingmember', 'removingmember',
  222. 'newgroup', 'grouprename', 'searchsave', 'namex', 'save'
  223. );
  224. return html::tag('ul', $attrib, $out, html::$common_attrib);
  225. }
  226. function rcmail_savedsearch_list($attrib)
  227. {
  228. global $RCMAIL, $OUTPUT;
  229. if (!$attrib['id'])
  230. $attrib['id'] = 'rcmsavedsearchlist';
  231. $out = '';
  232. $line_templ = html::tag('li', array(
  233. 'id' => 'rcmli%s', 'class' => '%s'),
  234. html::a(array('href' => '#', 'rel' => 'S%s',
  235. 'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('listsearch', '%s', this)"), '%s'));
  236. // Saved searches
  237. $sources = $RCMAIL->user->list_searches(rcube_user::SEARCH_ADDRESSBOOK);
  238. foreach ($sources as $source) {
  239. $id = $source['id'];
  240. $js_id = rcube::JQ($id);
  241. // set class name(s)
  242. $classes = array('contactsearch');
  243. if (!empty($source['class_name']))
  244. $classes[] = $source['class_name'];
  245. $out .= sprintf($line_templ,
  246. rcube_utils::html_identifier('S'.$id, true),
  247. join(' ', $classes),
  248. $id,
  249. $js_id, rcube::Q($source['name'] ?: $id)
  250. );
  251. }
  252. $OUTPUT->add_gui_object('savedsearchlist', $attrib['id']);
  253. return html::tag('ul', $attrib, $out, html::$common_attrib);
  254. }
  255. function rcmail_contact_groups($args)
  256. {
  257. global $RCMAIL;
  258. $groups_html = '';
  259. $groups = $RCMAIL->get_address_book($args['source'])->list_groups();
  260. if (!empty($groups)) {
  261. $line_templ = html::tag('li', array(
  262. 'id' => 'rcmli%s', 'class' => 'contactgroup'),
  263. html::a(array('href' => '#',
  264. 'rel' => '%s:%s',
  265. 'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('listgroup',{'source':'%s','id':'%s'},this)"), '%s'));
  266. // append collapse/expand toggle and open a new <ul>
  267. $is_collapsed = strpos($RCMAIL->config->get('collapsed_abooks',''), '&'.rawurlencode($args['source']).'&') !== false;
  268. $args['out'] .= html::div('treetoggle ' . ($is_collapsed ? 'collapsed' : 'expanded'), '&nbsp;');
  269. foreach ($groups as $group) {
  270. $groups_html .= sprintf($line_templ,
  271. rcube_utils::html_identifier('G' . $args['source'] . $group['ID'], true),
  272. $args['source'], $group['ID'],
  273. $args['source'], $group['ID'], rcube::Q($group['name'])
  274. );
  275. $args['jsdata']['G'.$args['source'].$group['ID']] = array(
  276. 'source' => $args['source'], 'id' => $group['ID'],
  277. 'name' => $group['name'], 'type' => 'group');
  278. }
  279. }
  280. $args['out'] .= html::tag('ul',
  281. array('class' => 'groups', 'style' => ($is_collapsed || empty($groups) ? "display:none;" : null)),
  282. $groups_html);
  283. return $args;
  284. }
  285. // return the contacts list as HTML table
  286. function rcmail_contacts_list($attrib)
  287. {
  288. global $RCMAIL, $CONTACTS, $OUTPUT;
  289. // define list of cols to be displayed
  290. $a_show_cols = array('name','action');
  291. // add id to message list table if not specified
  292. if (!strlen($attrib['id']))
  293. $attrib['id'] = 'rcmAddressList';
  294. // create XHTML table
  295. $out = $RCMAIL->table_output($attrib, array(), $a_show_cols, $CONTACTS->primary_key);
  296. // set client env
  297. $OUTPUT->add_gui_object('contactslist', $attrib['id']);
  298. $OUTPUT->set_env('current_page', (int)$CONTACTS->list_page);
  299. $OUTPUT->include_script('list.js');
  300. // add some labels to client
  301. $OUTPUT->add_label('deletecontactconfirm', 'copyingcontact', 'movingcontact', 'contactdeleting');
  302. return $out;
  303. }
  304. function rcmail_js_contacts_list($result, $prefix='')
  305. {
  306. global $OUTPUT, $RCMAIL;
  307. if (empty($result) || $result->count == 0) {
  308. return;
  309. }
  310. // define list of cols to be displayed
  311. $a_show_cols = array('name','action');
  312. while ($row = $result->next()) {
  313. $emails = rcube_addressbook::get_col_values('email', $row, true);
  314. $row['CID'] = $row['ID'];
  315. $row['email'] = reset($emails);
  316. $source_id = $OUTPUT->get_env('source');
  317. $a_row_cols = array();
  318. $classes = array($row['_type'] ?: 'person');
  319. // build contact ID with source ID
  320. if (isset($row['sourceid'])) {
  321. $row['ID'] = $row['ID'].'-'.$row['sourceid'];
  322. $source_id = $row['sourceid'];
  323. }
  324. // format each col
  325. foreach ($a_show_cols as $col) {
  326. $val = '';
  327. switch ($col) {
  328. case 'name':
  329. $val = rcube::Q(rcube_addressbook::compose_list_name($row));
  330. break;
  331. case 'action':
  332. if ($row['_type'] == 'group') {
  333. $val = html::a(array(
  334. 'href' => '#list',
  335. 'rel' => $row['ID'],
  336. 'title' => $RCMAIL->gettext('listgroup'),
  337. 'onclick' => sprintf("return %s.command('pushgroup',{'source':'%s','id':'%s'},this,event)", rcmail_output::JS_OBJECT_NAME, $source_id, $row['CID']),
  338. ), '&raquo;');
  339. }
  340. else
  341. $val = '';
  342. break;
  343. default:
  344. $val = rcube::Q($row[$col]);
  345. break;
  346. }
  347. $a_row_cols[$col] = $val;
  348. }
  349. if ($row['readonly'])
  350. $classes[] = 'readonly';
  351. $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols, join(' ', $classes), array_intersect_key($row, array('ID'=>1,'readonly'=>1,'_type'=>1,'email'=>1,'name'=>1)));
  352. }
  353. }
  354. function rcmail_contacts_list_title($attrib)
  355. {
  356. global $OUTPUT, $RCMAIL;
  357. $attrib += array('label' => 'contacts', 'id' => 'rcmabooklisttitle', 'tag' => 'span');
  358. unset($attrib['name']);
  359. $OUTPUT->add_gui_object('addresslist_title', $attrib['id']);
  360. $OUTPUT->add_label('contacts','uponelevel');
  361. return html::tag($attrib['tag'], $attrib, $RCMAIL->gettext($attrib['label']), html::$common_attrib);
  362. }
  363. // similar function as /steps/settings/identities.inc::rcmail_identity_frame()
  364. function rcmail_contact_frame($attrib)
  365. {
  366. global $OUTPUT;
  367. if (!$attrib['id'])
  368. $attrib['id'] = 'rcmcontactframe';
  369. return $OUTPUT->frame($attrib, true);
  370. }
  371. function rcmail_rowcount_display($attrib)
  372. {
  373. global $RCMAIL;
  374. if (!$attrib['id'])
  375. $attrib['id'] = 'rcmcountdisplay';
  376. $RCMAIL->output->add_gui_object('countdisplay', $attrib['id']);
  377. if ($attrib['label'])
  378. $_SESSION['contactcountdisplay'] = $attrib['label'];
  379. return html::span($attrib, $RCMAIL->gettext('loading'));
  380. }
  381. function rcmail_get_rowcount_text($result=null)
  382. {
  383. global $RCMAIL, $CONTACTS, $PAGE_SIZE;
  384. // read nr of contacts
  385. if (!$result) {
  386. $result = $CONTACTS->get_result();
  387. }
  388. if ($result->count == 0)
  389. $out = $RCMAIL->gettext('nocontactsfound');
  390. else
  391. $out = $RCMAIL->gettext(array(
  392. 'name' => $_SESSION['contactcountdisplay'] ?: 'contactsfromto',
  393. 'vars' => array(
  394. 'from' => $result->first + 1,
  395. 'to' => min($result->count, $result->first + $PAGE_SIZE),
  396. 'count' => $result->count)
  397. ));
  398. return $out;
  399. }
  400. function rcmail_get_type_label($type)
  401. {
  402. global $RCMAIL;
  403. $label = 'type'.$type;
  404. if ($RCMAIL->text_exists($label, '*', $domain))
  405. return $RCMAIL->gettext($label, $domain);
  406. else if (preg_match('/\w+(\d+)$/', $label, $m)
  407. && ($label = preg_replace('/(\d+)$/', '', $label))
  408. && $RCMAIL->text_exists($label, '*', $domain))
  409. return $RCMAIL->gettext($label, $domain) . ' ' . $m[1];
  410. return ucfirst($type);
  411. }
  412. function rcmail_contact_form($form, $record, $attrib = null)
  413. {
  414. global $RCMAIL;
  415. // group fields
  416. $head_fields = array(
  417. 'names' => array('prefix','firstname','middlename','surname','suffix'),
  418. 'displayname' => array('name'),
  419. 'nickname' => array('nickname'),
  420. 'organization' => array('organization'),
  421. 'department' => array('department'),
  422. 'jobtitle' => array('jobtitle'),
  423. );
  424. // Allow plugins to modify contact form content
  425. $plugin = $RCMAIL->plugins->exec_hook('contact_form', array(
  426. 'form' => $form, 'record' => $record, 'head_fields' => $head_fields));
  427. $form = $plugin['form'];
  428. $record = $plugin['record'];
  429. $head_fields = $plugin['head_fields'];
  430. $edit_mode = $RCMAIL->action != 'show' && $RCMAIL->action != 'print';
  431. $del_button = $attrib['deleteicon'] ? html::img(array('src' => $RCMAIL->output->get_skin_file($attrib['deleteicon']), 'alt' => $RCMAIL->gettext('delete'))) : $RCMAIL->gettext('delete');
  432. $out = '';
  433. unset($attrib['deleteicon']);
  434. // get default coltypes
  435. $coltypes = $GLOBALS['CONTACT_COLTYPES'];
  436. $coltype_labels = array();
  437. foreach ($coltypes as $col => $prop) {
  438. if ($prop['subtypes']) {
  439. $subtype_names = array_map('rcmail_get_type_label', $prop['subtypes']);
  440. $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype', 'title' => $prop['label'] . ' ' . $RCMAIL->gettext('type')));
  441. $select_subtype->add($subtype_names, $prop['subtypes']);
  442. $coltypes[$col]['subtypes_select'] = $select_subtype->show();
  443. }
  444. if ($prop['childs']) {
  445. foreach ($prop['childs'] as $childcol => $cp)
  446. $coltype_labels[$childcol] = array('label' => $cp['label']);
  447. }
  448. }
  449. foreach ($form as $section => $fieldset) {
  450. // skip empty sections
  451. if (empty($fieldset['content'])) {
  452. continue;
  453. }
  454. $select_add = new html_select(array('class' => 'addfieldmenu', 'rel' => $section));
  455. $select_add->add($RCMAIL->gettext('addfield'), '');
  456. // render head section with name fields (not a regular list of rows)
  457. if ($section == 'head') {
  458. $content = '';
  459. // unset display name if it is composed from name parts
  460. if ($record['name'] == rcube_addressbook::compose_display_name(array('name' => '') + (array)$record)) {
  461. unset($record['name']);
  462. }
  463. foreach ($head_fields as $blockname => $colnames) {
  464. $fields = '';
  465. foreach ($colnames as $col) {
  466. // skip cols unknown to the backend
  467. if (!$coltypes[$col])
  468. continue;
  469. // skip cols not listed in the form definition
  470. if (is_array($fieldset['content']) && !in_array($col, array_keys($fieldset['content']))) {
  471. continue;
  472. }
  473. // only string values are expected here
  474. if (is_array($record[$col]))
  475. $record[$col] = join(' ', $record[$col]);
  476. if (!$edit_mode) {
  477. if (!empty($record[$col]))
  478. $fields .= html::span('namefield ' . $col, rcube::Q($record[$col])) . " ";
  479. }
  480. else {
  481. $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col];
  482. $colprop['id'] = 'ff_'.$col;
  483. if (empty($record[$col]) && !$colprop['visible']) {
  484. $colprop['style'] = 'display:none';
  485. $select_add->add($colprop['label'], $col);
  486. }
  487. $fields .= rcube_output::get_edit_field($col, $record[$col], $colprop, $colprop['type']);
  488. }
  489. }
  490. $content .= html::div($blockname, $fields);
  491. }
  492. if ($edit_mode)
  493. $content .= html::p('addfield', $select_add->show(null));
  494. $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, rcube::Q($fieldset['name'])) : '') . $content) ."\n";
  495. continue;
  496. }
  497. $content = '';
  498. if (is_array($fieldset['content'])) {
  499. foreach ($fieldset['content'] as $col => $colprop) {
  500. // remove subtype part of col name
  501. list($field, $subtype) = explode(':', $col);
  502. if (!$subtype) $subtype = 'home';
  503. $fullkey = $col.':'.$subtype;
  504. // skip cols unknown to the backend
  505. if (!$coltypes[$field] && empty($colprop['value'])) {
  506. continue;
  507. }
  508. // merge colprop with global coltype configuration
  509. if ($coltypes[$field]) {
  510. $colprop += $coltypes[$field];
  511. }
  512. $label = isset($colprop['label']) ? $colprop['label'] : $RCMAIL->gettext($col);
  513. // prepare subtype selector in edit mode
  514. if ($edit_mode && is_array($colprop['subtypes'])) {
  515. $subtype_names = array_map('rcmail_get_type_label', $colprop['subtypes']);
  516. $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype', 'title' => $colprop['label'] . ' ' . $RCMAIL->gettext('type')));
  517. $select_subtype->add($subtype_names, $colprop['subtypes']);
  518. }
  519. else {
  520. $select_subtype = null;
  521. }
  522. if (!empty($colprop['value'])) {
  523. $values = (array)$colprop['value'];
  524. }
  525. else {
  526. // iterate over possible subtypes and collect values with their subtype
  527. if (is_array($colprop['subtypes'])) {
  528. $values = $subtypes = array();
  529. foreach (rcube_addressbook::get_col_values($field, $record) as $st => $vals) {
  530. foreach((array)$vals as $value) {
  531. $i = count($values);
  532. $subtypes[$i] = $st;
  533. $values[$i] = $value;
  534. }
  535. // TODO: add $st to $select_subtype if missing ?
  536. }
  537. }
  538. else {
  539. $values = $record[$fullkey] ?: $record[$field];
  540. $subtypes = null;
  541. }
  542. }
  543. // hack: create empty values array to force this field to be displayed
  544. if (empty($values) && $colprop['visible'])
  545. $values[] = '';
  546. if (!is_array($values)) {
  547. // $values can be an object, don't use (array)$values syntax
  548. $values = !empty($values) ? array($values) : array();
  549. }
  550. $rows = '';
  551. foreach ($values as $i => $val) {
  552. if ($subtypes[$i])
  553. $subtype = $subtypes[$i];
  554. $colprop['id'] = 'ff_' . $col . intval($coltypes[$field]['count']);
  555. // render composite field
  556. if ($colprop['type'] == 'composite') {
  557. $composite = array(); $j = 0;
  558. $template = $RCMAIL->config->get($col . '_template', '{'.join('} {', array_keys($colprop['childs'])).'}');
  559. foreach ($colprop['childs'] as $childcol => $cp) {
  560. if (!empty($val) && is_array($val)) {
  561. $childvalue = $val[$childcol] ?: $val[$j];
  562. }
  563. else {
  564. $childvalue = '';
  565. }
  566. if ($edit_mode) {
  567. if ($colprop['subtypes'] || $colprop['limit'] != 1) $cp['array'] = true;
  568. $composite['{'.$childcol.'}'] = rcube_output::get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " ";
  569. }
  570. else {
  571. $childval = $cp['render_func'] ? call_user_func($cp['render_func'], $childvalue, $childcol) : rcube::Q($childvalue);
  572. $composite['{'.$childcol.'}'] = html::span('data ' . $childcol, $childval) . " ";
  573. }
  574. $j++;
  575. }
  576. $coltypes[$field] += (array)$colprop;
  577. $coltypes[$field]['count']++;
  578. $val = preg_replace('/\{\w+\}/', '', strtr($template, $composite));
  579. }
  580. else if ($edit_mode) {
  581. // call callback to render/format value
  582. if ($colprop['render_func'])
  583. $val = call_user_func($colprop['render_func'], $val, $col);
  584. $coltypes[$field] = (array)$colprop + $coltypes[$field];
  585. if ($colprop['subtypes'] || $colprop['limit'] != 1)
  586. $colprop['array'] = true;
  587. // load jquery UI datepicker for date fields
  588. if ($colprop['type'] == 'date') {
  589. $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
  590. if (!$colprop['render_func'])
  591. $val = rcmail_format_date_col($val);
  592. }
  593. $val = rcube_output::get_edit_field($col, $val, $colprop, $colprop['type']);
  594. $coltypes[$field]['count']++;
  595. }
  596. else if ($colprop['render_func'])
  597. $val = call_user_func($colprop['render_func'], $val, $col);
  598. else if (is_array($colprop['options']) && isset($colprop['options'][$val]))
  599. $val = $colprop['options'][$val];
  600. else
  601. $val = rcube::Q($val);
  602. // use subtype as label
  603. if ($colprop['subtypes'])
  604. $label = rcmail_get_type_label($subtype);
  605. // add delete button/link
  606. if ($edit_mode && !($colprop['visible'] && $colprop['limit'] == 1))
  607. $val .= html::a(array('href' => '#del', 'class' => 'contactfieldbutton deletebutton', 'title' => $RCMAIL->gettext('delete'), 'rel' => $col), $del_button);
  608. // display row with label
  609. if ($label) {
  610. if ($RCMAIL->action == 'print') {
  611. $_label = rcube::Q($colprop['label'] . ($label != $colprop['label'] ? ' (' . $label . ')' : ''));
  612. }
  613. else {
  614. $_label = $select_subtype ? $select_subtype->show($subtype) : html::label($colprop['id'], rcube::Q($label));
  615. }
  616. $rows .= html::div('row',
  617. html::div('contactfieldlabel label', $_label) .
  618. html::div('contactfieldcontent '.$colprop['type'], $val));
  619. }
  620. // row without label
  621. else {
  622. $rows .= html::div('row', html::div('contactfield', $val));
  623. }
  624. }
  625. // add option to the add-field menu
  626. if (!$colprop['limit'] || $coltypes[$field]['count'] < $colprop['limit']) {
  627. $select_add->add($colprop['label'], $col);
  628. $select_add->_count++;
  629. }
  630. // wrap rows in fieldgroup container
  631. if ($rows) {
  632. $c_class = 'contactfieldgroup ' . ($colprop['subtypes'] ? 'contactfieldgroupmulti ' : '') . 'contactcontroller' . $col;
  633. $with_label = $colprop['subtypes'] && $RCMAIL->action != 'print';
  634. $content .= html::tag(
  635. 'fieldset',
  636. array('class' => $c_class, 'style' => ($rows ? null : 'display:none')),
  637. ($with_label ? html::tag('legend', null, rcube::Q($colprop['label'])) : ' ') . $rows
  638. );
  639. }
  640. }
  641. if (!$content && (!$edit_mode || !$select_add->_count))
  642. continue;
  643. // also render add-field selector
  644. if ($edit_mode)
  645. $content .= html::p('addfield', $select_add->show(null, array('style' => $select_add->_count ? null : 'display:none')));
  646. $content = html::div(array('id' => 'contactsection' . $section), $content);
  647. }
  648. else {
  649. $content = $fieldset['content'];
  650. }
  651. if ($content)
  652. $out .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($fieldset['name'])) . $content) ."\n";
  653. }
  654. if ($edit_mode) {
  655. $RCMAIL->output->set_env('coltypes', $coltypes + $coltype_labels);
  656. $RCMAIL->output->set_env('delbutton', $del_button);
  657. $RCMAIL->output->add_label('delete');
  658. }
  659. return $out;
  660. }
  661. function rcmail_contact_photo($attrib)
  662. {
  663. global $SOURCE_ID, $CONTACTS, $CONTACT_COLTYPES, $RCMAIL;
  664. if ($result = $CONTACTS->get_result())
  665. $record = $result->first();
  666. $photo_img = $attrib['placeholder'] ? $RCMAIL->output->abs_url($attrib['placeholder'], true) : 'program/resources/blank.gif';
  667. if ($record['_type'] == 'group' && $attrib['placeholdergroup'])
  668. $photo_img = $RCMAIL->output->abs_url($attrib['placeholdergroup'], true);
  669. $RCMAIL->output->set_env('photo_placeholder', $RCMAIL->output->asset_url($photo_img));
  670. unset($attrib['placeholder']);
  671. $plugin = $RCMAIL->plugins->exec_hook('contact_photo', array('record' => $record, 'data' => $record['photo']));
  672. // check if we have photo data from contact form
  673. if ($GLOBALS['EDIT_RECORD']) {
  674. $rec = $GLOBALS['EDIT_RECORD'];
  675. if ($rec['photo'] == '-del-') {
  676. $record['photo'] = '';
  677. }
  678. else if ($_SESSION['contacts']['files'][$rec['photo']]) {
  679. $record['photo'] = $file_id = $rec['photo'];
  680. }
  681. }
  682. if ($plugin['url'])
  683. $photo_img = $plugin['url'];
  684. else if (preg_match('!^https?://!i', $record['photo']))
  685. $photo_img = $record['photo'];
  686. else if ($record['photo']) {
  687. $url = array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $SOURCE_ID);
  688. if ($file_id) {
  689. $url['_photo'] = $ff_value = $file_id;
  690. }
  691. $photo_img = $RCMAIL->url($url);
  692. }
  693. else {
  694. $ff_value = '-del-'; // will disable delete-photo action
  695. }
  696. $content = html::div($attrib, html::img(array(
  697. 'src' => $photo_img,
  698. 'alt' => $RCMAIL->gettext('contactphoto'),
  699. 'onerror' => 'this.src = rcmail.env.photo_placeholder',
  700. )));
  701. if ($CONTACT_COLTYPES['photo'] && ($RCMAIL->action == 'edit' || $RCMAIL->action == 'add')) {
  702. $RCMAIL->output->add_gui_object('contactphoto', $attrib['id']);
  703. $hidden = new html_hiddenfield(array('name' => '_photo', 'id' => 'ff_photo', 'value' => $ff_value));
  704. $content .= $hidden->show();
  705. }
  706. return $content;
  707. }
  708. function rcmail_format_date_col($val)
  709. {
  710. global $RCMAIL;
  711. return $RCMAIL->format_date($val, $RCMAIL->config->get('date_format', 'Y-m-d'), false);
  712. }
  713. /**
  714. * Updates saved search after data changed
  715. */
  716. function rcmail_search_update($return = false)
  717. {
  718. global $RCMAIL;
  719. if (($search_request = $_REQUEST['_search']) && isset($_SESSION['search'][$search_request])) {
  720. $search = (array)$_SESSION['search'][$search_request];
  721. $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  722. $afields = $return ? $RCMAIL->config->get('contactlist_fields') : array('name', 'email');
  723. $records = array();
  724. foreach ($search as $s => $set) {
  725. $source = $RCMAIL->get_address_book($s);
  726. // reset page
  727. $source->set_page(1);
  728. $source->set_pagesize(9999);
  729. $source->set_search_set($set);
  730. // get records
  731. $result = $source->list_records($afields);
  732. if (!$result->count) {
  733. unset($search[$s]);
  734. continue;
  735. }
  736. if ($return) {
  737. while ($row = $result->next()) {
  738. $row['sourceid'] = $s;
  739. $key = rcube_addressbook::compose_contact_key($row, $sort_col);
  740. $records[$key] = $row;
  741. }
  742. unset($result);
  743. }
  744. $search[$s] = $source->get_search_set();
  745. }
  746. $_SESSION['search'][$search_request] = $search;
  747. return $records;
  748. }
  749. return false;
  750. }
  751. /**
  752. * Returns contact ID(s) and source(s) from GET/POST data
  753. *
  754. * @return array List of contact IDs per-source
  755. */
  756. function rcmail_get_cids($filter = null, $request_type = rcube_utils::INPUT_GPC)
  757. {
  758. // contact ID (or comma-separated list of IDs) is provided in two
  759. // forms. If _source is an empty string then the ID is a string
  760. // containing contact ID and source name in form: <ID>-<SOURCE>
  761. $cid = rcube_utils::get_input_value('_cid', $request_type);
  762. $source = (string) rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  763. if (is_array($cid)) {
  764. return $cid;
  765. }
  766. if (!preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)) {
  767. return array();
  768. }
  769. $cid = explode(',', $cid);
  770. $got_source = strlen($source);
  771. $result = array();
  772. // create per-source contact IDs array
  773. foreach ($cid as $id) {
  774. // extract source ID from contact ID (it's there in search mode)
  775. // see #1488959 and #1488862 for reference
  776. if (!$got_source) {
  777. if ($sep = strrpos($id, '-')) {
  778. $contact_id = substr($id, 0, $sep);
  779. $source_id = (string) substr($id, $sep+1);
  780. if (strlen($source_id)) {
  781. $result[$source_id][] = $contact_id;
  782. }
  783. }
  784. }
  785. else {
  786. if (substr($id, -($got_source+1)) === "-$source") {
  787. $id = substr($id, 0, -($got_source+1));
  788. }
  789. $result[$source][] = $id;
  790. }
  791. }
  792. return $filter !== null ? $result[$filter] : $result;
  793. }