コード例 #1
0
ファイル: Item.inc.php プロジェクト: kskod/dataserver
	/**
	 * @param 	string|int	$field				Field name or ID
	 * @param	mixed		$value				Field value
	 * @param	bool		$loadIn				Populate the data fields without marking as changed
	 */
	public function setField($field, $value, $loadIn=false) {
		if (is_string($value)) {
			$value = trim($value);
		}
		
		if (empty($field)) {
			throw new Exception("Field not specified");
		}
		
		if ($field == 'id' || $field == 'libraryID' || $field == 'key') {
			return $this->setIdentifier($field, $value);
		}
		
		if (($this->_id || $this->_key) && !$this->loaded['primaryData']) {
			$this->loadPrimaryData();
		}
		
		// Primary field
		if (Zotero_Items::isPrimaryField($field)) {
			if ($loadIn) {
				throw new Exception("Cannot set primary field $field in loadIn mode");
			}
			
			switch ($field) {
			case 'itemTypeID':
				break;
				
			case 'dateAdded':
			case 'dateModified':
				if (Zotero_Date::isISO8601($value)) {
					$value = Zotero_Date::iso8601ToSQL($value);
				}
				break;
			
			case 'version':
				$value = (int) $value;
				break;
			
			case 'synced':
				$value = !!$value;
			
			default:
				throw new Exception("Primary field $field cannot be changed");
			}
			
			if ($this->{"_$field"} === $value) {
				Z_Core::debug("Field '$field' has not changed", 4);
				return false;
			}
			
			Z_Core::debug("Field $field has changed from " . $this->{"_$field"} . " to $value", 4);
			
			if ($field == 'itemTypeID') {
				$this->setType($value, $loadIn);
			}
			else {
				$this->{"_$field"} = $value;
				$this->changed['primaryData'][$field] = true;
			}
			return true;
		}
		
		//
		// itemData field
		//
		
		if ($field == 'accessDate' && Zotero_Date::isISO8601($value)) {
			$value = Zotero_Date::iso8601ToSQL($value);
		}
		
		if (!$this->_itemTypeID) {
			trigger_error('Item type must be set before setting field data', E_USER_ERROR);
		}
		
		// If existing item, load field data first unless we're already in
		// the middle of a load
		if ($this->_id) {
			if (!$loadIn && !$this->loaded['itemData']) {
				$this->loadItemData();
			}
		}
		else {
			$this->loaded['itemData'] = true;
		}
		
		$fieldID = Zotero_ItemFields::getID($field);
		
		if (!$fieldID) {
			throw new Exception("'$field' is not a valid itemData field.", Z_ERROR_INVALID_INPUT);
		}
		
		if ($value === "") {
			$value = false;
		}
		
		if ($value !== false && !Zotero_ItemFields::isValidForType($fieldID, $this->_itemTypeID)) {
			throw new Exception("'$field' is not a valid field for type '"
				. Zotero_ItemTypes::getName($this->_itemTypeID) . "'", Z_ERROR_INVALID_INPUT);
		}
		
		if (!$loadIn) {
			// Save date field as multipart date
			if (Zotero_ItemFields::isFieldOfBase($fieldID, 'date') &&
					!Zotero_Date::isMultipart($value)) {
				$value = Zotero_Date::strToMultipart($value);
				if ($value === "") {
					$value = false;
				}
			}
			// Validate access date
			else if ($fieldID == Zotero_ItemFields::getID('accessDate')) {
				if ($value && (!Zotero_Date::isSQLDate($value) &&
						!Zotero_Date::isSQLDateTime($value) &&
						$value != 'CURRENT_TIMESTAMP')) {
					Z_Core::debug("Discarding invalid accessDate '" . $value . "'");
					return false;
				}
			}
			
			// If existing value, make sure it's actually changing
			if ((!isset($this->itemData[$fieldID]) && $value === false) ||
					(isset($this->itemData[$fieldID]) && $this->itemData[$fieldID] === $value)) {
				return false;
			}
			
			//Z_Core::debug("Field $field has changed from {$this->itemData[$fieldID]} to $value", 4);
			
			// TODO: Save a copy of the object before modifying?
		}
		
		$this->itemData[$fieldID] = $value;
		
		if (!$loadIn) {
			if (!isset($changed['itemData'])) {
				$changed['itemData'] = [];
			}
			$this->changed['itemData'][$fieldID] = true;
		}
		return true;
	}
コード例 #2
0
ファイル: Item.inc.php プロジェクト: robinpaulson/dataserver
 /**
  * @param 	string|int	$field				Field name or ID
  * @param	mixed		$value				Field value
  * @param	bool		$loadIn				Populate the data fields without marking as changed
  */
 public function setField($field, $value, $loadIn = false)
 {
     if (empty($field)) {
         trigger_error("Field not specified", E_USER_ERROR);
     }
     // Set id, libraryID, and key without loading data first
     switch ($field) {
         case 'id':
         case 'libraryID':
         case 'key':
             // Allow libraryID to be set if empty, even if primary data is loaded,
             // which happens if an item type is set in the constructor
             if ($field == 'libraryID' && !$this->{$field}) {
             } else {
                 if ($this->loaded['primaryData']) {
                     throw new Exception("Cannot set {$field} after item is already loaded");
                 }
             }
             //this._checkValue(field, val);
             $this->{$field} = $value;
             return;
     }
     if ($this->id || $this->key) {
         if (!$this->loaded['primaryData']) {
             $this->loadPrimaryData(true);
         }
     } else {
         $this->loaded['primaryData'] = true;
     }
     // Primary field
     if (in_array($field, Zotero_Items::$primaryFields)) {
         if ($loadIn) {
             throw new Exception("Cannot set primary field {$field} in loadIn mode");
         }
         switch ($field) {
             case 'itemID':
             case 'serverDateModified':
             case 'itemVersion':
             case 'numNotes':
             case 'numAttachments':
                 trigger_error("Primary field '{$field}' cannot be changed through setField()", E_USER_ERROR);
         }
         if (!Zotero_ItemFields::validate($field, $value)) {
             trigger_error("Value '{$value}' of type " . gettype($value) . " does not validate for field '{$field}'", E_USER_ERROR);
         }
         if ($this->{$field} != $value) {
             Z_Core::debug("Field {$field} has changed from {$this->{$field}} to {$value}", 4);
             if ($field == 'itemTypeID') {
                 $this->setType($value, $loadIn);
             } else {
                 $this->{$field} = $value;
                 $this->changed['primaryData'][$field] = true;
             }
         }
         return true;
     }
     //
     // itemData field
     //
     if (!$this->itemTypeID) {
         trigger_error('Item type must be set before setting field data', E_USER_ERROR);
     }
     // If existing item, load field data first unless we're already in
     // the middle of a load
     if ($this->id) {
         if (!$loadIn && !$this->loaded['itemData']) {
             $this->loadItemData();
         }
     } else {
         $this->loaded['itemData'] = true;
     }
     $fieldID = Zotero_ItemFields::getID($field);
     if (!$fieldID) {
         throw new Exception("'{$field}' is not a valid itemData field.", Z_ERROR_INVALID_INPUT);
     }
     if ($value !== false && !Zotero_ItemFields::isValidForType($fieldID, $this->itemTypeID)) {
         throw new Exception("'{$field}' is not a valid field for type '" . Zotero_ItemTypes::getName($this->itemTypeID) . "'", Z_ERROR_INVALID_INPUT);
     }
     if (!$loadIn) {
         // Save date field as multipart date
         if (Zotero_ItemFields::isFieldOfBase($fieldID, 'date') && !Zotero_Date::isMultipart($value)) {
             $value = Zotero_Date::strToMultipart($value);
         } else {
             if ($fieldID == Zotero_ItemFields::getID('accessDate')) {
                 if ($value && (!Zotero_Date::isSQLDate($value) && !Zotero_Date::isSQLDateTime($value) && $value != 'CURRENT_TIMESTAMP')) {
                     Z_Core::debug("Discarding invalid accessDate '" . $value . "'");
                     return false;
                 }
             }
         }
         // If existing value, make sure it's actually changing
         if (!isset($this->itemData[$fieldID]) && $value === false || isset($this->itemData[$fieldID]) && $this->itemData[$fieldID] === $value) {
             return false;
         }
         // TODO: Save a copy of the object before modifying?
     }
     $this->itemData[$fieldID] = $value;
     if (!$loadIn) {
         $this->changed['itemData'][$fieldID] = true;
     }
     return true;
 }