index.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack mail@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. function contacts_namesort($a,$b){
  23. return strcmp($a['name'],$b['name']);
  24. }
  25. // Init owncloud
  26. require_once('../../lib/base.php');
  27. // Check if we are a user
  28. OC_Util::checkLoggedIn();
  29. OC_Util::checkAppEnabled('contacts');
  30. // Check if the user has an addressbook
  31. $addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
  32. if( count($addressbooks) == 0){
  33. OC_Contacts_Addressbook::add(OC_User::getUser(),'default','Default Address Book');
  34. $addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
  35. }
  36. $prefbooks = OC_Preferences::getValue(OC_User::getUser(),'contacts','openaddressbooks',null);
  37. if(is_null($prefbooks)){
  38. $prefbooks = $addressbooks[0]['id'];
  39. OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',$prefbooks);
  40. }
  41. // Load the files we need
  42. OC_App::setActiveNavigationEntry( 'contacts_index' );
  43. // Load a specific user?
  44. $id = isset( $_GET['id'] ) ? $_GET['id'] : null;
  45. // sort addressbooks (use contactsort)
  46. usort($addressbooks,'contacts_namesort');
  47. // Addressbooks to load
  48. $openaddressbooks = explode(';',$prefbooks);
  49. $contacts = array();
  50. foreach( $openaddressbooks as $addressbook ){
  51. $addressbookcontacts = OC_Contacts_VCard::all($addressbook);
  52. foreach( $addressbookcontacts as $contact ){
  53. if(is_null($contact['fullname'])){
  54. continue;
  55. }
  56. $contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
  57. }
  58. }
  59. usort($contacts,'contacts_namesort');
  60. $details = array();
  61. if( !is_null($id) || count($contacts)){
  62. if(is_null($id)) $id = $contacts[0]['id'];
  63. $contact = OC_Contacts_VCard::find($id);
  64. $vcard = OC_Contacts_VCard::parse($contact['carddata']);
  65. $details = OC_Contacts_VCard::structureContact($vcard);
  66. }
  67. $l10n = new OC_L10N('contacts');
  68. $adr_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'ADR');
  69. $phone_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'TEL');
  70. // Process the template
  71. $tmpl = new OC_Template( 'contacts', 'index', 'user' );
  72. $tmpl->assign('adr_types',$adr_types);
  73. $tmpl->assign('phone_types',$phone_types);
  74. $tmpl->assign('addressbooks', $addressbooks);
  75. $tmpl->assign('contacts', $contacts);
  76. $tmpl->assign('details', $details );
  77. $tmpl->assign('id',$id);
  78. $tmpl->printPage();