Skip to content

Commit

Permalink
#211 - removed the majority of the custom logic from the ArticleContr…
Browse files Browse the repository at this point in the history
…oller::doDELETE() method, with the exception of a call to clear the unit of work from the session which was causing a bad re-direct
  • Loading branch information
alphadevx committed Sep 13, 2015
1 parent 33beb44 commit 10513b3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 81 deletions.
21 changes: 17 additions & 4 deletions Alpha/Controller/ActiveRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ public function doGET($request)
$view = View::getInstance($record, false, $accept);

$body .= View::displayPageHead($this);

$message = $this->getStatusMessage();
if (!empty($message)) {
$body .= $message;
}

$body .= View::renderDeleteForm($this->request->getURI());

foreach ($records as $record) {
Expand Down Expand Up @@ -383,7 +389,7 @@ public function doPOST($request)
if (isset($params['statusMessage'])) {
$this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
} else {
$this->setStatusMessage(View::displayUpdateMessage('Created a new '.$record->getFriendlyClassName().' record'));
$this->setStatusMessage(View::displayUpdateMessage('Created'));
}

ActiveRecord::disconnect();
Expand Down Expand Up @@ -471,7 +477,11 @@ public function doPUT($request)

self::$logger->action('Saved '.$ActiveRecordType.' instance with OID '.$record->getOID());

$this->setStatusMessage(View::displayUpdateMessage('Saved '.$ActiveRecordType.' instance with OID '.$record->getOID()));
if (isset($params['statusMessage'])) {
$this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
} else {
$this->setStatusMessage(View::displayUpdateMessage('Saved'));
}

ActiveRecord::disconnect();
} catch (SecurityException $e) {
Expand Down Expand Up @@ -576,7 +586,11 @@ public function doDELETE($request)
} else {
$response = new Response(301);

$this->setStatusMessage(View::displayUpdateMessage('Deleted '.$ActiveRecordType.' instance with OID '.$params['ActiveRecordOID']));
if (isset($params['statusMessage'])) {
$this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
} else {
$this->setStatusMessage(View::displayUpdateMessage('Deleted'));
}

if ($this->getNextJob() != '') {
$response->redirect($this->getNextJob());
Expand All @@ -596,7 +610,6 @@ public function doDELETE($request)
throw new ResourceNotFoundException('The item that you have requested cannot be found!');
} catch (AlphaException $e) {
self::$logger->error($e->getMessage());
$body .= View::displayErrorMessage('Error deleting the BO of OID ['.$params['ActiveRecordOID'].'], check the log!');
ActiveRecord::rollback();
}

Expand Down
76 changes: 5 additions & 71 deletions Alpha/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,90 +473,24 @@ public function doPUT($request)
}

/**
* Method to handle PUT requests.
* Method to handle DELETE requests.
*
* @param Alpha\Util\Http\Request
*
* @return Alpha\Util\Http\Response
*
* @since 2.0
*
* @todo handle all of this functionality with ActiveRecordController
*/
/*public function doDELETE($request)
public function doDELETE($request)
{
self::$logger->debug('>>doDELETE($request=['.var_export($request, true).'])');

$config = ConfigProvider::getInstance();
$params = $request->getParams();
try {
// check the hidden security fields before accepting the form DELETE data
if (!$this->checkSecurityFields()) {
throw new SecurityException('This page cannot accept post data from remote servers!');
self::$logger->debug('<<doPUT');
}
if (isset($params['title']) || isset($params['deleteOID'])) {
if (isset($params['deleteOID'])) {
$record->load($params['deleteOID']);
} else {
$title = str_replace($config->get('cms.url.title.separator'), ' ', $params['title']);
$record->loadbyAttribute('title', $title);
}
try {
$title = $record->get('title');
$record->delete();
$record = null;
self::$logger->action('Article '.$title.' deleted.');
// if we are deleting a record from a single request request, just render a message
if (isset($params['title'])) {
$body = View::displayPageHead($this);
$body .= View::displayUpdateMessage('Article '.$title.' deleted.');
$body .= '<center>';
$temp = new Button("document.location = '".FrontController::generateSecureURL('act=Alpha\Controller\ActiveRecordController&ActiveRecordType='.get_class($record))."'",
'Back to List', 'cancelBut');
$body .= $temp->render();
$body .= '</center>';
$body .= View::displayPageFoot($this);
self::$logger->debug('<<doDELETE');
return new Response(200, $body, array('Content-Type' => 'text/html'));
}
$this->setStatusMessage(View::displayUpdateMessage('Article '.$title.' deleted.'));
self::$logger->debug('<<doDELETE');
return $this->doGET($request);
} catch (AlphaException $e) {
self::$logger->error($e->getTraceAsString());
$response = new Response(500, json_encode(array('message' => 'Error deleting the article, check the log!')), array('Content-Type' => 'application/json'));
self::$logger->debug('<<doDELETE');
return $response;
}
} else {
$body .= View::renderErrorPage(404, 'Failed to find the requested article!');
return new Response(404, $body, array('Content-Type' => 'text/html'));
}
} catch (SecurityException $e) {
self::$logger->warn($e->getMessage());
throw new ResourceNotAllowedException($e->getMessage());
}
$this->setUnitOfWork(array());

self::$logger->debug('<<doDELETE');
}*/
return parent::doDELETE($request);
}

/**
* Renders custom HTML header content.
Expand Down
4 changes: 4 additions & 0 deletions Alpha/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public function setUnitOfWork($jobs)
$sessionProvider = $config->get('session.provider.name');
$session = SessionProviderFactory::getInstance($sessionProvider);
$session->delete('unitOfWork');
$this->firstJob = null;
$this->previousJob = null;
$this->nextJob = null;
$this->lastJob = null;
$this->dirtyObjects = array();
$this->newObjects = array();

Expand Down
2 changes: 1 addition & 1 deletion Alpha/View/ArticleCommentView.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function editView($fields = array())
$('#dialogDiv').dialog({
buttons: {
'OK': function(event, ui) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('#deleteForm').submit();
},
'Cancel': function(event, ui) {
Expand Down
2 changes: 1 addition & 1 deletion Alpha/View/ArticleView.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function editView($fields = array())
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('#deleteForm').submit();
dialogItself.close();
}
Expand Down
8 changes: 4 additions & 4 deletions Alpha/View/Renderer/Html/RendererProviderHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function editView($fields = array())
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('#deleteForm').submit();
dialogItself.close();
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public function listView($fields = array())
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('#deleteForm').submit();
dialogItself.close();
}
Expand Down Expand Up @@ -388,7 +388,7 @@ public function detailedView($fields = array())
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$this->BO->getOID()."');
$('#deleteForm').submit();
dialogItself.close();
}
Expand Down Expand Up @@ -693,7 +693,7 @@ public static function renderDeleteForm($URI)
$config = ConfigProvider::getInstance();

$html = '<form action="'.$URI.'" method="POST" id="deleteForm" accept-charset="UTF-8">';
$fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID');
$fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID');
$html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value=""/>';
$fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('_METHOD')) : '_METHOD');
$html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="DELETE"/>';
Expand Down

0 comments on commit 10513b3

Please sign in to comment.