Esempio n. 1
0
 /**
  * Adds new version to file.
  *
  * The method may joins version with last version.
  *
  * @param array $file Structure like $_FILES.
  * @param int $createdBy Id of user.
  * @param bool $disableJoin If set false the method attempts to join version with last version (@see \Bitrix\Disk\File::SECONDS_TO_JOIN_VERSION).
  * @return Version|null
  * @throws \Bitrix\Main\SystemException
  */
 public function addVersion(array $file, $createdBy, $disableJoin = false)
 {
     $this->errorCollection->clear();
     $now = new DateTime();
     $needToJoin = false;
     if (!$disableJoin && $this->updateTime && $this->updatedBy == $createdBy) {
         $updateTimestamp = $this->updateTime->getTimestamp();
         if ($now->getTimestamp() - $updateTimestamp < self::SECONDS_TO_JOIN_VERSION) {
             $needToJoin = true;
         }
     }
     if (!$this->updateContent($file, $createdBy)) {
         return null;
     }
     if ($needToJoin) {
         $lastVersion = $this->getLastVersion();
         if ($lastVersion) {
             if (!$lastVersion->joinData(array_merge(array('CREATE_TIME' => $now), $this->getHistoricalData()))) {
                 $this->errorCollection->add($lastVersion->getErrors());
                 return null;
             }
             if ($this->prevFileId && $this->prevFileId != $this->fileId) {
                 CFile::delete($this->prevFileId);
             }
             return $lastVersion;
         }
     }
     $versionModel = Version::add(array_merge(array('OBJECT_ID' => $this->id, 'FILE_ID' => $this->fileId, 'NAME' => $this->name, 'CREATED_BY' => $createdBy), $this->getHistoricalData()), $this->errorCollection);
     if (!$versionModel) {
         return null;
     }
     $valueVersionUf = FileUserType::NEW_FILE_PREFIX . $versionModel->getId();
     /** @var User $createUser */
     $createUser = User::loadById($createdBy);
     if (!$createUser) {
         //skip
         return $versionModel;
     }
     $text = Loc::getMessage('DISK_FILE_MODEL_UPLOAD_NEW_VERSION_IN_COMMENT_M');
     if ($createUser->getPersonalGender() == 'F') {
         $text = Loc::getMessage('DISK_FILE_MODEL_UPLOAD_NEW_VERSION_IN_COMMENT_F');
     }
     foreach ($this->getAttachedObjects() as $attache) {
         AttachedObject::storeDataByObjectId($this->getId(), array('IS_EDITABLE' => $attache->isEditable(), 'ALLOW_EDIT' => $attache->getAllowEdit()));
         $attache->getConnector()->addComment($createdBy, array('text' => $text, 'versionId' => $valueVersionUf));
         AttachedObject::storeDataByObjectId($this->getId(), null);
     }
     unset($attache);
     return $versionModel;
 }