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

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