Commit bd4205ea84a111b7efd2c05ca9beb34aeb62e409

Authored by Neil Blakey-Milner
1 parent 1459dba1

Flatten the conditionals so that the logic and code is easier to

understand.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3620 c91229c3-7414-0410-bfa2-8a42b809f60b
presentation/lookAndFeel/knowledgeTree/documentmanagement/moveDocumentBL.php
@@ -30,9 +30,7 @@ require_once("../../../../config/dmsDefaults.php"); @@ -30,9 +30,7 @@ require_once("../../../../config/dmsDefaults.php");
30 KTUtil::extractGPC('fConfirmed', 'fDocumentIDs', 'fFolderID', 'fForMove', 'fRememberDocumentID'); 30 KTUtil::extractGPC('fConfirmed', 'fDocumentIDs', 'fFolderID', 'fForMove', 'fRememberDocumentID');
31 31
32 require_once("$default->fileSystemRoot/lib/security/Permission.inc"); 32 require_once("$default->fileSystemRoot/lib/security/Permission.inc");
33 -  
34 require_once("$default->fileSystemRoot/lib/users/User.inc"); 33 require_once("$default->fileSystemRoot/lib/users/User.inc");
35 -  
36 require_once("$default->fileSystemRoot/lib/documentmanagement/DocumentTransaction.inc"); 34 require_once("$default->fileSystemRoot/lib/documentmanagement/DocumentTransaction.inc");
37 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc"); 35 require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
38 require_once("$default->fileSystemRoot/lib/documentmanagement/PhysicalDocumentManager.inc"); 36 require_once("$default->fileSystemRoot/lib/documentmanagement/PhysicalDocumentManager.inc");
@@ -49,165 +47,162 @@ require_once("$default->fileSystemRoot/presentation/Html.inc"); @@ -49,165 +47,162 @@ require_once("$default->fileSystemRoot/presentation/Html.inc");
49 47
50 $aUnmovedDocs = array(); 48 $aUnmovedDocs = array();
51 49
52 -if (checkSession()) { 50 +if (!checkSession()) {
  51 + die();
  52 +}
  53 +
  54 +if (isset($fRememberDocumentID)) {
  55 + $fDocumentIDs = $_SESSION['documents'][$fRememberDocumentID];
  56 +} else {
  57 + $sUniqueID = KTUtil::randomString();
  58 + $_SESSION["documents"][$sUniqueID] = $fDocumentIDs;
  59 + $fRememberDocumentID = $sUniqueID;
  60 +}
  61 +
  62 +
  63 +/* if (!isset($fDocumentIDs) || !isset($fFolderID)) {
  64 + require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
  65 + $oPatternCustom = & new PatternCustom();
  66 + $oPatternCustom->setHtml("");
  67 + $main->setCentralPayload($oPatternCustom);
  68 + $main->setErrorMessage(_("No document/folder selected"));
  69 + $main->render();
  70 + exit(0);
  71 +} */
  72 +
  73 +if (!isset($fForMove)) {
  74 + require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
  75 + $oPatternCustom = & new PatternCustom();
  76 + $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID));
  77 + $main->setCentralPayload($oPatternCustom);
  78 + $main->setFormAction($_SERVER["PHP_SELF"] . "?fForMove=1&fRememberDocumentID=$fRememberDocumentID&fFolderID=$fFolderID");
  79 + $main->render();
  80 + exit(0);
  81 +}
  82 +
  83 +if (!$fConfirmed) {
  84 + require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
  85 + $oPatternCustom = & new PatternCustom();
  86 +
  87 + // Check for all docs
  88 + for ($i = 0; $i < count($fDocumentIDs); $i++) {
  89 + $oDocument = Document::get($fDocumentIDs[$i]);
  90 +
  91 + // check if the selected folder has the same document type as the document we're moving
  92 + if (!Folder::folderIsLinkedToDocType($fFolderID, $oDocument->getDocumentTypeID())) {
  93 + // the right document type isn't mapped
  94 + $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID, _("You can't move the document to this folder because it cannot store the document type of your document.") . " " . _("Please choose another directory")));
  95 + break;
  96 + }
  97 +
  98 + // check that there is no filename collision in the destination directory
  99 + $sNewDocumentFileSystemPath = Folder::getFolderPath($fFolderID) . $oDocument->getFileName();
  100 +
  101 + if (file_exists($sNewDocumentFileSystemPath)) {
  102 + // filename collision
  103 + $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID, _("This folder already contains a document of the same name.") . " " . _("Please choose another directory")));
  104 + break;
  105 + }
  106 + // display confirmation page
  107 + $oPatternCustom->setHtml(getConfirmationPage($fFolderID, $fRememberDocumentID));
  108 + }
  109 + $main->setFormAction($_SERVER["PHP_SELF"] . "?fForMove=1&fRememberDocumentID=$fRememberDocumentID&fFolderID=$fFolderID");
  110 + $main->setCentralPayload($oPatternCustom);
  111 + $main->render();
  112 + exit(0);
  113 +}
  114 +
  115 +for ($i = 0; $i < count($fDocumentIDs); $i++) {
  116 +
  117 + //we're trying to move a document
  118 + $oDocument = & Document::get($fDocumentIDs[$i]);
  119 + $oFolder = & Folder::get($fFolderID);
  120 + $iOldFolderID = $oDocument->getFolderID();
  121 +
  122 + // check that there is no filename collision in the destination directory
  123 + $sNewDocumentFileSystemPath = Folder::getFolderPath($fFolderID) . $oDocument->getFileName();
  124 +
  125 + if (file_exists($sNewDocumentFileSystemPath)) {
  126 + // Store the doc with problem
  127 + array_push($aUnmovedDocs, array($oDocument, _("This folder already contains a document of the same name. Please choose another directory")));
  128 + continue;
  129 + }
  130 +
  131 + if (!Permission::userHasDocumentWritePermission($oDocument) || !Permission::userHasFolderWritePermission($oFolder)) {
  132 + array_push($aUnmovedDocs, array($oDocument, _("You do not have rights to move this document")));
  133 + continue;
  134 + }
  135 +
  136 + //get the old document path
  137 + $sOldDocumentFileSystemPath = Folder::getFolderPath($iOldFolderID) . $oDocument->getFileName();
  138 +
  139 + //put the document in the new folder
  140 + $oDocument->setFolderID($fFolderID);
  141 + if (!$oDocument->update(true)) {
  142 + //had a problem with the database
  143 + array_push($aUnmovedDocs, array($oDocument, _("Could not update document in database")));
  144 + continue;
  145 + }
  146 +
  147 + //get the old document path
  148 + $sOldDocumentFileSystemPath = Folder::getFolderPath($iOldFolderID) . $oDocument->getFileName();
  149 + //move the document on the file system
  150 + if (!PhysicalDocumentManager::moveDocument($sOldDocumentFileSystemPath, $oDocument, $oFolder)) {
  151 + $oDocument->setFolderID($iOldFolderID);
  152 + $oDocument->update();
53 153
54 - if (isset($fRememberDocumentID)) {  
55 - $fDocumentIDs = $_SESSION['documents'][$fRememberDocumentID];  
56 - } else {  
57 - $sUniqueID = KTUtil::randomString();  
58 - $_SESSION["documents"][$sUniqueID] = $fDocumentIDs;  
59 - $fRememberDocumentID = $sUniqueID; 154 + // Store the doc with problem
  155 + array_push($aUnmovedDocs, array($oDocument, _("Could not move document on file system")));
  156 + continue;
60 } 157 }
61 158
62 -  
63 - if (isset($fDocumentIDs) && isset($fFolderID)) {  
64 - if (isset($fForMove)) {  
65 - if ($fConfirmed) {  
66 - for ($i = 0; $i < count($fDocumentIDs); $i++) {  
67 -  
68 - //we're trying to move a document  
69 - $oDocument = & Document::get($fDocumentIDs[$i]);  
70 - $oFolder = & Folder::get($fFolderID);  
71 - $iOldFolderID = $oDocument->getFolderID();  
72 -  
73 - // check that there is no filename collision in the destination directory  
74 - $sNewDocumentFileSystemPath = Folder::getFolderPath($fFolderID) . $oDocument->getFileName();  
75 - if (!file_exists($sNewDocumentFileSystemPath)) {  
76 -  
77 - if (Permission::userHasDocumentWritePermission($oDocument) && Permission::userHasFolderWritePermission($oFolder)) {  
78 - //if the user has both document and folder write permissions  
79 - //get the old document path  
80 - $sOldDocumentFileSystemPath = Folder::getFolderPath($iOldFolderID) . $oDocument->getFileName();  
81 - //put the document in the new folder  
82 - $oDocument->setFolderID($fFolderID);  
83 - if ($oDocument->update(true)) {  
84 - //get the old document path  
85 - $sOldDocumentFileSystemPath = Folder::getFolderPath($iOldFolderID) . $oDocument->getFileName();  
86 - //move the document on the file system  
87 - if (PhysicalDocumentManager::moveDocument($sOldDocumentFileSystemPath, $oDocument, $oFolder)) {  
88 - // fire subscription alerts for the moved document (and the folder its in)  
89 - $count = SubscriptionEngine::fireSubscription($fDocumentIDs[$i], SubscriptionConstants::subscriptionAlertType("MovedDocument"),  
90 - SubscriptionConstants::subscriptionType("DocumentSubscription"),  
91 - array( "folderID" => $iOldFolderID,  
92 - "modifiedDocumentName" => $oDocument->getName(),  
93 - "oldFolderName" => Folder::getFolderName($iOldFolderID),  
94 - "newFolderName" => Folder::getFolderName($fFolderID) ));  
95 - $default->log->info("moveDocumentBL.php fired $count subscription alerts for moved document " . $oDocument->getName());  
96 -  
97 - // fire folder subscriptions for the destination folder  
98 - $count = SubscriptionEngine::fireSubscription($fFolderID, SubscriptionConstants::subscriptionAlertType("MovedDocument"),  
99 - SubscriptionConstants::subscriptionType("FolderSubscription"),  
100 - array( "folderID" => $iOldFolderID,  
101 - "modifiedDocumentName" => $oDocument->getName(),  
102 - "oldFolderName" => Folder::getFolderName($iOldFolderID),  
103 - "newFolderName" => Folder::getFolderName($fFolderID) ));  
104 - $default->log->info("moveDocumentBL.php fired $count (folderID=$fFolderID) folder subscription alerts for moved document " . $oDocument->getName());  
105 - } else {  
106 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
107 - //we couldn't move the document on the file system  
108 - //so reset the database values  
109 - $oDocument->setFolderID($iOldFolderID);  
110 - $oDocument->update();  
111 -  
112 - // Store the doc with problem  
113 - array_push($aUnmovedDocs, array($oDocument, _("Could not move document on file system")));  
114 - }  
115 - } else {  
116 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
117 - //had a problem with the database  
118 - // Store the doc with problem  
119 - array_push($aUnmovedDocs, array($oDocument, _("Could not update document in database")));  
120 - }  
121 - } else {  
122 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
123 -  
124 - // Permission problem  
125 - // Store the doc with problem  
126 - array_push($aUnmovedDocs, array($oDocument, _("You do not have rights to move this document")));  
127 - }  
128 -  
129 - } else {  
130 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
131 -  
132 - // Store the doc with problem  
133 - array_push($aUnmovedDocs, array($oDocument, _("This folder already contains a document of the same name. Please choose another directory")));  
134 - }  
135 -  
136 - }  
137 -  
138 - // Move terminated  
139 -  
140 - // List undeleted documents  
141 - if (!empty($aUnmovedDocs) ) {  
142 -  
143 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
144 - $oPatternCustom = & new PatternCustom();  
145 -  
146 - $sError = _("An error occured moving the following document(s):") . " <br><br>";  
147 - foreach ($aUnmovedDocs as $oDoc) {  
148 - $sError .= $oDoc[0]->getDisplayPath() . ":&nbsp;&nbsp;&nbsp;" .$oDoc[1] . "<br>";  
149 - }  
150 - $sError .= "<br>" . _("The other documents are been moved.");  
151 -  
152 - $oPatternCustom = & new PatternCustom();  
153 - $oPatternCustom->setHtml(renderErrorPage($sError));  
154 - $main->setCentralPayload($oPatternCustom);  
155 - $main->render();  
156 -  
157 - reset($aUnmovedDocs);  
158 -  
159 - } else {  
160 - // redirect to the browse folder page  
161 - redirect("$default->rootUrl/control.php?action=browse&fFolderID=" . $oDocument->getFolderID());  
162 - }  
163 -  
164 - } else { // ($fConfirmed)  
165 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
166 - $oPatternCustom = & new PatternCustom();  
167 -  
168 - // Check for all docs  
169 - for ($i = 0; $i < count($fDocumentIDs); $i++) {  
170 -  
171 - $oDocument = Document::get($fDocumentIDs[$i]);  
172 -  
173 - // check if the selected folder has the same document type as the document we're moving  
174 - if (Folder::folderIsLinkedToDocType($fFolderID, $oDocument->getDocumentTypeID())) {  
175 - // check that there is no filename collision in the destination directory  
176 - $sNewDocumentFileSystemPath = Folder::getFolderPath($fFolderID) . $oDocument->getFileName();  
177 - if (!file_exists($sNewDocumentFileSystemPath)) {  
178 - // display confirmation page  
179 - $oPatternCustom->setHtml(getConfirmationPage($fFolderID, $fRememberDocumentID));  
180 - } else {  
181 - // filename collision  
182 - $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID, _("This folder already contains a document of the same name.") . " " . _("Please choose another directory")));  
183 - break;  
184 - }  
185 - } else {  
186 - // the right document type isn't mapped  
187 - $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID, _("You can't move the document to this folder because it cannot store the document type of your document.") . " " . _("Please choose another directory")));  
188 - break;  
189 - }  
190 - }  
191 - $main->setFormAction($_SERVER["PHP_SELF"] . "?fForMove=1&fRememberDocumentID=$fRememberDocumentID&fFolderID=$fFolderID");  
192 - $main->setCentralPayload($oPatternCustom);  
193 - $main->render();  
194 - }  
195 - } else { // (isset($fForMove))  
196 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
197 - $oPatternCustom = & new PatternCustom();  
198 - $oPatternCustom->setHtml(getPage($fFolderID, $fRememberDocumentID));  
199 - $main->setCentralPayload($oPatternCustom);  
200 - $main->setFormAction($_SERVER["PHP_SELF"] . "?fForMove=1&fRememberDocumentID=$fRememberDocumentID&fFolderID=$fFolderID");  
201 - $main->render();  
202 - }  
203 - } else { // (isset($fDocumentIDs) && isset($fFolderID))  
204 - require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");  
205 - $oPatternCustom = & new PatternCustom();  
206 - $oPatternCustom->setHtml("");  
207 - $main->setCentralPayload($oPatternCustom);  
208 - $main->setErrorMessage(_("No document/folder selected"));  
209 - $main->render();  
210 - } 159 + // fire subscription alerts for the moved document (and the folder its in)
  160 + $count = SubscriptionEngine::fireSubscription($fDocumentIDs[$i], SubscriptionConstants::subscriptionAlertType("MovedDocument"),
  161 + SubscriptionConstants::subscriptionType("DocumentSubscription"),
  162 + array(
  163 + "folderID" => $iOldFolderID,
  164 + "modifiedDocumentName" => $oDocument->getName(),
  165 + "oldFolderName" => Folder::getFolderName($iOldFolderID),
  166 + "newFolderName" => Folder::getFolderName($fFolderID),
  167 + )
  168 + );
  169 + $default->log->info("moveDocumentBL.php fired $count subscription alerts for moved document " . $oDocument->getName());
  170 +
  171 + // fire folder subscriptions for the destination folder
  172 + $count = SubscriptionEngine::fireSubscription($fFolderID, SubscriptionConstants::subscriptionAlertType("MovedDocument"),
  173 + SubscriptionConstants::subscriptionType("FolderSubscription"),
  174 + array(
  175 + "folderID" => $iOldFolderID,
  176 + "modifiedDocumentName" => $oDocument->getName(),
  177 + "oldFolderName" => Folder::getFolderName($iOldFolderID),
  178 + "newFolderName" => Folder::getFolderName($fFolderID),
  179 + )
  180 + );
  181 + $default->log->info("moveDocumentBL.php fired $count (folderID=$fFolderID) folder subscription alerts for moved document " . $oDocument->getName());
211 } 182 }
212 183
  184 +// Move terminated
  185 +
  186 +// List undeleted documents
  187 +if (!empty($aUnmovedDocs) ) {
  188 + require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
  189 + $oPatternCustom = & new PatternCustom();
  190 +
  191 + $sError = _("An error occured moving the following document(s):") . " <br><br>";
  192 + foreach ($aUnmovedDocs as $oDoc) {
  193 + $sError .= $oDoc[0]->getDisplayPath() . ":&nbsp;&nbsp;&nbsp;" .$oDoc[1] . "<br>";
  194 + }
  195 + $sError .= "<br>" . _("The other documents are been moved.");
  196 +
  197 + $oPatternCustom = & new PatternCustom();
  198 + $oPatternCustom->setHtml(renderErrorPage($sError));
  199 + $main->setCentralPayload($oPatternCustom);
  200 + $main->render();
  201 + reset($aUnmovedDocs);
  202 + exit(0);
  203 +}
  204 +
  205 +// redirect to the browse folder page
  206 +redirect("$default->rootUrl/control.php?action=browse&fFolderID=" . $oDocument->getFolderID());
  207 +
213 ?> 208 ?>