From 67412f3679e37a58ba3a780c7c7a3c5944dfa209 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Tue, 24 Sep 2013 13:39:45 +0200 Subject: [PATCH] Port controllers to OCP\AppFramework --- appinfo/routes.php | 54 ++++++----- js/app.js | 8 +- js/groups.js | 4 +- lib/contact.php | 24 ++++- lib/controller.php | 9 +- lib/controller/addressbookcontroller.php | 29 +----- lib/controller/contactcontroller.php | 55 ++--------- lib/controller/contactphotocontroller.php | 47 ++++----- lib/controller/exportcontroller.php | 111 ++++++++++++++++++++++ lib/controller/groupcontroller.php | 79 +++++++-------- lib/controller/importcontroller.php | 19 +--- lib/controller/settingscontroller.php | 4 +- lib/dispatcher.php | 24 +++-- lib/hooks.php | 39 ++++---- lib/imageresponse.php | 16 +++- lib/textdownloadresponse.php | 60 ++++++++++++ 16 files changed, 355 insertions(+), 227 deletions(-) create mode 100644 lib/controller/exportcontroller.php create mode 100644 lib/textdownloadresponse.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 3dd9e960..6de2e2b7 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -59,17 +59,6 @@ $this->create('contacts_address_book', 'addressbook/{backend}/{addressbookid}') ) ->requirements(array('backend', 'addressbookid')); -$this->create('contacts_address_book_export', 'addressbook/{backend}/{addressbookid}/export') - ->get() - ->action( - function($params) { - session_write_close(); - $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('AddressBookController', 'exportAddressBook', $params); - } - ) - ->requirements(array('backend', 'addressbookid')); - $this->create('contacts_address_book_update', 'addressbook/{backend}/{addressbookid}') ->post() ->action( @@ -191,6 +180,38 @@ $this->create('contacts_import_status', 'addressbook/{backend}/{addressbookid}/i ) ->requirements(array('backend', 'addressbookid')); +$this->create('contacts_address_book_export', 'addressbook/{backend}/{addressbookid}/export') + ->get() + ->action( + function($params) { + session_write_close(); + $dispatcher = new Dispatcher($params); + $dispatcher->dispatch('ExportController', 'exportAddressBook', $params); + } + ) + ->requirements(array('backend', 'addressbookid')); + +$this->create('contacts_contact_export', 'addressbook/{backend}/{addressbookid}/contact/{contactid}/export') + ->get() + ->action( + function($params) { + session_write_close(); + $dispatcher = new Dispatcher($params); + $dispatcher->dispatch('ExportController', 'exportContact', $params); + } + ) + ->requirements(array('backend', 'addressbook', 'contactid')); + +$this->create('contacts_export_selected', 'exportSelected') + ->get() + ->action( + function($params) { + session_write_close(); + $dispatcher = new Dispatcher($params); + $dispatcher->dispatch('ExportController', 'exportSelected', $params); + } + ); + $this->create('contacts_contact_photo', 'addressbook/{backend}/{addressbookid}/contact/{contactid}/photo') ->get() ->action( @@ -257,17 +278,6 @@ $this->create('contacts_crop_contact_photo', 'addressbook/{backend}/{addressbook ) ->requirements(array('backend', 'addressbook', 'contactid', 'key')); -$this->create('contacts_contact_export', 'addressbook/{backend}/{addressbookid}/contact/{contactid}/export') - ->get() - ->action( - function($params) { - session_write_close(); - $dispatcher = new Dispatcher($params); - $dispatcher->dispatch('ContactController', 'exportContact', $params); - } - ) - ->requirements(array('backend', 'addressbook', 'contactid')); - $this->create('contacts_contact_delete_property', 'addressbook/{backend}/{addressbookid}/contact/{contactid}/property/delete') ->post() ->action( diff --git a/js/app.js b/js/app.js index 9b1f899e..e70e3e3a 100644 --- a/js/app.js +++ b/js/app.js @@ -1058,9 +1058,11 @@ OC.Contacts = OC.Contacts || { } console.log('download'); var contacts = self.contacts.getSelectedContacts(); - var ids = $.map(contacts, function(c) {return c.getId();}); - document.location.href = OC.linkTo('contacts', 'export.php') - + '?selectedids=' + ids.join(','); + // Only get backend, addressbookid and contactid + contacts = $.map(contacts, function(c) {return c.metaData();}); + var url = OC.Router.generate('contacts_export_selected', {contacts:contacts}); + console.log('export url', url); + document.location.href = url; }); this.$contactListHeader.on('click keydown', '.merge', function(event) { diff --git a/js/groups.js b/js/groups.js index d0cd762c..2e166b7a 100644 --- a/js/groups.js +++ b/js/groups.js @@ -83,13 +83,13 @@ OC.Contacts = OC.Contacts || {}; */ GroupList.prototype.selectGroup = function(params) { var self = this; - if(!this.loaded) { + /*if(!this.loaded) { console.log('Not loaded'); setTimeout(function() { self.selectGroup(params); }, 100); return; - } + }*/ var id, $elem; if(typeof params.id !== 'undefined') { id = params.id; diff --git a/lib/contact.php b/lib/contact.php index aeb19407..0971985c 100644 --- a/lib/contact.php +++ b/lib/contact.php @@ -324,7 +324,7 @@ class Contact extends VObject\VCard implements IPIMObject { } elseif(!isset($this->props['carddata'])) { $result = $this->props['backend']->getContact( $this->getParent()->getId(), - $this->id + $this->getId() ); if($result) { if(isset($result['vcard']) @@ -714,7 +714,26 @@ class Contact extends VObject\VCard implements IPIMObject { return \OC_Cache::get($key); } + public function __get($key) { + if(!$this->isRetrieved()) { + $this->retrieve(); + } + + return parent::__get($key); + } + + public function __isset($key) { + if(!$this->isRetrieved()) { + $this->retrieve(); + } + + return parent::__isset($key); + } + public function __set($key, $value) { + if(!$this->isRetrieved()) { + $this->retrieve(); + } parent::__set($key, $value); if($key === 'FN') { $this->props['displayname'] = $value; @@ -723,6 +742,9 @@ class Contact extends VObject\VCard implements IPIMObject { } public function __unset($key) { + if(!$this->isRetrieved()) { + $this->retrieve(); + } parent::__unset($key); if($key === 'PHOTO') { $this->cacheThumbnail(null, true); diff --git a/lib/controller.php b/lib/controller.php index 492df14e..0ae032b3 100644 --- a/lib/controller.php +++ b/lib/controller.php @@ -9,7 +9,7 @@ namespace OCA\Contacts; -use OCP\AppFramework\IApi, +use OCP\AppFramework\IAppContainer, OC\AppFramework\Controller\Controller as BaseController, OCP\IRequest, OCA\Contacts\App; @@ -34,9 +34,10 @@ class Controller extends BaseController { */ protected $app; - public function __construct(IApi $api, IRequest $request, App $app) { - $this->api = $api; - $this->request = $request; + public function __construct(IAppContainer $container, App $app) { + $this->api = $container->query('API'); + $this->request = $container->query('Request'); + $this->server = $container->getServer(); $this->app = $app; } diff --git a/lib/controller/addressbookcontroller.php b/lib/controller/addressbookcontroller.php index a9e08d4e..f6105343 100644 --- a/lib/controller/addressbookcontroller.php +++ b/lib/controller/addressbookcontroller.php @@ -12,8 +12,7 @@ namespace OCA\Contacts\Controller; use OCA\Contacts\App, OCA\Contacts\JSONResponse, OCA\Contacts\Utils\JSONSerializer, - OCA\Contacts\Controller, - OCA\AppFramework\Http\TextDownloadResponse; + OCA\Contacts\Controller; /** * Controller class For Address Books @@ -78,32 +77,6 @@ class AddressBookController extends Controller { return $response; } - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function exportAddressBook() { - \OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG); - $params = $this->request->urlParams; - - $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); - $lastModified = $addressBook->lastModified(); - $response = new JSONResponse(); - - if(!is_null($lastModified)) { - $response->addHeader('Cache-Control', 'private, must-revalidate'); - $response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null); - $response->setETag(md5($lastModified)); - } - - $contacts = ''; - foreach($addressBook->getChildren() as $i => $contact) { - $contacts .= $contact->serialize() . "\r\n"; - } - $name = str_replace(' ', '_', $addressBook->getDisplayName()) . '.vcf'; - return new TextDownloadResponse($contacts, $name, 'text/directory'); - } - /** * @NoAdminRequired * @NoCSRFRequired diff --git a/lib/controller/contactcontroller.php b/lib/controller/contactcontroller.php index bf6f8e4f..691c54e2 100644 --- a/lib/controller/contactcontroller.php +++ b/lib/controller/contactcontroller.php @@ -15,9 +15,6 @@ use OCA\Contacts\App, OCA\Contacts\Utils\JSONSerializer, OCA\Contacts\Utils\Properties, OCA\Contacts\Controller; - //OCA\AppFramework\Core\API, - //OCA\AppFramework\Http\TextDownloadResponse; - /** * Controller class For Contacts @@ -25,17 +22,14 @@ use OCA\Contacts\App, class ContactController extends Controller { /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function getContact() { - $app = new App($this->api->getUserId()); $request = $this->request; $response = new JSONResponse(); - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); $contact = $addressBook->getChild($params['contactid']); if(!$contact) { @@ -51,41 +45,15 @@ class ContactController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption - */ - public function exportContact() { - $app = new App($this->api->getUserId()); - - $params = $this->request->urlParams; - - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); - $contact = $addressBook->getChild($params['contactid']); - - if(!$contact) { - $response = new JSONResponse(); - $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); - return $response; - } - - $name = str_replace(' ', '_', $contact->getDisplayName()) . '.vcf'; - return new TextDownloadResponse($contact->serialize(), $name, 'text/vcard'); - } - - /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function saveContact() { - $app = new App($this->api->getUserId()); $request = $this->request; $params = $this->request->urlParams; $response = new JSONResponse(); - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); $contact = $addressBook->getChild($params['contactid']); if(!$contact) { @@ -109,12 +77,9 @@ class ContactController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function deleteProperty() { - $app = new App($this->api->getUserId()); $request = $this->request; $params = $request->urlParams; @@ -126,8 +91,7 @@ class ContactController extends Controller { $response->debug(__METHOD__ . ', name: ' . print_r($name, true)); $response->debug(__METHOD__ . ', checksum: ' . print_r($checksum, true)); - $app = new App($this->api->getUserId()); - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); $contact = $addressBook->getChild($params['contactid']); if(!$contact) { @@ -168,13 +132,10 @@ class ContactController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function saveProperty() { $params = $this->request->urlParams; - $app = new App($this->api->getUserId()); $request = $this->request; $response = new JSONResponse(); @@ -189,7 +150,7 @@ class ContactController extends Controller { $response->debug(__METHOD__ . ', checksum: ' . print_r($checksum, true)); $response->debug(__METHOD__ . ', parameters: ' . print_r($parameters, true)); - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); $response->debug(__METHOD__ . ', addressBook: ' . print_r($addressBook, true)); $contact = $addressBook->getChild($params['contactid']); diff --git a/lib/controller/contactphotocontroller.php b/lib/controller/contactphotocontroller.php index 87179ce0..966fb378 100644 --- a/lib/controller/contactphotocontroller.php +++ b/lib/controller/contactphotocontroller.php @@ -21,18 +21,16 @@ use OCA\Contacts\App, class ContactPhotoController extends Controller { /** - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function getPhoto($maxSize = 170) { // TODO: Cache resized photo $params = $this->request->urlParams; - $app = new App($this->api->getUserId()); $etag = null; //$maxSize = isset($this->request['maxSize']) ? $this->request['maxSize'] : 170; - $addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']); + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); $contact = $addressBook->getChild($params['contactid']); if(!$contact) { @@ -77,9 +75,8 @@ class ContactPhotoController extends Controller { * Uploads a photo and saves in oC cache * @return JSONResponse with data.tmp set to the key in the cache. * - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function uploadPhoto() { $params = $this->request->urlParams; @@ -123,7 +120,7 @@ class ContactPhotoController extends Controller { $response->debug('Couldn\'t save correct image orientation: '.$tmpkey); } - if(!\OC_Cache::set($tmpkey, $image->data(), 600)) { + if(!$this->server->getCache()->set($tmpkey, $image->data(), 600)) { $response->bailOut(App::$l10n->t('Couldn\'t save temporary image: ').$tmpkey); return $response; } @@ -144,9 +141,8 @@ class ContactPhotoController extends Controller { * Saves the photo from the contact being edited to oC cache * @return JSONResponse with data.tmp set to the key in the cache. * - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function cacheCurrentPhoto() { $params = $this->request->urlParams; @@ -159,7 +155,7 @@ class ContactPhotoController extends Controller { $data = $photoResponse->render(); $tmpkey = 'contact-photo-' . $params['contactid']; - if(!\OC_Cache::set($tmpkey, $data, 600)) { + if(!$this->server->getCache()->set($tmpkey, $data, 600)) { $response->bailOut(App::$l10n->t('Couldn\'t save temporary image: ').$tmpkey); return $response; } @@ -181,9 +177,8 @@ class ContactPhotoController extends Controller { * Saves the photo from ownCloud FS to oC cache * @return JSONResponse with data.tmp set to the key in the cache. * - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function cacheFileSystemPhoto() { $params = $this->request->urlParams; @@ -215,7 +210,7 @@ class ContactPhotoController extends Controller { if(!$image->fixOrientation()) { // No fatal error so we don't bail out. $response->debug('Couldn\'t save correct image orientation: '.$localpath); } - if(!\OC_Cache::set($tmpkey, $image->data(), 600)) { + if(!$this->server->getCache()->set($tmpkey, $image->data(), 600)) { $response->bailOut('Couldn\'t save temporary image: '.$tmpkey); return $response; } @@ -235,9 +230,8 @@ class ContactPhotoController extends Controller { /** * Get a photo from the oC cache for cropping. - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function getTempPhoto() { $params = $this->request->urlParams; @@ -245,7 +239,7 @@ class ContactPhotoController extends Controller { $maxSize = isset($this->request->get['maxSize']) ? $this->request->get['maxSize'] : 400; $image = new \OCP\Image(); - $image->loadFromData(\OC_Cache::get($tmpkey)); + $image->loadFromData($this->server->getCache()->get($tmpkey)); if($image->valid()) { if($image->height() > $maxSize || $image->width() > $maxSize) { $image->resize($maxSize); @@ -261,9 +255,8 @@ class ContactPhotoController extends Controller { /** * Get a photo from the oC and crops it with the suplied geometry. - * @IsAdminExemption - * @IsSubAdminExemption - * @CSRFExemption + * @NoAdminRequired + * @NoCSRFRequired */ public function cropPhoto() { $params = $this->request->urlParams; @@ -285,7 +278,7 @@ class ContactPhotoController extends Controller { return $response; } - $data = \OC_Cache::get($tmpkey); + $data = $this->server->getCache()->get($tmpkey); if(!$data) { $response->bailOut(App::$l10n->t('Image has been removed from cache')); return $response; @@ -325,7 +318,7 @@ class ContactPhotoController extends Controller { if(isset($contact->PHOTO)) { $property = $contact->PHOTO; if(!$property) { - \OC_Cache::remove($tmpkey); + $this->server->getCache()->remove($tmpkey); $response->bailOut(App::$l10n ->t('Error getting PHOTO property.')); } @@ -350,7 +343,7 @@ class ContactPhotoController extends Controller { 'thumbnail' => $thumbnail, )); - \OC_Cache::remove($tmpkey); + $this->server->getCache()->remove($tmpkey); return $response; } diff --git a/lib/controller/exportcontroller.php b/lib/controller/exportcontroller.php new file mode 100644 index 00000000..aea65e7b --- /dev/null +++ b/lib/controller/exportcontroller.php @@ -0,0 +1,111 @@ +request->urlParams; + + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); + $lastModified = $addressBook->lastModified(); + + $contacts = ''; + foreach($addressBook->getChildren() as $i => $contact) { + $contacts .= $contact->serialize() . "\r\n"; + } + $name = str_replace(' ', '_', $addressBook->getDisplayName()) . '.vcf'; + $response = new TextDownloadResponse($contacts, $name, 'text/directory'); + if(!is_null($lastModified)) { + $response->addHeader('Cache-Control', 'private, must-revalidate'); + $response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null); + $response->setETag(md5($lastModified)); + } + + return $response; + } + + /** + * Export a single contact. + * + * @NoAdminRequired + * @NoCSRFRequired + */ + public function exportContact() { + + $params = $this->request->urlParams; + + $addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']); + $contact = $addressBook->getChild($params['contactid']); + + if(!$contact) { + $response = new JSONResponse(); + $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); + return $response; + } + + $name = str_replace(' ', '_', $contact->getDisplayName()) . '.vcf'; + return new TextDownloadResponse($contact->serialize(), $name, 'text/vcard'); + } + + /** + * Export a selected range of contacts potentially from different backends and address books. + * + * @NoAdminRequired + * @NoCSRFRequired + */ + public function exportSelected() { + $contacts = $this->request['contacts']; + + // First sort the contacts by backend and address book. + $targets = array(); + + foreach($contacts as $contact) { + if(!isset($targets[$contact['backend']])) { + $targets[$contact['backend']] = array(); + } + if(!isset($targets[$contact['backend']][$contact['addressbookid']])) { + $targets[$contact['backend']][$contact['addressbookid']] = array(); + } + $targets[$contact['backend']][$contact['addressbookid']][] = $contact['contactid']; + } + + $exports = ''; + foreach($targets as $backend => $addressBooks) { + foreach($addressBooks as $addressBookId => $contacts) { + $addressBook = $this->app->getAddressBook($backend, $addressBookId); + foreach($contacts as $contactId) { + $contact = $addressBook->getChild($contactId); + $exports .= $contact->serialize() . "\r\n"; + } + } + } + + $name = 'Selected_contacts' . '.vcf'; + return new TextDownloadResponse($exports, $name, 'text/vcard'); + } + +} \ No newline at end of file diff --git a/lib/controller/groupcontroller.php b/lib/controller/groupcontroller.php index 4c839783..5d37f697 100644 --- a/lib/controller/groupcontroller.php +++ b/lib/controller/groupcontroller.php @@ -19,23 +19,24 @@ use OCA\Contacts\App, class GroupController extends Controller { /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function getGroups() { - $app = new App($this->api->getUserId()); - $catmgr = new \OC_VCategories('contact', $this->api->getUserId()); - $categories = $catmgr->categories(\OC_VCategories::FORMAT_MAP); - foreach($categories as &$category) { - $ids = $catmgr->idsForCategory($category['name']); - $category['contacts'] = $ids; + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); + $tags = $tagMgr->getTags(); + foreach($tags as &$tag) { + try { + $ids = $tagMgr->getIdsForTag($tag['name']); + $tag['contacts'] = $ids; + } catch(\Exception $e) { + $this->api->log(__METHOD__ . ' ' . $e->getMessage()); + } } - $favorites = $catmgr->getFavorites(); + $favorites = $tagMgr->getFavorites(); $groups = array( - 'categories' => $categories, + 'categories' => $tags, 'favorites' => $favorites, 'shared' => \OCP\Share::getItemsSharedWith('addressbook', \OCA\Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS), 'lastgroup' => \OCP\Config::getUserValue($this->api->getUserId(), 'contacts', 'lastgroup', 'all'), @@ -46,9 +47,7 @@ class GroupController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function addGroup() { $name = $this->request->post['name']; @@ -58,8 +57,8 @@ class GroupController extends Controller { $response->bailOut(App::$l10n->t('No group name given.')); } - $catman = new \OC_VCategories('contact', $this->api->getUserId()); - $id = $catman->add($name); + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); + $id = $tagMgr->add($name); if($id === false) { $response->bailOut(App::$l10n->t('Error adding group.')); @@ -70,9 +69,7 @@ class GroupController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function deleteGroup() { $name = $this->request->post['name']; @@ -83,16 +80,15 @@ class GroupController extends Controller { return $response; } - $catman = new \OC_VCategories('contact', $this->api->getUserId()); + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); try { - $ids = $catman->idsForCategory($name); + $ids = $tagMgr->getIdsForTag($name); } catch(\Exception $e) { $response->setErrorMessage($e->getMessage()); return $response; } if($ids !== false) { - $app = new App($this->api->getUserId()); - $backend = $app->getBackend('local'); + $backend = $this->app->getBackend('local'); foreach($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( @@ -112,7 +108,7 @@ class GroupController extends Controller { } } try { - $catman->delete($name); + $tagMgr->delete($name); } catch(\Exception $e) { $response->setErrorMessage($e->getMessage()); } @@ -120,9 +116,7 @@ class GroupController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function renameGroup() { $from = $this->request->post['from']; @@ -138,15 +132,14 @@ class GroupController extends Controller { return $response; } - $catman = new \OC_VCategories('contact', $this->api->getUserId()); - if(!$catman->rename($from, $to)) { + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); + if(!$tagMgr->rename($from, $to)) { $response->bailOut(App::$l10n->t('Error renaming group.')); return $response; } - $ids = $catman->idsForCategory($to); + $ids = $tagMgr->getIdsForTag($to); if($ids !== false) { - $app = new App($this->api->getUserId()); - $backend = $app->getBackend('local'); + $backend = $this->app->getBackend('local'); foreach($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( @@ -168,9 +161,7 @@ class GroupController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function addToGroup() { $response = new JSONResponse(); @@ -195,9 +186,8 @@ class GroupController extends Controller { return $response; } - $app = new App($this->api->getUserId()); - $backend = $app->getBackend('local'); - $catman = new \OC_VCategories('contact', $this->api->getUserId()); + $backend = $this->app->getBackend('local'); + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); foreach($ids as $contactid) { $contact = $backend->getContact(null, $contactid, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( @@ -212,16 +202,14 @@ class GroupController extends Controller { $backend->updateContact(null, $contactid, $obj, array('noCollection' => true)); } $response->debug('contactid: ' . $contactid . ', categoryid: ' . $categoryid); - $catman->addToCategory($contactid, $categoryid); + $tagMgr->tagAs($contactid, $categoryid); } return $response; } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function removeFromGroup() { $response = new JSONResponse(); @@ -241,9 +229,8 @@ class GroupController extends Controller { return $response; } - $app = new App($this->api->getUserId()); - $backend = $app->getBackend('local'); - $catman = new \OC_VCategories('contact', $this->api->getUserId()); + $backend = $this->app->getBackend('local'); + $tagMgr = $this->server->getTagManager()->loadTagsFor('contact'); foreach($ids as $contactid) { $contact = $backend->getContact(null, $contactid, array('noCollection' => true)); if(!$contact) { @@ -264,7 +251,7 @@ class GroupController extends Controller { $response->debug('Error parsing contact: ' . $contactid); } $response->debug('contactid: ' . $contactid . ', categoryid: ' . $categoryid); - $catman->removeFromCategory($contactid, $categoryid); + $tagMgr->unTag($contactid, $categoryid); } return $response; diff --git a/lib/controller/importcontroller.php b/lib/controller/importcontroller.php index 95248034..7031738d 100644 --- a/lib/controller/importcontroller.php +++ b/lib/controller/importcontroller.php @@ -9,7 +9,7 @@ namespace OCA\Contacts\Controller; -use OCA\Contacts\App; +use OCA\Contacts\App, OCA\Contacts\JSONResponse, OCA\Contacts\Controller, Sabre\VObject; @@ -20,9 +20,7 @@ use OCA\Contacts\App; class ImportController extends Controller { /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function upload() { $request = $this->request; @@ -102,9 +100,7 @@ class ImportController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function prepare() { $request = $this->request; @@ -143,10 +139,7 @@ class ImportController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax - * @API + * @NoAdminRequired */ public function start() { $request = $this->request; @@ -277,9 +270,7 @@ class ImportController extends Controller { } /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function status() { $request = $this->request; diff --git a/lib/controller/settingscontroller.php b/lib/controller/settingscontroller.php index 2e0955b3..9a7612e3 100644 --- a/lib/controller/settingscontroller.php +++ b/lib/controller/settingscontroller.php @@ -21,9 +21,7 @@ use OCA\Contacts\App, class SettingsController extends Controller { /** - * @IsAdminExemption - * @IsSubAdminExemption - * @Ajax + * @NoAdminRequired */ public function set() { $request = $this->request; diff --git a/lib/dispatcher.php b/lib/dispatcher.php index 075c2796..3ceaafa2 100644 --- a/lib/dispatcher.php +++ b/lib/dispatcher.php @@ -17,7 +17,8 @@ use OCP\AppFramework\App as MainApp, OCA\Contacts\Controller\ContactController, OCA\Contacts\Controller\ContactPhotoController, OCA\Contacts\Controller\SettingsController, - OCA\Contacts\Controller\ImportController; + OCA\Contacts\Controller\ImportController, + OCA\Contacts\Controller\ExportController; /** * This class manages our app actions @@ -38,30 +39,33 @@ class Dispatcher extends MainApp { $this->container['urlParams'] = $params; $this->middleware = $this->container->query('MiddlewareDispatcher'); $this->middleware->registerMiddleware(new HttpMiddleware($this->container->query('API'))); - $this->api = $this->container->query('API'); - $this->request = $this->container->query('Request'); - $this->app = new App($this->api->getUserId()); + //$this->api = $this->container->query('API'); + //$this->request = $this->container->query('Request'); + $this->app = new App($this->container->query('API')->getUserId()); $this->registerServices(); } public function registerServices() { $this->container->registerService('AddressBookController', function(IAppContainer $container) { - return new AddressBookController($this->api, $this->request, $this->app); + return new AddressBookController($container, $this->app); }); $this->container->registerService('GroupController', function(IAppContainer $container) { - return new GroupController($this->api, $this->request, $this->app); + return new GroupController($container, $this->app); }); $this->container->registerService('ContactController', function(IAppContainer $container) { - return new ContactController($this->api, $this->request, $this->app); + return new ContactController($container, $this->app); }); $this->container->registerService('ContactPhotoController', function(IAppContainer $container) { - return new ContactPhotoController($this->api, $this->request, $this->app); + return new ContactPhotoController($container, $this->app); }); $this->container->registerService('SettingsController', function(IAppContainer $container) { - return new SettingsController($this->api, $this->request, $this->app); + return new SettingsController($container, $this->app); }); $this->container->registerService('ImportController', function(IAppContainer $container) { - return new ImportController($this->api, $this->request, $this->app); + return new ImportController($container, $this->app); + }); + $this->container->registerService('ExportController', function(IAppContainer $container) { + return new ExportController($container, $this->app); }); } diff --git a/lib/hooks.php b/lib/hooks.php index a3d06fc4..44c39b99 100644 --- a/lib/hooks.php +++ b/lib/hooks.php @@ -71,8 +71,8 @@ class Hooks{ if(count($parameters['contactids'])) { // Remove contacts from groups - $catctrl = new \OC_VCategories('contact'); - $catctrl->purgeObjects($parameters['contactids']); + $tagMgr = \OC::$server->getTagManager()->loadTagsFor('contact'); + $tagMgr->purgeObjects($parameters['contactids']); // Purge property indexes Utils\Properties::purgeIndexes($parameters['contactids']); @@ -90,10 +90,10 @@ class Hooks{ * @param array $parameters Currently only the id of the contact. */ public static function contactDeletion($parameters) { - //\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG); + \OCP\Util::writeLog('contacts', __METHOD__.' id: '.$parameters['id'], \OCP\Util::DEBUG); $ids = is_array($parameters['id']) ? $parameters['id'] : array($parameters['id']); - $catctrl = new \OC_VCategories('contact'); - $catctrl->purgeObjects($ids); + $tagMgr = \OC::$server->getTagManager()->loadTagsFor('contact'); + $tagMgr->purgeObjects($ids); Utils\Properties::purgeIndexes($ids); // Contact sharing not implemented, but keep for future. @@ -101,14 +101,14 @@ class Hooks{ } public static function contactAdded($parameters) { - //\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG); + \OCP\Util::writeLog('contacts', __METHOD__.' id: '.$parameters['id'], \OCP\Util::DEBUG); $contact = $parameters['contact']; if(isset($contact->CATEGORIES)) { \OCP\Util::writeLog('contacts', __METHOD__.' groups: '.print_r($contact->CATEGORIES->getParts(), true), \OCP\Util::DEBUG); - $catctrl = new \OC_VCategories('contact'); + $tagMgr = \OC::$server->getTagManager()->loadTagsFor('contact'); foreach($contact->CATEGORIES->getParts() as $group) { \OCP\Util::writeLog('contacts', __METHOD__.' group: '.$group, \OCP\Util::DEBUG); - $catctrl->addToCategory($parameters['id'], $group); + $tagMgr->tagAs($parameters['id'], $group); } } Utils\Properties::updateIndex($parameters['id'], $contact); @@ -132,31 +132,38 @@ class Hooks{ $offset = 0; $limit = 10; - $categories = new \OC_VCategories('contact'); + $tagMgr = \OC::$server->getTagManager()->loadTagsFor('contact'); + $tags = array(); - $app = new App(); - $backend = $app->getBackend('local'); + foreach($tagMgr->getTags() as $tag) { + $tags[] = $tag['name']; + } + + // reset tags + $tagMgr->delete($tags); + + $backend = $this->app->getBackend('local'); $addressBookInfos = $backend->getAddressBooksForUser(); foreach($addressBookInfos as $addressBookInfo) { $addressBook = new AddressBook($backend, $addressBookInfo); while($contacts = $addressBook->getChildren($limit, $offset, false)) { foreach($contacts as $contact) { - $cards[] = array($contact['id'], $contact['carddata']); + if(isset($contact->CATEGORIES)) { + $tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $contact->getId()); + } } \OCP\Util::writeLog('contacts', - __CLASS__.'::'.__METHOD__ - .', scanning: ' . $limit . ' starting from ' . $offset, + __METHOD__ .', scanning: ' . $limit . ' starting from ' . $offset, \OCP\Util::DEBUG); // only reset on first batch. - $categories->rescan($cards, true, ($offset === 0 ? true : false)); $offset += $limit; } } } /** - * Scan vCards for categories. + * Scan vCards for properties. */ public static function indexProperties() { $offset = 0; diff --git a/lib/imageresponse.php b/lib/imageresponse.php index ed1ad95c..058586c8 100644 --- a/lib/imageresponse.php +++ b/lib/imageresponse.php @@ -1,6 +1,6 @@ setImage($image); + if(!is_null($image)) { + $this->setImage($image); + } } + /** + * @param OCP\Image $image + */ public function setImage(\OCP\Image $image) { if(!$image->valid()) { throw new InvalidArgumentException(__METHOD__. ' The image resource is not valid.'); diff --git a/lib/textdownloadresponse.php b/lib/textdownloadresponse.php new file mode 100644 index 00000000..5107ab88 --- /dev/null +++ b/lib/textdownloadresponse.php @@ -0,0 +1,60 @@ +. + * + */ + + +namespace OCA\Contacts; + +use OC\AppFramework\Http\DownloadResponse; + +/** + * Prompts the user to download the a textfile + */ +class TextDownloadResponse extends DownloadResponse { + + private $content; + private $filename; + private $contentType; + + /** + * Creates a response that prompts the user to download a file which + * contains the passed string + * @param string $content the content that should be written into the file + * @param string $filename the name that the downloaded file should have + * @param string $contentType the mimetype that the downloaded file should have + */ + public function __construct($content, $filename, $contentType){ + parent::__construct($filename, $contentType); + $this->content = $content; + } + + + /** + * Simply sets the headers and returns the file contents + * @return string the file contents + */ + public function render(){ + return $this->content; + } + + +}