From 1d29b943ae29afdc26df94b666f121dcfcf5684a Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Thu, 17 Oct 2013 02:10:34 +0200 Subject: [PATCH] Finally works with core master --- appinfo/app.php | 7 +- appinfo/routes.php | 82 ++++++----- import.php | 165 ---------------------- index.php | 59 -------- js/addressbooks.js | 2 +- js/app.js | 1 + js/contacts.js | 9 +- js/storage.js | 2 +- lib/app.php | 18 +-- lib/controller/addressbookcontroller.php | 72 ++++------ lib/controller/contactcontroller.php | 35 ++--- lib/controller/contactphotocontroller.php | 37 ++--- lib/controller/pagecontroller.php | 75 ++++++++++ lib/dispatcher.php | 4 + lib/imageresponse.php | 5 +- lib/jsonresponse.php | 12 +- 16 files changed, 200 insertions(+), 385 deletions(-) delete mode 100644 import.php delete mode 100644 index.php create mode 100644 lib/controller/pagecontroller.php diff --git a/appinfo/app.php b/appinfo/app.php index 3c54fcb5..bf63bf48 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -3,7 +3,7 @@ namespace OCA\Contacts; use \OC\AppFramework\Core\API; -//require_once __DIR__ . '/../controller/groupcontroller.php'; +//require_once __DIR__ . '/../lib/controller/pagecontroller.php'; \Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard'; \Sabre\VObject\Property::$classMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty'; \Sabre\VObject\Property::$classMap['FN'] = '\OC\VObject\StringProperty'; @@ -20,8 +20,6 @@ use \OC\AppFramework\Core\API; \Sabre\VObject\Property::$classMap['GEO'] = '\OC\VObject\CompoundProperty'; \Sabre\VObject\Property::$classMap['ORG'] = '\OC\VObject\CompoundProperty'; -$api = new API('contacts'); - \OC::$server->getNavigationManager()->add(array( 'id' => 'contacts', 'order' => 10, @@ -30,7 +28,8 @@ $api = new API('contacts'); 'name' => \OCP\Util::getL10N('contacts')->t('Contacts') ) ); -\OC::$server->getNavigationManager()->setActiveEntry('contacts_index'); + +$api = new API('contacts'); $api->connectHook('OC_User', 'post_createUser', '\OCA\Contacts\Hooks', 'userCreated'); $api->connectHook('OC_User', 'post_deleteUser', '\OCA\Contacts\Hooks', 'userDeleted'); diff --git a/appinfo/routes.php b/appinfo/routes.php index d9bb5582..76bad369 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -11,28 +11,26 @@ namespace OCA\Contacts; use OCA\Contacts\Dispatcher; //define the routes -//for the index $this->create('contacts_index', '/') - ->actionInclude('contacts/index.php'); -// ->action( -// function($params){ -// // -// } -// ); + ->get() + ->action( + function($params){ + session_write_close(); + $dispatcher = new Dispatcher($params); + $dispatcher->dispatch('PageController', 'index'); + } + ); $this->create('contacts_jsconfig', 'ajax/config.js') ->actionInclude('contacts/js/config.php'); -/* TODO: Check what it requires to be a RESTful API. I think maybe {user} - shouldn't be in the URI but be authenticated in headers or elsewhere. -*/ $this->create('contacts_address_books_for_user', 'addressbooks/') ->get() ->action( function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'userAddressBooks', $params); + $dispatcher->dispatch('AddressBookController', 'userAddressBooks'); } ); @@ -42,7 +40,7 @@ $this->create('contacts_address_book_add', 'addressbook/{backend}/add') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'addAddressBook', $params); + $dispatcher->dispatch('AddressBookController', 'addAddressBook'); } ) ->requirements(array('backend')); @@ -53,7 +51,7 @@ $this->create('contacts_address_book', 'addressbook/{backend}/{addressBookId}') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'getAddressBook', $params); + $dispatcher->dispatch('AddressBookController', 'getAddressBook'); } ) ->requirements(array('backend', 'addressBookId')); @@ -64,7 +62,7 @@ $this->create('contacts_address_book_update', 'addressbook/{backend}/{addressBoo function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'updateAddressBook', $params); + $dispatcher->dispatch('AddressBookController', 'updateAddressBook'); } ) ->requirements(array('backend', 'addressBookId')); @@ -75,7 +73,7 @@ $this->create('contacts_address_book_delete', 'addressbook/{backend}/{addressBoo function($params) { $dispatcher = new Dispatcher($params); session_write_close(); - $dispatcher->dispatch('AddressBookController', 'deleteAddressBook', $params); + $dispatcher->dispatch('AddressBookController', 'deleteAddressBook'); } ) ->requirements(array('backend', 'addressBookId')); @@ -86,7 +84,7 @@ $this->create('contacts_address_book_activate', 'addressbook/{backend}/{addressB function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'activateAddressBook', $params); + $dispatcher->dispatch('AddressBookController', 'activateAddressBook'); } ) ->requirements(array('backend', 'addressBookId')); @@ -97,7 +95,7 @@ $this->create('contacts_address_book_add_contact', 'addressbook/{backend}/{addre function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'addChild', $params); + $dispatcher->dispatch('AddressBookController', 'addChild'); } ) ->requirements(array('backend', 'addressBookId')); @@ -108,7 +106,7 @@ $this->create('contacts_address_book_delete_contact', 'addressbook/{backend}/{ad function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'deleteChild', $params); + $dispatcher->dispatch('AddressBookController', 'deleteChild'); } ) ->requirements(array('backend', 'addressBookId', 'contactId')); @@ -119,7 +117,7 @@ $this->create('contacts_address_book_delete_contacts', 'addressbook/{backend}/{a function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'deleteChildren', $params); + $dispatcher->dispatch('AddressBookController', 'deleteChildren'); } ) ->requirements(array('backend', 'addressBookId', 'contactId')); @@ -130,7 +128,7 @@ $this->create('contacts_address_book_move_contact', 'addressbook/{backend}/{addr function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'moveChild', $params); + $dispatcher->dispatch('AddressBookController', 'moveChild'); } ) ->requirements(array('backend', 'addressBookId', 'contactId')); @@ -141,7 +139,7 @@ $this->create('contacts_import_upload', 'addressbook/{backend}/{addressBookId}/i function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ImportController', 'upload', $params); + $dispatcher->dispatch('ImportController', 'upload'); } ) ->requirements(array('backend', 'addressBookId')); @@ -152,7 +150,7 @@ $this->create('contacts_import_prepare', 'addressbook/{backend}/{addressBookId}/ function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ImportController', 'prepare', $params); + $dispatcher->dispatch('ImportController', 'prepare'); } ) ->requirements(array('backend', 'addressBookId')); @@ -163,7 +161,7 @@ $this->create('contacts_import_start', 'addressbook/{backend}/{addressBookId}/im function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ImportController', 'start', $params); + $dispatcher->dispatch('ImportController', 'start'); } ) ->requirements(array('backend', 'addressBookId')); @@ -174,7 +172,7 @@ $this->create('contacts_import_status', 'addressbook/{backend}/{addressBookId}/i function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ImportController', 'status', $params); + $dispatcher->dispatch('ImportController', 'status'); } ) ->requirements(array('backend', 'addressBookId')); @@ -185,7 +183,7 @@ $this->create('contacts_address_book_export', 'addressbook/{backend}/{addressBoo function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ExportController', 'exportAddressBook', $params); + $dispatcher->dispatch('ExportController', 'exportAddressBook'); } ) ->requirements(array('backend', 'addressBookId')); @@ -196,7 +194,7 @@ $this->create('contacts_contact_export', 'addressbook/{backend}/{addressBookId}/ function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ExportController', 'exportContact', $params); + $dispatcher->dispatch('ExportController', 'exportContact'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -207,7 +205,7 @@ $this->create('contacts_export_selected', 'exportSelected') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ExportController', 'exportSelected', $params); + $dispatcher->dispatch('ExportController', 'exportSelected'); } ); @@ -217,7 +215,7 @@ $this->create('contacts_contact_photo', 'addressbook/{backend}/{addressBookId}/c function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'getPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'getPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -228,7 +226,7 @@ $this->create('contacts_upload_contact_photo', 'addressbook/{backend}/{addressBo function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'uploadPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'uploadPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -239,7 +237,7 @@ $this->create('contacts_cache_contact_photo', 'addressbook/{backend}/{addressBoo function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'cacheCurrentPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'cacheCurrentPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -250,7 +248,7 @@ $this->create('contacts_cache_fs_photo', 'addressbook/{backend}/{addressBookId}/ function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'cacheFileSystemPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'cacheFileSystemPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -261,7 +259,7 @@ $this->create('contacts_tmp_contact_photo', 'addressbook/{backend}/{addressBookI function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'getTempPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'getTempPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId', 'key')); @@ -272,7 +270,7 @@ $this->create('contacts_crop_contact_photo', 'addressbook/{backend}/{addressBook function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactPhotoController', 'cropPhoto', $params); + $dispatcher->dispatch('ContactPhotoController', 'cropPhoto'); } ) ->requirements(array('backend', 'addressbook', 'contactId', 'key')); @@ -284,7 +282,7 @@ $this->create('contacts_contact_patch', 'addressbook/{backend}/{addressBookId}/c function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactController', 'patch', $params); + $dispatcher->dispatch('ContactController', 'patch'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -295,7 +293,7 @@ $this->create('contacts_contact_get', 'addressbook/{backend}/{addressBookId}/con function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactController', 'getContact', $params); + $dispatcher->dispatch('ContactController', 'getContact'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -307,7 +305,7 @@ $this->create('contacts_contact_save_all', 'addressbook/{backend}/{addressBookId function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactController', 'saveContact', $params); + $dispatcher->dispatch('ContactController', 'saveContact'); } ) ->requirements(array('backend', 'addressbook', 'contactId')); @@ -318,7 +316,7 @@ $this->create('contacts_categories_list', 'groups/') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'getGroups', $params); + $dispatcher->dispatch('GroupController', 'getGroups'); } ); @@ -328,7 +326,7 @@ $this->create('contacts_categories_add', 'groups/add') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'addGroup', $params); + $dispatcher->dispatch('GroupController', 'addGroup'); } ); @@ -338,7 +336,7 @@ $this->create('contacts_categories_delete', 'groups/delete') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'deleteGroup', $params); + $dispatcher->dispatch('GroupController', 'deleteGroup'); } ); @@ -348,7 +346,7 @@ $this->create('contacts_categories_rename', 'groups/rename') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'renameGroup', $params); + $dispatcher->dispatch('GroupController', 'renameGroup'); } ); @@ -358,7 +356,7 @@ $this->create('contacts_categories_addto', 'groups/addto/{categoryId}') function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'addToGroup', $params); + $dispatcher->dispatch('GroupController', 'addToGroup'); } ); @@ -368,7 +366,7 @@ $this->create('contacts_categories_removefrom', 'groups/removefrom/{categoryId}' function($params) { session_write_close(); $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('GroupController', 'removeFromGroup', $params); + $dispatcher->dispatch('GroupController', 'removeFromGroup'); } ) ->requirements(array('categoryId')); diff --git a/import.php b/import.php deleted file mode 100644 index 6190686b..00000000 --- a/import.php +++ /dev/null @@ -1,165 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -namespace OCA\Contacts; - -use Sabre\VObject; - -//check for addressbooks rights or create new one -ob_start(); - -\OCP\JSON::checkLoggedIn(); -\OCP\App::checkAppEnabled('contacts'); -\OCP\JSON::callCheck(); -session_write_close(); - -$nl = "\n"; - -global $progresskey; -$progresskey = 'contacts.import-' . (isset($_GET['progresskey'])?$_GET['progresskey']:''); - -if (isset($_GET['progress']) && $_GET['progress']) { - echo \OC_Cache::get($progresskey); - die; -} - -function writeProgress($pct) { - global $progresskey; - \OC_Cache::set($progresskey, $pct, 300); -} -writeProgress('10'); -$view = null; -$inputfile = strtr($_POST['file'], array('/' => '', "\\" => '')); -if(\OC\Files\Filesystem::isFileBlacklisted($inputfile)) { - \OCP\JSON::error(array('data' => array('message' => 'Upload of blacklisted file: ' . $inputfile))); - exit(); -} -if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { - $view = \OCP\Files::getStorage('contacts'); - $file = $view->file_get_contents('/imports/' . $inputfile); -} else { - $file = \OC\Files\Filesystem::file_get_contents($_POST['path'] . '/' . $inputfile); -} -if(!$file) { - \OCP\JSON::error(array('data' => array('message' => 'Import file was empty.'))); - exit(); -} - -$id = $_POST['id']; - -if(!$id) { - \OCP\JSON::error( - array( - 'data' => array( - 'message' => 'Error getting the ID of the address book.', - 'file'=>\OCP\Util::sanitizeHTML($inputfile) - ) - ) - ); - exit(); -} - -$app = new App(); -$addressBook = $app->getAddressBook('local', $id); - -//analyse the contacts file -writeProgress('40'); -$file = str_replace(array("\r","\n\n"), array("\n","\n"), $file); -$lines = explode($nl, $file); - -$inelement = false; -$parts = array(); -$card = array(); -foreach($lines as $line) { - if(strtoupper(trim($line)) == 'BEGIN:VCARD') { - $inelement = true; - } elseif (strtoupper(trim($line)) == 'END:VCARD') { - $card[] = $line; - $parts[] = implode($nl, $card); - $card = array(); - $inelement = false; - } - if ($inelement === true && trim($line) != '') { - $card[] = $line; - } -} -//import the contacts -writeProgress('70'); -$imported = 0; -$failed = 0; -$partial = 0; -if(!count($parts) > 0) { - \OCP\JSON::error( - array( - 'data' => array( - 'message' => 'No contacts to import in ' - . \OCP\Util::sanitizeHTML($inputfile).'. Please check if the file is corrupted.', - 'file'=>OCP\Util::sanitizeHTML($inputfile) - ) - ) - ); - if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { - if(!$view->unlink('/imports/' . $inputfile)) { - \OCP\Util::writeLog('contacts', - 'Import: Error unlinking OC_FilesystemView ' . '/' . \OCP\Util::sanitizeHTML($inputfile), - \OCP\Util::ERROR); - } - } - exit(); -} -foreach($parts as $part) { - try { - $vcard = VObject\Reader::read($part); - } catch (VObject\ParseException $e) { - try { - $vcard = VObject\Reader::read($part, VObject\Reader::OPTION_IGNORE_INVALID_LINES); - $partial += 1; - \OCP\Util::writeLog('contacts', - 'Import: Retrying reading card. Error parsing VCard: ' . $e->getMessage(), - \OCP\Util::ERROR); - } catch (\Exception $e) { - $failed += 1; - \OCP\Util::writeLog('contacts', - 'Import: skipping card. Error parsing VCard: ' . $e->getMessage(), - \OCP\Util::ERROR); - continue; // Ditch cards that can't be parsed by Sabre. - } - } - try { - if($addressBook->addChild($vcard)) { - $imported += 1; - } else { - $failed += 1; - } - } catch (\Exception $e) { - \OCP\Util::writeLog('contacts', __LINE__ . ' ' . - 'Error importing vcard: ' . $e->getMessage() . $nl . $vcard->serialize(), - \OCP\Util::ERROR); - $failed += 1; - } -} -//done the import -writeProgress('100'); -sleep(3); -\OC_Cache::remove($progresskey); -if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { - if(!$view->unlink('/imports/' . $inputfile)) { - \OCP\Util::writeLog('contacts', - 'Import: Error unlinking OC_FilesystemView ' . '/' . $inputfile, - \OCP\Util::ERROR); - } -} -\OCP\JSON::success( - array( - 'data' => array( - 'imported'=>$imported, - 'failed'=>$failed, - 'file'=>\OCP\Util::sanitizeHTML($inputfile), - ) - ) -); diff --git a/index.php b/index.php deleted file mode 100644 index f8ca9539..00000000 --- a/index.php +++ /dev/null @@ -1,59 +0,0 @@ - - * Copyright (c) 2011 Jakob Sack mail@jakobsack.de - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -namespace OCA\Contacts; - -// Check if we are a user -\OCP\User::checkLoggedIn(); -\OCP\App::checkAppEnabled('contacts'); - -\OCP\App::setActiveNavigationEntry('contacts_index'); - -$impp_types = Utils\Properties::getTypesForProperty('IMPP'); -$adr_types = Utils\Properties::getTypesForProperty('ADR'); -$phone_types = Utils\Properties::getTypesForProperty('TEL'); -$email_types = Utils\Properties::getTypesForProperty('EMAIL'); -$ims = Utils\Properties::getIMOptions(); -$im_protocols = array(); -foreach($ims as $name => $values) { - $im_protocols[$name] = $values['displayname']; -} - -$maxUploadFilesize = \OCP\Util::maxUploadFilesize('/'); - -\OCP\Util::addScript('', 'jquery.multiselect'); -\OCP\Util::addScript('', 'tags'); -\OCP\Util::addScript('contacts', 'jquery.combobox'); -\OCP\Util::addScript('contacts', 'modernizr.custom'); -\OCP\Util::addScript('contacts', 'app'); -\OCP\Util::addScript('contacts', 'addressbooks'); -\OCP\Util::addScript('contacts', 'contacts'); -\OCP\Util::addScript('contacts', 'storage'); -\OCP\Util::addScript('contacts', 'groups'); -\OCP\Util::addScript('contacts', 'jquery.ocaddnew'); -\OCP\Util::addScript('files', 'jquery.fileupload'); -\OCP\Util::addScript('3rdparty/Jcrop', 'jquery.Jcrop'); -\OCP\Util::addStyle('3rdparty/fontawesome', 'font-awesome'); -\OCP\Util::addStyle('contacts', 'font-awesome'); -\OCP\Util::addStyle('', 'jquery.multiselect'); -\OCP\Util::addStyle('contacts', 'jquery.combobox'); -\OCP\Util::addStyle('contacts', 'jquery.ocaddnew'); -\OCP\Util::addStyle('3rdparty/Jcrop', 'jquery.Jcrop'); -\OCP\Util::addStyle('contacts', 'contacts'); - -$tmpl = new \OCP\Template( "contacts", "contacts", "user" ); -$tmpl->assign('uploadMaxFilesize', $maxUploadFilesize); -$tmpl->assign('uploadMaxHumanFilesize', - \OCP\Util::humanFileSize($maxUploadFilesize), false); -$tmpl->assign('phone_types', $phone_types); -$tmpl->assign('email_types', $email_types); -$tmpl->assign('adr_types', $adr_types); -$tmpl->assign('impp_types', $impp_types); -$tmpl->assign('im_protocols', $im_protocols); -$tmpl->printPage(); diff --git a/js/addressbooks.js b/js/addressbooks.js index 1b2d42ac..a6503dcc 100644 --- a/js/addressbooks.js +++ b/js/addressbooks.js @@ -535,7 +535,7 @@ OC.Contacts = OC.Contacts || {}; */ AddressBookList.prototype.add = function(name, cb) { console.log('AddressBookList.add', name, typeof cb); - var defer = $.Deferred; + var defer = $.Deferred(); // Check for wrong, duplicate or empty name if(typeof name !== 'string') { throw new TypeError('BadArgument: AddressBookList.add() only takes String arguments.'); diff --git a/js/app.js b/js/app.js index a9e545e9..6543ffbb 100644 --- a/js/app.js +++ b/js/app.js @@ -586,6 +586,7 @@ OC.Contacts = OC.Contacts || { if(data.deleteOther) { self.contacts.delayedDelete(mergees); } + console.log('merger', merger); self.openContact(merger.getId()); } }); diff --git a/js/contacts.js b/js/contacts.js index dead35c8..408bef8a 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -419,7 +419,7 @@ OC.Contacts = OC.Contacts || {}; this.setAsSaving(this.$fullelem, true); var data = JSON.stringify(this.data); //console.log('stringified', data); - $.when(this.storage.saveAllProperties(this.metadata.backend, this.metadata.parent, this.id, data)) + $.when(this.storage.saveAllProperties(this.metadata.backend, this.metadata.parent, this.id, {data:this.data})) .then(function(response) { if(!response.error) { self.data = response.data.data; @@ -758,8 +758,9 @@ OC.Contacts = OC.Contacts || {}; self.data = response.data.data; self.$groupSelect.multiselect('enable'); // Add contact to current group - if(self.groupprops && self.groupprops.currentgroup.id !== 'all' - && self.groupprops.currentgroup.id !== 'fav') { + if(self.groupprops + && ['all', 'fav', 'uncategorized'].indexOf(self.groupprops.currentgroup.id) === -1 + ) { if(!self.data.CATEGORIES) { self.addToGroup(self.groupprops.currentgroup.name); $(document).trigger('request.contact.addtogroup', { @@ -1860,7 +1861,7 @@ OC.Contacts = OC.Contacts || {}; $(document).bind('status.contact.added', function(e, data) { self.length += 1; self.contacts[String(data.id)] = data.contact; - self.insertContact(data.contact.renderListItem(true)); + //self.insertContact(data.contact.renderListItem(true)); }); $(document).bind('status.contact.moved', function(e, data) { var contact = data.contact; diff --git a/js/storage.js b/js/storage.js index 31711952..af4cbad2 100644 --- a/js/storage.js +++ b/js/storage.js @@ -30,7 +30,7 @@ OC.Contacts = OC.Contacts || {}; } } else { this.error = false; - this.data = response.data || response; + this.data = response; } } }; diff --git a/lib/app.php b/lib/app.php index d5859ef4..c7814dcd 100644 --- a/lib/app.php +++ b/lib/app.php @@ -74,10 +74,10 @@ class App { * @param string $name * @return \Backend\AbstractBackend */ - public function getBackend($name, $user = null) { + public function getBackend($name) { $name = $name ? $name : 'local'; if (isset(self::$backendClasses[$name])) { - return new self::$backendClasses[$name]($user); + return new self::$backendClasses[$name]($this->user); } else { throw new \Exception('No backend for: ' . $name, '404'); } @@ -98,12 +98,16 @@ class App { $backend = self::getBackend($backendName, $this->user); $addressBooks = $backend->getAddressBooksForUser(); if($backendName === 'local' && count($addressBooks) === 0) { - $id = $backend->createAddressBook(array('displayname' => 'Contacts')); + $id = $backend->createAddressBook(array('displayname' => self::$l10n->t('Contacts'))); if($id !== false) { $addressBook = $backend->getAddressBook($id); $addressBooks = array($addressBook); } else { - // TODO: Write log + \OCP\Util::writeLog( + 'contacts', + __METHOD__ . ', Error creating default address book', + \OCP\Util::ERROR + ); } } foreach($addressBooks as $addressBook) { @@ -123,16 +127,15 @@ class App { * @return AddressBook|null */ public function getAddressBook($backendName, $addressbookid) { - \OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG); + //\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG); foreach(self::$addressBooks as $addressBook) { if($addressBook->getBackend()->name === $backendName && $addressBook->getId() === $addressbookid ) { - //\OCP\Util::writeLog('contacts', __METHOD__ . ' returning: '. print_r($addressBook, true), \OCP\Util::DEBUG); return $addressBook; } } - // TODO: Check for return values + $backend = self::getBackend($backendName, $this->user); $info = $backend->getAddressBook($addressbookid); if(!$info) { @@ -154,7 +157,6 @@ class App { */ public function getContact($backendName, $addressbookid, $id) { $addressBook = $this->getAddressBook($backendName, $addressbookid); - // TODO: Check for return value return $addressBook->getChild($id); } diff --git a/lib/controller/addressbookcontroller.php b/lib/controller/addressbookcontroller.php index 09f7c0ab..eab8369d 100644 --- a/lib/controller/addressbookcontroller.php +++ b/lib/controller/addressbookcontroller.php @@ -25,11 +25,11 @@ class AddressBookController extends Controller { */ public function userAddressBooks() { $addressBooks = $this->app->getAddressBooksForUser(); - $response = array(); + $result = array(); $lastModified = 0; foreach($addressBooks as $addressBook) { $data = $addressBook->getMetaData(); - $response[] = $data; + $result[] = $data; if(!is_null($data['lastmodified'])) { $lastModified = max($lastModified, $data['lastmodified']); } @@ -42,7 +42,7 @@ class AddressBookController extends Controller { ); $response = new JSONResponse(array( - 'addressbooks' => $response, + 'addressbooks' => $result, )); if($lastModified > 0) { @@ -57,7 +57,6 @@ class AddressBookController extends Controller { * @NoAdminRequired */ public function getAddressBook() { - \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . print_r($this->request->urlParams, true), \OCP\Util::DEBUG); $params = $this->request->urlParams; $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); @@ -72,8 +71,10 @@ class AddressBookController extends Controller { $response->setETag($etag); } - $response->debug('comparing: "' . $etag . '" to ' . $this->request->getHeader('If-None-Match')); - if(!is_null($etag) && $this->request->getHeader('If-None-Match') === '"'.$etag.'"') { + //$response->debug('comparing: "' . $etag . '" to ' . $this->request->getHeader('If-None-Match')); + if(!is_null($etag) + && $this->request->getHeader('If-None-Match') === '"'.$etag.'"') + { return $response->setStatus(Http::STATUS_NOT_MODIFIED); } else { $contacts = array(); @@ -103,17 +104,13 @@ class AddressBookController extends Controller { try { $id = $backend->createAddressBook($this->request->post); } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } if($id === false) { - $response->bailOut(App::$l10n->t('Error creating address book')); - return $response; + return $response->bailOut(App::$l10n->t('Error creating address book')); } - $response->setStatus('201'); - $response->setParams($backend->getAddressBook($id)); - return $response; + return $response->setStatus('201')->setParams($backend->getAddressBook($id)); } /** @@ -127,15 +124,12 @@ class AddressBookController extends Controller { $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); try { if(!$addressBook->update($this->request['properties'])) { - $response->bailOut(App::$l10n->t('Error updating address book')); - return $response; + return $response->bailOut(App::$l10n->t('Error updating address book')); } } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } - $response->setParams($addressBook->getMetaData()); - return $response; + return $response->setParams($addressBook->getMetaData()); } /** @@ -157,16 +151,14 @@ class AddressBookController extends Controller { $addressBookInfo = $backend->getAddressBook($params['addressBookId']); if(!$addressBookInfo['permissions'] & \OCP\PERMISSION_DELETE) { - $response->bailOut(App::$l10n->t( + return $response->bailOut(App::$l10n->t( 'You do not have permissions to delete the "%s" address book'), array($addressBookInfo['displayname'] )); - return $response; } if(!$backend->deleteAddressBook($params['addressBookId'])) { - $response->bailOut(App::$l10n->t('Error deleting address book')); - return $response; + return $response->bailOut(App::$l10n->t('Error deleting address book')); } \OCP\Config::setUserValue($this->api->getUserId(), 'contacts', 'last_address_book_deleted', time()); return $response; @@ -200,13 +192,11 @@ class AddressBookController extends Controller { try { $id = $addressBook->addChild(); } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } if($id === false) { - $response->bailOut(App::$l10n->t('Error creating contact.')); - return $response; + return $response->bailOut(App::$l10n->t('Error creating contact.')); } $contact = $addressBook->getChild($id); @@ -222,8 +212,7 @@ class AddressBookController extends Controller { ) ) ); - $response->setParams(JSONSerializer::serializeContact($contact)); - return $response; + return $response->setParams(JSONSerializer::serializeContact($contact)); } /** @@ -239,15 +228,13 @@ class AddressBookController extends Controller { try { $result = $addressBook->deleteChild($params['contactId']); } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } if($result === false) { - $response->bailOut(App::$l10n->t('Error deleting contact.')); + return $response->bailOut(App::$l10n->t('Error deleting contact.')); } - $response->setStatus('204'); - return $response; + return $response->setStatus('204'); } /** @@ -264,12 +251,10 @@ class AddressBookController extends Controller { try { $result = $addressBook->deleteChildren($contacts); } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } - $response->setParams(array('result' => $result)); - return $response; + return $response->setParams(array('result' => $result)); } /** @@ -293,20 +278,17 @@ class AddressBookController extends Controller { try { $contactId = $targetAddressBook->addChild($contact); } catch(Exception $e) { - $response->bailOut($e->getMessage()); - return $response; + return $response->bailOut($e->getMessage()); } $contact = $targetAddressBook->getChild($contactId); if(!$contact) { - $response->bailOut(App::$l10n->t('Error saving contact.')); - return $response; + return $response->bailOut(App::$l10n->t('Error saving contact.')); } if(!$fromAddressBook->deleteChild($params['contactId'])) { // Don't bail out because we have to return the contact - $response->debug(App::$l10n->t('Error removing contact from other address book.')); + return $response->debug(App::$l10n->t('Error removing contact from other address book.')); } - $response->setParams(JSONSerializer::serializeContact($contact)); - return $response; + return $response->setParams(JSONSerializer::serializeContact($contact)); } } diff --git a/lib/controller/contactcontroller.php b/lib/controller/contactcontroller.php index ce3f59a9..3aa3097c 100644 --- a/lib/controller/contactcontroller.php +++ b/lib/controller/contactcontroller.php @@ -34,15 +34,13 @@ class ContactController extends Controller { $contact = $addressBook->getChild($params['contactId']); if(!$contact) { - $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); - return $response; + return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); } $data = JSONSerializer::serializeContact($contact); - $response->setParams($data); + return $response->setData($data); - return $response; } /** @@ -52,29 +50,28 @@ class ContactController extends Controller { $request = $this->request; $params = $this->request->urlParams; + $data = isset($request->post['data']) ? $request->post['data'] : null; $response = new JSONResponse(); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); - if(!$contact) { - $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); - return $response; + if(!$data) { + return $response->bailOut(App::$l10n->t('No contact data in request.')); } - if(!$contact->mergeFromArray($request->params)) { - $response->bailOut(App::$l10n->t('Error merging into contact.')); - return $response; + if(!$contact) { + return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); + } + + if(!$contact->mergeFromArray($data)) { + return $response->bailOut(App::$l10n->t('Error merging into contact.')); } if(!$contact->save()) { - $response->bailOut(App::$l10n->t('Error saving contact to backend.')); - return $response; + return $response->bailOut(App::$l10n->t('Error saving contact to backend.')); } - $data = JSONSerializer::serializeContact($contact); - $response->setParams($data); - - return $response; + return $response->setData(JSONSerializer::serializeContact($contact)); } /** @@ -85,19 +82,13 @@ class ContactController extends Controller { $patch = $this->request->patch; $response = new JSONResponse(); - $response->debug(__METHOD__ .', upload_max_filesize: ' . ini_get('upload_max_filesize')); $name = $patch['name']; $value = $patch['value']; $checksum = isset($patch['checksum']) ? $patch['checksum'] : null; $parameters = isset($patch['parameters']) ? $patch['parameters'] : null; - $response->debug(__METHOD__ . ', name: ' . print_r($name, true)); - $response->debug(__METHOD__ . ', value: ' . print_r($value, true)); - $response->debug(__METHOD__ . ', checksum: ' . print_r($checksum, true)); - $response->debug(__METHOD__ . ', parameters: ' . print_r($parameters, true)); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); - //$response->debug(__METHOD__ . ', addressBook: ' . print_r($addressBook, true)); $contact = $addressBook->getChild($params['contactId']); if(!$contact) { diff --git a/lib/controller/contactphotocontroller.php b/lib/controller/contactphotocontroller.php index c9b329e9..19b548cf 100644 --- a/lib/controller/contactphotocontroller.php +++ b/lib/controller/contactphotocontroller.php @@ -192,17 +192,15 @@ class ContactPhotoController extends Controller { $tmpkey = 'contact-photo-' . $params['contactId']; if(!file_exists($localpath)) { - $response->bailOut(App::$l10n->t('File doesn\'t exist:').$localpath); + return $response->bailOut(App::$l10n->t('File doesn\'t exist:').$localpath); } $image = new \OCP\Image(); if(!$image) { - $response->bailOut(App::$l10n->t('Error loading image.')); - return $response; + return $response->bailOut(App::$l10n->t('Error loading image.')); } if(!$image->loadFromFile($localpath)) { - $response->bailOut(App::$l10n->t('Error loading image.')); - return $response; + return $response->bailOut(App::$l10n->t('Error loading image.')); } if($image->width() > 400 || $image->height() > 400) { $image->resize(400); // Prettier resizing than with browser and saves bandwidth. @@ -211,11 +209,10 @@ class ContactPhotoController extends Controller { $response->debug('Couldn\'t save correct image orientation: '.$localpath); } if(!$this->server->getCache()->set($tmpkey, $image->data(), 600)) { - $response->bailOut('Couldn\'t save temporary image: '.$tmpkey); - return $response; + return $response->bailOut('Couldn\'t save temporary image: '.$tmpkey); } - $response->setParams(array( + return $response->setData(array( 'tmp'=>$tmpkey, 'metadata' => array( 'contactId'=> $params['contactId'], @@ -224,8 +221,6 @@ class ContactPhotoController extends Controller { ), )); - return $response; - } /** @@ -248,8 +243,7 @@ class ContactPhotoController extends Controller { return $response; } else { $response = new JSONResponse(); - $response->bailOut('Error getting temporary photo'); - return $response; + return $response->bailOut('Error getting temporary photo'); } } @@ -274,35 +268,30 @@ class ContactPhotoController extends Controller { $response = new JSONResponse(); if(!$contact) { - $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); - return $response; + return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); } $data = $this->server->getCache()->get($tmpkey); if(!$data) { - $response->bailOut(App::$l10n->t('Image has been removed from cache')); - return $response; + return $response->bailOut(App::$l10n->t('Image has been removed from cache')); } $image = new \OCP\Image(); if(!$image->loadFromData($data)) { - $response->bailOut(App::$l10n->t('Error creating temporary image')); - return $response; + return $response->bailOut(App::$l10n->t('Error creating temporary image')); } $w = ($w !== -1 ? $w : $image->width()); $h = ($h !== -1 ? $h : $image->height()); if(!$image->crop($x, $y, $w, $h)) { - $response->bailOut(App::$l10n->t('Error cropping image')); - return $response; + return $response->bailOut(App::$l10n->t('Error cropping image')); } if($image->width() < $maxSize || $image->height() < $maxSize) { if(!$image->resize(200)) { - $response->bailOut(App::$l10n->t('Error resizing image')); - return $response; + return $response->bailOut(App::$l10n->t('Error resizing image')); } } @@ -319,7 +308,7 @@ class ContactPhotoController extends Controller { $property = $contact->PHOTO; if(!$property) { $this->server->getCache()->remove($tmpkey); - $response->bailOut(App::$l10n + return $response->bailOut(App::$l10n ->t('Error getting PHOTO property.')); } $property->setValue(strval($image)); @@ -335,7 +324,7 @@ class ContactPhotoController extends Controller { 'TYPE' => $type)); } if(!$contact->save()) { - $response->bailOut(App::$l10n->t('Error saving contact.')); + return $response->bailOut(App::$l10n->t('Error saving contact.')); } $thumbnail = $contact->cacheThumbnail($image); $response->setParams(array( diff --git a/lib/controller/pagecontroller.php b/lib/controller/pagecontroller.php new file mode 100644 index 00000000..f9d620c5 --- /dev/null +++ b/lib/controller/pagecontroller.php @@ -0,0 +1,75 @@ + + * Copyright (c) 2011 Jakob Sack mail@jakobsack.de + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCA\Contacts\Controller; + +use OCA\Contacts\App, + OCA\Contacts\Controller, + OCA\Contacts\Utils\Properties, + OCP\AppFramework\Http\TemplateResponse; + + +/** + * Controller class for groups/categories + */ +class PageController extends Controller { + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function index() { + \OC::$server->getNavigationManager()->setActiveEntry('contacts'); + + $impp_types = Properties::getTypesForProperty('IMPP'); + $adr_types = Properties::getTypesForProperty('ADR'); + $phone_types = Properties::getTypesForProperty('TEL'); + $email_types = Properties::getTypesForProperty('EMAIL'); + $ims = Properties::getIMOptions(); + $im_protocols = array(); + foreach($ims as $name => $values) { + $im_protocols[$name] = $values['displayname']; + } + + $maxUploadFilesize = \OCP\Util::maxUploadFilesize('/'); + + \OCP\Util::addScript('', 'jquery.multiselect'); + \OCP\Util::addScript('', 'tags'); + \OCP\Util::addScript('contacts', 'jquery.combobox'); + \OCP\Util::addScript('contacts', 'modernizr.custom'); + \OCP\Util::addScript('contacts', 'app'); + \OCP\Util::addScript('contacts', 'addressbooks'); + \OCP\Util::addScript('contacts', 'contacts'); + \OCP\Util::addScript('contacts', 'storage'); + \OCP\Util::addScript('contacts', 'groups'); + \OCP\Util::addScript('contacts', 'jquery.ocaddnew'); + \OCP\Util::addScript('files', 'jquery.fileupload'); + \OCP\Util::addScript('3rdparty/Jcrop', 'jquery.Jcrop'); + \OCP\Util::addStyle('3rdparty/fontawesome', 'font-awesome'); + \OCP\Util::addStyle('contacts', 'font-awesome'); + \OCP\Util::addStyle('', 'jquery.multiselect'); + \OCP\Util::addStyle('contacts', 'jquery.combobox'); + \OCP\Util::addStyle('contacts', 'jquery.ocaddnew'); + \OCP\Util::addStyle('3rdparty/Jcrop', 'jquery.Jcrop'); + \OCP\Util::addStyle('contacts', 'contacts'); + + $response = new TemplateResponse('contacts', 'contacts'); + $response->setParams(array( + 'uploadMaxFilesize' => $maxUploadFilesize, + 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), + 'phone_types' => $phone_types, + 'email_types' => $email_types, + 'adr_types' => $adr_types, + 'impp_types' => $impp_types, + 'im_protocols' => $im_protocols, + )); + + return $response; + } +} diff --git a/lib/dispatcher.php b/lib/dispatcher.php index 9ee65d01..6766e39f 100644 --- a/lib/dispatcher.php +++ b/lib/dispatcher.php @@ -12,6 +12,7 @@ use OCP\AppFramework\App as MainApp, OCP\AppFramework\IAppContainer, OCA\Contacts\App, OCA\Contacts\Middleware\Http as HttpMiddleware, + OCA\Contacts\Controller\PageController, OCA\Contacts\Controller\AddressBookController, OCA\Contacts\Controller\GroupController, OCA\Contacts\Controller\ContactController, @@ -48,6 +49,9 @@ class Dispatcher extends MainApp { } public function registerServices() { + $this->container->registerService('PageController', function(IAppContainer $container) { + return new PageController($container, $this->app); + }); $this->container->registerService('AddressBookController', function(IAppContainer $container) { return new AddressBookController($container, $this->app); }); diff --git a/lib/imageresponse.php b/lib/imageresponse.php index 058586c8..e8c83085 100644 --- a/lib/imageresponse.php +++ b/lib/imageresponse.php @@ -35,10 +35,11 @@ class ImageResponse extends Response { */ public function setImage(\OCP\Image $image) { if(!$image->valid()) { - throw new InvalidArgumentException(__METHOD__. ' The image resource is not valid.'); + throw new \InvalidArgumentException(__METHOD__. ' The image resource is not valid.'); } $this->image = $image; $this->addHeader('Content-Type', $image->mimeType()); + return $this; } /** @@ -47,7 +48,7 @@ class ImageResponse extends Response { */ public function render() { if(is_null($this->image)) { - throw new BadMethodCallException(__METHOD__. ' Image must be set either in constructor or with setImage()'); + throw new \BadMethodCallException(__METHOD__. ' Image must be set either in constructor or with setImage()'); } return $this->image->data(); } diff --git a/lib/jsonresponse.php b/lib/jsonresponse.php index cccc9ef5..5d50b478 100644 --- a/lib/jsonresponse.php +++ b/lib/jsonresponse.php @@ -17,9 +17,9 @@ use OCP\AppFramework\Http\JSONResponse as OriginalResponse, */ class JSONResponse extends OriginalResponse { - public function __construct($params = array(), $statusCode=Http::STATUS_OK) { + public function __construct($params = array(), $statusCode = Http::STATUS_OK) { parent::__construct(array(), $statusCode); - $this->data['data'] = $params; + $this->data = $params; } /** @@ -30,11 +30,9 @@ class JSONResponse extends OriginalResponse { public function setParams(array $params) { $this->setData($params); return $this; - $this->data['data'] = $params; - $this->data['status'] = 'success'; } - public function setData($data){ + public function setData($data) { $this->data = $data; return $this; } @@ -52,7 +50,6 @@ class JSONResponse extends OriginalResponse { $this->error = true; $this->data = $message; return $this; - //$this->data['status'] = 'error'; } function bailOut($msg, $tracelevel = 1, $debuglevel = \OCP\Util::ERROR) { @@ -61,8 +58,7 @@ class JSONResponse extends OriginalResponse { $this->setStatus($msg->getCode()); } $this->setErrorMessage($msg); - $this->debug($msg, $tracelevel, $debuglevel); - return $this; + return $this->debug($msg, $tracelevel, $debuglevel); } function debug($msg, $tracelevel = 0, $debuglevel = \OCP\Util::DEBUG) {