Example #1
0
 public function testConvert()
 {
     $original = "Abcdefg Âéìøü 这是一个测试。";
     $str = Zotero_Attachments::encodeRelativeDescriptorString($original);
     // assert
     $str = Zotero_Attachments::decodeRelativeDescriptorString($str);
     $this->assertEquals($original, $str);
 }
Example #2
0
	private function setAttachmentField($field, $val) {
		Z_Core::debug("Setting attachment field $field to '$val'");
		switch ($field) {
			case 'mimeType':
				$field = 'mimeType';
				$fieldCap = 'MIMEType';
				break;
			
			case 'linkMode':
			case 'charset':
			case 'storageModTime':
			case 'storageHash':
			case 'path':
			case 'filename':
				$fieldCap = ucwords($field);
				break;
				
			default:
				trigger_error("Invalid attachment field $field", E_USER_ERROR);
		}
		
		if (!$this->isAttachment()) {
			trigger_error("attachment$fieldCap can only be set for attachment items", E_USER_ERROR);
		}
		
		// Validate linkMode
		if ($field == 'linkMode') {
			Zotero_Attachments::linkModeNumberToName($val);
		}
		else if ($field == 'filename') {
			$linkMode = $this->getAttachmentLinkMode();
			$linkMode = Zotero_Attachments::linkModeNumberToName($linkMode);
			if ($linkMode == "LINKED_URL") {
				throw new Exception("Linked URLs cannot have filenames");
			}
			else if ($linkMode == "LINKED_FILE") {
				throw new Exception("Cannot change filename for linked file");
			}
			
			$field = 'path';
			$fieldCap = 'Path';
			$val = 'storage:' . Zotero_Attachments::encodeRelativeDescriptorString($val);
		}
		
		/*if (!is_int($val) && !$val) {
			$val = '';
		}*/
		
		$fieldName = 'attachment' . $fieldCap;
		
		if ($val === $this->$fieldName) {
			return;
		}
		
		// Don't allow changing of existing linkMode
		if ($field == 'linkMode' && $this->$fieldName !== null) {
			throw new Exception("Cannot change existing linkMode for item "
				. $this->libraryID . "/" . $this->key);
		}
		
		$this->changed['attachmentData'][$field] = true;
		$this->attachmentData[$field] = $val;
	}