addcard.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Init owncloud
  23. require_once('../../../lib/base.php');
  24. $aid = $_POST['id'];
  25. $l10n = new OC_L10N('contacts');
  26. // Check if we are a user
  27. OC_JSON::checkLoggedIn();
  28. OC_JSON::checkAppEnabled('contacts');
  29. $addressbook = OC_Contacts_Addressbook::find( $aid );
  30. if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
  31. OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your addressbook.')))); // Same here (as with the contact error). Could this error be improved?
  32. exit();
  33. }
  34. $fn = $_POST['fn'];
  35. $values = $_POST['value'];
  36. $parameters = $_POST['parameters'];
  37. $vcard = new Sabre_VObject_Component('VCARD');
  38. $vcard->add(new Sabre_VObject_Property('FN',$fn));
  39. $vcard->add(new Sabre_VObject_Property('UID',OC_Contacts_VCard::createUID()));
  40. // Data to add ...
  41. $add = array('TEL', 'EMAIL', 'ORG');
  42. $address = false;
  43. for($i = 0; $i < 7; $i++){
  44. if( isset($values['ADR'][$i] ) && $values['ADR'][$i]) $address = true;
  45. }
  46. if( $address ) $add[] = 'ADR';
  47. // Add data
  48. foreach( $add as $propname){
  49. if( !( isset( $values[$propname] ) && $values[$propname] )){
  50. continue;
  51. }
  52. $value = $values[$propname];
  53. if( isset( $parameters[$propname] ) && count( $parameters[$propname] )){
  54. $prop_parameters = $parameters[$propname];
  55. }
  56. else{
  57. $prop_parameters = array();
  58. }
  59. OC_Contacts_VCard::addVCardProperty($vcard, $propname, $value, $prop_parameters);
  60. }
  61. $id = OC_Contacts_VCard::add($aid,$vcard->serialize());
  62. $adr_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'ADR');
  63. $phone_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'TEL');
  64. $details = OC_Contacts_VCard::structureContact($vcard);
  65. $name = $details['FN'][0]['value'];
  66. $tmpl = new OC_Template('contacts','part.details');
  67. $tmpl->assign('details',$details);
  68. $tmpl->assign('id',$id);
  69. $tmpl->assign('adr_types',$adr_types);
  70. $tmpl->assign('phone_types',$phone_types);
  71. $page = $tmpl->fetchPage();
  72. OC_JSON::success(array('data' => array( 'id' => $id, 'name' => $name, 'page' => $page )));