public function writeItems($items) { $writeItems = array(); foreach ($items as $item) { $itemKey = $item->get('itemKey'); if ($itemKey == "") { $newItemKey = Zotero_Lib_Utils::getKey(); $item->set('itemKey', $newItemKey); $item->set('itemVersion', 0); } $writeItems[] = $item; //add separate note items if this item has any $itemNotes = $item->get('notes'); if ($itemNotes && count($itemNotes) > 0) { foreach ($itemNotes as $note) { $note->set('parentItem', $item->get('itemKey')); $note->set('itemKey', Zotero_Lib_Utils::getKey()); $note->set('itemVersion', 0); $writeItems[] = $note; } } } $config = array('target' => 'items', 'libraryType' => $this->owningLibrary->libraryType, 'libraryID' => $this->owningLibrary->libraryID, 'content' => 'json'); $requestUrl = $this->owningLibrary->apiRequestString($config); $chunks = array_chunk($writeItems, 50); foreach ($chunks as $chunk) { $writeArray = array(); foreach ($chunk as $item) { $writeArray[] = $item->writeApiObject(); } $requestData = json_encode(array('items' => $writeArray)); $writeResponse = $this->owningLibrary->_request($requestUrl, 'POST', $requestData, array('Content-Type' => 'application/json')); if ($writeResponse->isError()) { foreach ($chunk as $item) { $item->writeFailure = array('code' => $writeResponse->getStatus(), 'message' => $writeResponse->getBody()); } } else { Zotero_Lib_Utils::UpdateObjectsFromWriteResponse($chunk, $writeResponse); } } return $writeItems; }
public function writeCollections($collections) { $writeCollections = array(); foreach ($collections as $collection) { $collectionKey = $collection->get('collectionKey'); if ($collectionKey == "") { $newCollectionKey = Zotero_Lib_Utils::getKey(); $collection->set('collectionKey', $newCollectionKey); $collection->set('collectionVersion', 0); } $writeCollections[] = $collection; } $config = array('target' => 'collections', 'libraryType' => $this->owningLibrary->libraryType, 'libraryID' => $this->owningLibrary->libraryID, 'content' => 'json'); $requestUrl = $this->owningLibrary->apiRequestString($config); $chunks = array_chunk($writeCollections, 50); foreach ($chunks as $chunk) { $writeArray = array(); foreach ($chunk as $collection) { $writeArray[] = $collection->writeApiObject(); } $requestData = json_encode(array('collections' => $writeArray)); $writeResponse = $this->owningLibrary->_request($requestUrl, 'POST', $requestData, array('Content-Type' => 'application/json')); if ($writeResponse->isError()) { foreach ($chunk as $collection) { $collection->writeFailure = array('code' => $writeResponse->getStatus(), 'message' => $writeResponse->getBody()); } } else { Zotero_Lib_Utils::UpdateObjectsFromWriteResponse($chunk, $writeResponse); } } return $writeCollections; }