createaddressbook.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * Copyright (c) 2011-2012 Thomas Tanghus <thomas@tanghus.net>
  4. * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. // Check if we are a user
  10. OCP\JSON::checkLoggedIn();
  11. OCP\JSON::checkAppEnabled('contacts');
  12. OCP\JSON::callCheck();
  13. $userid = OCP\USER::getUser();
  14. $name = trim(strip_tags($_POST['name']));
  15. if(!$name) {
  16. OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Cannot add addressbook with an empty name.'))));
  17. OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Cannot add addressbook with an empty name: '.strip_tags($_POST['name']), OCP\Util::ERROR);
  18. exit();
  19. }
  20. $bookid = OC_Contacts_Addressbook::add($userid, $name, null);
  21. if(!$bookid) {
  22. OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error adding addressbook.'))));
  23. OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Error adding addressbook: '.$_POST['name'], OCP\Util::ERROR);
  24. exit();
  25. }
  26. if(!OC_Contacts_Addressbook::setActive($bookid, 1)) {
  27. OCP\JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error activating addressbook.'))));
  28. OCP\Util::writeLog('contacts','ajax/createaddressbook.php: Error activating addressbook: '.$bookid, OCP\Util::ERROR);
  29. //exit();
  30. }
  31. $addressbook = OC_Contacts_App::getAddressbook($bookid);
  32. $tmpl = new OCP\Template('contacts', 'part.chooseaddressbook.rowfields');
  33. $tmpl->assign('addressbook', $addressbook);
  34. OCP\JSON::success(array(
  35. 'page' => $tmpl->fetchPage(),
  36. 'addressbook' => $addressbook,
  37. ));