/** * Moves file to folder from another storage. * @see moveInAnotherStorage(), moveInSameStorage() to understand logic. * This method is specific and can use nobody. Be careful! * * * @internal * @param Folder $folder Destination folder. * @param int $movedBy User id of user, which move file. * @param bool $generateUniqueName Generates unique name for object if in destination directory. * @return bool * @throws \Bitrix\Main\ArgumentException */ public function moveToAnotherFolder(Folder $folder, $movedBy, $generateUniqueName = false) { $realFolderId = $folder->getRealObject()->getId(); if ($this->getParentId() == $realFolderId) { return true; } $possibleNewName = $this->name; if ($generateUniqueName) { $possibleNewName = $this->generateUniqueName($this->name, $realFolderId); } $needToRename = $possibleNewName != $this->name; if (!$this->isUniqueName($possibleNewName, $realFolderId)) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_OBJECT_MODEL_ERROR_NON_UNIQUE_NAME'), self::ERROR_NON_UNIQUE_NAME))); return false; } $this->name = $possibleNewName; if ($needToRename) { $successUpdate = $this->update(array('NAME' => $possibleNewName)); if (!$successUpdate) { return false; } } /** @var FileTable $tableClassName */ $tableClassName = $this->getTableClassName(); $moveResult = $tableClassName::move($this->id, $realFolderId); if (!$moveResult->isSuccess()) { $this->errorCollection->addFromResult($moveResult); return false; } $this->setAttributesFromResult($moveResult); Driver::getInstance()->getRightsManager()->setAfterMove($this); $subscribersAfterMove = Driver::getInstance()->collectSubscribers($this); Driver::getInstance()->sendChangeStatus($subscribersAfterMove); if ($folder->getRealObject()->getStorageId() != $this->storageId) { $changeStorageIdResult = $tableClassName::changeStorageId($this->id, $folder->getRealObject()->getStorageId()); if (!$changeStorageIdResult->isSuccess()) { $this->errorCollection->addFromResult($changeStorageIdResult); return false; } } $success = $this->update(array('UPDATE_TIME' => new DateTime(), 'UPDATED_BY' => $movedBy)); if (!$success) { return null; } return $this; }
/** * Returns real object of object. * * For example if object is link (@see FolderLink, @see FileLink), then method returns original object. * @return Folder * @throws ObjectException */ public function getRealObject() { $realObject = parent::getRealObject(); if ($realObject === null) { return null; } if (!$realObject instanceof Folder) { throw new ObjectException('Wrong value in realObjectId, which do not specifies subclass of Folder'); } return $realObject; }
/** * @param Folder $folder * @param int $movedBy * @return $this|null * @throws \Bitrix\Main\ArgumentException */ protected function moveInSameStorage(Folder $folder, $movedBy) { $subscribersBeforeMove = Driver::getInstance()->collectSubscribers($this); $realFolderId = $folder->getRealObject()->getId(); /** @var ObjectTable $tableClassName */ $tableClassName = $this->getTableClassName(); $moveResult = $tableClassName::move($this->id, $realFolderId); if (!$moveResult->isSuccess()) { $this->errorCollection->addFromResult($moveResult); return null; } $this->setAttributesFromResult($moveResult); Driver::getInstance()->getRightsManager()->setAfterMove($this); $subscribersAfterMove = Driver::getInstance()->collectSubscribers($this); DeletedLog::addAfterMove($this, array_unique(array_diff($subscribersBeforeMove, $subscribersAfterMove)), $movedBy, $this->errorCollection); //notify new subscribers (in DeletedLog we notify subscribers only missed access) if ($this instanceof Folder) { Driver::getInstance()->cleanCacheTreeBitrixDisk(array_keys($subscribersAfterMove)); } Driver::getInstance()->sendChangeStatus($subscribersAfterMove); $success = $this->update(array('UPDATE_TIME' => new DateTime())); if (!$success) { return null; } return $this; }