Commit 41adce884a3e971cea7e994d8c4e2b22855c554d
1 parent
6eebf621
Add transparent (requested) transaction support for dispatcher. A
dispatcher can be configured to always use transactions, or you can start a transaction. Either way, rollback occurs when errorPage or errorRedirectTo is used, and committing happens in standard case and in successRedirectTo. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3692 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
43 additions
and
7 deletions
lib/dispatcher.inc.php
| @@ -2,6 +2,8 @@ | @@ -2,6 +2,8 @@ | ||
| 2 | 2 | ||
| 3 | class KTDispatcher { | 3 | class KTDispatcher { |
| 4 | var $event_var = "action"; | 4 | var $event_var = "action"; |
| 5 | + var $bAutomaticTransaction = false; | ||
| 6 | + var $bTransactionStarted = false; | ||
| 5 | 7 | ||
| 6 | function dispatch () { | 8 | function dispatch () { |
| 7 | $method = 'do_main'; | 9 | $method = 'do_main'; |
| @@ -12,19 +14,50 @@ class KTDispatcher { | @@ -12,19 +14,50 @@ class KTDispatcher { | ||
| 12 | } | 14 | } |
| 13 | } | 15 | } |
| 14 | 16 | ||
| 17 | + if ($this->bAutomaticTransaction) { | ||
| 18 | + $this->startTransaction(); | ||
| 19 | + } | ||
| 20 | + | ||
| 15 | $ret = $this->$method(); | 21 | $ret = $this->$method(); |
| 16 | $this->handleOutput($ret); | 22 | $this->handleOutput($ret); |
| 23 | + | ||
| 24 | + if ($this->bTransactionStarted) { | ||
| 25 | + $this->commitTransaction(); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + function startTransaction() { | ||
| 30 | + DBUtil::startTransaction(); | ||
| 31 | + $this->bTransactionStarted = true; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + function commitTransaction() { | ||
| 35 | + DBUtil::commit(); | ||
| 36 | + $this->bTransactionStarted = false; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + function rollbackTransaction() { | ||
| 40 | + DBUtil::rollback(); | ||
| 41 | + $this->bTransactionStarted = false; | ||
| 17 | } | 42 | } |
| 18 | 43 | ||
| 19 | function errorRedirectTo($event, $error_message, $sQuery = "") { | 44 | function errorRedirectTo($event, $error_message, $sQuery = "") { |
| 20 | - /* $method = 'do_main'; | ||
| 21 | - if (method_exists($this, 'do_' . $event)) { | ||
| 22 | - $method = 'do_' . $event; | ||
| 23 | - }*/ | 45 | + if ($this->bTransactionStarted) { |
| 46 | + $this->rollbackTransaction(); | ||
| 47 | + } | ||
| 48 | + | ||
| 24 | $_SESSION['KTErrorMessage'][] = $error_message; | 49 | $_SESSION['KTErrorMessage'][] = $error_message; |
| 25 | - //exit(redirect($_SERVER["PHP_SELF"] . '?action=' . $event)); | ||
| 26 | - exit($this->redirectTo($event, $sQuery)); | ||
| 27 | - //return $this->$method(); | 50 | + $this->redirectTo($event, $sQuery); |
| 51 | + } | ||
| 52 | + | ||
| 53 | + function successRedirectTo($event, $info_message, $sQuery = "") { | ||
| 54 | + if ($this->bTransactionStarted) { | ||
| 55 | + $this->commitTransaction(); | ||
| 56 | + } | ||
| 57 | + if (!empty($info_message)) { | ||
| 58 | + $_SESSION['KTInfoMessage'][] = $info_message; | ||
| 59 | + } | ||
| 60 | + $this->redirectTo($event, $sQuery); | ||
| 28 | } | 61 | } |
| 29 | 62 | ||
| 30 | function redirectTo($event, $sQuery = "") { | 63 | function redirectTo($event, $sQuery = "") { |
| @@ -109,6 +142,9 @@ class KTStandardDispatcher extends KTDispatcher { | @@ -109,6 +142,9 @@ class KTStandardDispatcher extends KTDispatcher { | ||
| 109 | } | 142 | } |
| 110 | 143 | ||
| 111 | function errorPage($errorMessage) { | 144 | function errorPage($errorMessage) { |
| 145 | + if ($this->bTransactionStarted) { | ||
| 146 | + $this->rollbackTransaction(); | ||
| 147 | + } | ||
| 112 | $this->handleOutput($errorMessage); | 148 | $this->handleOutput($errorMessage); |
| 113 | exit(0); | 149 | exit(0); |
| 114 | } | 150 | } |