Exemple #1
0
 protected function handleObjectWrite($objectType, $obj = null)
 {
     if (!is_object($obj) && !is_null($obj)) {
         throw new Exception('$obj must be a data object or null');
     }
     $objectTypePlural = Zotero_Utilities::getObjectTypePlural($objectType);
     $objectsClassName = "Zotero_" . ucwords($objectTypePlural);
     $json = !empty($this->body) ? $this->jsonDecode($this->body) : false;
     $objectVersionValidated = $this->checkSingleObjectWriteVersion($objectType, $obj, $json);
     $this->libraryVersion = Zotero_Libraries::getUpdatedVersion($this->objectLibraryID);
     // Update item
     if ($this->method == 'PUT' || $this->method == 'PATCH') {
         if ($this->apiVersion < 2) {
             $this->allowMethods(['PUT']);
         }
         if (!$obj) {
             $className = "Zotero_" . ucwords($objectType);
             $obj = new $className();
             $obj->libraryID = $this->objectLibraryID;
             $obj->key = $this->objectKey;
         }
         if ($objectType == 'item') {
             $changed = Zotero_Items::updateFromJSON($obj, $json, null, $this->queryParams, $this->userID, $objectVersionValidated ? 0 : 2, $this->method == 'PATCH');
         } else {
             $changed = $objectsClassName::updateFromJSON($obj, $json, $this->queryParams, $this->userID, $objectVersionValidated ? 0 : 2, $this->method == 'PATCH');
         }
         // If not updated, return the original library version
         if (!$changed) {
             $this->libraryVersion = Zotero_Libraries::getOriginalVersion($this->objectLibraryID);
         }
         if ($cacheKey = $this->getWriteTokenCacheKey()) {
             Z_Core::$MC->set($cacheKey, true, $this->writeTokenCacheTime);
         }
     } else {
         if ($this->method == 'DELETE') {
             $objectsClassName::delete($this->objectLibraryID, $this->objectKey);
         } else {
             throw new Exception("Unexpected method {$this->method}");
         }
     }
     if ($this->apiVersion >= 2 || $this->method == 'DELETE') {
         $this->e204();
     }
     return $obj;
 }