public function listNotebooks()
{
/**
* 1. Get all of the user's personal notebooks.
*/
try {
$personalNotebooks = $this->listPersonalNotebooks();
} catch (EDAMUserException $e) {
$personalNotebooks = array();
} catch (EDAMSystemException $e) {
$personalNotebooks = array();
}
$resultNotebooks = array();
$guidsToNotebooks = array();
foreach ($personalNotebooks as $personalNotebook) {
$resultNotebook = new Notebook($personalNotebook);
$resultNotebooks[] = $resultNotebook;
$guidsToNotebooks[$personalNotebook->guid] = $resultNotebook;
}
/**
* Get shared notebooks and flag the matching notebook as shared
*/
try {
$sharedNotebooks = $this->listSharedNotebooks();
} catch (EDAMUserException $e) {
$sharedNotebooks = array();
} catch (EDAMSystemException $e) {
$sharedNotebooks = array();
}
foreach ($sharedNotebooks as $sharedNotebook) {
$guidsToNotebooks[$sharedNotebook->notebookGuid]->isShared = true;
}
/**
* 2. Get all of the user's linked notebooks. These will include business and/or shared notebooks.
*/
try {
$linkedNotebooks = $this->listLinkedNotebooks();
} catch (EDAMUserException $e) {
$linkedNotebooks = array();
} catch (EDAMSystemException $e) {
$linkedNotebooks = array();
}
if (count($linkedNotebooks) > 0) {
/**
* 3. Business user
*/
if (null !== $this->getBusinessNoteStore()) {
/**
* a. Get the business's shared notebooks. Some of these may match to personal linked notebooks.
*
*/
$businessSharedNotebooks = $this->getBusinessSharedNotebooks();
$sharedBusinessNotebookGuids = array();
$sharedBusinessNotebooks = array();
foreach ($businessSharedNotebooks as $businessSharedNotebook) {
$sharedBusinessNotebooks[$businessSharedNotebook->shareKey] = $businessSharedNotebook;
$sharedBusinessNotebookGuids[] = $businessSharedNotebook->notebookGuid;
}
$guidsCount = array_count_values($sharedBusinessNotebookGuids);
$businessNotebooksGuids = array();
/**
* b. Get the business's linked notebooks. Some of these will match to shared notebooks in (a), providing a
* complete authorization story for the notebook.
*/
$businessNotebooks = $this->getBusinessLinkedNotebooks();
foreach ($businessNotebooks as $businessNotebook) {
$businessNotebooksGuids[$businessNotebook->guid] = $businessNotebook;
}
foreach ($linkedNotebooks as $linkedNotebook) {
if (array_key_exists($linkedNotebook->shareKey, $sharedBusinessNotebooks)) {
$sharedNotebook = $sharedBusinessNotebooks[$linkedNotebook->shareKey];
$businessNotebook = $businessNotebooksGuids[$sharedNotebook->notebookGuid];
$result = new Notebook($businessNotebook, $linkedNotebook, $sharedNotebook, $businessNotebook);
if (array_key_exists($sharedNotebook->notebookGuid, $guidsCount) && $guidsCount[$sharedNotebook->notebookGuid] > 1 || $businessNotebook->businessNotebook !== null) {
$result->isShared = true;
}
$resultNotebooks[] = $result;
} else {
if (null === $linkedNotebook->shareKey) {
continue;
}
$resultNotebooks[] = $this->getNoteBookByLinkedNotebook($linkedNotebook);
}
}
} else {
foreach ($linkedNotebooks as $linkedNotebook) {
try {
$resultNotebooks[] = $this->getNoteBookByLinkedNotebook($linkedNotebook);
} catch (\Exception $e) {
$e = ExceptionFactory::create($e);
$this->logger->error('An error occured while fetching a linked notebook as a business user', array('exception' => $e, 'token' => $this->getToken()));
}
}
}
}
return $resultNotebooks;
}