You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

func.inc 39KB

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