Commit 843112237cc57e0cfad8f6ae7313472658834500

Authored by Jalaloedien Abrahams
1 parent f465d831

KTS-2644

"Add collision detection to document adding"
Fixed.

Committed By: Jalaloedien Abrahams
Reviewed By: Conrad Vermeulen

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7614 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/documentutil.inc.php
... ... @@ -255,9 +255,6 @@ class KTDocumentUtil {
255 255 'creatorid' => $oUser->getID(),
256 256 'documenttypeid' => $iDocumentTypeId,
257 257 ));
258   - if (PEAR::isError($oDocument)) {
259   - return $oDocument;
260   - }
261 258  
262 259 if (is_null($oContents)) {
263 260 $res = KTDocumentUtil::setIncomplete($oDocument, 'contents');
... ... @@ -551,34 +548,15 @@ class KTDocumentUtil {
551 548 // {{{ _in_add
552 549 function &_in_add($oFolder, $sFilename, $oUser, $aOptions) {
553 550 $aOrigOptions = $aOptions;
554   - if (KTDocumentUtil::fileExists($oFolder, $sFilename)) {
555   - $oDoc = Document::getByFilenameAndFolder($sFilename, $oFolder->getId());
556   - if (PEAR::isError($oDoc)) {
557   - return PEAR::raiseError(_kt('Document with that filename already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
558   - } else {
559   - if ($oDoc->getStatusID() != LIVE) {
560   - $sError = _kt('Document with that filename already exists in this folder, but it has been archived or deleted and is still available for restoration. To prevent it being overwritten, you are not allowed to add a document with the same title or filename.');
561   - } else {
562   - $sError = _kt('Document with that filename already exists in this folder.');
563   - }
564   -
565   - $sError .= _kt(' Document') . ': ' . $oDoc->getName() . ' (ID:' . $oDoc->getId() . ')';
566   - return PEAR::raiseError($sError);
567   - }
  551 + while(KTDocumentUtil::fileExists($oFolder, $sFilename)) {
  552 + $oDoc = Document::getByFilenameAndFolder($sFilename, $oFolder->getId());
  553 + $sFilename = KTDocumentUtil::generateNewDocumentFilename($oDoc->getFileName());
568 554 }
569 555 $sName = KTUtil::arrayGet($aOptions, 'description', $sFilename);
570   - if (KTDocumentUtil::nameExists($oFolder, $sName)) {
571   - $oDoc = Document::getByNameAndFolder($sName, $oFolder->getId());
572   - if (PEAR::isError($oDoc)) {
573   - return PEAR::raiseError(_kt('Document with that title already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
574   - } else {
575   - if ($oDoc->getStatusID != LIVE) {
576   - return PEAR::raiseError(_kt('Document with that title already exists in this folder, but it has been archived or deleted and is still available for restoration. To prevent it being overwritten, you are not allowed to add a document with the same title or filename.'));
577   - } else {
578   - return PEAR::raiseError(_kt('Document with that title already exists in this folder.'));
579   - }
580   - }
581   -
  556 + while(KTDocumentUtil::nameExists($oFolder, $sName)) {
  557 + $oDoc = Document::getByNameAndFolder($sName, $oFolder->getId());
  558 + $aOptions['description'] = KTDocumentUtil::generateNewDocumentName($oDoc->getName());
  559 + $sName = KTDocumentUtil::generateNewDocumentName($oDoc->getName());
582 560 }
583 561  
584 562 $oUploadChannel =& KTUploadChannel::getSingleton();
... ... @@ -660,7 +638,46 @@ class KTDocumentUtil {
660 638 return $oDocument;
661 639 }
662 640 // }}}
663   -
  641 +
  642 + function generateNewDocumentFilename($sDocFilename){
  643 + if(preg_match("/\([0-9]+\)(\.[^\.]+){1,}$/", $sDocFilename)){
  644 + preg_match("/\([0-9]+\)\./", $sDocFilename, $matches);
  645 + $new_one = substr($matches[0], 1);
  646 + $new_two = explode(')', $new_one);
  647 + $new = $new_two[0]+1;
  648 +
  649 + $pattern[0] = '/\([0-9]+\)\./';
  650 + $replacement[0] = ' ('.$new.').';
  651 + $sFilename = preg_replace($pattern, $replacement, $sDocFilename);
  652 + }else{
  653 + $matches = explode('.', $sDocFilename);
  654 + $prefix = $matches[0].' (2)';
  655 + for($i = 1; $i < count($matches); $i++ ){
  656 + $suffix .= '.'.$matches[$i];
  657 + }
  658 + $sFilename = $prefix.$suffix;
  659 + }
  660 +
  661 + return $sFilename;
  662 + }
  663 +
  664 + function generateNewDocumentName($sDocName){
  665 + if(preg_match("/\([0-9]+\)$/", $sDocName)){
  666 + preg_match("/\([0-9]+\)$/", $sDocName, $matches);
  667 + $new_one = substr($matches[0], 1);
  668 + $new_two = explode(')', $new_one);
  669 + $new = $new_two[0]+1;
  670 +
  671 + $pattern[0] = '/\([0-9]+\)$/';
  672 + $replacement[0] = '('.$new.')';
  673 + $sName = preg_replace($pattern, $replacement, $sDocName);
  674 + }else{
  675 + $sName = $sDocName.' (2)';
  676 + }
  677 +
  678 + return $sName;
  679 + }
  680 +
664 681 // {{{ fileExists
665 682 function fileExists($oFolder, $sFilename) {
666 683 return Document::fileExists($sFilename, $oFolder->getID());
... ...