00001 <?php
00014 class SPOSTEventHandler extends SObject {
00016 protected $eventHandler;
00018 protected $swat;
00019
00020 public function doc() {
00021 return "Generates SEvent objects from POST array, calls "
00022 . "SEventHandler to process them.";
00023 }
00024
00030 public function setSWAT($swat) {
00031 $this->swat = $swat;
00032 }
00033
00039 public function getSWAT() {
00040 return $this->swat;
00041 }
00042
00051 protected function generateEventHandler() {
00052 if ($this->eventHandler != null) return $this->eventHandler;
00053 $this->setEventHandler(new SEventHandler());
00054 return $this->eventHandler;
00055 }
00056
00065 public function setEventHandler($eh) {
00066 $this->eventHandler = $eh;
00067 }
00068
00077 protected function generateEvent() {
00078
00079 if (count($_POST) < 1) return true;
00080
00081
00082 if (!isset($_POST['action'])){
00083 $this->setPrettyError('handleEvents', 'No action set for event.');
00084 return false;
00085 }
00086 if (!isset($_POST['transaction'])){
00087 $this->setPrettyError('handleEvents', 'No transactionId set for event.');
00088 return false;
00089 }
00090 if(!($this->getSWAT()->getSession()->getTransaction()->getValidTransaction($_POST['transaction']))) {
00091 $this->setPrettyError('handleEvents', "Transaction was already performed successfully."
00092 . " This transaction id is no longer valid.");
00093 return false;
00094 }
00095
00096
00097
00098 $e = new SEvent();
00099
00100 # set action
00101 $e->setAction($_POST['action']);
00102
00103 # set user
00104 $e->setSource($this->getSWAT()->getProjectUser());
00105 if ($e->getSource() == null) {
00106 $e->setSource($this->getSWAT()->getGlobalUser());
00107 }
00108
00109 # set target
00110 if (isset($_POST['target'])){
00111 $e->setTarget($_POST['target']);
00112 }
00113
00114 # set transaction
00115 $e->setTransaction($_POST['transaction']);
00116
00117 return $e;
00118 }
00119
00130 protected function callEventHandler($e) {
00131 # generate the event handler class
00132 $h = $this->generateEventHandler();
00133 if ($h == null) {
00134 $this->setPrettyError('callEventHandler',
00135 'Could not get instance of EventHandler class');
00136 return false;
00137 }
00138
00139 # call the event handler class
00140 $h->handleEvent($e);
00141 if ($h->hasError()){
00142 $this->getErrorFrom($h);
00143 return false;
00144 }
00145 return true;
00146 }
00147
00157 public function handleEvents() {
00158 # set a warning for form POSTs that are not actions,
00159 # but otherwise ignore them.
00160 if (!isset($_POST['action'])){
00161 $this->setPrettyWarning("handleEvents", "No action specified. "
00162 . "Assuming this is not an event to be handled by POSTActionHandler");
00163 return true;
00164 }
00165
00166 # generate the event object, checking for errors
00167 $e = $this->generateEvent();
00168 if ($e == false){
00169 $this->getErrorFrom($e);
00170 return false;
00171 }
00172 if ($this->hasError()){
00173 return false;
00174 }
00175
00176 # call our preferred event handler class
00177 $result = $this->callEventHandler($e);
00178
00179 # log the transaction so that it can't be done again
00180 if ($result == true && !($this->hasError())){
00181 $this->getSWAT()->getSession()->getTransaction()->setTransaction($e->getTransaction());
00182 $this->getSWAT()->syncSession();
00183 return true;
00184 }
00185 return false;
00186 }
00187 }
00188 ?>